在Linux服务器中如何使用iptables防止端口扫描和smurf攻击
时间:2019-08-20 17:58:18 来源:igfitidea点击:
这里我们将通过一个脚本来防止端口被扫描和smurf攻击
Smurf攻击是一种拒绝服务攻击,在这种攻击中,使用IP广播地址向计算机网络广播大量Internet Control Message Protocol (ICMP)包,并欺骗目标的源IP。
脚本的功能:
(1)当攻击者试图对服务器进行端口扫描时,首先由于iptable的原因,攻击者不会得到任何打开端口的信息。其次,攻击IP地址将被列入黑名单24小时。第三,在此之后,攻击者将无法打开任何访问权限,例如,即使攻击者也不会看到任何通过web浏览器在服务器上运行的网站,也无法ssh、telnet。表示完全受限。
(2)抵御smurf攻击
(3)是在IPTABLE的帮助下编写的,因此不会出现CPU高、内存占用等系统性能问题,也不会使用第三方工具
服务器详情
操作系统:CentOS 6.4(适用于Red hat和CentOS服务器)
IP地址:192.168.1.4
创建一个脚本 iptablescript.sh
vi /root/iptablescript.sh
添加下面的内容
#!/bin/sh # # # Script is for stoping Portscan and smurf attack ### first flush all the iptables Rules iptables -F # INPUT iptables Rules # Accept loopback input iptables -A INPUT -i lo -p all -j ACCEPT # allow 3 way handshake iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT ### DROPspoofing packets iptables -A INPUT -s 10.0.0.0/8 -j DROP iptables -A INPUT -s 169.254.0.0/16 -j DROP iptables -A INPUT -s 172.16.0.0/12 -j DROP iptables -A INPUT -s 127.0.0.0/8 -j DROP iptables -A INPUT -s 192.168.0.0/24 -j DROP iptables -A INPUT -s 224.0.0.0/4 -j DROP iptables -A INPUT -d 224.0.0.0/4 -j DROP iptables -A INPUT -s 240.0.0.0/5 -j DROP iptables -A INPUT -d 240.0.0.0/5 -j DROP iptables -A INPUT -s 0.0.0.0/8 -j DROP iptables -A INPUT -d 0.0.0.0/8 -j DROP iptables -A INPUT -d 239.255.255.0/24 -j DROP iptables -A INPUT -d 255.255.255.255 -j DROP #for SMURF attack protection iptables -A INPUT -p icmp -m icmp --icmp-type address-mask-request -j DROP iptables -A INPUT -p icmp -m icmp --icmp-type timestamp-request -j DROP iptables -A INPUT -p icmp -m icmp -m limit --limit 1/second -j ACCEPT # Droping all invalid packets iptables -A INPUT -m state --state INVALID -j DROP iptables -A FORWARD -m state --state INVALID -j DROP iptables -A OUTPUT -m state --state INVALID -j DROP # flooding of RST packets, smurf attack Rejection iptables -A INPUT -p tcp -m tcp --tcp-flags RST RST -m limit --limit 2/second --limit-burst 2 -j ACCEPT # Protecting portscans # Attacking IP will be locked for 24 hours (3600 x 24 = 86400 Seconds) iptables -A INPUT -m recent --name portscan --rcheck --seconds 86400 -j DROP iptables -A FORWARD -m recent --name portscan --rcheck --seconds 86400 -j DROP # Remove attacking IP after 24 hours iptables -A INPUT -m recent --name portscan --remove iptables -A FORWARD -m recent --name portscan --remove # These rules add scanners to the portscan list, and log the attempt. iptables -A INPUT -p tcp -m tcp --dport 139 -m recent --name portscan --set -j LOG --log-prefix "portscan:" iptables -A INPUT -p tcp -m tcp --dport 139 -m recent --name portscan --set -j DROP iptables -A FORWARD -p tcp -m tcp --dport 139 -m recent --name portscan --set -j LOG --log-prefix "portscan:" iptables -A FORWARD -p tcp -m tcp --dport 139 -m recent --name portscan --set -j DROP # Allow the following ports through from outside iptables -A INPUT -p tcp -m tcp --dport 25 -j ACCEPT iptables -A INPUT -p tcp -m tcp --dport 80 -j ACCEPT iptables -A INPUT -p tcp -m tcp --dport 443 -j ACCEPT iptables -A INPUT -p tcp -m tcp --dport 22 -j ACCEPT # Allow ping means ICMP port is open (If you do not want ping replace ACCEPT with REJECT) iptables -A INPUT -p icmp -m icmp --icmp-type 8 -j ACCEPT # Lastly reject All INPUT traffic iptables -A INPUT -j REJECT ################# Below are for OUTPUT iptables rules ############################################# ## Allow loopback OUTPUT iptables -A OUTPUT -o lo -j ACCEPT iptables -A OUTPUT -m state --state ESTABLISHED,RELATED -j ACCEPT # Allow the following ports through from outside # SMTP = 25 # DNS =53 # HTTP = 80 # HTTPS = 443 # SSH = 22 ### You can also add or remove port no. as per your requirement iptables -A OUTPUT -p tcp -m tcp --dport 25 -j ACCEPT iptables -A OUTPUT -p udp -m udp --dport 53 -j ACCEPT iptables -A OUTPUT -p tcp -m tcp --dport 80 -j ACCEPT iptables -A OUTPUT -p tcp -m tcp --dport 443 -j ACCEPT iptables -A OUTPUT -p tcp -m tcp --dport 22 -j ACCEPT # Allow pings iptables -A OUTPUT -p icmp -m icmp --icmp-type 8 -j ACCEPT # Lastly Reject all Output traffic iptables -A OUTPUT -j REJECT ## Reject Forwarding traffic iptables -A FORWARD -j REJECT
出于安全考虑,只允许root用户读写执行权限。
chmod 700 /root/iptablescript.sh chown root:root /root/iptablescript.sh
现在运行脚本
sh /root/iptablescript.sh
检查IPTABLES规则
iptables -nL
测试
从远程对服务器进行测试。
登录任意系统,尝试进行端口扫描
nmap -sT 服务器ip
来自系统的结果应该如下所示:
(a)nmap没有任何输出
(b)无法对任何端口进行telnet,例如“telnet Server ip address 22”`
运行nmap后意味着端口扫描你的ip地址被列入黑名单。
重新登录到服务器并在/var/log中检查日志,通过关键字为 portscan可以找到进行扫描的客户端IP。
如何安装nmap
在 Red Hat 和 CentOS 中 yum install nmap 在 Debian 和 Ubuntu 中 apt-get install nmap