วันศุกร์ที่ 22 สิงหาคม พ.ศ. 2551

โปรแกรม portsentry

พอดีมีประสบการณ์ในด้านนี้มาบ้างเลยอยากจะแนะนำให้เพื่อนๆพี่ๆน้องได้ลองทำ ดูกัน ก็เลยนำเอาการ config โปรแกรม Portsentry มาให้ดูกัน หวังว่าคงจะเป็นประโยชน์กันบ้างไม่มากไม่น้อย ซึ่งขึ้นอยู่กับการนำไปประยุกต์กับระบบของคุณเอง

โปรแกรม portsentry เป็นโปรแกรมที่สร้างขึ้นเพื่อตรวจสอบการ scan port แบบ real time สามารถตรวจจับการบุกรุกมายัง port ต่างๆที่ server เปิดให้บริการอยู่ บางคนตั้ง server แบบไม่ระมัดระวังพอติดตั้ง NOS เสร็จก็สนใจแต่เรื่องการ config ในส่วนที่จะให้บริการกับลูกข่ายเท่านั้น สามารถหา download โปรแกรมนี้ได้จาก www.psionic.com ปัจจุบันได้เปลี่ยนแปลงชื่อโปรแกรมไปเป็นชื่ออื่นแล้ว ลองศึกษาจากเวปไซต์ดังกล่าวดูนะคับ

สมมุตว่าผมไป download เจ้าโปรแกรมนี้มาแล้วที่มีชื่อว่า portsentry-1.1.tar.gz

# cd /tmp
# gzip -cd portsentry-1.1.tar.gz | tar xvf -
# cd portsentry-1.1
# pico Makefile (เข้าไปแก้ไขไฟล์ compile โดยแก้ไขตามนี้นะครับ)

CC = cc แก้ไขเป็น
CC = egcs
(ใน server ต้องติดตั้งโปรแกรม egcs ไว้ก่อน) หรือสามารถหาติดตั้งได้ใน /usr/ports

CFLAGS = -O -Wall แก้เป็น
CFLAGS = -O3 -march=i386 -mcpu=i386 -funroll-loops -fomit-frame-pointer -Wall

INSTALLDIR=/usr/local/psionic แก้ไขเป็น
INSTALLDIR=/etc

เสร็จแล้วทำการ save ไฟล์โดยการ Ctrl+X แล้วกด Y
หลังจากนั้นแก้ไขไฟล์ portserntry_config.h

#pico portsentry.h แล้วแก้ไขตามนี้นะครับ

define CONFIG_FILE "/usr/local/psionic/portsentry/portsentry.conf" แก้ไขเป็น
define CONFIG_FILE "/etc/portsentry/portserntry.conf"
เสร็จแล้วทำการ save ไฟล์โดยการ Ctrl+X แล้วกด Y

แล้วเริ่มต้นการ compile โดยใช้คำสั่งดังนี้
# make linux
# make install

ทำการลบไฟล์ต้นฉบับทิ้ง
# cd /tmp
# rm -rf portsentry*

หลังจากนั้นเข้าไปแก้ไขไฟล์ config ของ portsentry ใน path /etc/portsentry/

# pico portsentry.conf
เพิ่มคำสั่งพวกนี้เข้าไปในไฟล์ สามารถใส่ตรงบรรทัดไหนในไฟล์ก็ได้

# กำหนดหมายเลข port ที่ป้องกันการถูก scan
TCP_PORTS="1,11,15,79,111,119,143,540,635,1080,1524,2000,5742,6667,12345,12346,20034,31337,32771,32772,32774,40421,49724,54320"

#กำหนดหมายเลข Ports ว่างที่มักถูกผู้บุกรุก scan และใช้โจมตี
ADVANCED_PORTS_TCP="1023"
ADVANCED_PORTS_UDP="1023"

