Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2774194
  • 博文数量: 77
  • 博客积分: 10204
  • 博客等级: 上将
  • 技术积分: 5035
  • 用 户 组: 普通用户
  • 注册时间: 2006-03-05 22:13
文章分类
文章存档

2013年(1)

2010年(1)

2009年(17)

2008年(58)

我的朋友

分类: BSD

2008-09-26 16:58:19

PortSentry

監視所有進來的網路交通,監聽指定的ports,偵測可能是port scan的網路交通。當這類情形發生,PortSentry將在black-hole route或IPFW rule中阻擋該遠端機器存取本機器。

【port的安裝路徑】
/usr/ports/security/portsentry
binary會安裝到/usr/local/bin
config會安裝到/usr/local/etc/portsentry.conf,要編輯這個設定檔

【編輯設定檔】
把portsentry.conf檔打開,可以發現裡面已做了很好的分類,也有清楚的英文解釋,這裡就只做簡單的介紹

一、先看一下Port Configurations這部份,這部份決定要讓PortSentry監視那些ports,其中用TCP_PORTS,UDP_PORTS列出了三組,光看ports數也知道第一組是最嚴格的,第三組是最鬆的,預設是用中間這一組。如果要改用不同組的設定,將前面的 # 拿掉(uncomment,移除註釋)就對了。也可以自訂。要記得把service有用到的port從裡面移除,否則這個service就白搭了。


二、找到Dropping Routes這部份,在這裡決定如何阻擋(block)可疑主機的方式,有二種:
1.用IPFW
2.block-hole route(若無IPFW時,用這種)
用拿掉KILL_ROUTE前面 # 的方式,選用所想要的阻擋方式

如果使用IPFW,找到下列的文字,拿掉KILL_ROUTE前的#
# For those of you running FreeBSD (and compatible) you can
# use their built in firewalling as well.
#
KILL_ROUTE="/sbin/ipfw add 1 deny all from $TARGET$:255.255.255.255 to any"

如果沒有用IPFW,就用block-hole route方式。找到下列的文字,拿掉KILL_ROUTE前的#
# FreeBSD
KILL_ROUTE="route add -net $TARGET$ -netmask 255.255.255.255 127.0.0.1 -blackhole"

【啟動指令】
一旦啟用block的方式,PortSentry就準備好可執行了,由於目前的版本portsentry-1.1,沒有自動執行的script,所以照以下做一個portsentry.sh,並放在/usr/local/etc/rc.d目錄,並設為可執行,以後系統開機後,PortSentry就會啟動。

#!/bin/sh

PORTSENTRY="/usr/local/bin/portsentry"

case "$1" in
start)
${PORTSENTRY} -tcp && echo " Starting PortSentry TCP mode..."
${PORTSENTRY} -udp && echo " Starting PortSentry UDP mode..."
;;

stop)
killall `basename ${PORTSENTRY}`
;;
*)
echo ""
echo "Usage: `basename $0` { start | stop }"
echo ""
;;
esac

【觀看目前監聽的ports】
當PortSentry執行時,可使用sockstat指令觀看目前正監聽哪些ports
# sockstat
USER COMMAND PID FD PROTO LOCAL ADDRESS FOREIGN ADDRESS
root portsent 119 0 udp4 *:1 *:*
root portsent 119 1 udp4 *:7 *:*
root portsent 119 2 udp4 *:9 *:*
root portsent 119 3 udp4 *:69 *:*
root portsent 119 4 udp4 *:161 *:*
root portsent 119 5 udp4 *:162 *:*

【偵測記錄】
當偵測到攻擊,會記錄在這兩個檔案:
TCP的記錄:/usr/local/etc/portsentry.blocked.tcp
UDP的記錄:/usr/local/etc/portsentry.blocked.udp
上述的設定檔裡面就可看到記錄的位置了
PortSentry就是依這兩個檔案來追蹤那些機器已經被block,下回再偵測到相同時,即不再做一次上述的block動作

