วันพุธที่ 21 ตุลาคม พ.ศ. 2552

Portsentry Program

สิ่งที่เหล่าบรรดา hacker นิยมทำก็คือ การทำการสแกน port ของเครื่อง server เป้าหมายเพื่อให้ทราบว่ามี port ใดเปิดอยู่บ้าง แล้วจึงค่อยทำการเจาะไปยัง service ที่เปิด port นั้นๆ ดังนั้นบรรดา Admin จึงจำเป็นจะต้องมีการป้องกันการสแกน port เอาไว้ สำหรับระบบปฏิบัติการ Linux นั้นก็มีโปรแกรมการป้องกันเอาไว้ให้ด้วยเช่นกัน นั้นคือ โปรแกรม portsentry ซึ่งจะช่วยตรวจจับผู้ที่ทำการ scan port แล้วนำค่า IP Address ของผู้บุกรุกที่กำลัง scan ไปเปรียบเทียบกับค่าที่ตั้งเอาไว้ว่าถูก block หรือไม่อนุญาตให้เข้ามาสู่ server ในคราวต่อไป โดยโปรแกรม portsentry เป็นโปรแกรมตรวจจับการ scan port แบบ real time และที่สำคัญ portsentry เป็นโปรแกรม Open Source ที่สามารถ ที่จะ download ได้ที่ http://sourceforge.net/projects/sentrytools โดยมีทั้งที่เป็นโปรแกรมที่จะต้องนำมา compile เอง และสำหรับ Linux ตระกูล RedHat หรือ Fedora ก็จะมีโปรแกรมพวก .rpm ด้วยเช่นกัน

การติดตั้งและการใช้งาน
1. ในการทดลองนี้ใช้ Fedora core 8 โดย download file portsentry-1.2-1.te.i386.rpm ได้ที่
http://rpm.pbone.net มาทำการติดตั้ง

[root@localhost ~]# rpm -ivh portsentry-1.2-1.te.i386.rpm
warning: portsentry-1.2-1.te.i386.rpm: Header V3 DSA signature: NOKEY, key ID df3d5207
Preparing... ########################################### [100%]
1:portsentry ########################################### [100%]
[root@localhost ~]#

โดยเมื่อติดตั้งเสร็จแล้ว ก็จะมีไฟล์ต่างๆเก็บไว้ที่ /etc/portsentry ซึ่งมีไฟล์หลักๆที่จะต้องทำการ configure อยู่ 2 ไฟล์ คือ

/etc/portsentry/portsentry.conf เป็นไฟล์ configuration หลักที่เก็บกฎในการตรวจสอบทั้งหมดไว้

/etc/portsentry/portsentry.ignore เป็นส่วนที่สำคัญมาก เพราะเป็นที่เก็บค่า IP
Address ของ Server เพื่อไม่ให้เกิดความผิดพลาดเนื่องจากการเรียกใช้งาน port ที่อยู่ใน List จะทำให้ Server โดน Back List ไม่สามารถใช้งานตามปกติได้

2. ทำการ configure ไฟล์ /etc/portsentry/portsentry.conf

#######################
# Port Configurations #
#######################
# กำหนดหมายเลข port ที่ป้องกันการ scan
TCP_PORTS="1,11,15,79,111,119,143,540,635,1080,1524,2000,5742,6667,12345,12346,20034,27665,31337,32771,32772,32773,32774,40421,49724,54320"
UDP_PORTS="1,7,9,69,161,162,513,635,640,641,700,37444,34555,31335,32770,32771,32772,32773,32774,31337,54321"
#
###########################################
# Advanced Stealth Scan Detection Options #
###########################################
# กำหนดหมายเลข port ว่างที่มักถูกผู้บุกรุก scan และใช้โจมตี
ADVANCED_PORTS_TCP="1024"
ADVANCED_PORTS_UDP="1024"
#
# กำหนด port ต้องห้ามไม่ให้เข้าในระบบเพราะเป็น port ที่ทำงานขณะทีเครื่อง Boot คือ บริการ ident(113), # NetBIOS(137-138), RIP(520), bootp broadcasts(67)
# Default TCP ident and NetBIOS service
ADVANCED_EXCLUDE_TCP="21,22,25,53,80,110,113,135,137,138,139,443"
# Default UDP route (RIP), NetBIOS, bootp broadcasts.
ADVANCED_EXCLUDE_UDP="520,517,518,513,138,137,123,68,67,53"

