写本片文章目的,是进行知识积累,方便以后查看,以前一位客户主机上面出现个这样问题,今天恰巧看到一个群里面发送了这个链接,感觉挺重要,就进行了一下记录

参考:http://bbs.51cto.com/thread-1091316-1.html


   不知道是否是新出的病毒IptabLex大家都知道,互联网每时每刻都会存在***的可能性,(系统频繁发包,SYN***,等等)安全问题是系统管理员不可忽视的一个问题。当系统出现了问题,我们该如何下手,如何去排查系统问题呢?

     由于是系统在内网发包,把我们的网络都堵塞了,以至于我不能远程登录到linux系统上,查看cacti流量图都查看不了。ping也全部丢包。这时去机房也不可能,所以,我只好等待发包不频繁的时机,趁此登录系统,所以登录系统后,直接奔系统进程查看。查看结果如下:

# ps -ef (只显示可疑进程)
root      2013     1  0 10:01 ?        00:00:00 /boot/.IptabLex
root      5953     1  0 10:01 ?        00:00:00 /boot/.IptabLex
root      6035     1  0 10:01 ?        00:00:00 /boot/.IptabLes
root     24852     1  2 14:31 ?        00:04:29 /boot/.IptabLes
root     27322     1  3 15:08 ?        00:03:50 /boot/.IptabLes

由于程序还在发作,所以只保留唯一一个进程,剩下的进程全部kill掉,以便排查系统的根源在哪里

# kill -9 5953 6035 24852

查看打开的文件,及系统关联的文件都有哪些

# lsof -p 27322 ; lsof -p 2013
COMMAND     PID USER   FD   TYPE DEVICE SIZE/OFF    NODE NAME
.IptabLes 27322 root  cwd    DIR  253,0     4096       2 /
.IptabLes 27322 root  rtd    DIR  253,0     4096       2 /
.IptabLes 27322 root  txt    REG  104,1  1103243    5905 /boot/.IptabLes
.IptabLes 27322 root    0u   REG  253,0        5   98310 /.mylisthbS.pid
.IptabLes 27322 root    1u   REG  253,0        5   98313 /.mylisthb.pid
.IptabLes 27322 root    2u  sock    0,5      0t0 3442424 can't identify protocol
.IptabLes 27322 root    3u   raw             0t0 3445564 00000000:00FF->00000000:0000 st=07
.IptabLes 27322 root    4u   raw             0t0 3445565 00000000:00FF->00000000:0000 st=07
.IptabLes 27322 root    5u   raw             0t0 3445566 00000000:00FF->00000000:0000 st=07
COMMAND    PID USER   FD   TYPE  DEVICE SIZE/OFF  NODE NAME
.IptabLex 2013 root  cwd    DIR   253,0     4096     2 /
.IptabLex 2013 root  rtd    DIR   253,0     4096     2 /
.IptabLex 2013 root  txt    REG   104,1   722580  5906 /boot/.IptabLex
.IptabLex 2013 root    0u   REG   253,0        5 98309 /.mylisthbSx.pid
.IptabLex 2013 root    1uW  REG   253,0        5 98311 /.mylisthbx.pid
.IptabLex 2013 root    2u  IPv4 3479690      0t0   TCP 192.168.116.11:10038->59.63.167.168:1001(ESTABLISHED)

经百度查看59.63.167.168的IP为江西省南昌市 电信

查看Cacti流量图。很遗憾,论坛现在上传图片上传不了。
显示的结果是 Output  300M 跑满了。


查看产生的可疑所有的文件。
# ll -a /   (只显示可疑的文件)
-rw-r--r--   1 root  root      5 Jan 12 17:15 .mylisthb.pid
-rw-r--r--   1 root  root      5 Jan 12 10:01 .mylisthbS.pid
-rw-r--r--   1 root  root      5 Jan 12 10:01 .mylisthbSx.pid
-rw-r--r--   1 root  root      5 Jan 12 16:57 .mylisthbx.pid

查看/boot/分区下
# ll -a /boot/
-rw-r--r--   1 root  root      5 Jan 12 10:01 .IptabLes
-rw-r--r--   1 root  root      5 Jan 12 10:01 .IptabLex
-rw-r--r--   1 root  root      5 Jan 12 10:01 IptabLes -> /etc/rc.d/init.d/IptabLes
-rw-r--r--   1 root  root      5 Jan 12 10:01 IptabLex -> /etc/rc.d/init.d/IptabLex


