Chặn IP theo dải (IP range) sử dụng iptables

Khi thuê sử dụng VPS Linux, đôi khi chúng ta cần firewall để chặn truy cập từ 1 vùng, 1 quốc gia hay 1 IP ranges cụ thể. Điều này hoàn toàn đơn giản và dễ thực hiện với IP tables.

1. Các bước như sau:

Lấy địa chỉ các IP ranges của 1 quốc gia từ link sau http://www.find-ip-address.org và cho vào 1 file gọi là country_IP
Tạo 1 scripts với cú pháp như sau gọi là iptables_autoallow.sh

#!/bin/bash
if [ -f country_IP ]
then
for IP_select in `cat country_IP`
do
iptables -A INPUT -s $IP_select -p tcp -j DROP
done
else
echo “Can’t read country_IP”
fi

Có thể add thêm các port cụ thể như port 22:

iptables -A INPUT -s $IP_select -p tcp –dport 22 -j ACCEPT or
iptables -A INPUT -s $IP_select -p tcp –dport 22 -j DROP

Thay đổi quyền cho scripts và thực thi nó

chmod 755 iptables_autoallow.sh
sh iptables_autoallow.sh

Leave a Reply