通过配置iptables有效防止syn、ddos、ping等攻击

linux下iptables防止syn ddos ping 等攻击

配置防火墙防止syn,ddos攻击
[root@m176com ~]# vim /etc/sysconfig/iptables
在iptables中加入下面几行
#anti syn,ddos
-A FORWARD -p tcp –syn -m limit –limit 1/s –limit-burst 5 -j ACCEPT
-A FORWARD -p tcp –tcp-flags SYN,ACK,FIN,RST RST -m limit –limit 1/s -j ACCEPT
-A FORWARD -p icmp –icmp-type echo-request -m limit –limit 1/s -j ACCEPT
说明:
第一行:每秒中最多允许5个新连接
第二行:防止各种端口扫描
第三行:Ping洪水攻击(Ping of Death)
可以根据需要调整或关闭
重启防火墙
[root@m176com ~]# /etc/init.d/iptables restart
屏蔽一个IP
# iptables -I INPUT -s 192.168.0.1 -j DROP

怎么防止别人ping我??
# iptables -A INPUT -p icmp -j DROP
防止同步包洪水(Sync Flood)
# iptables -A FORWARD -p tcp –syn -m limit –limit 1/s -j ACCEPT
防止各种端口扫描
# iptables -A FORWARD -p tcp –tcp-flags SYN,ACK,FIN,RST RST -m limit –limit 1/s -j ACCEPT
Ping洪水攻击(Ping of Death)
#iptables -A FORWARD -p icmp –icmp-type echo-request -m limit –limit 1/s -j ACCEPT

# NMAP FIN/URG/PSH
# iptables -A INPUT -i eth0 -p tcp –tcp-flags ALL FIN,URG,PSH -j DROP

# Xmas Tree
iptables -A INPUT -i eth0 -p tcp –tcp-flags ALL ALL -j DROP

# Another Xmas Tree
# iptables -A INPUT -i eth0 -p tcp –tcp-flags ALL SYN,RST,ACK,FIN,URG -j DROP

# Null Scan(possibly)
iptables -A INPUT -i eth0 -p tcp –tcp-flags ALL NONE -j DROP

# SYN/RST
# iptables -A INPUT -i eth0 -p tcp –tcp-flags SYN,RST SYN,RST -j DROP

# SYN/FIN — Scan(possibly)
# iptables -A INPUT -i eth0 -p tcp –tcp-flags SYN,FIN SYN,FIN -j DROP
##限制对内部封包的发送速度
#iptables -A INPUT -f -m limit –limit 100/s –limit-burst 100 -j ACCEPT

##限制建立联机的转
#iptables -A FORWARD -f -m limit –limit 100/s –limit-burst 100 -j ACCEPT

一个不错的防火墙代码
#####################################################

-A INPUT -f -m limit –limit 100/sec –limit-burst 100 -j ACCEPT

-A INPUT -p tcp -m tcp –tcp-flags SYN,RST,ACK SYN -m limit –limit 20/sec –limit-burst 200 -j

ACCEPT

-A INPUT -p udp -m udp –dport 138 -j DROP

-A INPUT -p udp -m udp –dport 137 -j DROP

-A INPUT -p tcp -m tcp –dport 1068 -j DROP

-A INPUT -p icmp -m limit –limit 12/min –limit-burst 2 -j DROP

-A FORWARD -f -m limit –limit 100/sec –limit-burst 100 -j ACCEPT

-A FORWARD -p tcp -m tcp –tcp-flags SYN,RST,ACK SYN -m limit –limit 20/sec –limit-burst 200

-j ACCEPT

-A FORWARD -p tcp -m tcp –dport 445 -j DROP

-A FORWARD -p udp -m udp –dport 138 -j DROP

-A FORWARD -p udp -m udp –dport 137 -j DROP

-A FORWARD -p tcp -m tcp –dport 1068 -j DROP

-A FORWARD -p tcp -m tcp –dport 5554 -j DROP

-A FORWARD -p icmp -j DROP

:PREROUTING ACCEPT [986908:53126959]

:POSTROUTING ACCEPT [31401:2008714]

:OUTPUT ACCEPT [30070:1952143]

-A POSTROUTING -p tcp -m tcp –dport 445 -j DROP

#####################################################

iptables 防火墙例子

#!/bin/bash
#
# The interface that connect Internet

# echo
echo “Enable IP Forwarding…”
echo 1 > /proc/sys/net/ipv4/ip_forward
echo “Starting iptables rules…”

IFACE=”eth0″

# include module
modprobe ip_tables
modprobe iptable_nat
modprobe ip_nat_ftp
modprobe ip_nat_irc
modprobe ip_conntrack
modprobe ip_conntrack_ftp
modprobe ip_conntrack_irc
modprobe ipt_MASQUERADE

# init
/sbin/iptables -F
/sbin/iptables -X
/sbin/iptables -Z
/sbin/iptables -F -t nat
/sbin/iptables -X -t nat
/sbin/iptables -Z -t nat

/sbin/iptables -X -t mangle

# drop all
/sbin/iptables -P INPUT DROP
/sbin/iptables -P FORWARD ACCEPT
/sbin/iptables -P OUTPUT ACCEPT
/sbin/iptables -t nat -P PREROUTING ACCEPT
/sbin/iptables -t nat -P POSTROUTING ACCEPT
/sbin/iptables -t nat -P OUTPUT ACCEPT

/sbin/iptables -A INPUT -f -m limit –limit 100/sec –limit-burst 100 -j ACCEPT
/sbin/iptables -A INPUT -p tcp -m tcp –tcp-flags SYN,RST,ACK SYN -m limit –limit 20/sec —

