防止惡意掃描 用PortSentry保護(hù)Linux服務(wù)器
原創(chuàng)【51CTO獨(dú)家特稿】我在檢查一臺(tái)CentOS 5.5服務(wù)器的安全環(huán)境時(shí),發(fā)現(xiàn)很多IP在惡意掃描此服務(wù)器的端口。本來想部署snort防入侵環(huán)境的,后來發(fā)現(xiàn)snort環(huán)境部署非常復(fù)雜,而以上的惡意掃描完全可以用PortSentry來實(shí)現(xiàn)。PortSentry是入侵檢測(cè)工具中配置最簡(jiǎn)單、效果最直接的工具之一。PortSentry是Abacus工程的一個(gè)組成部分。Abacus工程的目標(biāo)是建立一個(gè)基于主機(jī)的網(wǎng)絡(luò)入侵檢測(cè)系統(tǒng),可以從http://www.psonic.com得到關(guān)于Abacus工程更為詳細(xì)的信息。雖然PortSentry被Cisco收購后不再開發(fā),但絲毫不影響此軟件的強(qiáng)大功能。PortSentry可以實(shí)時(shí)檢測(cè)幾乎所有類型的網(wǎng)絡(luò)掃描,并對(duì)掃描行為做出反應(yīng)。一旦發(fā)現(xiàn)可疑的行為,PortSentry可以采取如下一些特定措施來加強(qiáng)防范:
◆給出虛假的路由信息,把所有的信息流都重定向到一個(gè)不存在的主機(jī);
◆自動(dòng)將對(duì)服務(wù)器進(jìn)行端口掃描的主機(jī)加到TCP-Wrappers的/etc/hosts.deny文件中去,我個(gè)人比較喜歡這種方式,因?yàn)榫€上許多環(huán)境并非都能打開iptables,這個(gè)選項(xiàng)也是PortSentry默認(rèn)的功能;
◆利用Netfilter機(jī)制,用包過濾程序,比如iptables和ipchain等,把所有非法數(shù)據(jù)包(來自對(duì)服務(wù)器進(jìn)行端口掃描的主機(jī))都過濾掉;
◆通過syslog()函數(shù)給出一個(gè)目志消息,甚至可以返回給掃描者一段警告信息。
一、PortSentry的安裝
下面詳細(xì)介紹PortSentry工具的安裝和配置方法。
1.從http://sourceforge.net/projects/sentrytools/下載軟件的最新版portsentry-1.2.tar.gz,用root用戶執(zhí)行如下命令進(jìn)行安裝:
#tar zxvf portsentry-1.2.tar.gz #cd portsentry-1.2_beta #make #make install
進(jìn)行到這步時(shí)發(fā)現(xiàn)報(bào)錯(cuò),系統(tǒng)生成不了protsentry執(zhí)行文件,我們查看Makefile文件時(shí)發(fā)現(xiàn),make后面根據(jù)操作系統(tǒng)的不同有許多選項(xiàng)。
所以我們重新執(zhí)行此步操作,將目錄刪除重新解壓縮
然后我們執(zhí)行make linux,發(fā)現(xiàn)系統(tǒng)仍然報(bào)錯(cuò),如下:
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字樣的那行調(diào)整為一行即可,如圖1-1所示
圖1-1 文字有白線標(biāo)記的那行代碼應(yīng)調(diào)整為一行
調(diào)整后我們?cè)賵?zhí)行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.
#p#
二、PortSentry的配置
1.修改配置文件portsentry.conf
通過PortSentry進(jìn)行入侵檢測(cè),首先需要為它定制一份需要監(jiān)視的端口清單,以及相應(yīng)的阻止對(duì)策。然后啟動(dòng)后臺(tái)進(jìn)程對(duì)這些端口進(jìn)行檢測(cè),一旦發(fā)現(xiàn)有人掃描這些端口,就啟動(dòng)相應(yīng)的對(duì)策進(jìn)行阻攔。
(1)設(shè)置端口清單
下面給出portsentry.conf中關(guān)于端口的默認(rèn)配置情況:
#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"
可以有選擇地去掉前面的注釋來啟用默認(rèn)配置,也可以根據(jù)自己的實(shí)際情況定制一份新的清單,格式和原來的一樣即可。端口列表要根據(jù)具體情況而定,假如服務(wù)器為Web服務(wù)器,那么Web端口就不需要監(jiān)視。反之,如果是FTP服務(wù)器,那么監(jiān)視Web端口也是有必要的。
(2)portsentry.conf里的相關(guān)文件
在portsentry.conf中自動(dòng)配置了許多文件,我們看下它們有哪些用途:
#此文件記錄允許合法掃描服務(wù)器的主機(jī)地址 IGNORE_FILE="/usr/local/psionic/portsentry/portsentry.ignore" #此文件中保留了以往所有入侵主機(jī)的IP歷史記錄 HISTROY_FILE="/usr/lcal/psionic/portsentry/portsentry.history" #此文件中是已經(jīng)被阻止連接的主機(jī)IP記錄 BLOCKED_FILE="/usr/local/psionic/portsentry/portsentry.blocked"
(3)設(shè)置路由重定向
通過配置portsentry.conf文件,可以設(shè)置一條虛擬路由記錄,把數(shù)據(jù)包重定向到一個(gè)未知的主機(jī).使之無法獲取信息。相應(yīng)配置代碼如下:
#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
針對(duì)不同的平臺(tái)有不同的路由命令,在配置文件中選擇適臺(tái)自己平臺(tái)的命令即可。我的服務(wù)器是CentOS 5.5 x86_64,以上語法適合Linux平臺(tái)的機(jī)器;PortSentry非常人性化,下面都有系統(tǒng)對(duì)應(yīng)的配置文件,我們只需要依樣操作即可。
(4)我們還可以利用Linux中的iptables命令,可以切斷攻擊主機(jī)的連接:
KILL_ROUTE="/usr/local/sbin/iptables -I INPUT -s $TARGET$ -j DROP"
也可以直接把攻擊者的IP記錄到/etc/hosts.deny文件中,利用TCP_Wrappers保護(hù)機(jī)制來防止攻擊:
KILL_HOSTS_DENY="ALL:$TARGET$ # Portsentry blocked"
系統(tǒng)默認(rèn)是利用TCP_Wrappers來切斷與主機(jī)之間的連接
(5)定制警告信息
我們也可以定制一條警告信息,警告攻擊者。不過,手冊(cè)上建議不要使用該選項(xiàng),因?yàn)檫@樣做可能會(huì)暴露主機(jī)的IDS系統(tǒng)。
PORT_BANNER="** UNAUTHORIZED ACCESS PROHIBITED *** YOUR CONNECTION ATTEMPT HAS BEEN LOGGED. GO AWAY."
修改完畢后,改變文件的權(quán)限.以保證其安全性:
chmod 600 /usr/local/psionic/portsentry/portsentry.conf
2.配置portsentry.ignore文件
/usr/psionic/portsentry/portsentry.ignore文件中設(shè)置了希望PortSentry忽略的主機(jī)IP,即允許合法掃描的主機(jī)地址下面是配置情況:
#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
記得帶上本機(jī)地址,以防萬一。
修改完成后同樣需要改變文件默認(rèn)的權(quán)限:
chmod 600 /usr/local/psionic/portsentry/portsentry.ignore
#p#
三、啟動(dòng)檢測(cè)模式
最后介紹一下PortSentry的啟動(dòng)檢測(cè)模式。對(duì)應(yīng)TCP和UDF兩種協(xié)議方式,PortSentry分別有三種啟動(dòng)模式,即基本、秘密和高級(jí)秘密掃描檢測(cè)模式,合計(jì)6個(gè)模式。
◆ portsentry-tcp,TCP的基本端口綁定模式;
◆ portsentry-udp,UDP的基本端口綁定模式;
◆ portsentry-stcp,TCP的秘密掃描檢測(cè)模式;
◆ portsentry-sudp,UDP的秘密掃描檢測(cè)模式;
◆ portsentry-atcp,TCP的高級(jí)秘密掃描檢測(cè)模式;
◆ portsentry-audp,UDP的高級(jí)秘密掃描檢測(cè)模式。
一般情況下,建議使用秘密掃描檢測(cè)模式或高級(jí)秘密掃描檢測(cè)模式。
使用高級(jí)秘密掃描檢測(cè)模式(Advanced Stealth Scan Detection Mode),PortSentry會(huì)自動(dòng)檢查服務(wù)器上正在運(yùn)行的端口, 然后把這些端口從配置文件中移去, 只監(jiān)控其它的端口。這樣會(huì)加快對(duì)端口掃描的反應(yīng)速度,并且只占用很少的CPU時(shí)間,這種模式非常智能,我比較喜歡用
啟動(dòng)PortSentry的命令如下:
#/usr/psionic/portsentry/portsentry -atcp
可以把啟動(dòng)命令加到“/etc/rc.d/rc.local”腳本文件中,果想讓它和其它后臺(tái)進(jìn)程一樣可以隨時(shí)啟動(dòng)、停止并查看進(jìn)程狀態(tài), 可以去這樣當(dāng)重新啟動(dòng)計(jì)算機(jī)的時(shí)候PortSentry就會(huì)自動(dòng)運(yùn)行。
四、測(cè)試
我們?cè)?92.168.1.102上啟動(dòng)PortSentry后,先暫時(shí)清掉portsentry.ignore里的文件,然后在另一臺(tái)192.168.1.104的機(jī)器上啟動(dòng)掃描命令nmap -sS 192.168.1.102。稍等片刻,就會(huì)發(fā)現(xiàn)/etc/hosts.deny里會(huì)出現(xiàn)ALL:192.168.1.104的字樣,證明此軟件配置都是生效的。
為了證明其有效性,我拿自己的線上的一臺(tái)LVS機(jī)器布署了portsentry1.2,查看日志發(fā)現(xiàn)
[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,發(fā)現(xiàn)如下惡意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
感覺現(xiàn)在的無聊人士也是非常多的,一天到晚開著掃描器在公網(wǎng)上,難道欲捕肉雞乎?
作者簡(jiǎn)介:余洪春(博客),網(wǎng)名撫琴煮酒,英文名Andrew.Yu,武漢某外企高級(jí)Linux/Unix系統(tǒng)管理員、項(xiàng)目實(shí)施工程師,紅帽RHCE講師,擅長負(fù)載均衡高可用和中小型證券類和商務(wù)網(wǎng)站架構(gòu),目前關(guān)注網(wǎng)站架構(gòu)研究及網(wǎng)絡(luò)安全。
【編輯推薦】