网络拓扑图
安装
(要么firewall,要么iptables,不用删除,选其一即可)
systemctl disable --now firewalld
yum install iptables-services -y
systemctl enable --now iptables
systemctl status iptables
清除查看规则
(默认为filter表)
(清除所有规则)
iptables -F
(清空其它表所有规则)
iptables -t nat -F
iptables -t raw -F
iptables -t mangle -F(查看默认规则)
(-t 指定表,默认filter表)
iptables -nL
iptables -t nat -nL
备份还原规则
iptables-save > /etc/sysconfig/iptables
iptables-save > firewall.bak
iptables-restore < firewall.bak
设置默认策略
(防止把自己锁在外面,先执行,切记! )
iptables -I INPUT -p tcp --dport 22 -j ACCEPT
(默认为filter表)
(默认链策略是ACCEPT)
iptables -P INPUT DROP
iptables -P OUTPUT ACCEPT
iptables -P FORWARDING ACCEPT
追加/删除/插入规则
追加
cat /etc/services | grep ssh
iptables -I INPUT -p tcp --dport 80 -j ACCEPT
iptables -I INPUT -p tcp --dport 22 -j ACCEPTiptables -I INPUT -p tcp --dport 80,22 -j ACCEPT
iptables -I INPUT -p icmp -j REJECT
iptables -I INPUT -p icmp -j DROPREJECT:Destination Host Prohibited
[root@wai ~]# ping 192.168.170.170
PING 192.168.170.170 (192.168.170.170) 56(84) bytes of data.
From 192.168.170.170 icmp_seq=1 Destination Port UnreachableDROP:无提示卡住
[root@wai ~]# ping 192.168.170.170
PING 192.168.170.170 (192.168.170.170) 56(84) bytes of data.
添加一条规则, 不允许所有,必要允许,其它拒绝,放在最后一条,慎用!
iptables -A INPUT -j REJECTiptables -A INPUT -j DROP
删除
查看规则并显示序号
iptables -nL --line-numbers
删除INPUT链序号3规则
iptables -D INPUT 3
插入
查看规则并显示序号
iptables -nL --line-numbers
在INPUT链序号4规则后面插入一条规则
iptables -I INPUT 4 -p icmp -j ACCEPT
limit
限制单位时间内连接的数据包个数,500个/秒,超过拒绝连接
iptables -I INPUT -m limit --limit 500/sec -j ACCEPT
iptables -P INPUT DROP拒绝转发关键词、敏感信息数据包
iptables -I FORWARD -m string --algo bm--string "/etc/passwd" -j REJECT
iptables -I FORWARD -m string --algo bm--string "qq" -j REJECT
state
状态检测,放行所有出站数据包,放行入站的回应数据包。拒绝入站的新连接请求与无效连接
状态检测:NEW、ESTABLISHED、RELATED、INVALID
除了从本机出去的数据包由NAT表的OUTPUT链处理外,其它所有的状态检测都在NAT表中的PREROUTING链中处理,具体状态信息如下:
1、NEW状态:由外主动发起连接的第一个数据包
2、ESTABLISHED状态:由内主动发起并收到对方响应返回的第一个数据包,从NEW变为ESTABLISHED,并且该状态会继续匹配这个连接后继数据包
3、RELATED状态:当一个数据包的状态处于ESTABLISHED状态的连接有关系的时候,就会被认为是RELATED,也就是说一个连接想要是RELATED状态,首先要有一个ESTABLISHED的连接(承接ESTABLISHED)
4、INVALID状态:不能被识别属于哪个连接状态或没有任何关系的状态,都是被拒绝的
iptables -A INPUT -m state --state NEW -j DROP
iptables -A INPUT -m state --state INVALID -j DROP
iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
禁止网络嗅探
iptables -A INPUT -p tcp --tcp-flags hosts FIN,URG,PSH -j DROP
iptables -A INPUT -p tcp --tcp-flags SYN,RST SYN,RST -j DROP
iptables -A INPUT -p tcp --tcp-flags SYN,FIN SYN,FIN -j DROP
iptables -A INPUT -p tcp --tcp-flags SYN,SYN --dport 80 -j DROP
snat
iptables --table nat -F
开启路由转发
vim /etc/sysctl.conf
net.ipv4.ip_forward = 1
sysctl -pPOSTROUTING:路由选择后处理数据包
$ 固定ip
iptables -t nat -I POSTROUTING -s 192.168.180.0/24 -j SNAT --to-source 20.20.20.21or
iptables -t nat -A POSTROUTING -o ens190 -j SNAT --to-source 20.20.20.21$ 动态ip
iptables -t nat -I POSTROUTING -s 192.168.180.0/24 -o ens160 -j MASQUERADE
$ 查看
iptables --table nat -nL
dnat
PREROUTING:路由选择前处理数据包
iptables -t nat -I PREROUTING -d 192.168.170.170 -p tcp --dport 80 -j DNAT --to-destination 192.168.180.181
查看
iptables --table nat -nL