limit-burst 200 -j ACCEPT

/sbin/iptables -A INPUT -p icmp -m limit –limit 12/min –limit-burst 2 -j DROP

/sbin/iptables -A FORWARD -f -m limit –limit 100/sec –limit-burst 100 -j ACCEPT
/sbin/iptables -A FORWARD -p tcp -m tcp –tcp-flags SYN,RST,ACK SYN -m limit –limit 20/sec —

limit-burst 200 -j ACCEPT

# open ports
/sbin/iptables -A INPUT -i $IFACE -p tcp –dport 21 -j ACCEPT
/sbin/iptables -A INPUT -i $IFACE -p tcp –dport 22 -j ACCEPT
/sbin/iptables -A INPUT -i $IFACE -p tcp –dport 25 -j ACCEPT
/sbin/iptables -A INPUT -i $IFACE -p tcp –dport 53 -j ACCEPT
/sbin/iptables -A INPUT -i $IFACE -p udp –dport 53 -j ACCEPT
/sbin/iptables -A INPUT -i $IFACE -p tcp –dport 80 -j ACCEPT
/sbin/iptables -A INPUT -i $IFACE -p tcp –dport 100 -j ACCEPT
/sbin/iptables -A INPUT -i $IFACE -p tcp –dport 113 -j ACCEPT

# close ports
iptables -I INPUT -p udp –dport 69 -j DROP
iptables -I INPUT -p tcp –dport 135 -j DROP
iptables -I INPUT -p udp –dport 135 -j DROP
iptables -I INPUT -p tcp –dport 136 -j DROP
iptables -I INPUT -p udp –dport 136 -j DROP
iptables -I INPUT -p tcp –dport 137 -j DROP
iptables -I INPUT -p udp –dport 137 -j DROP
iptables -I INPUT -p tcp –dport 138 -j DROP
iptables -I INPUT -p udp –dport 138 -j DROP
iptables -I INPUT -p tcp –dport 139 -j DROP
iptables -I INPUT -p udp –dport 139 -j DROP
iptables -I INPUT -p tcp –dport 445 -j DROP
iptables -I INPUT -p udp –dport 445 -j DROP
iptables -I INPUT -p tcp –dport 593 -j DROP
iptables -I INPUT -p udp –dport 593 -j DROP
iptables -I INPUT -p tcp –dport 1068 -j DROP
iptables -I INPUT -p udp –dport 1068 -j DROP
iptables -I INPUT -p tcp –dport 4444 -j DROP
iptables -I INPUT -p udp –dport 4444 -j DROP
iptables -I INPUT -p tcp –dport 5554 -j DROP
iptables -I INPUT -p tcp –dport 1434 -j DROP
iptables -I INPUT -p udp –dport 1434 -j DROP
iptables -I INPUT -p tcp –dport 2500 -j DROP
iptables -I INPUT -p tcp –dport 5800 -j DROP
iptables -I INPUT -p tcp –dport 5900 -j DROP
iptables -I INPUT -p tcp –dport 6346 -j DROP
iptables -I INPUT -p tcp –dport 6667 -j DROP
iptables -I INPUT -p tcp –dport 9393 -j DROP

iptables -I FORWARD -p udp –dport 69 -j DROP
iptables -I FORWARD -p tcp –dport 135 -j DROP
iptables -I FORWARD -p udp –dport 135 -j DROP
iptables -I FORWARD -p tcp –dport 136 -j DROP
iptables -I FORWARD -p udp –dport 136 -j DROP
iptables -I FORWARD -p tcp –dport 137 -j DROP
iptables -I FORWARD -p udp –dport 137 -j DROP
iptables -I FORWARD -p tcp –dport 138 -j DROP
iptables -I FORWARD -p udp –dport 138 -j DROP
iptables -I FORWARD -p tcp –dport 139 -j DROP
iptables -I FORWARD -p udp –dport 139 -j DROP
iptables -I FORWARD -p tcp –dport 445 -j DROP
iptables -I FORWARD -p udp –dport 445 -j DROP
iptables -I FORWARD -p tcp –dport 593 -j DROP
iptables -I FORWARD -p udp –dport 593 -j DROP
iptables -I FORWARD -p tcp –dport 1068 -j DROP
iptables -I FORWARD -p udp –dport 1068 -j DROP
iptables -I FORWARD -p tcp –dport 4444 -j DROP
iptables -I FORWARD -p udp –dport 4444 -j DROP
iptables -I FORWARD -p tcp –dport 5554 -j DROP
iptables -I FORWARD -p tcp –dport 1434 -j DROP
iptables -I FORWARD -p udp –dport 1434 -j DROP
iptables -I FORWARD -p tcp –dport 2500 -j DROP
iptables -I FORWARD -p tcp –dport 5800 -j DROP
iptables -I FORWARD -p tcp –dport 5900 -j DROP
iptables -I FORWARD -p tcp –dport 6346 -j DROP
iptables -I FORWARD -p tcp –dport 6667 -j DROP
iptables -I FORWARD -p tcp –dport 9393 -j DROP

/sbin/iptables -A INPUT -i $IFACE -m state –state RELATED,ESTABLISHED -j ACCEPT
/sbin/iptables -A INPUT -i $IFACE -m state –state NEW,INVALID -j DROP

# drop ping
/sbin/iptables -A INPUT -p icmp -j DROP

/sbin/iptables -I INPUT -s 222.182.40.241 -j DROP

Copyright © 2014 - 2021 創雲信息科技(國際)電信集團 All Rights Reserved