ข่าว: ข่าวประกาศ

  • 19 เมษายน 2024, 19:26:45

เข้าสู่ระบบด้วยชื่อผู้ใช้ รหัสผ่าน และระยะเวลาในเซสชั่น

ผู้เขียน หัวข้อ: Kernel-2.6.24 + iptables-1.4.0 + L7-filter-2.17 + ipp2p-0.8.2  (อ่าน 25321 ครั้ง)

admin

  • Administrator
  • Hero Member
  • *****
  • กระทู้: 3820
    • ดูรายละเอียด
    • อีเมล์
Kernel-2.6.24 + iptables-1.4.0 + L7-filter-2.17 + ipp2p-0.8.2
« เมื่อ: 30 พฤษภาคม 2008, 18:14:05 »
Ref : http://suchart.wordpress.com/2008/02/02/kernel-2624-iptables-140-l7-filter-217-ipp2p-082-on-debian-40/

Kernel-2.6.24 + iptables-1.4.0 + L7-filter-2.17 + ipp2p-0.8.2 on Debian 4.0
Filed under: Debian, Linux, Ubuntu — suchart @ 2:20 pm
20080202-1400

Kernel-2.6.24 + iptables-1.4.0 + L7-filter-2.17 + ipp2p-0.8.2 on Debian 4.0

คราวนี้ ผมไม่ใช้ ipp2p-0.8.1_rc1 จาก patch-o-matic แล้ว จะใช้ ipp2p-0.8.2 จาก ipp2p.org แทน บวกกับ patch ที่แอบขโมยมาจาก gentoo

1.

apt-get update
apt-get install build-essential kernel-package libncurses5-dev fakeroot

2.

cd /usr/src/
wget http://www.kernel.org/pub/linux/kernel/v2.6/linux-2.6.24.tar.bz2
tar xvjf linux-2.6.24.tar.bz2
ln -s linux-2.6.24 linux

wget http://nchc.dl.sourceforge.net/sourceforge/l7-filter/netfilter-layer7-v2.17.tar.gz
wget http://jaist.dl.sourceforge.net/sourceforge/l7-filter/l7-protocols-2008-01-16.tar.gz

wget http://iptables.org/projects/iptables/files/iptables-1.4.0.tar.bz2

tar xvzf l7-protocols-2008-01-16.tar.gz
tar xvzf netfilter-layer7-v2.17.tar.gz
tar xvjf iptables-1.4.0.tar.bz2
ln -s iptables-1.4.0 iptables

3. patch linux kernel & iptables with l7-filter

cd /usr/src/linux
patch -p1 < ../netfilter-layer7-v2.17/kernel-2.6.22-2.6.24-layer7-2.17.patch
cd ../iptables
patch -p1 < ../netfilter-layer7-v2.17/iptables-1.4-for-kernel-2.6.20forward-layer7-2.17.patch
chmod +x extensions/.layer7-test

4. config and compile kernel

cd /usr/src/linux
cp /boot/config-2.6.18-5-686 ./.config
make menuconfig

เลือกไปที่

Networking –> Networking options –> Network packet filtering framework (Netfilter) –> Core netfilter configuration
เลือกเพิ่ม
<M> Netfilter connection tracking support
<M> “layer7″ match support
  • Layer 7 debugging output


ส่วนอันอื่นที่ยังไม่ได้เลือก ก็สามารถเลือกได้ ตามต้องการ สำหรับผม ในการติดตั้งครั้งนี้ จะเลือกทุกอันเลย โดยจะเลือกเป็นโมดูล [M] ไว้ก่อน
แล้ว exit ออกมา โดยกด Esc 2 ครั้งติดๆกัน แล้วไปที่ IP: Netfilter Configuration เลือกเพิ่ม

<M> IPv4 connection tracking support (required for NAT)
<M> Full NAT

อยู่ด้านล่างสุดเลย แล้ว exit ออกมา โดยกด Esc 2 ครั้ง ไปเรื่อยๆ จนกระทั่งปรากฏหน้าจอ

Do you wish to save your new kernel configuration?

ให้กด Enter เพื่อบันทึกค่าคอนฟิก

5. คอมไพล์และสร้าง kernel image ใหม่ ขั้นตอนนี้ต้องรอนานหน่อยนะครับ

make-kpkg clean
make-kpkg --initrd --append-to-version=-l7 kernel_image kernel_headers

6. หลังจากรอคอยมานาน ต่อไปก็ติดตั้ง kernel ใหม่

cd /usr/src
dpkg -i linux-image-2.6.24-l7_2.6.24-l7-10.00.Custom_i386.deb
dpkg -i linux-headers-2.6.24-l7_2.6.24-l7-10.00.Custom_i386.deb

