[ 永遠的UNIX::UNIX技術資料的寶庫 ]   GB | BIG5

首頁 > 安全技術 > 工具 > 正文
portsentry的安裝配置
本文出自: 作者: Yiming Gong(yiming@security.zz.ha.cn) (2001-09-27 12:00:01)
     -------------- 

      Copyright 2000-2003 by 水銀流動 Yiming Gong(yiming@security.zz.ha.cn)

      All rights reserved. This document may only be reproduced (whole or in 
      part) for non-commercial purposes. All reproductions must contain this 
      copyright notice and must not be altered, except by permission of the 
      author.

      --------------

       
      在網絡入侵中,幾乎所有攻擊行為的前奏都是針對目標主機的漏洞掃描,入侵者通過掃描發現目標系統的漏洞,然針對性的採取攻擊手段。一旦目標主機有沒有及時修復的漏洞,那這個系統所要面臨的局面就可怕了。另一方面,即使主機系統是安全的,某些開放的端口也會為有意入侵者提供不必要的敏感消息。
      自成熟的掃描器鼻祖工具SATAN在1995年4月誕生以來,越來越多的可供免費下載的掃描器在互連網上被公布出來,每天也都有越來越多的掃描事件發生。利用掃描器發現潛在的可攻擊的目標和發現系統漏洞已經成為網絡入侵者必備的技能,它也大大方便了入侵者的攻擊工作。我們可以看看下面的掃描記錄,這是我們利用目前在入侵者中使用極為廣泛的掃描工具vetescan對目標主機掃描產生的部分掃描報告:
      21/tcp open ftp
      23/tcp open telnet
      80/tcp open http
      111/tcp open sunrpc
      513/tcp open login
      514/tcp open shell
      800/tcp open unknown

      TCP Sequence Prediction: Class=random positive increments
      Difficulty=14856 (Worthy challenge)
      Remote operating system guess: Solaris 2.5, 2.5.1

      Nmap run completed -- 1 IP address (1 host up) scanned in 5 seconds
      =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
      Lets see what OS the Bitch has: Solaris 2.5, 2.5.1
      =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
      Checking for rpcinfo services: bitch said no
      =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
      Checking for rpc.cmsd vulnerability: bitch said no
      =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
      Checking for OpenLink 3.2 vulnerability: bitch said no
      =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
      Checking for amd vulnerability: bitch said no
      =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
      Checking for sadmin vulnerability: right on
      =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
      Checking for ftp services: bitch said no
      =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
      Vulnerable version of wu-ftpd: no 
      可以看到在摘錄的段內這個掃描工具報告了目標主機的活動端口,提供的服務,操作系統的類別,版本,以及系統的漏洞等等。從上述這個很短的報告中,我們就可以很清楚的了解到目標主機運行的是solaris2.5或solaris2.5.1系統,而且這個系統上有兩個"名"的漏洞sadmin,adm!我們知道,利用這兩個漏洞,入侵者可以很容易的取得超級用戶的權限!現在是我們自身對網絡的掃描,如果這次掃描是入侵者發起的呢,當入侵者利用相似的漏洞掃描軟件掃描到這台主機及其上兩個惡名遠揚的漏洞時,剩下的侵入工作就顯得太簡單了。
      如果管理員打算阻攔這樣的網絡掃描行為,那Portsentry是一個很好的選擇,這個軟件是Rowland所寫的用偵測,阻擋網絡掃描的免費工具。目前它的版本可用大多數主流的unix操作系統,如:Solaris,HPUNIX,Freebsd,AIX,SCO,Linux等。主機在安裝了這個系統以,可以對指定的一個或多個tcp/udp端口進行監聽,當這些端口被試圖連接或掃描時,portsentry能在瞬間捕捉這種連接或掃描企圖,並立刻加以屏蔽。除此以外,portsentry還有如下特別功能
      1: 可對外界的掃描動作生成詳細的日志記錄,包括發起掃描的主機名,掃描時間,連接的tcp/udp端口等。
      2:在linux操作系統下可捕捉SYN/half-open, FIN, NULL, X-MAS等諸多stealth掃描模式。
      3:可有效捕捉非連續隨機掃描,我們知道,很多防掃描軟件不能辨別隨機端口的掃描,portsentry不然,通過可記憶的內置引擎,即使掃描是隨機的,portsentry也可以立刻辨別出來並迅速加以屏蔽。
      4:可以與經典防火牆軟件tcp_wrapper結合,將發起掃描的主機寫入tcp_wrapper的hosts.deny文件中。
      5:可根據設置自動將發起掃描的主機在路由上重指,屏蔽續連接功能。使發起掃描的主機與系統喪失正常連接。

      下面我們就來介紹一下這個軟件的安裝使用
      1:下載
      管理員可以在http://www.psionic.com/abacus/portsentry這個網站下載相應文件,當前portsentry的最新版本為1.0版,下載,需要進行解壓,如下:
      Yiming.unix.org#gzip -d portsentry-1.0.tar.gz
      Yiming.unix.org#tar xvf portsentry-1.0.tar
      這樣會生成一個名為portsentry-1.0的目錄,我們進入這個目錄,可以看到內有README.install等相關文件,為了順利安裝它,我們主要需要關注兩個配置文件,portsentry_config.h和portsentry.conf。

      portsentry_config.h文件:
      這個文件主要是對系統的設置,我們看一看它的主要部分:
      - -------------
      /* These are probably ok. Be sure you change the Makefile if you */
      /* change the path */
      #define CONFIG_FILE "/usr/local/psionic/portsentry/portsentry.conf"

      /* The location of Wietse Venema's TCP Wrapper hosts.deny file */
      #define WRAPPER_HOSTS_DENY "/etc/hosts.deny"

      /* The default syslog is as daemon.notice. You can also use */
      /* any of the facilities from syslog.h to send messages to (LOCAL0, etc) 
*/
      #define SYSLOG_FACILITY LOG_LOCAL3
      #define SYSLOG_LEVEL LOG_NOTICE 
      - -------------
      CONFIG_FILE - 
      這裡代表著portsentry配置文件的路徑,可不做改動,當然,也可按照需要情況修改,但注意一點,如果此處改動了,需要同時修改Makefile中的INSTALLDIR和CHILDDIR。
      WRAPPER_HOSTS_DENY - 
      如果系統安裝了tcp_wrapper文件,則在這裡填入hosts.deny文件的路徑即可。這樣portsentry在察覺到有掃描活動時,可以將發起掃描的主機加入tcp_wrapper的hosts.deny文件。
      SYSLOG_FACILITY - 
      使用syslog的精靈級別,建議使用local級別的,如local3,這樣可以單獨生成portsentry自己的日志文件,簡潔方便。
      SYSLOG_LEVEL - 這裡可以保持原有配置不變。
      在本例中我們的SYSLOG_FACILITY使用的是local3,而為了使日志功能工作正常,我們知道需要在solaris的syslogd,所以在/etc/syslog.conf文件中我們需要加入相應的配置,在本例中如下:
      - -------------
      #logfile for portsentry
      local3.info /var/log/portsentry.log
      - -------------
      加入再在/var/log下生成portsentry.log文件,kill -1 'cat 
      /etc/syslog.pid'重起syslogd進程,使syslogd讀新的配置,那關日志這一部分的準備工作即完成了。

      portsentry.conf
      這個文件是控制portsentry本身的各項設置的,文件的第一部分首先是對端口的基本配置,包括tcp和udp端口,在TCP_PORTS和UDP_PORTS是portsentry需要監控的端口,每個端口之間用","號分隔開,中間沒有空格,我們可以看以下片斷:
      #######################
      # Port Configurations #
      #######################
      # 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,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,32770,32771,32772,32773,32774,31337,54321"
      #
      # Use these if you just want to be aware:
      TCP_PORTS="1,11,15,79,82,88,111,119,143,540,635,1080,1524,2000,5742,6667,12345,12346,20034,31337,32771,32772,32773,32774,40421,49724,54320"
      UDP_PORTS="1,7,9,69,161,162,513,635,640,641,700,32770,32771,32772,32773,32774,31337,54321"
      #
      # Use these for just bare-bones
      #TCP_PORTS="1,11,15,110,111,143,540,635,1080,524,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"
      我們可以看到這一片斷中提供了3個小的選擇部分,由上而下,tcp和udp監控的端口數目逐漸減少, 
      portsentry軟件默認是選擇了第二部分,因為這一部分TCP_PORTS和UDP_PORTS前面的"#"注釋符號被取消了。管理員可以根據需要對監控的級別加以變化。同時,管理員也可以再添加需要監控的tcp和udp端口,只要是每一項總的端口不超過64個即可。

      我們再往下看portsentry.conf這個配置文件:
      ######################

      # Configuration Files#
      ######################
      #
      # Hosts to ignore
      IGNORE_FILE="/usr/local/psionic/portsentry/portsentry.ignore"
      # Hosts that have been denied (running history)
      HISTORY_FILE="/usr/local/psionic/portsentry/portsentry.history"
      # Hosts that have been denied this session only (temporary until next 
      restart)
      BLOCKED_FILE="/usr/local/psionic/portsentry/portsentry.blocked"
      在這一部分的設置中IGNORE_FILE項目表示凡是在portsentry.ignore文件中的主機發起的掃描及對特定端口的連接portsentry不會加以屏蔽。如果管理員打算加入可信主機時,在這個文件中加入可信主機的ip即可。
      在HISTORY_FILE所跟的portsentry.history為發起掃描主機的歷史紀錄文件,BLOCKED_FILE所跟的portsentry.blocked為存放本次portsentry啟動發起掃描的主機記錄文件。它們均為系統自動生成的,格式類似下面的記錄:
      965974779 - 08/11/2000 14:19:39 Host: hack.unix.org/11.22.33.44 Port: 1 
      TCP Blocked,從中管理員可以檢查端口掃描的情況。

      再來看portsentry.conf中下一個比較重要的部分:
      在本文開始的時候,曾經講到portsentry具有路由重指屏蔽功能,當portsentry攔截到掃描企圖時,可以將系統與發起掃描的主機之間的路由重指,使發起掃描的主機喪失與安裝portsentry所在系統的正常連接,這樣一方面使掃描工作無法進行下去,另一方面,使發起掃描的主機在路由上不可能與安裝portsentry的系統再次連解。也就是說,當路由重指,發起掃描的主機與安裝portsentry的連接被"永遠"中斷了。這一部分的配置如下:
      ###################
      # Dropping Routes:#
      ###################
      # 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"

      # Newer versions of Linux support the reject flag now。 This 
      # is cleaner than the above option。
      #KILL_ROUTE="/sbin/route add -host $TARGET$ reject"

      # Generic BSD (BSDI, OpenBSD, NetBSD, FreeBSD)
      #KILL_ROUTE="/sbin/route add $TARGET$ 333.444.555.666"

      # Generic Sun 
      KILL_ROUTE="/usr/sbin/route add $TARGET$ 11.22.33.55 1"

      # NEXTSTEP
      #KILL_ROUTE="/usr/etc/route add $TARGET$ 127.0.0.1 1"

      # FreeBSD (Not well tested。)
      #KILL_ROUTE="route add -net $TARGET$ -netmask 255.255.255.255 127.0.0.1 
      -blackhole"

      # Digital UNIX 4.0D (OSF/1 / Compaq Tru64 UNIX)
      #KILL_ROUTE="/sbin/route add -host -blackhole $TARGET$ 127.0.0.1"

      # Generic HP-UX
      #KILL_ROUTE="/usr/sbin/route add net $TARGET$ netmask 255.255.255.0 
      127.0.0.1"
      如上我們根據具體安裝的系統類型選擇即可,本例中我們安裝portsentry的操作系統用的是solaris,所以我們將Generic 
      Sun部分下面的#注釋號取消,同時,將KILL_ROUTE="/usr/sbin/route add 
      $TARGET$跟一個"死"的網關,比如與安裝portsentry系統在同一網段的主機ip,這樣,就從路由上改變了數據包的方向。

      配置文件的最一部分是告警標示,在PORT_BANNER任意輸入你打算給掃描者的告警信息即可。
      ######################
      # Port Banner Section#
      ######################
      PORT_BANNER="** please go away! -----Yiming----zzpenetrate**"
      至此,基本功能的配置就結束了。
      其實除此以外,portsentry還提供了一些其它的安裝選項,如果管理員有更多的興趣,可以在portsentry.conf中的ADVANCED_PORTS_TCP, 
      External Command等地方對配置加以細化。

      2:編譯:
      在上述關鍵文件設置無誤,在portsentry的主目錄下執行make <systype>,<systype>可為linux, bsd, 
      solaris, hpux, hpux-gcc,freebsd, openbsd, netbsd, bsdi, aix, osf, 
      generic,管理員根據所在操作系統選擇即可,我們實驗的操縱系統為solaris,所以執行make 
      solaris編譯即可,編譯結束,再執行make 
      install,這樣,portsentry的關鍵文件即會生成到前面配置文件中所設的/usr/local/psionic/portsentry下。我們可以在這個目錄下看到portsentry,portsentry.conf,portsentry.ignore三個文件。

      3:運行
      portsentry有基本的工作狀態有兩中:portsentry -tcp, portsentry -udp,分別代表對tcp 
      和udp端口進行監控,對linux來講,還有其它四種模式: portsentry -stcp, portsentry -atcp, 
      portsentry -sudp, portsentry -audp,這裡就不介紹了。
      現在我們打算對tcp端口進行監控,所以我們在/usr/local/psionic/portsentry下執行./portsentry 
      -tcp,portsentry系統即開始運行,我們可以看上面講到的日志文件/var/log/port文件,如下:
      Aug 11 22:12:20 secu.unix.org portsentry[8398]: adminalert: Psionic 
      PortSentry 1.0 is starting.
      Aug 11 22:12:20 secu.unix.org portsentry[8399]: adminalert: Going into 
      listen mode on TCP port: 1
      Aug 11 22:12:20 secu.unix.org portsentry[8399]: adminalert: Going into 
      listen mode on TCP port: 11
      Aug 11 22:12:20 secu.unix.org portsentry[8399]: adminalert: Going into 
      listen mode on TCP port: 15
      Aug 11 22:12:20 secu.unix.org portsentry[8399]: adminalert: Going into 
      listen mode on TCP port: 15
      Aug 11 22:12:20 secu.unix.org portsentry[8399]: adminalert: Going into 
      listen mode on TCP port: 79
      Aug 11 22:12:20 secu.unix.org portsentry[8399]: adminalert: Going into 
      listen mode on TCP port: 82
      Aug 11 22:12:20 secu.unix.org portsentry[8399]: adminalert: Going into 
      listen mode on TCP port: 88
      Aug 11 22:12:20 secu.unix.org portsentry[8399]: adminalert: Going into 
      listen mode on TCP port: 111
      Aug 11 22:12:20 secu.unix.org portsentry[8399]: adminalert: Going into 
      listen mode on TCP port: 119
      Aug 11 22:12:20 secu.unix.org portsentry[8399]: adminalert: Going into 
      listen mode on TCP port: 143
      Aug 11 22:12:20 secu.unix.org portsentry[8399]: adminalert: Going into 
      listen mode on TCP port: 540
      Aug 11 22:12:20 secu.unix.org portsentry[8399]: adminalert: Going into 
      listen mode on TCP port: 635
      Aug 11 22:12:20 secu.unix.org portsentry[8399]: adminalert: Going into 
      listen mode on TCP port: 1080
      Aug 11 22:12:20 secu.unix.org portsentry[8399]: adminalert: Going into 
      listen mode on TCP port: 1524
      (省略一部分) Aug 11 22:12:20 secu.unix.org portsentry[8399]: adminalert: 
      PortSentry is now active and listening。
      我們看到在系統啟動有三個過程,首先是Psionic PortSentry 1.0 
      程序本身啟動。其是對所設置需要監控的端口逐個加載,最PortSentry進入激活偵聽狀態。這時portsentry即啟動完畢。

      4:掃描試驗
      我們可以就以上設置來做實驗,首先,我們來從hack.unix.org這台主機來嘗試掃描安裝了portsentry的主機secu.unix.org,我們使用的端口掃描工具為目前在unix下很流行的nmap,操作如下,首先,在未啟動portsentry的狀況下開始掃描。 
      Hack.unix.org# nmap secu.unix.org
      Starting nmap V. 2.30BETA17 by fyodor@insecure.org ( 
      www.insecure.org/nmap/ )
      Interesting ports on secu.unix.org
      Port State Service
      1/tcp open tcpmux
      11/tcp open systat
      15/tcp open netstat
      21/tcp open ftp
      23/tcp open telnet
      25/tcp open smtp
      37/tcp open time
      43/tcp open whois
      79/tcp open finger
      80/tcp open http
      110/tcp open pop-3
      111/tcp open sunrpc
      119/tcp open nntp
      143/tcp open imap2
      540/tcp open uucp
      635/tcp open unknown
      901/tcp open samba-swat
      1080/tcp open socks
      1524/tcp open ingreslock
      1999/tcp open tcp-id-port
      2000/tcp open callbook
      4045/tcp open lockd
      6000/tcp open X11
      6667/tcp open irc
      31337/tcp open Elite
      32771/tcp open sometimes-rpc5
      32772/tcp open sometimes-rpc7
      32773/tcp open sometimes-rpc9
      32775/tcp open sometimes-rpc13
      32776/tcp open sometimes-rpc15
      Nmap run completed -- 1 IP address (1 host up) scanned in 1 second
      我們看到,portsentry沒有啟動時,secu.unix.org這台主機上所有的端口被清楚的掃描出來了。現在我們啟動portsentry以,再掃描看有什反應。
      Hack.unix.org# nmap secu.unix.org
      Starting nmap V. 2.30BETA17 by fyodor@insecure.org ( 
      www.insecure.org/nmap/)
      Interesting ports on secu.unix.org

      我們看到沒有任何端口信息輸出,nmap的端口查詢被終止了。此時,看一下portsentry的日志文件
      Jun 21 21:34:38 secu.unix.org portsentry[451]: attackalert: Connect from 
      host: hack.unix.org/hack.unix.org to TCP port: 1
      Jun 21 21:34:38 secu.unix.org portsentry[451]: attackalert: Host 
      hack.unix.org has been blocked via wrappers with string: "ALL: 
      hack.unix.org"
      Jun 21 21:34:38 secu.unix.org portsentry[451]: attackalert: Host 
      hack.unix.org has been blocked via dropped route using command: 
      "/usr/sbin/route add hack.unix.org 11.22.33.55 1"
      可以看到portsentry在掃描開始的瞬間即被激活。隨portsentry將hack.unix.org這台主機加入了tcp_wrapper的hosts.deny文件中。然,將路由重指。我們可以看看secu.unix.org此時的路由表:
      secu.unix.org#netstat -rn 
      相關部分如下:
      Routing Table:
      Destination Gateway Flags Ref Use Interface
      - -------------------- -------------------- ----- ----- ------ ---------
      hack.unix.org 11.22.33.55 UGH 0 5571
      這時,因為網關被改為11.22.33.55,所以hack.unix.org與secu.unix.org不可能正常連接了,此時,別說掃描了,連hack.unix.org 
      發起的ping這個動作,secu.unix.org都不會再響應。

      上面是對主機的掃描,我們再對portsentry監控的單個端口連接看看,在下面的例子中,我們連接secu.unix.org的tcp 11端口。
      hack2.unix.org#telnet secu.unix.org 11
      Trying secu.unix.org...
      Connected to secu.unix.org.
      Escape character is '^]'.
      ** please go away! -----Yiming----zzpenetrate**
      Connection closed by foreign host
      我們看到,對端口11的連接被拒絕了,同時所設定的告警標示也產生作用。與上面的例子相同,此時secu.unix.org的路由表中hack2.unix.org也被重新定向,hack2.unix.org這個來源的數據包將喪失與secu.unix.org的連接。

      由上我們可以看出,portsentry在防范網絡掃描,阻攔端口惡意連接上的功能是極為強大的。對安全管理員來講,當自己的網絡倍受掃描的威脅時,portsentry不失為一個很好的應對工具 (http://www.fanqiang.com)
    進入【UNIX論壇

相關文章
 

★  樊強制作 歡迎分享  ★