网络无间,拉近你我。聊天、上网或者视频,这些是“看得见”的网络连接,在浩瀚的互联网上每时每刻“看不见”的网络连接是没有人能数清的。恶意扫描就是一种隐秘性很强的黑客行为,如何知道访问你的主机的连接是正常行为还是黑客行为,这是一个问题。

  应对黑客恶意扫描,许多人想到了部署snort防入侵环境,诚然它是一种非常强大专业的工具,但是部署非常复杂,对于没有特殊威胁我们完全可以采用更加轻量的PortSentry来实现。PortSentry是一款配置简单、效果直接的防入侵检测工具,现已被思科收购。PortSentry可以实时检测几乎所有类型的网络扫描,并对扫描行为做出反应。

  一旦检测到恶意扫描,PortSentry可以向攻击者发出虚假的路由信息,把所有的信息流都重定向到一个不存在的主机;或者自动将对服务器进行端口扫描的主机加到TCP-Wrappers的/etc/hosts.deny文件中去,利用Netfilter机制,用包过滤程序,比如iptables和ipchain等,或者把所有非法数据包(来自对服务器进行端口扫描的主机)都过滤掉;或者通过syslog()函数给出一个目志消息,甚至可以返回给扫描者一段警告信息。

  安装PortSentry

  1.从sourceforge网站下载软件的最新版portsentry-1.2.tar.gz,用root用户执行如下命令进行安装:

  #tar zxvf portsentry-1.2.tar.gz
  #cd portsentry-1.2_beta
  #make
  #make install

  PortSentry顺利安装成功,其安装路径为/usr/local/psionic/portsentry,如下所示表示成功安装此软件:

  Edit /usr/local/psionic/portsentry/portsentry.conf and change
  your settings if you haven’t already. (route,etc)
  WARNING:This version and above now use a new
  directory structure for storing the program
  and config files (/usr/local/psionic/portsentry).
  Please make sure you delete the old files when
  the testing of this install is complete.

配置PortSentry 设置端口清单
  配置PortSentry

  1.修改配置文件portsentry.conf

  通过PortSentry进行入侵检测,首先需要为它定制一份需要监视的端口清单,以及相应的阻止对策。然后启动后台进程对这些端口进行检测,一旦发现有人扫描这些端口,就启动相应的对策进行阻拦。

  (1)设置端口清单

  下面给出portsentry.conf中关于端口的默认配置情况:

  #Un-comment these if you are really anal;
  #TCP_PORTS=”1,7,9,11,15,70,79,80,109,110,111,119,138,139,143,512,513,514,515,540,636,1080,1424,2000,2001,[..]
  #UDP_PORTS=”1,7,9,66,67,68,69,111,137,138,161,162,474,513,517,518,635,640,641,666,700,2049,31335,27444,34555,[..]
  #Use these if you just want to be aware:
  TCP_PORTS=”1,11,15,79,111,119,143,540,635,1080,1524,2000,5742,6667,12345,12346,20034,27665,31337,32771,32772,[..]
  UDP_PORTS=”1,7,9,69,161,162,513,635,640,641,700,37444,34555,31335,32770,32771,32772,32773,32774,31337,54321″
  #Use these for juse bare-bones
  #TCP_PORTS=”1,11,15,110,111,143,540,635,180,1524,2000,12345,12346,20034,32771,32772,32773,32774,49724,54320″
  #UDP_PORTS=”1,7,9,69,161,162,513,640,700,32770,32771,32772,32773,32774,31337,54321″

  可以有选择地去掉前面的注释来启用默认配置,也可以根据自己的实际情况定制一份新的清单,格式和原来的一样即可。端口列表要根据具体情况而定,假如服务器为Web服务器,那么Web端口就不需要监视。反之,如果是FTP服务器,那么监视Web端口也是有必要的。

  (2)portsentry.conf里的相关文件

  在portsentry.conf中自动配置了许多文件,我们看下它们有哪些用途:

  #此文件记录允许合法扫描服务器的主机地址
  IGNORE_FILE=”/usr/local/psionic/portsentry/portsentry.ignore”
  #此文件中保留了以往所有入侵主机的IP历史记录
  HISTROY_FILE=”/usr/lcal/psionic/portsentry/portsentry.history”
  #此文件中是已经被阻止连接的主机IP记录
  BLOCKED_FILE=”/usr/local/psionic/portsentry/portsentry.blocked”

设置PortSentry应对恶意检测策略
  设置路由重定向

  通过配置portsentry.conf文件,可以设置一条虚拟路由记录,把数据包重定向到一个未知的主机.使之无法获取信息。相应配置代码如下:

  #Generic
  #KILL_ROUTE=”/sbin/route add $TARGET$ 333.444.555.666″
  #Generic Linux
  KILL_ROUTE=”/sbin/route add -host $TARGET$ gw 333.444.555.666

  针对不同的平台有不同的路由命令,在配置文件中选择适台自己平台的命令即可。我的服务器是Centos5.5 x86_64,以上语法适合Linux平台的机器;PortSentry非常人性化,下面都有系统对应的配置文件,我们只需要依样操作即可。

  我们还可以利用Linux中的iptables命令,可以切断攻击主机的连接:

  KILL_ROUTE=”/usr/local/sbin/iptables -I INPUT -s $TARGET$ -j DROP”

  也可以直接把攻击者的IP记录到/etc/hosts.deny文件中,利用TCP_Wrappers保护机制来防止攻击:

  KILL_HOSTS_DENY=”ALL:$TARGET$ # Portsentry blocked”

  系统默认是利用TCP_Wrappers来切断与主机之间的连接

  定制警告信息

  我们也可以定制一条警告信息,警告攻击者。不过,手册上建议不要使用该选项,因为这样做可能会暴露主机的IDS系统。

  PORT_BANNER=”** UNAUTHORIZED ACCESS PROHIBITED *** YOUR CONNECTION ATTEMPT HAS BEEN LOGGED. GO AWAY.”

  修改完毕后,改变文件的权限.以保证其安全性:

  chmod 600 /usr/local/psionic/portsentry/portsentry.conf

