中毒后的反思,我再也受不了

尽量做好系统安全防护,修复所有已知的危险行为,同时,在系统遭受攻击后能够迅速有效地处理攻击行为,最大限度地降低攻击对系统产生的影响。

一、处理服务器遭受攻击的一般思路

系统遭受攻击并不可怕,可怕的是面对攻击束手无策,下面就详细介绍下在服务器遭受攻击后的一般处理思路。

1.切断网络

所有的攻击都来自于网络,因此,在得知系统正遭受黑客的攻击后,首先要做的就是断开服务器的网络连接,这样除了能切断攻击源之外,也能保护服务器所在网络的其他主机。

2.查找攻击源

可以通过分析系统日志或登录日志文件,查看可疑信息,同时也要查看系统都打开了哪些端口,运行哪些进程,并通过这些进程分析哪些是可疑的程序。这个过程要根据经验和综合判断能力进行追查和分析。下面的章节会详细介绍这个过程的处理思路。

3.分析入侵原因和途径

既然系统遭到入侵,那么原因是多方面的,可能是系统漏洞,也可能是程序漏洞,一定要查清楚是哪个原因导致的,并且还要查清楚遭到攻击的途径,找到攻击源,因为只有知道了遭受攻击的原因和途径,才能删除攻击源同时进行漏洞的修复。

4.备份用户数据

在服务器遭受攻击后,需要立刻备份服务器上的用户数据,同时也要查看这些数据中是否隐藏着攻击源。如果攻击源在用户数据中,一定要彻底删除,然后将用户数据备份到一个安全的地方。

5.重新安装系统

永远不要认为自己能彻底清除攻击源,因为没有人能比黑客更了解攻击程序,在服务器遭到攻击后,最安全也最简单的方法就是重新安装系统,因为大部分攻击程序都会依附在系统文件或者内核中,所以重新安装系统才能彻底清除攻击源。

6.修复程序或系统漏洞

在发现系统漏洞或者应用程序漏洞后,首先要做的就是修复系统漏洞或者更改程序bug,因为只有将程序的漏洞修复完毕才能正式在服务器上运行。

7.恢复数据和连接网络

将备份的数据重新复制到新安装的服务器上,然后开启服务,最后将服务器开启网络连接,对外提供服务。

二、检查并锁定可疑用户

当发现服务器遭受攻击后,首先要切断网络连接,但是在有些情况下,比如无法马上切断网络连接时,就必须登录系统查看是否有可疑用户,如果有可疑用户登录了系统,那么需要马上将这个用户锁定,然后中断此用户的远程连接。

1.登录系统查看可疑用户

通过root用户登录,然后执行“w”命令即可列出所有登录过系统的用户,如下图所示。

wKiom1QfmtKB1WX1AAFwFYCbdaY768.jpg 

通过这个输出可以检查是否有可疑或者不熟悉的用户登录,同时还可以根据用户名以及用户登录的源地址和它们正在运行的进程来判断他们是否为非法用户。

2.锁定可疑用户

一旦发现可疑用户,就要马上将其锁定,例如上面执行“w”命令后发现nobody用户应该是个可疑用户(因为nobody默认情况下是没有登录权限的),于是首先锁定此用户,执行如下操作:

[root@server ~]# passwd -l nobody

锁定之后,有可能此用户还处于登录状态,于是还要将此用户踢下线,根据上面“w”命令的输出,即可获得此用户登录进行的pid值,操作如下:

[root@server ~]# ps -ef|grep @pts/3

531   6051  6049  0 19:23 ?  00:00:00 sshd: nobody@pts/3

[root@server ~]# kill -9 6051

这样就将可疑用户nobody从线上踢下去了。如果此用户再次试图登录它已经无法登录了。

3.通过last命令查看用户登录事件

last命令记录着所有用户登录系统的日志,可以用来查找非授权用户的登录事件,而last命令的输出结果来源于/var/log/wtmp文件,稍有经验的入侵者都会删掉/var/log/wtmp以清除自己行踪,但是还是会露出蛛丝马迹在此文件中的。

三、查看系统日志

查看系统日志是查找攻击源最好的方法,可查的系统日志有/var/log/messages、/var/log/secure等,这两个日志文件可以记录软件的运行状态以及远程用户的登录状态,还可以查看每个用户目录下的.bash_history文件,特别是/root目录下的.bash_history文件,这个文件中记录着用户执行的所有历史命令。

四、检查并关闭系统可疑进程

