作者:煮酒品茶
> Linux服务器被黑,是常见的事情,如何及时发现攻击行为,并检查定位到攻击源以及事后处理是运维工程师所要做的。
> 常见检查手段如下x
### 1、检查端口
#查看是否有异常端口,非业务端口关闭,远程维护端口指定IP访问
` netstat -ntlup`
#查看iptables是否启动以及被添加规则x
` service iptables status`
### 2、网卡流量
#查看网卡流量,如果超过G的就需要进行关注并定位问题
`ifconfig eth0`
### 3、检查帐户
品茶:注意家目录为/home/user的都需要检查,象/sbin/nologin则没有那么重要。
~~~
less /etc/passwd
grep :0: /etc/passwd(检查是否产生了新用户,和UID、GID是0的用户)
ls -l /etc/passwd(查看文件修改日期)
awk -F: ‘$3= =0 {print $1}’ /etc/passwd(查看是否存在特权用户)
awk -F: ‘length($2)= =0 {print $1}’ /etc/shadow(查看是否存在空口令帐户)
w (查看当前登陆信息)
~~~
### 4、检查日志
~~~
last(查看正常情况下登录到本机的所有用户的历史记录)
tail /var/log/secure (查看ssh登陆异常日志)
tail /var/log/messages(查看错误ssh登陆日志)
~~~
### 5、检查进程
~~~
# ps aux(注意UID是0的)
# lsof -p pid(察看该进程所打开端口和文件)
# cat /etc/inetd.conf | grep -v "^#"(检查守护进程)
检查隐藏进程
# ps -ef|awk '{print }'|sort -n|uniq >1
# ls /porc |sort -n|uniq >2
# diff 1 2
# cat /etc/inetd.conf | grep -v "^#" (检查系统守护进程)
~~~
### 6、检查文件
~~~
# find / -uid 0 –perm -4000 –print
# find / -size +10000k -print
# find / -name "…" –print
# find / -name ".. " –print
# find / -name ". " –print
# find / -name " " –print
# find / -mmin 30 -print 如果能判断是什么时候被黑的,可以搜索这个时间段被修改的文件(现在很多是用脚本程序跑的,修改时间会很一致)。
注意SUID文件,可疑大于10M和空格文件
# find / -name core -exec ls -l {} ;(检查系统中的core文件)
检查系统文件完整性
# rpm –qf /bin/ls
# rpm -qf /bin/login
# md5sum –b 文件名
# md5sum –t 文件名
~~~
### 7、检查RPM
~~~
# rpm –Va输出格式:
S – File size differs
M – Mode differs (permissions)
5 – MD5 sum differs
D – Device number mismatch
L – readLink path mismatch
U – user ownership differs
G – group ownership differs
T – modification time differs
注意相关的 /sbin, /bin, /usr/sbin, and /usr/bin
~~~
### 8、检查网络
~~~
# ip link | grep PROMISC(正常网卡不该在promisc模式,可能存在sniffer)
# lsof –i
# netstat –nap(察看不正常打开的TCP/UDP端口)
# arp –a
~~~
### 9、查计划任务
~~~
# crontab –u root –l
# cat /etc/crontab
# ls /etc/cron.*
~~~
### 10、检查后门
~~~
# cat /etc/crontab
# ls /var/spool/cron/
# cat /etc/rc.d/rc.local
# ls /etc/rc.d
# ls /etc/rc3.d
# find / -type f -perm 4000
~~~
### 11、检查内核模块
`# lsmod`
### 12、检查系统服务
~~~
# chkconfig
# rpcinfo -p(查看RPC服务)
~~~
### 13、检查rootkit
~~~
# rkhunter -c
# chkrootkit -q
~~~
### 14、查看应用日志
~~~
nginx
php
mysql
~~~