查看/etc/rc.d/init.d/IptabLex和/etc/rc.d/init.d/IptabLes文件内容
# cat /etc/rc.d/init.d/IptabLex
#!/bin/sh
/boot/.IptabLex
exit 0
# cat /etc/rc.d/init.d/IptabLes
#!/bin/sh
/boot/.IptabLes
exit 0

查看两个文件的文件类型,可知是32位的可执行文件
# file /boot/.IptabLex
.IptabLex: sticky ELF 32-bit LSB executable, Intel 80386, version 1 (SYSV), for GNU/Linux 2.6.9, statically linked, not stripped
# file /boot/.IptabLes
.IptabLes: sticky ELF 32-bit LSB executable, Intel 80386, version 1 (SYSV), for GNU/Linux 2.6.9, statically linked, not stripped


查看message信息
# cat /var/log/message
Jan 12 11:36:24 localhost kernel: .IptabLes[12688]: segfault at 0000000000000000 rip 000000000804e837 rsp 00000000ffeb1fb0 error 4
Jan 12 11:36:39 localhost kernel: .IptabLes[12706]: segfault at 0000000000000000 rip 000000000804e837 rsp 00000000ffeb1fb0 error 4
Jan 12 11:36:54 localhost kernel: .IptabLes[12723]: segfault at 0000000000000000 rip 000000000804e837 rsp 00000000ffeb1fb0 error 4
Jan 12 11:37:10 localhost kernel: .IptabLes[12739]: segfault at 0000000000000000 rip 000000000804e837 rsp 00000000ffeb1fb0 error 4
Jan 12 11:37:46 localhost kernel: .IptabLes[12756]: segfault at 0000000000000000 rip 000000000804e837 rsp 00000000ffeb1fb0 error 4



处理方法,删除关联的文件
# rm -f /.mylisthb*   产生的pid文件
# rm -f /boot/.IptabLex
# rm -f /boot/..IptabLes              编译后的二进制文件
# rm -f /etc/rc.d/init.d/IptabLex
# rm -f /etc/rc.d/init.d/IptabLes

因为这个文件发包基于什么协议,或者基于什么系统命令发包尚未清楚,所以在iptables这无法断定基于什么协议或者基于什么端口进行限制。



使用Denyhosts防止***SSHD扫描

denyhost是一个由python写的脚本会分析sshd的日志文件(/var/log/secure),可以实现对自动查找恶意ssh连接,然后把恶意ip加入到/etc/hosts.deny文件里面,以实现对服务器的安全防护。

1、检查安装条件
1) 判断系统安装的sshd是否支持tcp_wrappers(默认都支持),命令如下:
 ldd /usr/sbin/sshd |grep libwrap.so.0
如果有输出:libwrap.so.0 => /lib64/libwrap.so.0 则为支持
2)判断是否有python(centos默认都有):
python -v

3)要使用邮件报警功能,需要启动sendmail服务,使用带附件的功能,则还要安装sharutils

/etc/init.d/sendmail start

yum -y install sharutils

常用邮件发送方式:

 1.如何写一般的邮件:

  mail flynewton@gmail 编辑抄送对象,邮件主题,邮件正文后,按Ctrl-D结束

 2.快速发送方式:

  echo “邮件正文” | mail -s 邮件主题 flynewton@gmail.com

 3.以文件内容作为邮件正文来发送:

  mail -s test flynewton@gmail.com < test.txt

 4.发送带附件的邮件:

  uuencode 附件名称 附件显示名称 | mail -s 邮件主题 发送地址

 例如:uuencode test.txt test.txt | mail -s Test flynewton@gmail.com



2、安装及配置denyhosts工具
1)安装denyhosts

wget http://jaist.dl.sourceforge.net/sourceforge/denyhosts/DenyHosts-2.6.tar.gz
tar -xvzf DenyHosts-2.6.tar.gz 
cd DenyHosts-2.6
python setup.py install

根据输出可以看到:
程序脚本自动安装到/usr/share/denyhosts
2)设置启动脚本,配置配置文件:

cd /usr/share/denyhosts
cp daemon-control-dist daemon-control
chown root daemon-control
chmod 700 daemon-control
ln -s /usr/share/denyhosts/daemon-control /etc/init.d/denyhosts
chkconfig --add denyhosts
chkconfig denyhosts on

