Hi!请登陆

CentOS下安装PortSentry防止恶意扫描

2020-10-27 53 10/27

我在检查一台Centos5.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的安装

下面详细介绍PortSentry工具的安装和配置方法.
从http://sourceforge.net/projects/sentrytools/ 下载软件的最新版 portsentry-1.2.tar.gz,用root用户执行如下命令进行安装:

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

进行到这步时发现报错,系统生成不了protsentry执行文件,
我们查看Makefile文件时发现,make后面根据操作系统的不同有许多选项.
所以我们重新执行此步操作,将目录删除重新解压缩.
然后我们执行make linux,发现系统仍然报错,如下:

SYSTYPE=linux
Making
cc -O -Wall -DLINUX -DSUPPORT_STEALTH -o ./portsentry ./portsentry.c
./portsentry_io.c ./portsentry_util.c
./portsentry.c: In function ?.ortSentryModeTCP?.
./portsentry.c:1187: warning: pointer targets in passing argument 3 of ?.ccept?.differ in signedness
./portsentry.c: In function ?.ortSentryModeUDP?.
./portsentry.c:1384: warning: pointer targets in passing argument 6 of ?.ecvfrom?.differ in signedness
./portsentry.c: In function ?.sage?.
./portsentry.c:1584: error: missing terminating " character
./portsentry.c:1585: error: ?.ourceforget?.undeclared (first use in this function)
./portsentry.c:1585: error: (Each undeclared identifier is reported only once
./portsentry.c:1585: error: for each function it appears in.)
./portsentry.c:1585: error: expected ?.?.before ?.ot?
./portsentry.c:1585: error: stray ?.?.in program
./portsentry.c:1585: error: missing terminating " character
./portsentry.c:1595: error: expected ?.?.before ?.?.token
make: *** [linux] Error 1

解决方法:

我们打开portsentry.c文件,在1590行左右,我们将带有Copyright 1997-2003字样的那行调整为一行即可,
调整后我们再执行make linux&& 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的配置

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/local/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$ gw 333.444.555.666

针对不同的平台有不同的路由命令,在配置文件中选择适台自己平台的命令即可.我的服务器是Centos5.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 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
192.168.1.103
192.168.1.102
127.0.0.1

记得带上本机地址,以防万一.
修改完成后同样需要改变文件默认的权限:

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

三、启动检测模式

最后介绍一下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/local/psionic/portsentry/portsentry -atcp

可以把启动命令加到 /etc/rc.d/rc.local 脚本文件中,果想让它和其它后台进程一样可以随时启动、停止并查看进程状态, 可以去这样当重新启动计算机的时候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
Tag:

相关推荐