检查可疑进程的命令很多,例如ps、top等,但是有时候只知道进程的名称无法得知路径,此时可以通过如下命令查看:

首先通过pidof命令可以查找正在运行的进程PID,例如要查找sshd进程的PID,执行如下命令:

1
2
[root@server ~] pidof sshd
13276 12942 4284

然后进入内存目录,查看对应PID目录下exe文件的信息:

1
2
[root@server ~] # ls -al /proc/13276/exe 
lrwxrwxrwx 1 root root 0 Oct  4 22:09  /proc/13276/exe  ->  /usr/sbin/sshd

这样就找到了进程对应的完整执行路径。如果还有查看文件的句柄,可以查看如下目录:

[root@server ~]# ls -al /proc/13276/fd

通过这种方式基本可以找到任何进程的完整执行信息,此外还有很多类似的命令可以帮助系统运维人员查找可疑进程。例如,可以通过指定端口或者tcp、udp协议找到进程PID,进而找到相关进程:

1
2
3
4
5
6
7
8
9
[root@server ~] # fuser -n tcp 111
111 /tcp :              1579
[root@server ~] # fuser -n tcp 25
25 /tcp :               2037
[root@server ~] # ps -ef|grep 2037
root      2037     1  0 Sep23 ?        00:00:05  /usr/libexec/postfix/master
postfix   2046  2037  0 Sep23 ?        00:00:01 qmgr -l -t fifo -u
postfix   9612  2037  0 20:34 ?        00:00:00 pickup -l -t fifo -u
root     14927 12944  0 21:11 pts /1     00:00:00  grep  2037

在有些时候,攻击者的程序隐藏很深,例如rootkits后门程序,在这种情况下ps、top、netstat等命令也可能已经被替换,如果再通过系统自身的命令去检查可疑进程就变得毫不可信,此时,就需要借助于第三方工具来检查系统可疑程序,例如前面介绍过的chkrootkit、RKHunter等工具,通过这些工具可以很方便的发现系统被替换或篡改的程序。


五、检查文件系统的完好性

检查文件属性是否发生变化是验证文件系统完好性最简单、最直接的方法,例如可以检查被入侵服务器上/bin/ls文件的大小是否与正常系统上此文件的大小相同,以验证文件是否被替换,但是这种方法比较低级。此时可以借助于Linux下rpm这个工具来完成验证,操作如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
[root@server ~] # rpm -Va
....L...  c  /etc/pam .d /system-auth
S.5.....  c  /etc/security/limits .conf
S.5....T  c  /etc/sysctl .conf
S.5....T     /etc/sgml/docbook-simple . cat
S.5....T  c  /etc/login .defs
S.5.....  c  /etc/openldap/ldap .conf
S.5....T  c  /etc/sudoers
..5....T  c  /usr/lib64/security/classpath .security
....L...  c  /etc/pam .d /system-auth
S.5.....  c  /etc/security/limits .conf
S.5.....  c  /etc/ldap .conf
S.5....T  c  /etc/ssh/sshd_config

对于输出中每个标记的含义介绍如下:

S 表示文件长度发生了变化

M 表示文件的访问权限或文件类型发生了变化

5 表示MD5校验和发生了变化

D 表示设备节点的属性发生了变化

L 表示文件的符号链接发生了变化

U 表示文件/子目录/设备节点的owner发生了变化

G 表示文件/子目录/设备节点的group发生了变化

T 表示文件最后一次的修改时间发生了变化

如果在输出结果中有“M”标记出现,那么对应的文件可能已经遭到篡改或替换,此时可以通过卸载这个rpm包重新安装来清除受攻击的文件。

不过这个命令有个局限性,那就是只能检查通过rpm包方式安装的所有文件,对于通过非rpm包方式安装的文件就无能为力了。同时,如果rpm工具也遭到替换,就不能通过这个方法了,此时可以从正常的系统上复制一个rpm工具进行检测。

对文件系统的检查也可以通过chkrootkit、RKHunter这两个工具来完成,关于chkrootkit、RKHunter工具的使用,下面我们将使用两个工具进行检查


这两天突然发现我们的服务器产生大量DNS解析连线。为了查明问题,就下载网上找工具检查问题所在。用了两个工具,一个chkrootkit,另外一个rootkit huntur。在使用了这两个产品后,觉得rootkit的信息更加详细一些。现在就对两个工具的操作和使用写一个记录,也方便之后自己查看。
 
1. chkrootkit的使用
 
1.1 解压安装
#tar -zxvf chkrootkit.tar.gz
#make sense
 
