Áp dụng module ipt_connlimit của iptables trong Centos 5
Trong nhu cầu bảo mật ngày càng cao và có nhu cầu giới hạn số connect từ một client đến server, ta có thể xây dựng firewall với nhiều tùy chọn. Để đơn giản hơn với firewall bằng iptables ta có thể tích hợp thêm module ipt_connlimit. Mặc định module này không đuôc build sẵn vì thế để có thể sử dụng module ta phải build từ source của kernel và iptables.
Trong bài viết này mình sẽ trình bày cách build module ipt_connection của iptables trong Centos 5, để hiểu rõ các câu lệnh đó làm gì và hoạt động như thế nào các bạn vui lòng xem bài viết khác.
Bước 1: Xây dựng môi trường và download package
Ta có thể build module từ user thường không nhất thiết phải dùng user root. Ta phải bảo đảm trong hệ thống đã có sẵn gói rpm-build.
Có thể kiểm tra:
# rpm -qi rpm-build
Nếu package này không tồn tại trong hệ thống ta phải cài đặt từ user có quyền.
# yum install rpm-build
Download các package và đặt ở thư mục home của user:
$ wget http://mirror.centos.org/centos/5/os/SRPMS/kernel-2.6.18-53.el5.src.rpm
$ wget http://www.netfilter.org/projects/iptables/files/iptables-1.4.0.tar.bz2
$ wget http://ftp.netfilter.org/pub/patch-o-matic-ng/snapshot/patch-o-matic-ng-20080228.tar.bz2
Xây dựng môi trường build cho user
$ cd
$ mkdir -p rpmbuild/{BUILD,RPMS,SOURCES,SPECS,SRPMS}
$ echo “%_topdir %(echo $HOME)/rpmbuild” > .rpmmacros
Bước 2: Tiến hành build
Cài đặt kernel source
$ rpm -i kernel-2.6.18-53.el5.src.rpm
$ cd ~/rpmbuild/SPECS
$ rpmbuild -bp –target=`uname -m` kernel-2.6.spec
$ cd
$ tar xvfj iptables-1.4.0.tar.bz2
$ tar xvfj patch-o-matic-ng-20080228.tar.bz2
$ cd patch-o-matic-ng-20080228
$ ./runme –download
$ ./runme connlimit
Trong quá trình chạy lệnh runme bạn sẽ được hỏi thư mục kernel source và iptables source.
Kernel source là thư mục rpmbuild/BUILD/kernel-2.6.18/linux-2.6.18.i686 trong home của user
Iptables source là thư mục iptables-1.4.0 trong home của user
$ cd ~/rpmbuild/BUILD/kernel-2.6.18/linux-2.6.18.i686
$ make oldconfig
$ make prepare
$ make modules_prepare
$ cd ~/rpmbuild/BUILD/kernel-2.6.18/linux-2.6.18.i686/net/ipv4/netfilter
$ make -C ~/rpmbuild/BUILD/kernel-2.6.18/linux-2.6.18.i686 M=$PWD modules
$ cp ipt_connlimit.ko /lib/modules/2.6.18-53.1.13.el5/updates
$ depmod 2.6.18-53.1.13.el5
$ modprobe ipt_connlimit
Bước 3: Áp dụng
Ví dụ: Giới hạn chỉ có 2 connection ssh vào server
# iptables -A INPUT -p tcp –syn –dport 22 -m connlimit –connlimit-above 2 -j REJECT