ตรวจสอบไฟล์ /boot/grub/menu.lst ว่าค่า default เป็นเคอร์เนลที่ติดตั้งใหม่

shutdown -r now

8. คอมไพล์และติดตั้ง iptables

cd /usr/src/iptables
make KERNEL_DIR=/usr/src/linux
make install
mv /usr/src/l7-protocols-2008-01-16 /etc/l7-protocols

9. คอมไพล์และติดตั้ง ipp2p

cd /usr/src
wget http://ipp2p.org/downloads/ipp2p-0.8.2.tar.gz
wget http://sources.gentoo.org/viewcvs.py/*checkout*/gentoo-x86/net-firewall/ipp2p/files/ipp2p-0.8.2-kernel-2.6.22.patch
wget http://sources.gentoo.org/viewcvs.py/*checkout*/gentoo-x86/net-firewall/ipp2p/files/ipp2p-0.8.2-iptables-1.4.0.patch
tar xvzf ipp2p-0.8.2.tar.gz

cp -a ipp2p-0.8.2 ipp2p-0.8.2.orig
cd ipp2p-0.8.2
patch -p1 <../ipp2p-0.8.2-kernel-2.6.22.patch
patch -p1 <../ipp2p-0.8.2-iptables-1.4.0.patch

# ถ้าใช้ ld จะมีปัญหาในการโหลดโมดูล แก้ให้ใช้ gcc ที่บรรทัดท้ายๆของไฟล์ Makefile

vi Makefile

libipt_ipp2p.so: libipt_ipp2p.c ipt_ipp2p.h
$(CC) $(CFLAGS) $(IPTABLES_OPTION) $(IPTABLES_INCLUDE) -fPIC -c libipt_ipp2p.c
@# ld -shared -o libipt_ipp2p.so libipt_ipp2p.o
$(CC) -shared -o libipt_ipp2p.so libipt_ipp2p.o

clean:
-rm -f *.o *.so *.ko .*.cmd *.mod.c
endif

make
cp libipt_ipp2p.so /usr/local/lib/iptables/
cp ipt_ipp2p.ko /lib/modules/2.6.24-l7/kernel/net/netfilter/
depmod -a

11. Test

iptables -A FORWARD -m ipp2p --ipp2p -j DROP
iptables -A FORWARD -m layer7 --l7proto bittorrent -j DROP

mydebian:/usr/src/ipp2p-0.8.2# iptables -nvL
Chain INPUT (policy ACCEPT 1116 packets, 82622 bytes)
pkts bytes target prot opt in out source destination

Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
pkts bytes target prot opt in out source destination
0 0 DROP all — * * 0.0.0.0/0 0.0.0.0/0 ipp2p v0.8.2 –ipp2p
0 0 DROP all — * * 0.0.0.0/0 0.0.0.0/0 LAYER7 l7proto bittorrent

Chain OUTPUT (policy ACCEPT 664 packets, 91251 bytes)
pkts bytes target prot opt in out source destination

mydebian:/usr/src/ipp2p-0.8.2# lsmod | egrep 'ipp2p|layer7'
xt_layer7 11236 1
nf_conntrack 62720 2 nf_conntrack_ipv4,xt_layer7
ipt_ipp2p 6848 1
x_tables 14372 3 xt_layer7,ipt_ipp2p,ip_tables

« แก้ไขครั้งสุดท้าย: 29 พฤษภาคม 2009, 16:29:38 โดย admin »
<a href="http://www.hadyaiinternet.com/images/inetcafe.swf" target="_blank" rel="noopener noreferrer" class="bbc_link bbc_flash_disabled new_win">http://www.hadyaiinternet.com/images/inetcafe.swf</a>

admin

  • Administrator
  • Hero Member
  • *****
  • กระทู้: 3820
    • ดูรายละเอียด
    • อีเมล์
Re: Kernel-2.6.24 + iptables-1.4.0 + L7-filter-2.17 + ipp2p-0.8.2
« ตอบกลับ #1 เมื่อ: 30 พฤษภาคม 2008, 18:41:33 »

สำหรับ Ubuntu 8.04 ต้องดัดแปลงนิดหน่อย

ตรง download source code ก็สามารถใช้

cd /usr/src
apt-get source linux-source-2.6.24
chmod -R a-s /usr/src

และตรงเลือก option สำหรับ compile

ก็เลือกเฉพาะ L7 เนื่องจาก Connection Tracking ตัว Ubuntu ได้ Compile ใส่มาให้แล้ว

และให้เอา Xen guest support ออก ตรง
Processor type and features >> Paravirtualized guest support >> Xen guset support
เนื่องจาก Source Code ไม่มี Code ของ XEN