1.2 运行监测
#./chkrootkit
运行结果在
附件:环境扫描结果
判断有问题的信息如下:
Checking `ifconfig'... INFECTED
Checking `ls'... INFECTED
Checking `netstat'... INFECTED
Checking `ps'... INFECTED
Checking `top'... INFECTED
Searching for Madalin rootkit default files... Possible Madalin rootkit installed
Checking `bindshell'... INFECTED (PORTS:  145)
Checking `lkm'... You have    77 process hidden for readdir command
You have    77 process hidden for ps command
chkproc: Warning: Possible LKM Trojan installed
Checking `sniffer'... eth0: PROMISC
不幸中招,系统只能够重新安装了。在看了这个文档之后,对chkrootkit这个工具又看了一些其他资料。可以做rootkit的自动检查操作
 
1.3 chkrootkit参数说明
Usage: ./chkrootkit [options] [test ...]
Options:
        -h                显示帮助信息
        -V                显示版本信息
        -l                显示测试内容
        -d                debug模式,显示检测过程的相关指令程序
        -q                安静模式,只显示有问题部分,
        -x                高级模式,显示所有检测结果
        -r dir            设定指定的目录为根目录
        -p dir1:dir2:dirN 检测指定目录
        -n                跳过NFS连接的目录
 
 
2. rootkit hunter的使用
 
2.1 解压安装
解压
#tar -zxvf rkhunter-1.3.4.tar.gz
安装
#cd rkhunter-1.3.4
#./installer.sh -h
Usage: ./installer.sh <parameters>
Ordered valid parameters:
--help (-h)      : 显示帮助
--examples       : 显示安装实例
--layout <value> : 选择安装模板(安装必选参数).
                   模板选择:
                    - default: (FHS compliant),
                    - /usr,
                    - /usr/local,
                    - oldschool: 之前版本安装路径,
                    - custom: 自定义安装路径,
                    - RPM: for building RPM's. Requires $RPM_BUILD_ROOT.
                    - DEB: for building DEB's. Requires $DEB_BUILD_ROOT.
--striproot      : Strip path from custom layout (for package maintainers).
--install        : 根据选择目录安装
--show           : 显示安装路径
--remove         : 卸载rkhunter
--version        : 显示安装版本
 
我使用的安装指令
#./installer.sh --layout default --install
 
2.2 rkhunter操作
#/usr/local/bin/rkhunter --propupd
#/usr/local/bin/rkhunter -c --sk -rwo
 
