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,查看日誌發現

[[email protected]_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
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章