เสร็จแล้วก็ Compile ได้เลย ด้วยคำสั่ง
make-kpkg clean
make-kpkg --rootcmd fakeroot --initrd --append-to-version=-l7support kernel-image kernel-headers

หรือ
make-kpkg clean
fakeroot make-kpkg --initrd --append-to-version=-l7support kernel_image kernel_headers


ถ้า compile ipp2p แล้ว error ให้แก้ไฟล์ Makefile
---------
libipt_ipp2p.so: libipt_ipp2p.c ipt_ipp2p.h
        $(CC) $(CFLAGS) $(IPTABLES_OPTION) $(IPTABLES_INCLUDE) -I$(KERNEL_SRC)/include -fPIC -c libipt_ipp2p.c
        $(CC) -shared -o libipt_ipp2p.so libipt_ipp2p.o

clean:
        -rm -f *.o *.so *.ko .*.cmd *.mod.c
endif
---------

เมื่อติดตั้งเสร็จก็ทำการทดสอบ จะได้

root@Happy2:~# uname -a
Linux Happy2 2.6.24.3-l7support #1 SMP Fri May 30 14:02:09 ICT 2008 i686 GNU/Linux
root@Happy2:~# iptables -nvL
Chain INPUT (policy ACCEPT 276K packets, 17M bytes)
 pkts bytes target     prot opt in     out     source               destination

Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
 pkts bytes target     prot opt in     out     source               destination
    0     0 DROP       all  --  *      *       0.0.0.0/0            0.0.0.0/0           ipp2p v0.8.2 --ipp2p
    0     0 DROP       all  --  *      *       0.0.0.0/0            0.0.0.0/0           LAYER7 l7proto bittorrent

Chain OUTPUT (policy ACCEPT 252K packets, 267M bytes)
 pkts bytes target     prot opt in     out     source               destination
root@Happy2:~#
« แก้ไขครั้งสุดท้าย: 01 มิถุนายน 2008, 06:29:30 โดย admin »
<a href="http://www.hadyaiinternet.com/images/inetcafe.swf" target="_blank" rel="noopener noreferrer" class="bbc_link bbc_flash_disabled new_win">http://www.hadyaiinternet.com/images/inetcafe.swf</a>

admin

  • Administrator
  • Hero Member
  • *****
  • กระทู้: 3820
    • ดูรายละเอียด
    • อีเมล์
Re: Kernel-2.6.24 + iptables-1.4.0 + L7-filter-2.17 + ipp2p-0.8.2
« ตอบกลับ #2 เมื่อ: 30 พฤษภาคม 2008, 21:18:55 »
module time เป็นอีกตัวที่น่าสนใจ ในการนำมาใช้กับ iptables

เช่น เวลาติดตั้งระบบให้สำนักงานหรือออฟฟิต ต้องการ block bit ช่วงเวลางาน ก็สามารถทำได้โดย

iptables -A FORWARD -m ipp2p --ipp2p -m time --timestart 09:00 --timestop 18:00 --weekdays Mon,Tue,Wed,Thu,Fri -j DROP

หรือในระบบเน็ตหอพัก ที่ต้องการ block bit ในช่วงเวลา peak time เพื่อลดความแออัดในช่วงเวลานั้น ก็สามารถทำได้โดย

iptables -A FORWARD -m ipp2p --ipp2p -m time --timestart 16:00 --timestop 22:00 -j DROP

หรือจะใช้งานร่วมกับ CONNMARK ทำการ MARK แล้วเข้า QoS เพื่อลด B/W ก็ได้เช่นเดียวกัน

iptables -A PREROUTING -t mangle -m ipp2p --ipp2p -m time --timestart 16:00 --timestop 22:00 -j MARK --set-mark 0x8000



Chain INPUT (policy ACCEPT 537 packets, 39531 bytes)
 pkts bytes target     prot opt in     out     source               destination

Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
 pkts bytes target     prot opt in     out     source               destination
    0     0 DROP       all  --  *      *       0.0.0.0/0            0.0.0.0/0           ipp2p v0.8.2 --ipp2p TIME from 09:00:00 to 18:00:00 on Mon,Tue,Wed,Thu,Fri
    0     0 DROP       all  --  *      *       0.0.0.0/0            0.0.0.0/0           ipp2p v0.8.2 --ipp2p TIME from 16:00:00 to 22:00:00


Chain OUTPUT (policy ACCEPT 475 packets, 41464 bytes)
 pkts bytes target     prot opt in     out     source               destination