Warning: File '/bin/awk' has the immutable-bit set.
Warning: File '/bin/basename' has the immutable-bit set.
Warning: File '/bin/bash' has the immutable-bit set.
Warning: File '/bin/cat' has the immutable-bit set.
Warning: File '/bin/chmod' has the immutable-bit set.
Warning: File '/bin/chown' has the immutable-bit set.
Warning: File '/bin/cp' has the immutable-bit set.
Warning: File '/bin/csh' has the immutable-bit set.
Warning: File '/bin/cut' has the immutable-bit set.
Warning: File '/bin/date' has the immutable-bit set.
Warning: File '/bin/df' has the immutable-bit set.
Warning: File '/bin/dmesg' has the immutable-bit set.
Warning: File '/bin/echo' has the immutable-bit set.
Warning: File '/bin/ed' has the immutable-bit set.
Warning: File '/bin/egrep' has the immutable-bit set.
Warning: File '/bin/env' has the immutable-bit set.
Warning: File '/bin/fgrep' has the immutable-bit set.
Warning: File '/bin/grep' has the immutable-bit set.
Warning: File '/bin/kill' has the immutable-bit set.
Warning: File '/bin/login' has the immutable-bit set.
Warning: File '/bin/ls' has the immutable-bit set.
Warning: File '/bin/mail' has the immutable-bit set.
Warning: File '/bin/mktemp' has the immutable-bit set.
Warning: File '/bin/more' has the immutable-bit set.
Warning: File '/bin/mount' has the immutable-bit set.
Warning: File '/bin/mv' has the immutable-bit set.
Warning: File '/bin/netstat' has the immutable-bit set.
Warning: File '/bin/ps' has the immutable-bit set.
Warning: File '/bin/pwd' has the immutable-bit set.
Warning: File '/bin/rpm' has the immutable-bit set.
Warning: File '/bin/sed' has the immutable-bit set.
Warning: File '/bin/sh' has the immutable-bit set.
Warning: File '/bin/sort' has the immutable-bit set.
Warning: File '/bin/su' has the immutable-bit set.
Warning: File '/bin/touch' has the immutable-bit set.
Warning: File '/bin/uname' has the immutable-bit set.
Warning: File '/bin/gawk' has the immutable-bit set.
Warning: File '/bin/tcsh' has the immutable-bit set.
Warning: File '/usr/bin/awk' has the immutable-bit set.
Warning: File '/usr/bin/cut' has the immutable-bit set.
Warning: File '/usr/bin/env' has the immutable-bit set.
Warning: The command '/usr/bin/GET' has been replaced by a script: /usr/bin/GET: perl script text executable
Warning: The command '/usr/bin/groups' has been replaced by a script: /usr/bin/groups: Bourne shell script text executable
Warning: File '/usr/bin/kill' has the immutable-bit set.
Warning: The command '/usr/bin/ldd' has been replaced by a script: /usr/bin/ldd: Bourne shell script text executable
Warning: File '/usr/bin/top' has the immutable-bit set.
Warning: The command '/usr/bin/whatis' has been replaced by a script: /usr/bin/whatis: Bourne shell script text executable
Warning: File '/usr/bin/gawk' has the immutable-bit set.
Warning: File '/sbin/chkconfig' has the immutable-bit set.
Warning: File '/sbin/depmod' has the immutable-bit set.
Warning: File '/sbin/fuser' has the immutable-bit set.
Warning: File '/sbin/ifconfig' has the immutable-bit set.
Warning: File '/sbin/ifdown' has the immutable-bit set.
Warning: The command '/sbin/ifdown' has been replaced by a script: /sbin/ifdown: Bourne-Again shell script text executable
Warning: File '/sbin/ifup' has the immutable-bit set.
Warning: The command '/sbin/ifup' has been replaced by a script: /sbin/ifup: Bourne-Again shell script text executable
Warning: File '/sbin/init' has the immutable-bit set.
Warning: File '/sbin/insmod' has the immutable-bit set.
Warning: File '/sbin/ip' has the immutable-bit set.
Warning: File '/sbin/lsmod' has the immutable-bit set.
Warning: File '/sbin/modinfo' has the immutable-bit set.
Warning: File '/sbin/modprobe' has the immutable-bit set.
Warning: File '/sbin/nologin' has the immutable-bit set.
Warning: File '/sbin/rmmod' has the immutable-bit set.
Warning: File '/sbin/runlevel' has the immutable-bit set.
Warning: File '/sbin/sulogin' has the immutable-bit set.
Warning: File '/sbin/sysctl' has the immutable-bit set.
Warning: File '/sbin/syslogd' has the immutable-bit set.
Warning: File '/usr/sbin/adduser' has the immutable-bit set.
Warning: No hash value found for file '/usr/sbin/amd' in the rkhunter.dat file.
Warning: File '/usr/sbin/amd' has the immutable-bit set.
Warning: File '/usr/sbin/chroot' has the immutable-bit set.
Warning: File '/usr/sbin/groupadd' has the immutable-bit set.
Warning: File '/usr/sbin/groupdel' has the immutable-bit set.
Warning: File '/usr/sbin/groupmod' has the immutable-bit set.
Warning: File '/usr/sbin/grpck' has the immutable-bit set.
Warning: File '/usr/sbin/kudzu' has the immutable-bit set.
Warning: File '/usr/sbin/lsof' has the immutable-bit set.
Warning: File '/usr/sbin/prelink' has the immutable-bit set.
Warning: File '/usr/sbin/pwck' has the immutable-bit set.
Warning: File '/usr/sbin/sestatus' has the immutable-bit set.
Warning: File '/usr/sbin/tcpd' has the immutable-bit set.
Warning: File '/usr/sbin/useradd' has the immutable-bit set.
Warning: File '/usr/sbin/userdel' has the immutable-bit set.
Warning: File '/usr/sbin/usermod' has the immutable-bit set.
Warning: File '/usr/sbin/vipw' has the immutable-bit set.
Warning: File '/usr/sbin/xinetd' has the immutable-bit set.
Warning: Dreams Rootkit                           [ Warning ]
         File '/usr/bin/sense' found
         File '/usr/bin/sl2' found
         File '/usr/bin/(swapd)' found
Warning: Checking for possible rootkit strings    [ Warning ]
         Found string '/dev/ttyoa' in file '/bin/netstat'. Possible rootkit: Sin Rootkit
