之前公司的服务器木马很猖獗,由于一直找不到后门,且木马目的就是改TDK,因此从TDK入手,开始展开无脑博弈
于是乎我们在一边查询后门,一边设置障眼法
开始讲解下思路:
河马杀毒参考:https://www.shellpub.com/doc/hm_linux_usage.html
0.安装河马杀毒并首次升级
wget -O /opt/hm-linux.tgz http://dl.shellpub.com/hm/latest/hm-linux-amd64.tgz?version=1.7.0 cd /opt/ tar xvf hm-linux.tgz ./hm update
1.病毒文件定位
之前博文其实有讲,我们是利用了河马杀毒做的定时任务,但是由于河马杀毒也怕误报,不会自动清理核实的文件,因此我们将河马扫描文件的'建议清理'作为合法目标
2.自动升级病毒库
为了防止木马变种无法查杀,开始把河马的自动更新写进去
cd /opt/hm ./hm update
3.执行杀毒并自动清理
/opt/hm deepscan /www/wwwroot/ 设置深度扫描宝塔所有站点目录
4.设置 自动清理病毒
python /opt/killtool.py opt目录下有个python脚本,内容如下:
# -*- coding: utf-8 -*- import os import re path = os.getcwd() file = '/opt/result.csv' with open(file,'r') as w: for line in w.readlines(): if '建议清理' in line: print('发现木马,正在执行清理!') bee_re = re.search(r'清理,[0-9a-z/-].{0,}', line, re.I) if bee_re: line = bee_re.group().replace(',','').replace('清理','') print(line) try: os.remove(line) print('清理木马:%s 成功!'%(line)) except Exception as e: print('当前木马文件已经被清理!') print('清理完毕!')
5.清理被修改的TDK 打扫战场
这里可能不同的木马执行的标准不一样,需要自己改合适的脚本,我这里是会被加上加密TDK 加上一个js 替换真实的TDK,障眼法让网站管理员认为TDK正常
根据这个特性,木马攻击者会保留原始TDK,于是等下就可以还原
木马会改动index_m.php index.html index.php等
opt目录下新建key.py 源码:
# -*- coding: utf-8 -*- import os import re list_dir = ['//www//wwwroot//aay.qiugw.com','//www//wwwroot//adu.838.net'] #这里写好每一个网站的index路径 for item in list_dir: index_file = item + r"/index.html" index_php = item + r"/index.php" if os.path.exists(index_file) != True: if os.path.exists(index_php) != True: print("当前目录不存在操作文件index.html或者index.php,退出程序!") pass else: with open(index_php,'r') as f: if ';&#' in f.read(): print('当前路径为:%s'%(item)) print('找到木马js正在处理!') with open(index_php,'r') as f,open('out.php','w') as n: for line in f.readlines(): if ';&#' in line: pass elif 'String.fromCharCode' in line: pass elif 'document.write' in line: pass elif 'baiduspider|sogou|360spider|yisou' in line: title_re = re.search(r'(title =\'.{0,}\')', line, re.I) if title_re: key = title_re.group().replace('title =','').replace('\'','') line = '<title>' + key + '</title>' n.write(line+'\n') elif 'document.title' in line: title_re = re.search(r'(title =\'.{0,}\')', line, re.I) if title_re: key = title_re.group().replace('title =','').replace('\'','') line = '<title>' + key + '</title>' n.write(line+'\n') else: n.write(line) os.remove(index_php) os.rename('out.php',index_php) else: pass else: with open(index_file,'r') as f: if ';&#' in f.read(): print('当前路径为:%s'%(item)) print('找到木马js正在处理!') with open(index_file,'r') as f,open('out.html','w') as n: for line in f.readlines(): if ';&#' in line: pass elif 'String.fromCharCode' in line: pass elif 'baiduspider|sogou|360spider|yisou' in line: title_re = re.search(r'(title =\'.{0,}\')', line, re.I) if title_re: key = title_re.group().replace('title =','').replace('\'','') line = '<title>' + key + '</title>' n.write(line+'\n') elif 'document.title' in line: title_re = re.search(r'(title =\'.{0,}\')', line, re.I) if title_re: key = title_re.group().replace('title =','').replace('\'','') line = '<title>' + key + '</title>' n.write(line+'\n') else: n.write(line) os.remove(index_file) os.rename('out.html',index_file) else: pass print('处理完毕!如果上述没有任何输出,则没有被感染的网站!')
新建执行任务
反正具体问题具体分析,设置时间也灵活点
我现在是10分钟查杀一次,然后每2分钟就会清理一次TDK,以防止中毒处理不及时
始终是障眼法,一定要及时找到后门处理掉!
这是个升级版本 之前的木马js升级了 加密导致失效 所以没事还要多看看 万一变种了就又要费脑筋了 抓住关键词处理 稳当!