当前位置: 移动技术网 > 科技>操作系统>Linux > Linux iptables 配置规则

Linux iptables 配置规则

2019年01月11日  | 移动技术网科技  | 我要评论

鹿鼎记趣,semm7,吉祥坊89168澳门官方

linux iptables 防火墙配置规则

前言:把网上我感觉不错iptables的访问规则都统一在这里,以后做参考。

modprobe ipt_masquerade
modprobe ip_conntrack_ftp
modprobe ip_nat_ftp
iptables -f
iptables -t nat -f
iptables -x
iptables -t nat -x
###########################input键###################################
iptables -p input drop
iptables -a input -m state --state established,related -j accept
iptables -a input -p tcp -m multiport --dports 110,80,25 -j accept
iptables -a input -p tcp -s 192.168.0.0/24 --dport 139 -j accept
#允许内网samba,smtp,pop3,连接
iptables -a input -i eth1 -p udp -m multiport --dports 53 -j accept
#允许dns连接
iptables -a input -p tcp --dport 1723 -j accept
iptables -a input -p gre -j accept
#允许外网***连接
iptables -a input -s 192.186.0.0/24 -p tcp -m state --state established,related -j accept
iptables -a input -i ppp0 -p tcp --syn -m connlimit --connlimit-above 15 -j drop
#为了防止dos太多连接进来,那么可以允许最多15个初始连接,超过的丢弃
iptables -a input -s 192.186.0.0/24 -p tcp --syn -m connlimit --connlimit-above 15 -j drop
#为了防止dos太多连接进来,那么可以允许最多15个初始连接,超过的丢弃
iptables -a input -p icmp -m limit --limit 3/s -j log --log-level info --log-prefix "icmp packet in: "
iptables -a input -p icmp -j drop
#禁止icmp通信-ping 不通
iptables -t nat -a postrouting -o ppp0 -s 192.168.0.0/24 -j masquerade
#内网转发
iptables -n syn-flood
iptables -a input -p tcp --syn -j syn-flood
iptables -i syn-flood -p tcp -m limit --limit 3/s --limit-burst 6 -j return
iptables -a syn-flood -j reject
#防止syn攻击 轻量
#######################forward链###########################
iptables -p forward drop
iptables -a forward -p tcp -s 192.168.0.0/24 -m multiport --dports 80,110,21,25,1723 -j accept
iptables -a forward -p udp -s 192.168.0.0/24 --dport 53 -j accept
iptables -a forward -p gre -s 192.168.0.0/24 -j accept
iptables -a forward -p icmp -s 192.168.0.0/24 -j accept
#允许 ***客户走***网络连接外网
iptables -a forward -m state --state established,related -j accept
iptables -i forward -p udp --dport 53 -m string --string "tencent" -m time --timestart 8:15 --timestop 12:30 --days mon,tue,wed,thu,fri,sat  -j drop
#星期一到星期六的8:00-12:30禁止qq通信
iptables -i forward -p udp --dport 53 -m string --string "tencent" -m time --timestart 8:15 --timestop 12:30 --days mon,tue,wed,thu,fri,sat  -j drop
#星期一到星期六的8:00-12:30禁止qq通信
iptables -i forward -p udp --dport 53 -m string --string "tencent" -m time --timestart 13:30 --timestop 20:30 --days mon,tue,wed,thu,fri,sat  -j drop
iptables -i forward -p udp --dport 53 -m string --string "tencent" -m time --timestart 13:30 --timestop 20:30 --days mon,tue,wed,thu,fri,sat  -j drop
#星期一到星期六的13:30-20:30禁止qq通信
iptables -i forward -s 192.168.0.0/24 -m string --string "qq.com" -m time --timestart 8:15 --timestop 12:30 --days mon,tue,wed,thu,fri,sat  -j drop
#星期一到星期六的8:00-12:30禁止qq网页
iptables -i forward -s 192.168.0.0/24 -m string --string "qq.com" -m time --timestart 13:00 --timestop 20:30 --days mon,tue,wed,thu,fri,sat  -j drop
#星期一到星期六的13:30-20:30禁止qq网页
iptables -i forward -s 192.168.0.0/24 -m string --string "ay2000.net" -j drop
iptables -i forward -d 192.168.0.0/24 -m string --string "宽频影院" -j drop
iptables -i forward -s 192.168.0.0/24 -m string --string "×××" -j drop
iptables -i forward -p tcp --sport 80 -m string --string "广告" -j drop
#禁止ay2000.net,宽频影院,×××,广告网页连接 !但中文 不是很理想
iptables -a forward -m ipp2p --edk --kazaa --bit -j drop
iptables -a forward -p tcp -m ipp2p --ares -j drop
iptables -a forward -p udp -m ipp2p --kazaa -j drop
#禁止bt连接
iptables -a forward -p tcp --syn --dport 80 -m connlimit --connlimit-above 15 --connlimit-mask 24
#######################################################################
sysctl -w net.ipv4.ip_forward=1 &>/dev/null
#打开转发
#######################################################################
sysctl -w net.ipv4.tcp_syncookies=1 &>/dev/null
#打开 syncookie (轻量级预防 dos 攻击)
sysctl -w net.ipv4.netfilter.ip_conntrack_tcp_timeout_established=3800 &>/dev/null
#设置默认 tcp 连接痴呆时长为 3800 秒(此选项可以大大降低连接数)
sysctl -w net.ipv4.ip_conntrack_max=300000 &>/dev/null
#设置支持最大连接树为 30w(这个根据你的内存和 iptables 版本来,每个 connection 需要 300 多个字节)
#######################################################################
iptables -i input -s 192.168.0.50 -j accept
iptables -i forward -s 192.168.0.50 -j accept
#########################################################################
外部接口ip:210.83.2.206
内部接口ip:192.168.1.1

ftp服务器 : ip 192.168.1.3
web服务器 : ip 192.168.1.4    
iptables -t nat -a prerouting -d 210.83.2.206 -p tcp --dport 21 -j dnat --to 192.168.1.3
iptables -t nat -a prerouting -d 210.83.2.206 -p tcp --dport 80 -j dnat --to 192.168.1.4    
#####################################################################################################
dnat 带端口映射(改变server的端口)
一个ftp server从内部192.168.100.125:21映射到216.94.87.37:2121的例子
iptables -t nat -a prerouting -p tcp -d 216.94.87.37 --dport 2121 -j dnat --to-destination 192.168.100.125:21    
通常外网dnat访问内网server,内网serv er回包的源地址是经过另一个单独的snat进程的。而不属于dnat static进程的一部分。
这样对于p-t-p的网络应用,就必须另设一个和dnat相适应的snat。
对于穿过nat,被nat映射改变端口号的应用,也必须用一个单独的snat对回包的端口进行映射
iptables -t nat -a postrouting -p tcp -s 192.168.100.125 --sport 21 -j snat --to-source 216.94.87.37:2121    
不这样做的话,ftp server会返回21到外网的客户机,外网用户发出一个to 2121的ftp request,收到一个from 21的,会不认
上面的好象不必,做过实验了:
/sbin/iptables -t nat -a postrouting -s 10.4.0.0/16 -o $wan_int -j snat --to 124.126.86.137
/sbin/iptables -t nat -a prerouting -d 124.126.86.138 -p tcp --dport 2022 -j dnat --to-destination 10.4.3.150:22
########################################################################################

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网