Warning: Found possible sniffer log file: /usr/lib/libice.log
Warning: Found enabled xinetd service: /etc/xinetd.d/auth
Warning: Found enabled xinetd service: /etc/xinetd.d/cups-lpd
Warning: Found enabled xinetd service: /etc/xinetd.d/swat
Warning: Found enabled xinetd service: /etc/xinetd.d/vmware-authd
Warning: Possible promiscuous interfaces:
         'ifconfig' command output:
         'ip' command output: eth0
Warning: Account 'test' is root equivalent (UID = 0)
Warning: Account 'james' is root equivalent (UID = 0)
Warning: Account 'master' is root equivalent (UID = 0)
Warning: Account 'admin' is root equivalent (UID = 0)
Warning: The SSH configuration option 'PermitRootLogin' has not been set.
         The default value may be 'yes', to allow root access.
Warning: The SSH configuration option 'Protocol' has not been set.
         The default value may be '2,1', to allow the use of protocol version 1.
Warning: Hidden file found: /usr/share/man/man1/..1.gz: gzip compressed data, from Unix, max compression
Warning: Application 'exim', version '4.43', is out of date, and possibly a security risk.
Warning: Application 'gpg', version '1.2.6', is out of date, and possibly a security risk.
Warning: Application 'openssl', version '0.9.7a', is out of date, and possibly a security risk.
Warning: Application 'php', version '4.3.9', is out of date, and possibly a security risk.
One or more warnings have been found while checking the system.
Please check the log file (/var/log/rkhunter.log)
结果就是“中招”,基本上就是重新安装系统的命了。不过从检查的结果来看,可以判断所中的rootkit的类型和被替换的系统文件。同时对一些程序版本进行检测,提供的信息比较多。
 
2.3 指令参数说明
#/usr/local/bin/rkhunter
Usage: rkhunter {--check | --update | --versioncheck |
                 --propupd [{filename | directory | package name},...] |
                 --list [{tests | {lang | languages} | rootkits},...] |
                 --version | --help} [options]
Current options are:
         --append-log                  在日志文件后追加日志,而不覆盖原有日志
         --bindir <directory>...       Use the specified command directories
     -c, --check                       检测当前系统
  --cs2, --color-set2                  Use the second color set for output
         --configfile <file>           使用特定的配置文件
         --cronjob                     作为cron定期运行
                                       (包含参数 -c, --sk , --nocolors )
         --dbdir <directory>           Use the specified database directory
         --debug                       Debug模式(不要使用除非要求使用)
         --disable <test>[,<test>...]  跳过指定检查对象(默认为无)
         --display-logfile             在最后显示日志文件内容
         --enable  <test>[,<test>...]  对指定检测对象进行检查
                                       (默认检测所有对象)
         --hash {MD5 | SHA1 | NONE |   使用指定的文件哈希函数
                 <command>}            (Default is SHA1)
     -h, --help                        显示帮助菜单
 --lang, --language <language>         指定使用的语言
                                       (Default is English)
         --list [tests | languages |   罗列测试对象明朝,使用语言,可检测的木马程序
                 rootkits]             
     -l, --logfile [file]              写到指定的日志文件名
                                       (Default is /var/log/rkhunter.log)
         --noappend-log                不追加日志,直接覆盖日志文件
         --nocolors                    输出只显示黑白两色
         --nolog                       不写入日志文件
--nomow, --no-mail-on-warning          如果有警告信息,不发送邮件
   --ns, --nosummary                   不显示检查结果的统计数据
 --novl, --no-verbose-logging          不显示详细记录
         --pkgmgr {RPM | DPKG | BSD |  使用特定的包管理用于文件的哈希值验证
                   NONE}               (Default is NONE)
         --propupd [file | directory | 更新整个文件属性数据库或仅仅更新指定条目
                    package]...        
     -q, --quiet                       安静模式(no output at all)
  --rwo, --report-warnings-only        只显示警告信息
     -r, --rootdir <directory>         使用指定的root目录
   --sk, --skip-keypress               自动完成所有检测,跳过键盘输入
         --summary                     显示检测结果的统计信息
                                       (This is the default)
         --syslog [facility.priority]  记录检测启动和结束时间到系统日志中
                                       (Default level is authpriv.notice)
         --tmpdir <directory>          使用指定的临时目录
         --update                      检测更新内容
   --vl, --verbose-logging             使用详细日志记录 (on by default)
     -V, --version                     显示版本信息
         --versioncheck                检测最新版本
     -x, --autox                       当X在使用时,自动启动检测
     -X, --no-autox                    当X在使用时,不自启检测


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值