######################
# Configuration Files#
######################
# กำหนดตำแหน่งที่อยูของไฟล์ต่างๆ
# Hosts to ignore
IGNORE_FILE="/etc/portsentry/portsentry.ignore"
HISTORY_FILE="/etc/portsentry/portsentry.history"
BLOCKED_FILE="/etc/portsentry/portsentry.blocked"

##############################
# Misc. Configuration Options#
##############################
RESOLVE_HOST = "1"

##################
# Ignore Options #
##################
# กำหนดค่าที่จะป้องกัน
# 0 = Do not block UDP/TCP scans. ไม่ block การ scan TCP/UDP
# 1 = Block UDP/TCP scans. block ทั้ง TCP/UDP
# 2 = Run external command only (KILL_RUN_CMD)

BLOCK_UDP="1"
BLOCK_TCP="1"

###################
# Dropping Routes:#
###################
# เป็นการใส่ rule ที่ iptables ไม่ให้ผู้ที่บุกรุกเข้ามาที่ server ได้อีกครั้งหลังจากตรวจพบแล้ว
KILL_ROUTE="/sbin/iptables -I INPUT -s $TARGET$ -j DROP"

###############
# TCP Wrappers#
###############
# กำหนดให้มีการป้องกันด้วยการเพิ่ม IP Address ที่ตรวจพบใส่ในไฟล์ host.deny
KILL_HOSTS_DENY="ALL: $TARGET$"

#####################
# Scan trigger value#
#####################
# กำหนดจำนวน port ที่ยอมให้ connect เข้ามาในระบบ ได้มีตั้งแต่ 1-2 ถ้ากำหนดเป็น 0 จะเป็นการสั่งให้มี
# การบันทึกค่าใน Log file ทันนทีเมื่อพบว่าถูก scan port เพื่อให้ผู้ดูแลระบบทราบค่า default = 2 เป็นการ
# ลดจำนวนครั้งที่เตือน โดยทั่วไปใช้ค่า "0"
SCAN_TRIGGER="0"

# EOF

3. ตรวจสอบไฟล์ /etc/portsentry/portsentry.ignore ว่ามี IP Address ที่เราไม่สนใจที่จะตรวจสอบหรือไม่ ในที่นี้เราติดตั้งเป็น .rpm โปรแกรมจะเพิ่ม IP Address ในระบบให้เอง

[root@localhost portsentry]# more portsentry.ignore
# 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.
#
# PortSentry can support full netmasks for networks as well. Format is:
#
# /
#
# Example:
#
# 192.168.2.0/24
# 192.168.0.0/16
# 192.168.2.1/32
# Etc.
#
# If you don't supply a netmask it is assumed to be 32 bits.
#
#

127.0.0.1/32
0.0.0.0
#########################################
# Do NOT edit below this line, if you #
# do, your changes will be lost when #
# portsentry is restarted via the #
# initscript. Make all changes above #
# this box. #
#########################################

# Exclude all local interfaces
# เป็น IP Address ของ Server
192.168.10.24
127.0.0.1

# Exclude the default gateway(s)

# Exclude the nameservers

# And last but not least...
0.0.0.0

สั่ง start การทำงานโดยใช้คำสั่ง
[root@localhost portsentry]# portsentry -audp
[root@localhost portsentry]# portsentry -atcp

ตรวจสอบว่ามี process portsentry ทำงานหรือไม่ ให้ใช้คำสั่ง

