# 此脚本是本人学习iptables时编写,并不适用其他机器,仅供学习参考
# 编写策略:
# 默认策略为ACCEPT, 这里我没有更改默认策略,而是先允许指定的规则通过,然后再DROP所有tcp,udp,icmp请求
# 读者可根据自己的习惯来编写规则,比如默认禁止所有,再允许指定规则
iptables -F
# 允许所有已建立的连接
iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
# 记录日志 LOG 日志记录在/var/log/message中
好像只能记录别人的icmp请求,不能记录自己的icmp请求
iptables -A INPUT -p icmp -j LOG --log-prefix "---firewall log for icmp---"
# 下面两条的含义为允许本机到别人的icmp请求
iptables -A INPUT -p icmp --icmp-type 8 -s 192.168.49.65 -j ACCEPT
iptables -A INPUT -p icmp --icmp-type 0 -s 192.168.49.65 -j ACCEPT
# 允许本机内部之间的通信
iptables -A INPUT -i lo -j ACCEPT
# iptables -A INPUT -s 192.168.0.0/16 -p tcp --dport 22 -m state --state NEW -j ACCEPT
#iptables -A INPUT -p tcp --dport 3306 -m state --state NEW -j ACCEPT
#iptables -A INPUT -p tcp --dport 80 -m state --state NEW -j ACCEPT
# 一些常用的显示扩展
# multiport -- 离散多端口匹配扩展
#iptables -A INPUT -s 192.168.0.0/16 -p tcp -m multiport --dports 22,3306,80 -m state --state NEW -j ACCEPT
# iprange -- 指定一段连续的ip地址
iptables -A INPUT -p tcp -m iprange --src-range 192.168.49.0-192.168.49.100 -m multiport --dports 22,3306,80 -m state --state N
EW -j ACCEPT
# connlimit -- 连接数限制
# 一个客户端只允许建立最多两个连接
iptables -A INPUT -d 192.168.49.65 -p tcp --dport 80 -m connlimit ! --connlimit-above 2 -j ACCEPT
# limit -- 流量控制
# --limit RATE
# --limit-burst 7
# string -- 字符串控制
# --algo {bm|kmp}
# --string "STING"
iptables -A OUTPUT -s 192.168.49.65 -m string --algo kmp --string "xiao" -j DROP
# 允许指定的规则通过,然后禁止所有
iptables -A INPUT -p tcp -j DROP
iptables -A INPUT -p udp -j DROP
iptables -A INPUT -p icmp -j DROP
service iptables save
iptables-save > /etc/sysconfig/iptables.my
iptables-restore < /etc/sysconfig/iptables.my
iptables总结脚本
最新推荐文章于 2021-07-01 11:07:28 发布