3)查看并配置denyhosts

root@node2:~# cat /etc/denyhosts.conf  | grep -v "^$"| grep -v "^#"

       ############ THESE SETTINGS ARE REQUIRED ############        //这些设定是必须的 
SECURE_LOG = /var/log/auth.log                        //指定sshd的日志文件,里面已经内置了一些日志文件的位置,只需要打开即可,打开的方法为删除#号。 
HOSTS_DENY = /etc/hosts.deny        //指定可以限制IP地址的文件,这里使用/etc/hosts.deny 
PURGE_DENY = 5m                             //过多长时间,把IP从/etc/hosts.deny里面清除 
BLOCK_SERVICE  = sshd                    //指定被保护的服务,这里要保护的是sshd 
DENY_THRESHOLD_INVALID = 1        //允许无效用户失败的次数,在/etc/passwd里面没有的用户(不包括root) 
DENY_THRESHOLD_VALID = 2            //允许正常用户失败的次数,在/etc/passwd里面有的用户(不包括root) 
DENY_THRESHOLD_ROOT = 1            //允许root用户失败的次数 
DENY_THRESHOLD_RESTRICTED = 1    //允许在$WORK_DIR/hosts-restricted里面出现的用户失败的次数 
WORK_DIR = /var/lib/denyhosts            //定义工作目录 
SUSPICIOUS_LOGIN_REPORT_ALLOWED_HOSTS=YES    //如果为YES,所有在$WORK_DIR/allowed-host时面的IP地址将会被认为是可疑的,如果设置为NO,所有在allowd-hosts试图登陆的结果,将不会发送警告邮件!所有不在$WORK_DIR/allowed-host时面的IP地址将会发送警告! 
HOSTNAME_LOOKUP=YES        //是否将IP地址解析为主机名,告警的时候使用主机名。 
LOCK_FILE = /run/denyhosts.pid        //定义PID文件的位置,确保同时只有一个Denyhost进程在运行 
       ############ THESE SETTINGS ARE OPTIONAL ############        //这里的设置是可选的 
ADMIN_EMAIL = gm100861@gmail.com               //当有人试图登录你的服务器,或者有IP被加入黑名单的时候,发送邮件到这里指定的邮箱。前提是,本机一定要可以发送邮件才行! 
SMTP_HOST = localhost                //指定smtp服务器 
SMTP_PORT = 25                            //指定SMTP端口号 
SMTP_FROM = DenyHosts <nobody@localhost>        //指定发件人 
SMTP_SUBJECT = DenyHosts Report                        //指定邮件主题 
AGE_RESET_VALID=5d                    //多长时间后,用户登录失败的次数被置为0,这里是指在/etc/passwd里面定义的用户,如果不设置,永远都不会置0 
AGE_RESET_ROOT=25d            //多长时间后,root用户登录失败的次数置为0,如果不设置,永远都不会置0 
AGE_RESET_RESTRICTED=25d    //在$WORK_DIR/hosts-restricted时面定义的用户,多长时间失败次数计数器被置为0 
AGE_RESET_INVALID=10d            //无效用户(不在/etc/passwd里面的用户)的失败次数计数器,多长时间被置为0 
RESET_ON_SUCCESS = yes            //当某个IP使用某个用户登录成功后,该IP对应的用户失败次数将会被置为0  
  ######### THESE SETTINGS ARE SPECIFIC TO DAEMON MODE  ########## 
DAEMON_LOG = /var/log/denyhosts        //当denyhost运行在守护进程模式的时候,日志文件存储的位置 
  
DAEMON_SLEEP = 30s                    //轮询查看SSHD日志的间隔 
DAEMON_PURGE = 1h                //多长时间清空$HOSTS_DENY中的IP地址,如果PURGE_DENY为空,这个设置将无效 
   #########   THESE SETTINGS ARE SPECIFIC TO     ########## 
   #########       DAEMON SYNCHRONIZATION         ##########

恶意的登录一下,再查看/etc/hosts.deny文件

