IT零起步-防止恶意扫描 安装PortSentry

       我在检查一台CentOS 5.5服务器的安全环境时,发现很多IP在恶意扫描此服务器的端口。本来想部署snort防入侵环境的,后来发现snort环境部署非常复杂,而以上的恶意扫描完全可以用PortSentry来实现。PortSentry是入侵检测工具中配置最简单、效果最直接的工具之一。PortSentry是Abacus工程的一个组成部分。Abacus工程的目标是建立一个基于主机的网络入侵检测系统,可以从http://www.psonic.com得到关于Abacus工程更为详细的信息。虽然PortSentry被Cisco收购后不再开发,但丝毫不影响此软件的强大功能。PortSentry可以实时检测几乎所有类型的网络扫描,并对扫描行为做出反应。一旦发现可疑的行为,PortSentry可以采取如下一些特定措施来加强防范:

◆给出虚假的路由信息,把所有的信息流都重定向到一个不存在的主机;

◆自动将对服务器进行端口扫描的主机加到TCP-Wrappers的/etc/hosts.deny文件中去,我个人比较喜欢这种方式,因为线上许多环境并非都能打开iptables,这个选项也是PortSentry默认的功能;

◆利用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 linux

# make install

注意:由于程序有误我们需要修改如下信息/portsentry_beta/portsentry.c

/* duh */

void

Usage (void)

{

  printf("PortSentry - Port Scan Detector.\n");

  printf("Copyright 1997-2003 Craig H. Rowland <craigrowland at users dot

sourceforget dot net>\n");

  printf("Licensing restrictions apply. Please see documentation\n");

  printf("Version: %s\n\n", VERSION);

 修改为:

/* duh */

void

Usage (void)

{

  printf("PortSentry - Port Scan Detector.\n");

  printf("Copyright 1997-2003 Craig H. Rowland <craigrowland at users dotsourceforget dot net>\n");

  printf("Licensing restrictions apply. Please see documentation\n");

  printf("Version: %s\n\n", VERSION);

 

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

 

  Edit /usr/local/psionic/portsentry/portsentry.confand change

  your settings if you haven't already. (route,etc)

  WARNING:This versionand 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 fileswhen

  the testing of this install is complete.

 

二、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,635,1080,1524,2000,2001,4000,4001,5742,6000,6001,6667,12345,12346,20034,27665,30303,32771,32772,32773,32774,31337,40421,40425,49724,54320"

#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,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"

(3)设置路由重定向

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

# Generic

#KILL_ROUTE="/sbin/route add $TARGET$333.444.555.666"

# Generic Linux

#KILL_ROUTE="/sbin/route add -host $TARGET$ gw333.444.555.666"

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

(4)我们还可以利用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来切断与主机之间的连接

(5)定制警告信息

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

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

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

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

2.配置portsentry.ignore文件

/usr/psionic/portsentry/portsentry.ignore文件中设置了希望PortSentry忽略的主机IP,即允许合法扫描的主机地址下面是配置情况:

#Put hosts in here you never want blocked,This includesthe IP addresses

#of all local interfaces on the protected host(i.evirtual host,mult-home)

#keep 127.0.0.1 and 0.0.0.0 to keep people from playinggames. 127.0.0.1/32 0.0.0.0

#Exclude all local interfaces   

192.168.1.103 192.168.1.102 127.0.0.1

记得带上本机地址,以防万一。

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

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

三、启动检测模式

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

◆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就会自动运行。

四、测试

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

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

[root@woiit.netportsentry_beta]# tail /var/log/messages Jun 6 13:11:07 localhostportsentry[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:07localhost portsentry[2555]: attackalert: Host 65.9.251.89 has been blocked viawrappers with string: "ALL: 65.9.251.89" Jun 6 13:11:07 localhostportsentry[2555]: attackalert: TCP SYN/Normal scan from host: adsl-65-9-251-89.mia.bellsouth.net/65.9.251.89to 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 Jun6 13:11:08 localhost portsentry[2555]: attackalert: TCP SYN/Normal scan fromhost: adsl-65-9-251-89.mia.bellsouth.net/65.9.251.89 to TCP port: 80 Jun 613:11:08 localhost portsentry[2555]: attackalert: Host:adsl-65-9-251-89.mia.bellsouth.net/65.9.251.89 is already blocked Ignoring Jun6 13:19:57 localhost portsentry[2555]: attackalert: TCP SYN/Normal scan fromhost: ns38534.ovh.net/91.121.14.153 to TCP port: 80 Jun 6 13:19:57 localhostportsentry[2555]: attackalert: Host 91.121.14.153 has been blocked via wrapperswith 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 TCPport: 80 Jun 6 13:35:44 localhost portsentry[2555]: attackalert: Host61.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

 

相关链接

1、http://tongwei1985.blog.163.com/blog/static/141483073201182811249641/

2、 http://www.2cto.com/Article/201209/153640.html
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值