#กำหนด ports ต้องห้ามไม่ให้เข้าในระบบเพราะว่าเป็น port ที่ทำงานขณะที่เครื่อง boot คือบริการ ident(113) , NetBios(137-138) , RIP(520) , bootp broadcast(67)
ADVANCED_PORTS_TCP=”113,139”
ADVANCED_PORTS_UDP=”520,138,137,67”

#กำหนดตำแหน่งที่อยู่ของไฟล์ต่างๆ
IGNORE_FILE=”/etc/portsentry/portsentry.ignore”
HISTORY_FILE=”/var/log/portsentry/portsentry.history”
BLOCK_FILE=”/var/log/portsentry/portsentry.blocked”

#กำหนดค่าที่จะป้องกัน
#0=ไม่ block การ scan TCP/UDP
#1=block ทั้ง TCP_UDP
#2=block external command เท่านั้น
BLOCK_UDP=”1”
BLOCK_TCP=”1”

#คำสั่งนี้ไม่ให้ผู้อื่นส่งคำสั่งมาเพิ่ม route ใหม่ในระบบ
KILL_ROUTE=”/sbin/route add –host $TARGET$ reject”
KILL_HOSTS_DENY=”ALL: $TARGET$”

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

#กำหนดข้อความแจ้งเตือนว่ามีการบุกรุก อันนี้แล้วแต่นะครับตามความต้องการของผู้ดูแลระบบ
PORT_BANNER=”** UNAUTHORIZED ACCESS PROHIBITED ** YOUR CONNECTION ATTEMPT HAS BEEN LOGGED. GO AWAY”

หลังจากแก้ไขและทำการตรวจสอบความถูกต้องแล้ว ให้ทำการ save ไฟล์ แล้วทำการกำหนด Permission ให้ไฟล์ด้วย
#chmod 600 /etc/portsentry/portsentry.conf

ตรวจสอบไฟล์ portsentry.ignore ดูว่ามีค่า IP Address ตามตัวอย่างหรือไม่ ถ้าเป็น Version ใหม่โปรแกรมจะเพิ่มค่า IP Address ให้เองไม่ต้องเข้าไปแก้ไขอะไร
#pico /etc/portsentry/portsentry.ignore
127.0.0.1
0.0.0.0

จากนั้นกำหนด Permission เป็น 600
#chmod 600 /etc/portsentry/portsentry.ignore

สร้าง Scripts ไปไว้ใน /etc/rc.d/init.d ชื่อ portsentry
#pico /etc/rc.d/init.d/portsentry (path ตามที่คุณได้ติดตั้ง init เอาไว้นะครับ ลองหาดูอาจจะไม่เหมือนของผมก็ได้)

แก้ไขในไฟล์ portsentry ตามนี้นะครับ

#/bin/sh
#
#portsentry start the portsentry port scan detector
#
#source function library
./etc/rc.d/init.d/functions
#get config
./etc/sysconfig/network
#check that networking is up
if [${NETWORKING}=”no”]
then
exit 0
fi
[ -f /usr/sbin/portsentry] || exit 0

#see how we were called
case “$1” in
start)
echo –n “Starting Port Scan Detector: ”
if [ -s /etc/portsentry/portsentry.modes] ; then
modes=’cut –d “#” –f (ต่อบรรทัดล่างด้วย)1 /etc/portsentry/portsentry.modes’
else
modes=”tcp udp”
fi
for i in $modes ; do
portsentry -$i
echo –n “$i”
done
echo
touch /var/lock/subsys/portsentry
;;
stop)
echo –n “Stopping Port Scan Detector: ”
killproc portsentry
echo
rm –f /var/lock/subsys/portsentry
;;
status)
status portsentry
esac
exit 0

จากนั้นกำหนด permission ให้ไฟล์ scrips นี้
#chmod 700 /etc/rc.d/init.d/portsentry

ทำการเพิ่ม script ให้ระบบเพื่อสั่งให้ทำงานขณะที่เครื่อง reboot
#chkconfig --portsentry
#chkconfig --level 345 portsentry on

สั่งให้โปรแกรมทำงาน
#/etc/rc.d/init.d/portsentry restart

คลังบทความของบล็อก