root@node2:~# cat /etc/hosts.deny 
# /etc/hosts.deny: list of hosts that are _not_ allowed to access the system. 
#                  See the manual pages hosts_access(5) and hosts_options(5). 
# 
# Example:    ALL: some.host.name, .some.domain 
#             ALL EXCEPT in.fingerd: other.host.name, .other.domain 
# 
# If you're going to protect the portmapper use the name "portmap" for the 
# daemon name. Remember that you can only use the keyword "ALL" and IP 
# addresses (NOT host or domain names) for the portmapper, as well as for 
# rpc.mountd (the NFS mount daemon). See portmap(8) and rpc.mountd(8) 
# for further information. 
# 
# The PARANOID wildcard matches any host whose name does not match its 
# address. 
# 
# You may wish to enable this to ensure any programs that don't 
# validate looked up hostnames still leave understandable logs. In past 
# versions of Debian this has been the default. 
# ALL: PARANOID 
 
# DenyHosts: Mon Jul 16 16:46:46 2012 | sshd: 1.1.1.254 
sshd: 1.1.1.254

再查看一下管理员邮箱,收到报警邮件.


linux防御小规模DDOS***

    DDoS deflate是一款运行于Linux下,专门用于防止/减轻类DDOS***的程序。大多数DDOS***都是采取若干傀儡机同时对某一服务器发出大量连接请求,以耗光服务器资源的方法。

    DDOS***的现场,目标服务器会出现难以理解的高负载、高请求数量、单IP同时发出大量请求等症状,导致的直接后果就是Service Unavailable(服务不可用)。对于大面积的DDOS***,采用硬件防火墙才能有效地解决问题;但对于小面积的DDOS***,一般软件防火墙即可。由于DDOS***即模拟正常的访问请求,因此***IP与正常访问IP并没有本质的区别(无非连接数多而已)。

    DDoS deflate的原理是通过netstat命令找出 发出过量连接的单个IP,并使用iptables防火墙将这些IP进行拒绝。由于iptables防火墙拒绝IP的连接远比从Apache层面上来得高效,因此iptables便成了运行在Apache前端的“过滤器”。同样的DDoS deflate也可以设置采用APF(高级防火墙)进行IP阻止。


执行:

netstat -ntu | awk '{print $5}' | cut -d: -f1 | sort | uniq -c | sort -n

将会显示服务器上所有的每个IP多少个连接数。

每个IP几个、十几个或几十个连接数都还算比较正常,如果像上面成百上千肯定就不正常了。

1、安装DDoS deflate
wget http://www.inetbase.com/scripts/ddos/install.sh   //下载DDoS  deflate
chmod 0700 install.sh    //添加权限
./install.sh             //执行

2、配置DDoS deflate

下面是DDoS deflate的默认配置位于/usr/local/ddos/ddos.conf ,内容如下:

##### Paths of the script and other files
PROGDIR="/usr/local/ddos"
PROG="/usr/local/ddos/ddos.sh"
IGNORE_IP_LIST="/usr/local/ddos/ignore.ip.list"  //IP地址白名单
CRON="/etc/cron.d/ddos.cron"    //定时执行程序
APF="/etc/apf/apf"
IPT="/sbin/iptables"

##### frequency in minutes for running the script
##### Caution: Every time this setting is changed, run the script with --cron
#####          option so that the new frequency takes effect
FREQ=1   //检查时间间隔,默认1分钟

##### How many connections define a bad IP? Indicate that below.
NO_OF_CONNECTIONS=150     //最大连接数,超过这个数IP就会被屏蔽,一般默认即可

##### APF_BAN=1 (Make sure your APF version is atleast 0.96)
##### APF_BAN=0 (Uses iptables for banning ips instead of APF)
APF_BAN=1        //使用APF还是iptables。推荐使用iptables,将APF_BAN的值改为0即可。

##### KILL=0 (Bad IPs are'nt banned, good for interactive execution of script)
##### KILL=1 (Recommended setting)
KILL=1   //是否屏蔽IP,默认即可

##### An email is sent to the following address when an IP is banned.
##### Blank would suppress sending of mails
EMAIL_TO="root"   //当IP被屏蔽时给指定邮箱发送邮件,推荐使用,换成自己的邮箱即可

##### Number of seconds the banned ip should remain in blacklist.
BAN_PERIOD=600    //禁用IP时间,默认600秒,可根据情况调整

用户可根据给默认配置文件加上的注释提示内容,修改配置文件。


卸载DDos deflate

wget http://www.inetbase.com/scripts/ddos/uninstall.ddos
chmod 0700 uninstall.ddos
./uninstall.ddos

/usr/local/ddos/ddos.sh -k 30 强行屏蔽连接数大于30的IP

更多使用说明,请直接 more /usr/local/ddos/ddos.sh 查看