Chain PREROUTING (policy ACCEPT 96 packets, 6752 bytes)
 pkts bytes target     prot opt in     out     source               destination
    0     0 MARK       all  --  *      *       0.0.0.0/0            0.0.0.0/0           ipp2p v0.8.2 --ipp2p TIME from 16:00:00 to 22:00:00 MARK set 0x8000


เป็นต้น
« แก้ไขครั้งสุดท้าย: 01 มิถุนายน 2008, 14:42:18 โดย admin »
<a href="http://www.hadyaiinternet.com/images/inetcafe.swf" target="_blank" rel="noopener noreferrer" class="bbc_link bbc_flash_disabled new_win">http://www.hadyaiinternet.com/images/inetcafe.swf</a>

admin

  • Administrator
  • Hero Member
  • *****
  • กระทู้: 3820
    • ดูรายละเอียด
    • อีเมล์
Re: Kernel-2.6.24 + iptables-1.4.0 + L7-filter-2.17 + ipp2p-0.8.2
« ตอบกลับ #3 เมื่อ: 01 มิถุนายน 2008, 14:35:42 »
module iprange ก็เป็นอีกโมดูลใน iptables ที่นำมาใช้ประโยชน์ได้เยอะ

โดยไม่ต้องทำการแบ่งเป็น subnet ย่อยๆ อีกต่อไป สามารถกำหนด ip ต้น และ ip ปลายได้เลย

เช่น การกำหนดให้ IP 172.16.6.200-254 สามารถ SSH เข้า Server 172.16.6.11 ได้ ก็สามาถใช้คำสั่ง

iptables -A INPUT -i eth0 -m iprange --src-range 172.16.6.200-172.16.6.254 -d 172.16.6.11 -p tcp --dport 22 -j ACCEPT

เป็นต้น

สามารถกำหนดต้นทางและปลายทางโดยไม่ต้องสนใจ netmask

เช่น   192.168.1.1-192.168.2.254 
หรือ   10.0.0.1-100.100.100.254

เป็นต้น
« แก้ไขครั้งสุดท้าย: 01 มิถุนายน 2008, 14:40:19 โดย admin »
<a href="http://www.hadyaiinternet.com/images/inetcafe.swf" target="_blank" rel="noopener noreferrer" class="bbc_link bbc_flash_disabled new_win">http://www.hadyaiinternet.com/images/inetcafe.swf</a>

admin

  • Administrator
  • Hero Member
  • *****
  • กระทู้: 3820
    • ดูรายละเอียด
    • อีเมล์
Re: Kernel-2.6.24 + iptables-1.4.0 + L7-filter-2.17 + ipp2p-0.8.2
« ตอบกลับ #4 เมื่อ: 01 มิถุนายน 2008, 15:06:11 »
การใช้ module recent ในการ Block IP ที่ทำการ Brute Force port SSH (เดาสุ่มพาสเวิร์ด)

iptables -I INPUT -i ppp+ -p tcp --dport 22 -m state --state NEW -m recent --set --name SSH
iptables -I INPUT -i ppp+ -p tcp --dport 22 -m state --state NEW -m recent --update --name SSH --seconds 60 --hitcount 4 -j DROP



จากตัวอย่างเป็นการตั้งค่า หากมีการเชื่อมต่อ Port 22 มาเกิน 3 ครั้งใน 1 นาที ก็จะทำการ Block IP นั้น เป็นต้น
« แก้ไขครั้งสุดท้าย: 01 มิถุนายน 2008, 15:13:15 โดย admin »
<a href="http://www.hadyaiinternet.com/images/inetcafe.swf" target="_blank" rel="noopener noreferrer" class="bbc_link bbc_flash_disabled new_win">http://www.hadyaiinternet.com/images/inetcafe.swf</a>

admin

  • Administrator
  • Hero Member
  • *****
  • กระทู้: 3820
    • ดูรายละเอียด
    • อีเมล์
Re: Kernel-2.6.24 + iptables-1.4.0 + L7-filter-2.17 + ipp2p-0.8.2
« ตอบกลับ #5 เมื่อ: 02 มิถุนายน 2008, 18:05:29 »
ทดสอบการใช้งาน Layer 7 filter

iptables -A FORWARD -m layer7 --l7proto msnmessenger -j ACCEPT
iptables -A FORWARD -m layer7 --l7proto msn-filetransfer -j ACCEPT
iptables -A FORWARD -m layer7 --l7proto worldofwarcraft -j ACCEPT
iptables -A FORWARD -m layer7 --l7proto ftp -j ACCEPT

ทดสอบผลดูสักระยะ
<a href="http://www.hadyaiinternet.com/images/inetcafe.swf" target="_blank" rel="noopener noreferrer" class="bbc_link bbc_flash_disabled new_win">http://www.hadyaiinternet.com/images/inetcafe.swf</a>