[root@localhost portsentry]# ps -ef grep portsentry
root 2885 1 0 06:03 ? 00:00:00 /usr/sbin/portsentry -atcp
root 2887 1 0 06:03 ? 00:00:00 /usr/sbin/portsentry -audp
root 2934 1 0 06:19 ? 00:00:00 portsentry -audp
root 2937 1 0 06:19 ? 00:00:00 portsentry -atcp
root 2939 2695 0 06:19 pts/1 00:00:00 grep portsentry
[root@localhost portsentry]#

ถ้าต้องการยกเลิกการทำงาน โดยการ kill process ของ portsentry

[root@localhost portsentry]# killall portsentry
[root@localhost portsentry]# ps -ef grep portsentry
root 3013 2695 0 06:25 pts/1 00:00:00 grep portsentry
[root@localhost portsentry]#

สั่งมีการ start โปรแกรมทุกครั้งเมื่อเปิดเครื่อง
[root@localhost portsentry]# chkconfig --level 345 portsentry on
[root@localhost portsentry]# chkconfig --list portsentry
portsentry 0:off 1:off 2:off 3:on 4:on 5:on 6:off
[root@localhost portsentry]#

ผลการ scan

รูปที่1.Network Diagram

ทำการทดลองดังรูปที่ 1 โดยมี IP Address Web server เป็น 192.168.10.24 เหมือนเดิม แต่เปลี่ยน IP Address ของ PC เป็น 192.168.10.26 และทำการ scan port โดยใช้ Zenmap และทำการตรวจจับ packet โดยใช้โปรแกรม Wireshark ที่เครื่อง PC

รูปที่ 2 แสดงผลการ FIN scan หลังติดตั้ง โปรแกรม portsentry ที่ Web server


รูปที่ 3 แสดงผลการ NULL scan หลังติดตั้ง โปรแกรม portsentry ที่ Web server

รูปที่ 4 Flow Graph ของ Wireshark จากการตรวจจับการ FIN scan และ NULL scan

หลังจากที่เราได้ติดตั้ง โปรแกรม portsentry ที่เครื่อง Web server แล้วทำการเซ็ตค่า configure ต่างๆ พร้อมทั้ง start โปรแกรม จากนั้นก็ทำการ FIN scan และ NULL scan ใหม่อีกครั้ง ผลที่ได้จะไม่ปรากฏ port ที่เปิดของ Web server แต่ตัวเครื่อง Web server ยังเปิดให้บริการปกติ ส่วน IP Address 192.168.10.26 จะถูกเครื่อง Web server block เอาไว้ ซึ่งสามารถดูได้จาก

[root@localhost portsentry]# more portsentry.blocked.atcp
1245988981 - 06/26/2009 11:03:01 Host: 192.168.10.26/192.168.10.26 Port: 261 TCP Blocked
[root@localhost portsentry]#

จากนั้นระบบก็จะทำการนำ IP Address ไปใส่ไว้เป็น rule ของ iptables
[root@localhost ~]# iptables -L
Chain INPUT (policy ACCEPT)
target prot opt source destination
DROP all -- 192.168.10.26 anywhere

Chain FORWARD (policy ACCEPT)
target prot opt source destination

Chain OUTPUT (policy ACCEPT)
target prot opt source destination
[root@localhost ~]#
---------------------------
ขอขอบคุณเพื่อน ๆ กลุ่ม Webgateway ที่ร่วมกันทำการทดลองนี้ขึ้น
(วิศวเครือข่าย มาหาวิทยาลัยเทคโนโลยีมหานคร-MSNE#2)
1.นาย ธีรพงษ์ ขจรวุฒิตระกูล
2.นาย จิตกร กั้วมาลา
3.นาย สัมฤทธิ์ เทวะภูมิ
4.นาย ธีระศักดิ์ ถึงเสียบญวน
5.นาย วิเชียร นวกฤษ
6.นาย สุรเชษฐ์ ณ นคร
7.นาย สุรเชฐ ราชูการ

ไม่มีความคิดเห็น:

แสดงความคิดเห็น