看一下真實的記錄例子(這些兔崽子,吃飽沒事幹,跑來整我的機器!)
# less portsentry.blocked.tcp
1024643476 - 06/21/2002 15:11:16 Host: netncare.postech.ac.kr/141.223.7.50 Port: 111 TCP Blocked
1024661878 - 06/21/2002 20:17:58 Host: h145-210-243-240.seed.net.tw/210.243.240.145 Port: 111 TCP Blocked
1024707182 - 06/22/2002 08:53:02 Host: pf111.plock.sdi.tpnet.pl/217.99.177.111 Port: 111 TCP Blocked
1024763681 - 06/23/2002 00:34:41 Host: adsl-34-227-20.bct.bellsouth.net/67.34.227.20 Port: 1080 TCP Blocked

注意一下,每當PortSentry啟動時,這些檔案會被清除。當機器reboot時,在執行期所加入的IPFW及routing table都會清除。所以之前被block的遠端機器,在機器reboot後,還可以再來連線。

如果使用IPFW KILL_ROUTE的方式,看目前的blocking rule的指令
# ipfw -a list
00001 35 2228 deny ip from 141.223.7.50 to any
00001 25 1684 deny ip from 210.243.240.145 to any
00001 0 0 deny ip from 217.99.177.111 to any
00001 17 800 deny ip from 67.34.227.20 to any

如果用black-hole routes的方式,則是:
# netstat -rn
Internet:
Destination Gateway Flags Refs Use Netif Expire
...
217.99.177.111/32 127.0.0.1 UGScB 0 20 lo0
(這個只是個例子)
那個 B flag就表示"block-hole" route,收到封包就丟棄。

另外,每次偵測到攻擊,/var/log/messages會記錄PortSentry的防制動作,以下是真實的記錄:
Jun 21 15:11:16 freebsd portsentry[117]: attackalert: Connect from host: netncare.postech.ac.kr/141.223.7.50 to TCP port: 111
Jun 21 15:11:16 freebsd portsentry[117]: attackalert: Host 141.223.7.50 has been blocked via wrappers with string: "ALL: 141.223.7.50"
Jun 21 15:11:16 freebsd portsentry[117]: attackalert: Host 141.223.7.50 has been blocked via dropped route using command: "/sbin/ipfw add 1 deny all from 141.223.7.50:255.255.255.255 to any"
Jun 21 15:11:16 freebsd portsentry[117]: attackalert: Connect from host: netncare.postech.ac.kr/141.223.7.50 to TCP port: 111
Jun 21 15:11:16 freebsd portsentry[117]: attackalert: Host: 141.223.7.50 is already blocked. Ignoring
Jun 21 20:17:58 freebsd portsentry[117]: attackalert: Connect from host: h145-210-243-240.seed.net.tw/210.243.240.145 to TCP port: 111
Jun 21 20:17:58 freebsd portsentry[117]: attackalert: Host 210.243.240.145 has been blocked via wrappers with string: "ALL: 210.243.240.145"
Jun 21 20:17:58 freebsd portsentry[117]: attackalert: Host 210.243.240.145 has been blocked via dropped route using command: "/sbin/ipfw add 1 deny all from 210.243.240.145:255.255.255.255 to any"

另一個同公司出品的工具,LogCheck,可以分析這個及其它記錄檔,並且寄出異常活動與攻擊偵測的每日報告給管理。

【萬一自己被擋了】
與IPFW一樣,當測試PortSentry時要小心,免得被擋住無法連線進去。萬一不慎被擋了,到別部機器或直接在console用下面指令的方式將錯誤的規則移除:
#ipfw delete 1

#route delet /32

若要避免某些機器不會被PortSentry擋住,將這些機器的IP加到/usr/local/etc/portsentry.ignore檔案中。這點在設定檔中也可看到。

【PortSentry的網站】


另外,以下是朋友寄給我的,在Linux下使用PortSentry的方法
portsentry 安裝方式:
1. make linux
2. make install
3. 修改 /usr/psionic/portsentry/portsentry.ignore
a. 將允許的 IP 輸入在這裡面、一般會放 default gateway, dns , mail, 等的 IP
4. 增加下面兩行於 /etc/rc.d/rc.local ,記得要順便執行下列兩行指令,不然要等到重開機才會生效..
/usr/psionic/portsentry/portsentry -atcp
/usr/psionic/portsentry/portsentry -sudp

5. 若有發生誤判時..
a. 請修改 /etc/hosts.deny 將被誤判的 IP 刪掉
b. 下此指令 route del -host 210.110.155.247 (被誤判的IP) reject 即可..


在Linux版還找到一篇關於PortSentry的文章

阅读(1288) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~