设置忽略检测主机开启检测模式
  配置portsentry.ignore文件

  /usr/psionic/portsentry/portsentry.ignore文件中设置了希望PortSentry忽略的主机IP,即允许合法扫描的主机地址下面是配置情况:
  #Put hosts in here you never want blocked,This includes the IP addresses
  #of all local interfaces on the protected host(i.e virtual host,mult-home)
  #keep 127.0.0.1 and 0.0.0.0 to keep people from playing games.
  127.0.0.1/32
  0.0.0.0
  #Exclude all local interfaces
  127.0.0.1

  修改完成后同样需要改变文件默认的权限:

  chmod 600 /usr/local/psionic/portsentry/portsentry.ignore

  启动检测模式 www.2cto.com

  最后介绍一下PortSentry的启动检测模式。对应TCP和UDF两种协议方式.分别有三种启动模式,即基本、秘密和高级秘密扫描检测模式。

  ◆portsentry-tcp,TCP的基本端口绑定模式;
  ◆portsentry-udp,UDP的基本端口绑定模式;
  ◆portsentry-stcp,TCP的秘密扫描检测模式;
  ◆portsentry-sudp,UDP的秘密扫描检测模式;
  ◆portsentry-atcp,TCP的高级秘密扫描检测模式;
  ◆portsentry-audp,UDP的高级秘密扫描检测模式。

  一般情况下,建议使用秘密扫描检测模式或高级秘密扫描检测模式。

  使用高级秘密扫描检测模式(Advanced Stealth Scan Detection Mode),PortSentry会自动检查服务器上正在运行的端口, 然后把这些端口从配置文件中移去, 只监控其它的端口。这样会加快对端口扫描的反应速度,并且只占用很少的CPU时间,这种模式非常智能,我比较喜欢用

  启动PortSentry的命令如下:

  #/usr/psionic/portsentry/portsentry -atcp

  可以把启动命令加到“/etc/rc.d/rc.local”脚本文件中,果想让它和其它后台进程一样可以随时启动、停止并查看进程状态, 可以去这样当重新启动计算机的时候PortSentry就会自动运行。

多少黑客在扫你 测试PortSentry

  测试效果

  我们在192.168.1.102上启动PortSentry后,我们先暂时清掉portsentry.ignore里的文件,我们在另一台192.168.1.104的机器上启动扫描命令nmap -sS 192.168.1.102,稍等片刻我们就会发现/etc/hosts.deny里会出现ALL:192.168.1.104的字样,证明此软件配置都是生效的。

  为了证明其有效性,我拿自己的线上的一台LVS机器布署了portsentry1.2,查看日志发现

  [root@localhost portsentry_beta]# tail /var/log/messages

  Jun 6 13:11:07 localhost portsentry[2555]:attackalert:TCP SYN/Normal scan from host: adsl-65-9-251-89.mia.bellsouth.net/65.9.251.89 to TCP port:80

  Jun 6 13:11:07 localhost portsentry[2555]:attackalert:Host 65.9.251.89 has been blocked via wrappers with string:”ALL:65.9.251.89″

  Jun 6 13:11:07 localhost portsentry[2555]:attackalert:TCP SYN/Normal scan from host:adsl-65-9-251-89.mia.bellsouth.net/65.9.251.89 to TCP port:80

  Jun 6 13:11:07 localhost portsentry[2555]:attackalert:Host:adsl-65-9-251-89.mia.bellsouth.net/65.9.251.89 is already blocked Ignoring

  Jun 6 13:11:08 localhost portsentry[2555]:attackalert:TCP SYN/Normal scan from host:adsl-65-9-251-89.mia.bellsouth.net/65.9.251.89 to TCP port:80

  Jun 6 13:11:08 localhost portsentry[2555]:attackalert:Host:adsl-65-9-251-89.mia.bellsouth.net/65.9.251.89 is already blocked Ignoring

  Jun 6 13:19:57 localhost portsentry[2555]:attackalert:TCP SYN/Normal scan from host:ns38534.ovh.net/91.121.14.153 to TCP port:80

  Jun 6 13:19:57 localhost portsentry[2555]:attackalert:Host 91.121.14.153 has been blocked via wrappers with string:”ALL:91.121.14.153″

  Jun 6 13:35:44 localhost portsentry[2555]:attackalert:TCP SYN/Normal scan from host:61.156.31.43/61.156.31.43 to TCP port:80

  Jun 6 13:35:44 localhost portsentry[2555]:attackalert:Host 61.156.31.43 has been blocked via wrappers with string:”ALL:61.156.31.43″

  检查了下/etc/hosts.deny,发现如下恶意IP:

  ALL:113.57.224.3

  ALL:124.238.249.246

  ALL:65.9.251.89

  ALL:91.121.14.153

  ALL:61.156.31.43

- EOF -