解压密码6Hai7Gf8 html
路由转发 linux
0是不转发,1是转发 web
[root@m01 roles]# cat /proc/sys/net/ipv4/ip_forward 算法
0 数据库
临时生效 vim
[root@m01 roles]# echo "1" > /proc/sys/net/ipv4/ip_forward windows
从配置文件读取配置,临时生效 centos
sysctl -p 缓存
sed -i '/net.ipv4.ip_forward = 0/ net.ipv4.ip_forward = 1/ /etc/sysctl.conf 服务器
sysctl -p
路由条目解释
有2个路由到20和10网段的路由
[root@route ~]# route -n
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
192.168.20.0 0.0.0.0 255.255.255.0 U 0 0 0 eth1
192.168.10.0 0.0.0.0 255.255.255.0 U 0 0 0 eth0
169.254.0.0 0.0.0.0 255.255.0.0 U 1002 0 0 eth0
169.254.0.0 0.0.0.0 255.255.0.0 U 1003 0 0 eth1
添加路由临时生效
route -add -net 192.168.80.0 netmask 255.255.255.0 gw 192.168.20.20
route del -net 192.168.80.0 netmask 255.255.255.0
永久生效
vim /etc/sysconfig/network-scripts/route-eth0
ADDERSS0=192.168.80.0
NETMASK0=255.255.255.0
GATEWAY0=192.168.20.20
systemctl restart network
[root@reoute-nei ~]# cat /etc/sysconfig/network-scripts/ifcfg-eth[10]
DEVICE=eth0
TYPE=Ethernet
ONBOOT=yes
BOOTPROTO=static
IPADDR=192.168.10.123
NETMASK=255.255.255.0
GATEWAY=192.168.10.10
[root@reoute-nei ~]# route -n
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
192.168.10.0 0.0.0.0 255.255.255.0 U 0 0 0 eth0
169.254.0.0 0.0.0.0 255.255.0.0 U 1002 0 0 eth0
0.0.0.0 192.168.10.10 0.0.0.0 UG 0 0 0 eth0
[root@route ~]# cat /etc/sysconfig/network-scripts/ifcfg-eth[10]
DEVICE=eth0
TYPE=Ethernet
ONBOOT=yes
BOOTPROTO=static
IPADDR=192.168.10.10
NETMASK=255.255.255.0
DEVICE=eth1
TYPE=Ethernet
ONBOOT=yes
BOOTPROTO=static
IPADDR=192.168.20.10
NETMASK=255.255.255.0
[root@route ~]# cat /etc/sysconfig/network-scripts/route-eth1
ADDRESS0=192.168.80.0
NETMASK0=255.255.255.0
GATEWAY0=192.168.20.20
sed -i '/net.ipv4.ip_forward = 0/ net.ipv4.ip_forward = 1/ /etc/sysctl.conf
sysctl -p
[root@route ~]# route -n
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
192.168.20.0 0.0.0.0 255.255.255.0 U 0 0 0 eth1
192.168.80.0 192.168.20.20 255.255.255.0 UG 0 0 0 eth1
192.168.10.0 0.0.0.0 255.255.255.0 U 0 0 0 eth0
169.254.0.0 0.0.0.0 255.255.0.0 U 1002 0 0 eth0
169.254.0.0 0.0.0.0 255.255.0.0 U 1003 0 0 eth1
[root@route2 ~]# cat /etc/sysconfig/network-scripts/ifcfg-eth[10]
DEVICE=eth0
TYPE=Ethernet
ONBOOT=yes
BOOTPROTO=static
IPADDR=192.168.80.10
DEVICE=eth1
TYPE=Ethernet
ONBOOT=yes
BOOTPROTO=static
IPADDR=192.168.20.20
[root@route2 ~]# cat /etc/sysconfig/network-scripts/route-eth1
ADDRESS0=192.168.10.0
NETMASK0=255.255.255.0
GATEWAY0=192.168.20.10
[root@route2 ~]# route -n
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
192.168.20.0 0.0.0.0 255.255.255.0 U 0 0 0 eth1
192.168.80.0 0.0.0.0 255.255.255.0 U 0 0 0 eth0
192.168.10.0 192.168.20.10 255.255.255.0 UG 0 0 0 eth1
169.254.0.0 0.0.0.0 255.255.0.0 U 1002 0 0 eth0
169.254.0.0 0.0.0.0 255.255.0.0 U 1003 0 0 eth1
sed -i '/net.ipv4.ip_forward = 0/ net.ipv4.ip_forward = 1/ /etc/sysctl.conf
sysctl -p
[root@route-wai ~]# cat /etc/sysconfig/network-scripts/ifcfg-eth0
DEVICE=eth0
TYPE=Ethernet
ONBOOT=yes
BOOTPROTO=static
IPADDR=192.168.80.123
GATEWAY=192.168.80.10
NDS=223.5.5.5
NETMASK=255.255.255.0
[root@route-wai ~]# route -n
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
192.168.80.0 0.0.0.0 255.255.255.0 U 0 0 0 eth0
169.254.0.0 0.0.0.0 255.255.0.0 U 1002 0 0 eth0
0.0.0.0 192.168.80.10 0.0.0.0 UG 0 0 0 eth0
数据包经过网卡进入到应用层,要通过 prerouting loca_in
从linux系统出去要通过, local_out,post_routing
转发数据包要通过prerouting forward postrouting
数据包通过这些钩子干吗呢?钩子函数会检查filter,nat,mangle表,安装表里面的规则进行过滤
iptalbes 查看规则
删除reject规则
临时生效
iptalbes -D FORWARD 1
永久生效
/etc/rc.d/init.d/iptables save
iptables -t table 命令 chian rules -j target
table
能够是filter nat mangle nat 默认是filter
命令
-P或--policy 用来定义默认策略,就是容许通不经过
-A或 --append 在规则列表的最后增长一条规则
-I 或insert 在指定的位置插入一条规则,不指定位置就是在最上面插入
-D 或 --delete 删除一个规则
-R或 --replace修改某条规则
-F或 --flush 删除表中的全部规则
iptables -t filter -P FORWARD ACCEPT 设置filter表ACCEPT链默认接受数据包
iptables -t filter -I INPUT -p icmp -j DROP 在INPUT首插入1条规则
iptables -I INPUT 2 -p icmp -j DROP 在INPUT链第二行插入1条规则
iptables -D INPUT 2 删除INPUT链的第二条规则
iptables -R INPUT 2 -p icmp -j DROP 修改INPUT第二条规则,改为DROP
iptables -F INPUT 清除INPUT链中的全部规则
iptables -F 不指定链是清除全部
禁止外网ping route 路由器,可是能ping通内网,因此,设置 filter参数,只是包过滤,
[root@route ~]# iptables -t filter -I INPUT -p icmp -i eth1 -j DROP
以后
wai网 ping eth1网卡就不通了
可是不影响外网ping10.123的机器
为何要用FORWARD 由于80.123经过route 链接10.123 对与FORWARD来讲.route负责的是转发
iptables -t filter -I FORWARD -p tcp -s 192.168.80.123/32 -d 192.168.10.123/32 --dport 22 -j DROP
以后 SSH 链接不上了
再次添加 192.168.80.0网段对 192.168.10.0网段有全部权限
[root@route ~]# iptables -t filter -I FORWARD -s 192.168.80.0/24 -d 192.168.10.0/24 -j ACCEPT
又能链接了,由于比较宽泛的规则在FORWARD最上面,匹配到了
iptables -t filter -I output -m state --state NEW -j DROP
在linux做为路由器上设置规则
禁止192.168.10.0/24 ping通192.168.80.0/24
容许192.168.80.0/24 ping通192.168.10.0/24
[root@route ~]# iptables -t filter -I FORWARD -s 192.168.10.0/24 -d 192.168.80.0/24
-p icmp -m icmp --icmp-type echo-request -j DROP
iptables -t 表 -I链 -s 谁 到谁哪里 -p 协议 (类型是 -m icmp --icmp--type echo -request) -j 动做
结果:
内网ping 不通外网
外网ping的通neiwang
查看iptables表
示例
[root@reoute-nei ~]# iptables -t filter -I INPUT -p icmp -m icmp --icmp-type echo-re
quest -j DROP
linux ping别人通
别人ping linxu不通
指定多端口号:
-m multiport
--sport 源端口
--port 目标端口
--ports 源和目标一块儿指
禁止192.168.80.0/24 访问 192.168.80.0/24 1-----1024端口
iptables -t filter -I FORWARD -p tcp -d 192.168.10.0/24 -s 192.168.80.0/24 -m multiport --dports 1:1024 -j DROP
-m iprange
--src-range ip-ip
--src-range ip-ip
禁止192.168.80.0/24地址段 访问 192.168.80.0/24
[root@route ~]# iptables -t filter -I FORWARD -m iprange --src-range 192.168.80.1-192.168.80.140 -j DROP
不能链接不能ping
--comlimit-above 限定大链接个数
路由器上设置
[root@route ~]# iptables -t filter -I FORWARD -s 192.168.10.0/24 -p icmp -j REJECT
内网显示目标端口不可达
路由器再在REJECT前面添加DROP动做
内外在次ping 显示超时
禁止windows远程桌面链接. 抓包显示
[root@route ~]# iptables -t filter -I FORWARD -s 192.168.10.0/24 -p tcp --dport 3380 -j REJECT
[root@route ~]# iptables -t filter -I FORWARD -s 192.168.10.123/24 -p tcp --dport 22 -j DROP
[root@route ~]# iptables -t filter -I FORWARD -s 192.168.10.123/24 -p tcp --dport 22 -j LOG --log-prefix "iptables-ssh"
先写禁止ssh链接,在写寄到日志,保证条目里日志在上面
在/var/log/messages里查看,就有iptables-ssh的日志
iptables -t filter -I INPUT -p icmp -j LOG --log-prefix "iptables-icmp"
数据包通过3个流向:
数据包经过网卡进入到应用层,要通过 prerouting loca_in
从linux系统出去要通过, local_out,post_routing
转发数据包穿过电脑(把电脑看成路由器)要通过prerouting forward postrouting
数据包通过这些钩子干吗呢?钩子函数会检查filter,nat,mangle表,安装表里面的规则进行过滤
能够是filter表 nat表 mangle表 nat表 默认是filter表
-N: new 自定义1条新的规则链
-X:delete 删除自定义的空的规则链
-P : policy 设置默认策略,对filter表中的链而言,默认策略有
ACCEPT: 接受
DROP: 丢弃
REJECT: 拒绝
-A或 --append 在规则列表的最后增长一条规则
-I 或insert 在指定的位置插入一条规则,不指定位置就是在最上面插入
-D 或 --delete 删除一个规则 -D input 2 或指名规则自己
-R或 --replace替换某条规则 -R INPUT 3
-F或 --flush 删除表中的全部规则
-Z:置零,iptables有2个计数器
[root@web02 ~]#iptables -vnL
Chain INPUT (policy DROP 10202 packets, 1085K bytes)
pkts bytes target prot opt in out source destination
2630 259K ACCEPT all -- * * 192.168.2.103 0.0.0.0/0
[root@web02 ~]#iptables -vnL --line-numbers
Chain INPUT (policy DROP 10212 packets, 1086K bytes)
num pkts bytes target prot opt in out source destination
1 2669 262K ACCEPT all -- * * 192.168.2.103 0.0.0.0/0
iptables -s 显示命令.能够复制下来放到脚本里
iptables -D INPUT 2 #删除INPUT2链的第二条规则
iptables -F -t nat
- Z
[root@web02 ~]#iptables -vnL --line-numbers
Chain INPUT (policy DROP 3 packets, 285 bytes)
num pkts bytes target prot opt in out source destination
1 3524 330K ACCEPT all -- * * 192.168.2.103 0.0.0.0/0
清空INPUT的计数器
[root@web02 ~]#iptables -Z INPUT
[root@web02 ~]#iptables -vnL --line-numbers
Chain INPUT (policy DROP 2 packets, 168 bytes)
num pkts bytes target prot opt in out source destination
1 6 396 ACCEPT all -- * * 192.168.2.103 0.0.0.0/0
iptables - I INPUT ! -s 192.168.2.103 - j DROP
除2.103地址之外的全都拒绝
[root@web02 ~]#iptables -vnL --line-numbers -t raw| filter | raw | nat
-N: new 自定义1条新的规则链
-X:delete 删除自定义的空的规则链
-P : policy 设置默认策略,对filter表中的链而言,默认策略有
ACCEPT: 接受
DROP: 丢弃
REJECT: 拒绝
大写的是动做.小写的是模块,支持不少
[root@web02 ~]#rpm -ql iptables
/etc/sysconfig/ip6tables-config
/etc/sysconfig/iptables-config
/usr/lib64/xtables/libip6t_REJECT.so
/usr/lib64/xtables/libip6t_SNAT.so
/usr/lib64/xtables/libip6t_hbh.so
/usr/lib64/xtables/libip6t_hl.so
/usr/lib64/xtables/libip6t_icmp6.so
查看内核编译配置文件iptables以模块方式编译进内核
[root@web02 ~]#grep -i iptables /boot/config-3.10.0-862.el7.x86_64
CONFIG_IP_NF_IPTABLES=m
CONFIG_IP6_NF_IPTABLES=m
# iptables trigger is under
-N: new 自定义1条新的规则链
-X:delete 删除自定义的空的规则链
-P : policy 设置默认策略,对filter表中的链而言,默认策略有
ACCEPT: 接受
DROP: 丢弃
REJECT: 拒绝
-P 修改默认规则
修改成DOOP
查看默认规则
[root@web02 ~]#iptables -vnL
Chain INPUT (policy ACCEPT 220 packets, 19696 bytes) 默承认以进入
pkts bytes target prot opt in out source destination
Chain FORWARD (policy ACCEPT 0 packets, 0 bytes) 默承认以转发
pkts bytes target prot opt in out source destination
Chain OUTPUT (policy ACCEPT 91 packets, 8412 bytes) 默承认以出去
pkts bytes target prot opt in out source destination
设置windows本机能链接linux 192.168.2.103
[root@web02 ~]#iptables -vnL
除了192.168.2.103 其余都不能链接我
[root@web02 ~]#iptables -A INPUT -s 192.168.2.103 -j ACCEPT 源地址2.103能够链接
[root@web02 ~]#iptables -A OUTPUT -d 192.168.2.103 -j ACCEPT 目标2.103能够链接
查看添加了规则
[root@web02 ~]#iptables -vnL
Chain INPUT (policy ACCEPT 2 packets, 168 bytes)
pkts bytes target prot opt in out source destination
62 4768 ACCEPT all -- * * 192.168.2.103 0.0.0.0/0
Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
pkts bytes target prot opt in out source destination
Chain OUTPUT (policy ACCEPT 2 packets, 168 bytes)
pkts bytes target prot opt in out source destination
6 840 ACCEPT all -- * * 0.0.0.0/0 192.168.2.103
改默认规则,至此,除了2.103能够链接机器,其余任何人都不能链接机器了
[root@web02 ~]#iptables -P INPUT DROP #改INPUT的默认规则
[root@web02 ~]#iptables -P OUTPUT DROP #改INPUT的默认规则
查看 policy drop 是拒绝的意思,下面的规则就是白名单,只有192.168.2.103 的能够链接
[root@web02 ~]#iptables -vnL
Chain INPUT (policy DROP 148 packets, 13224 bytes) # 除了192.168.2.103其余都拒绝
pkts bytes target prot opt in out source destination
195 13576 ACCEPT all -- * * 192.168.2.103 0.0.0.0/0
Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
pkts bytes target prot opt in out source destination
Chain OUTPUT (policy DROP 4 packets, 240 bytes)
pkts bytes target prot opt in out source destination
81 9528 ACCEPT all -- * * 0.0.0.0/0 192.168.2.103
[root@web02 ~]#iptables -A INPUT -d 192.168.2.7 -j ACCEPT 进
[root@web02 ~]#iptables -A OUTPUT -s 192.168.2.7 -j ACCEPT 出
[root@web02 ~]#tcpdump -i eth0 -nn icmp
tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
listening on eth0, link-type EN10MB (Ethernet), capture size 262144 bytes
00:41:58.482577 IP 192.168.2.7 > 192.168.2.8: ICMP echo request, id 2525, seq 174, length 64
00:41:58.482630 IP 192.168.2.8 > 192.168.2.7: ICMP echo reply, id 2525, seq 174, length 64
00:41:59.482914 IP 192.168.2.7 > 192.168.2.8: ICMP echo request, id 2525, seq 175, length 64
[root@web02 ~]#iptables -A INPUT -s 192.168.2.7 -p icmp -j ACCEPT
[root@web02 ~]#iptables -A OUTPUT -d 192.168.2.7 -p icmp -j ACCEPT
[root@web02 ~]#iptables -vnL
Chain INPUT (policy DROP 3 packets, 351 bytes)
pkts bytes target prot opt in out source destination
1847 719K ACCEPT all -- * * 192.168.2.103 0.0.0.0/0
26 2184 ACCEPT icmp -- * * 192.168.2.7 0.0.0.0/0
Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
pkts bytes target prot opt in out source destination
Chain OUTPUT (policy DROP 2 packets, 120 bytes)
pkts bytes target prot opt in out source destination
1204 133K ACCEPT all -- * * 0.0.0.0/0 192.168.2.103
16 1344 ACCEPT icmp -- * * 0.0.0.0/0 192.168.2.7
0是回应,8是请求
容许192.168.2.7ping我
[root@web02 ~]#iptables -A INPUT -p icmp --icmp-type 8 -j ACCEPT
[root@web02 ~]#iptables -A OUTPUT -p icmp --icmp-type 0 -j ACCEPT
[root@web02 ~]#iptables -vnL --line-numbers
Chain INPUT (policy DROP 11 packets, 1287 bytes)
num pkts bytes target prot opt in out source destination
1 12276 1263K ACCEPT all -- * * 192.168.2.103 0.0.0.0/0
2 364 30576 ACCEPT icmp -- * * 0.0.0.0/0 0.0.0.0/0 icmptype 8
Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
num pkts bytes target prot opt in out source destination
Chain OUTPUT (policy DROP 0 packets, 0 bytes)
num pkts bytes target prot opt in out source destination
1 16264 3581K ACCEPT all -- * * 0.0.0.0/0 192.168.2.103
2 57 4788 ACCEPT icmp -- * * 0.0.0.0/0 0.0.0.0/0 icmptype 0
[root@web02 ~]#tcpdump -i eth0 -nn icmp
tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
listening on eth0, link-type EN10MB (Ethernet), capture size 262144 bytes
03:37:28.997305 IP 192.168.2.7 > 192.168.2.8: ICMP echo request, id 2803, seq 304, length 64
03:37:28.997353 IP 192.168.2.8 > 192.168.2.7: ICMP echo reply, id 2803, seq 304, length 64
个人包发过去了,别人也给我回应了,还没完成,我要给别人回应,因此INPUT imcp-type 0
[root@web02 ~]#iptables -A OUTPUT -p icmp --icmp-type 8 -j ACCEPT
[root@web02 ~]#iptables -A INTPUT -p icmp --icmp-type 0 -j ACCEPT
[root@web02 ~]#iptables -vnL --line-numbers
Chain INPUT (policy DROP 13 packets, 1521 bytes)
num pkts bytes target prot opt in out source destination
1 12740 1297K ACCEPT all -- * * 192.168.2.103 0.0.0.0/0
2 684 57456 ACCEPT icmp -- * * 0.0.0.0/0 0.0.0.0/0 icmptype 8
3 1 84 ACCEPT icmp -- * * 0.0.0.0/0 0.0.0.0/0 icmptype 0
Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
num pkts bytes target prot opt in out source destination
Chain OUTPUT (policy DROP 2 packets, 120 bytes)
num pkts bytes target prot opt in out source destination
1 16574 3612K ACCEPT all -- * * 0.0.0.0/0 192.168.2.103
2 377 31668 ACCEPT icmp -- * * 0.0.0.0/0 0.0.0.0/0 icmptype 0
3 3 252 ACCEPT icmp -- * * 0.0.0.0/0 0.0.0.0/0 icmptype 8
[root@web02 ~]#ping 192.168.2.7
PING 192.168.2.7 (192.168.2.7) 56(84) bytes of data.
64 bytes from 192.168.2.7: icmp_seq=1 ttl=64 time=0.228 ms
--- 192.168.2.7 ping statistics ---
1 packets transmitted, 1 received, 0% packet loss, time 0ms
rtt min/avg/max/mdev = 0.228/0.228/0.228/0.000 ms
隐式和显示
容许192.168.2.7ssh链接 web02
分析: web02 做为服务器,.2.7链接我是源地址 -s 是2.7的IP .目标端口是我,因此是--dport 22
OUTPUT出去的时候是从22端口回去是源地址,因此是--sport22 , 目标的ip是2.7 因此是 -d
打个比方,好比回去的端口是100,要写 --sport100
[root@web02 ~]#iptables -A INPUT -s 192.168.2.7 -p tcp --dport 22 -j ACCEPT
[root@web02 ~]#iptables -A OUTPUT -d 192.168.2.7 -p tcp --sport 22 -j ACCEPT
[root@web02 ~]#iptables -vnL
Chain INPUT (policy DROP 57 packets, 6099 bytes)
pkts bytes target prot opt in out source destination
9668 1078K ACCEPT all -- * * 192.168.2.103 0.0.0.0/0
144 12096 ACCEPT icmp -- * * 192.168.2.7 0.0.0.0/0
78 8874 ACCEPT tcp -- * * 192.168.2.7 0.0.0.0/0 tcp dpt:22
Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
pkts bytes target prot opt in out source destination
Chain OUTPUT (policy DROP 16 packets, 1040 bytes)
pkts bytes target prot opt in out source destination
14689 3346K ACCEPT all -- * * 0.0.0.0/0 192.168.2.103
134 11256 ACCEPT icmp -- * * 0.0.0.0/0 192.168.2.7
55 8126 ACCEPT tcp -- * * 0.0.0.0/0 192.168.2.7 tcp spt:22
容许192.168.2.7访问http
道理和上面的22端口同样,
[root@web02 wordpress]#iptables -A INPUT -s 192.168.2.7 -p tcp --dport 80 -j ACCEPT
[root@web02 wordpress]#iptables -A OUTPUT -d 192.168.2.7 -p tcp --sport 80 -j ACCEPT
[root@web02 wordpress]#iptables -vnL
Chain INPUT (policy DROP 5 packets, 585 bytes)
pkts bytes target prot opt in out source destination
10599 1143K ACCEPT all -- * * 192.168.2.103 0.0.0.0/0
144 12096 ACCEPT icmp -- * * 192.168.2.7 0.0.0.0/0
29 1848 ACCEPT tcp -- * * 192.168.2.7 0.0.0.0/0 tcp dpt:80
Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
pkts bytes target prot opt in out source destination
Chain OUTPUT (policy DROP 2 packets, 120 bytes)
pkts bytes target prot opt in out source destination
15234 3424K ACCEPT all -- * * 0.0.0.0/0 192.168.2.103
134 11256 ACCEPT icmp -- * * 0.0.0.0/0 192.168.2.7
20 2068 ACCEPT tcp -- * * 0.0.0.0/0 192.168.2.7 tcp spt:80
192.168.2.7测试访问成功
[root@web01 packages]# curl 192.168.2.8
fdasfdsafdasf
--connect-timeout 1|2|3|.... 快速cur 1秒...不通马上显示结果
[root@web01 packages]# curl --connect-timeout 1 192.168.2.8
用--tcp-flags要了解三次握手,四次挥手的标记位.咱们复习一下,
A发的时候ack B回复ack+syn A回复ack
分手的时候多了个fin
还有 3种,
紧急标记位
psh若是=1数据就不会放到缓存,直接传到应用程序里
reset,发生故障,断开重连
[root@web01 packages]# ssh 192.168.2.8
root@192.168.2.8's password:
Last login: Tue Jan 22 01:19:16 2019 from 192.168.2.7
[root@web02 ~]#
[root@web02 ~]#iptables -I INPUT 2 -s 192.168.2.7 -p tcp --syn -j REJECT
[root@web02 wordpress]#iptables -vnL
Chain INPUT (policy DROP 10 packets, 1170 bytes)
pkts bytes target prot opt in out source destination
10982 1172K ACCEPT all -- * * 192.168.2.103 0.0.0.0/0
2 120 REJECT tcp -- * * 192.168.2.7 0.0.0.0/0 tcp flags:0x17/0x02 reject-with icmp-port-unreachable
144 12096 ACCEPT icmp -- * * 192.168.2.7 0.0.0.0/0
331 28595 ACCEPT tcp -- * * 192.168.2.7 0.0.0.0/0 tcp dpt:22
68 4384 ACCEPT tcp -- * * 192.168.2.7 0.0.0.0/0 tcp dpt:80
链接不上.卡哪里了
[root@web01 packages]# curl 192.168.2.8/index.html
[root@web02 ~] iptables -I INPUT 2 -s 192.168.2.7 -p tcp --dport 80 --syn -j ACCEPT
[root@web02 ~] iptables -I INPUT 3 -s 192.168.2.7 -p tcp --syn -j REJECT
[root@web02 wordpress]#iptables -vnL --line-numbers
Chain INPUT (policy DROP 172 packets, 15636 bytes)
num pkts bytes target prot opt in out source destination
1 11836 1232K ACCEPT all -- * * 192.168.2.103 0.0.0.0/0
2 1 60 ACCEPT tcp -- * * 192.168.2.7 0.0.0.0/0 tcp dpt:80 flags:0x17/0x02
3 20 1200 REJECT tcp -- * * 192.168.2.7 0.0.0.0/0 tcp flags:0x17/0x02 reject-with icmp-port-unreachable
4 35 4557 ACCEPT tcp -- * * 192.168.2.7 0.0.0.0/0 tcp dpt:22
5 32 2111 ACCEPT tcp -- * * 192.168.2.7 0.0.0.0/0 tcp dpt:80
Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
num pkts bytes target prot opt in out source destination
Chain OUTPUT (policy DROP 4 packets, 240 bytes)
num pkts bytes target prot opt in out source destination
1 15989 3550K ACCEPT all -- * * 0.0.0.0/0 192.168.2.103
2 28 4701 ACCEPT tcp -- * * 0.0.0.0/0 192.168. 2.7 tcp spt:22
3 23 2232 ACCEPT tcp -- * * 0.0.0.0/0 192.168.2.7 tcp spt:80
[root@web01 packages]# curl 192.168.2.8/index.html
fdasfdsafdasf
[root@web01 packages]# ping 192.168.2.8
PING 192.168.2.8 (192.168.2.8) 56(84) bytes of data.
显示扩展要认为的指定调用的模块名:怎么查看模块名呢
centos7命令帮助man iptables-extensions
[root@web02 ~]#rpm -ql iptables
以离散方式定义多个端口.最多15个端口
-m multiport --sports 指定源端口
-m multiprot --dports 指定目标端口
--port:不分源目标端口
意思是: 进入本机的20,23,443,80均可以访问,
[root@web02 ~]#iptables -A OUTPUT -p tcp -m multiport --sports 20:23,443,80 -j ACCEPT
[root@web02 ~]#iptables -A INPUTP -p tcp -m multiport --dports 20:23,443,80 -j ACCEPT
[root@web02 ~]#iptables -nvL --line-numbers
Chain INPUT (policy DROP 0 packets, 0 bytes)
num pkts bytes target prot opt in out source destination
1 4978 362K ACCEPT all -- * * 192.168.2.105 0.0.0.0/0
2 0 0 ACCEPT tcp -- * * 0.0.0.0/0 0.0.0.0/0 multiport dports 20:23,443,80
Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
num pkts bytes target prot opt in out source destination
Chain OUTPUT (policy DROP 0 packets, 0 bytes)
num pkts bytes target prot opt in out source destination
1 2978 349K ACCEPT all -- * * 0.0.0.0/0 192.168.2.105
2 0 0 ACCEPT tcp -- * * 0.0.0.0/0 0.0.0.0/0 multiport sports 20:23,443,80
-m iprange --src-range 源ip
-m iprange --dst-range 目标ip
只容许,2.1- 2.100这个网段的机器的20...80等端口链接我
[root@web02 ~]#iptables -A INPUT -p tcp -m iprange --src-range 192.168.2.1-192.168.2.100 -m multiport --dports 20:23,443,80 -j ACCEPT
#注意: INPUT进来的是目标地址,源地址是1-100 因此是--src
--dport 指定应该是访问web02全部,web02 是目标端口
[root@web02 ~]#iptables -A OUTPUT -p tcp -m iprange --dst-range 192.168.2.1-192.168.2.100 -m multiport --sports 20:23,443,80 -j ACCEPT
#注意: OUTPUT ,出去的是源端口,因此是--sport src
[root@web02 ~]#iptables -vnL -line-numbers
[root@web02 ~]#iptables -vnL --line-numbers
Chain INPUT (policy DROP 0 packets, 0 bytes)
num pkts bytes target prot opt in out source destination
1 8302 622K ACCEPT all -- * * 192.168.2.105 0.0.0.0/0
2 74 9344 ACCEPT tcp -- * * 0.0.0.0/0 0.0.0.0/0 source IP range 192.168.2.1-192.168.2.100 multiport dports 20:23,443,80
Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
num pkts bytes target prot opt in out source destination
Chain OUTPUT (policy DROP 0 packets, 0 bytes)
num pkts bytes target prot opt in out source destination
1 5484 643K ACCEPT all -- * * 0.0.0.0/0 192.168.2.105
2 0 0 ACCEPT tcp -- * * 0.0.0.0/0 0.0.0.0/0 destination IP range 192.168.2.1-192.168.2.100 multiport dports 20:23,443,80
[root@web02 ~]#iptables -A INPUT -s 192.168.2.17 -m mac --mac-source 00:0c:29:71:8c:5b -j ACCEPT
[root@web02 ~]#iptables -A OUTPUT -d 192.168.2.17 -j ACCEPT
[root@web02 ~]#iptables -vnL --line-numbers
Chain INPUT (policy DROP 21 packets, 2457 bytes)
num pkts bytes target prot opt in out source destination
1 10621 795K ACCEPT all -- * * 192.168.2.105 0.0.0.0/0
2 13 2741 ACCEPT all -- * * 192.168.2.17 0.0.0.0/0 MAC 00:0C:29:71:8C:5B
Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
num pkts bytes target prot opt in out source destination
Chain OUTPUT (policy DROP 7 packets, 464 bytes)
num pkts bytes target prot opt in out source destination
1 7068 810K ACCEPT all -- * * 0.0.0.0/0 192.168.2.105
2 12 2509 ACCEPT all -- * * 0.0.0.0/0 192.168.2.17
其余机器修改了ip地址和2.8不通,只有修改了IP地址和mac地址才行
[root@web01 ~]#tail /etc/sysconfig/network-scripts/ifcfg-eth0
NAME=eth0
DEVICE=eth0
IPADDR=192.168.2.17
DNS1=192.168.2.1
MACADDR=00:0C:29:71:8C:5B
[root@web01 ~]#ping 192.168.2.8
PING 192.168.2.8 (192.168.2.8) 56(84) bytes of data.
64 bytes from 192.168.2.8: icmp_seq=1 ttl=64 time=0.291 ms
iptables -A INPUT -s 192.168.2.17 -j ACCEPT
iptables -A OUTPUT -d 192.168.2.17 -j ACCEPT
[root@web02 wordpress]#iptables -vnL --line-numbers
Chain INPUT (policy DROP 9 packets, 1053 bytes)
num pkts bytes target prot opt in out source destination
1 6629 522K ACCEPT all -- * * 192.168.2.105 0.0.0.0/0
2 147 9606 ACCEPT all -- * * 192.168.2.17 0.0.0.0/0
Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
num pkts bytes target prot opt in out source destination
Chain OUTPUT (policy DROP 4 packets, 240 bytes)
num pkts bytes target prot opt in out source destination
1 3706 554K ACCEPT all -- * * 0.0.0.0/0 192.168.2.105
2 105 11064 ACCEPT all -- * * 0.0.0.0/0 192.168.2.17
[root@web02 wordpress]#iptables -I OUTPUT 2 -p tcp -m string --algo bm --string "google" -j REJECT
[root@web02 wordpress]#iptables -vnL --line-numbers
Chain INPUT (policy DROP 0 packets, 0 bytes)
num pkts bytes target prot opt in out source destination
1 6912 545K ACCEPT all -- * * 192.168.2.105 0.0.0.0/0
2 147 9606 ACCEPT all -- * * 192.168.2.17 0.0.0.0/0
Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
num pkts bytes target prot opt in out source destination
Chain OUTPUT (policy DROP 0 packets, 0 bytes)
num pkts bytes target prot opt in out source destination
1 3927 576K ACCEPT all -- * * 0.0.0.0/0 192.168.2.105
2 0 0 REJECT tcp -- * * 0.0.0.0/0 0.0.0.0/0 STRING match "google" ALGO name bm TO 65535 reject-with icmp-port-unreachable3
3 105 11064 ACCEPT all -- * * 0.0.0.0/0 192.168.2.17
[root@web02 ~]#echo "welcome to google" >> /code/wordpress/index2.html
curl还显示google有问题
[root@web03 ~]#curl 192.168.2.8/index2.html
安装httpd 测试
这个过滤google成功
[192.168.2.7-root@web01~]#cat index.html
fdafdsa
[192.168.2.7-root@web01~]#cat test.html
-s 本机是服务端,响应别人,因此源地址是本机的.2.7 -d是别人, -s 指定本机的端口
[192.168.2.7-root@web01~]#iptables -A OUTPUT -s 192.168.2.7 -d 192.168.2.17 -p tcp --sport 80 -m string --algo bm --string "google" -j REJECT
[192.168.2.7-root@web01~]#iptables -nvL
Chain INPUT (policy ACCEPT 415 packets, 57169 bytes)
pkts bytes target prot opt in out source destination
Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
pkts bytes target prot opt in out source destination
Chain OUTPUT (policy ACCEPT 190 packets, 35603 bytes)
pkts bytes target prot opt in out source destination
69 21429 REJECT tcp -- * * 192.168.2.7 192.168.2.17 tcp spt:80 STRING match "google" ALGO name bm TO 65535 reject-with icmp-port-unrea
chable
白名单--tring成功
[192.168.2.7-root@web01~]# iptables -A INPUT -s 192.168.2.105 -j ACCEPT
[192.168.2.7-root@web01~]#iptables -A OUTPUT -d 192.168.2.105 -j ACCEPT
[192.168.2.7-root@web01~]#iptables -P INPUT DROP
[192.168.2.7-root@web01~]#iptables -P OUTPUT DROP
[192.168.2.7-root@web01~]#iptables -A INPUT -s 192.168.2.17 -j ACCEPT
[192.168.2.7-root@web01~]#iptables -A OUTPUT -d 192.168.2.17 -j ACCEPT
[192.168.2.7-root@web01~]#iptables -I OUTPUT 2 -p tcp -m string --algo bm --string "google" -j REJECT添加的规则
[192.168.2.7-root@web01~]#iptables -vnL --line-numbers
Chain INPUT (policy DROP 109 packets, 12753 bytes)
num pkts bytes target prot opt in out source destination
1 717 48746 ACCEPT all -- * * 192.168.2.105 0.0.0.0/0
2 65 4068 ACCEPT all -- * * 192.168.2.17 0.0.0.0/0
Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
num pkts bytes target prot opt in out source destination
Chain OUTPUT (policy DROP 134 packets, 40388 bytes)
num pkts bytes target prot opt in out source destination
1 382 73328 ACCEPT all -- * * 0.0.0.0/0 192.168.2.105
2 116 36060 REJECT tcp -- * * 0.0.0.0/0 0.0.0.0/0 STRING match "google" ALGO name bm TO 65535 reject-with icmp-port-unreachable
3 27 2542 ACCEPT all -- * * 0.0.0.0/0 192.168.2.17
测试过滤成功
[192.168.2.17-root@web01~]#curl 192.168.2.7/index.html
fdafdsa
[192.168.2.17-root@web01~]#curl 192.168.2.7/test.html
在--string 过滤google基础上增长--time
[192.168.2.7-root@web01~]#iptables -I INPUT 2 -p tcp --dport 80 -m time --timestart 9:00 --timestop 22:00 --weekdays 1,3,5 -j REJECT
[192.168.2.7-root@web01~]#iptables -vnL
Chain INPUT (policy DROP 4 packets, 468 bytes)
pkts bytes target prot opt in out source destination
599 44244 ACCEPT all -- * * 192.168.2.105 0.0.0.0/0
2 120 REJECT tcp -- * * 0.0.0.0/0 0.0.0.0/0 tcp dpt:80 TIME from 09:00:00 to 22:00:00 on Mon,Wed,Fri UTC reject-with icmp-port-
unreachable 44 4724 ACCEPT all -- * * 192.168.2.17 0.0.0.0/0
Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
pkts bytes target prot opt in out source destination
Chain OUTPUT (policy DROP 4 packets, 284 bytes)
pkts bytes target prot opt in out source destination
408 45080 ACCEPT all -- * * 0.0.0.0/0 192.168.2.105
50 15558 REJECT tcp -- * * 0.0.0.0/0 0.0.0.0/0 STRING match "google" ALGO name bm TO 65535 reject-with icmp-port-unreachable
31 4483 ACCEPT all -- * * 0.0.0.0/0 192.168.2.17
--datestart YYYY[-MM[-DD[Thh[:mm[:ss]]]]]
年 月 日 小时 分 秒
--datestop YYYY[-MM[-DD[Thh[:mm[:ss]]]]]
--timestart hh:mm[:ss]
--timestop hh:mm[:ss]
--kerneltz使用内核上的时区,而不是UTC时区
2.0网段每周1,3,5 9点到22点禁止访问2.7 80端口
[192.168.2.7-root@web01~]#iptables -A INPUT -s 192.168.2.0/24 -d 192.168.2.7 -p tcp --dport 80 -m time --timestart 9:00 --timestop 22:00 --weekdays 1,3,5 --kerneltz -j REJECT
[192.168.2.7-root@web01~]#iptables -vnL
Chain INPUT (policy ACCEPT 8 packets, 630 bytes)
pkts bytes target prot opt in out source destination
1 60 REJECT tcp -- * * 192.168.2.0/24 192.168.2.7 tcp dpt:80 TIME from 09:00:00 to 22:00:00 on Mon,Wed,Fri reject-with icmp-port-unreachable
根据每一个客户端ip作并发来链接数量匹配
--connlimit-upto n: 链接数量小于n时匹配
--connlimit-above n: 链接数量小于n时匹配
其余客户端最多只能用ssh链接我2次,2次以上就拒绝
[192.168.2.7-root@web01~]#iptables -A INPUT -d 192.168.2.7 -p tcp --dport 22 -m connlimit --connlimit-above 2 -j REJECT
[192.168.2.7-root@web01~]#iptables -vnL
Chain INPUT (policy ACCEPT 6 packets, 396 bytes)
pkts bytes target prot opt in out source destination
2 104 REJECT tcp -- * * 0.0.0.0/0 192.168.2.7 tcp dpt:22 #conn src/32 > 2 reject-with icmp-port-unreachable
--limit rate[/second|/minute|/hour|/day] 放行的速率 每秒/每分钟/每小时/天天
--limit-burst n : 峰值速率
[192.168.2.7-root@web01~]#iptables -A INPUT -p icmp -m limit --limit-burst 4 --limit 3/minute -j ACCEPT
注意这个类功能要加2条规则,由于 指定的时每分种3个 --limit 3/minute,也就是说每分钟3个之外的包哪里去呢,因此要加一条 icmp都拒绝的,也就是说 每分钟3个以外的包,用下面的icmp -j DROP的规则都给拒绝掉
[192.168.2.7-root@web01~]#iptables -A INPUT -p icmp -j DROP
[192.168.2.7-root@web01~]#iptables -vnL
Chain INPUT (policy ACCEPT 62 packets, 4255 bytes)
pkts bytes target prot opt in out source destination
2 104 REJECT tcp -- * * 0.0.0.0/0 192.168.2.7 tcp dpt:22 #conn src/32 > 2 reject-with icmp-port-unreachable
24 2020 ACCEPT icmp -- * * 0.0.0.0/0 0.0.0.0/0 limit: avg 3/min burst 4
12 1008 DROP icmp -- * * 0.0.0.0/0 0.0.0.0/0
测试结果
容许windows链接 web02 ,其余人ping web02 ,峰值速率4,放行速率每分钟20个
[192.168.2.8-root@web02 ~]#iptables -A INPUT -p icmp -m limit --limit-burst 4 --limit 20/minute -j ACCEPT
[192.168.2.8-root@web02 ~]#iptables -A OUTPUT -p icmp -m limit --limit-burst 4 --limit 20/minute -j ACCEPT
[192.168.2.8-root@web02 ~]#iptables -vnL
Chain INPUT (policy DROP 8 packets, 771 bytes)
pkts bytes target prot opt in out source destination
59 4984 ACCEPT icmp -- * * 0.0.0.0/0 0.0.0.0/0 limit: avg 20/min burst 4
2062 146K ACCEPT all -- * * 192.168.2.105 0.0.0.0/0
Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
pkts bytes target prot opt in out source destination
Chain OUTPUT (policy DROP 2 packets, 120 bytes)
pkts bytes target prot opt in out source destination
9138 1078K ACCEPT all -- * * 0.0.0.0/0 192.168.2.105
8 672 ACCEPT icmp -- * * 0.0.0.0/0 0.0.0.0/0
做用:能够根据链接跟踪状态,检查每次发送的请求是新链接仍是旧链接
NEW:是新的请求,第一次握手
ESTABLISHED: 是NEW状态以后,即3次握手以后,有来有回了,不是第一次链接了,
有1个请求发过来了,
web02的OUTPUT上不能有NEW,也就是和外面主动创建链接,若是有和外面主动创建链接,这是不正常的,只有1种可能,web02中了病毒或木马,主动的发送信息,给黑客,下面就经过实验,不让这种事情发生
[192.168.2.8-root@web02 ~]#iptables -vnL
Chain INPUT (policy DROP 68 packets, 7956 bytes)
pkts bytes target prot opt in out source destination
4639 334K ACCEPT all -- * * 192.168.2.105 0.0.0.0/0
Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
pkts bytes target prot opt in out source destination
Chain OUTPUT (policy DROP 105 packets, 17704 bytes)
pkts bytes target prot opt in out source destination
10852 1257K ACCEPT all -- * * 0.0.0.0/0 192.168.2.105
这种ESTABLISTED的状态都要写在防火墙规则最上面,由于都是要创建链接的,上来不用找,直接就走这个规则,若是放到后面,用户访问,还要一层层的向下找,若是放到下面的话,效率过低,还浪费资源
[192.168.2.8-root@web02 ~]#iptables -I INPUT 1 -s 192.168.2.17 -p tcp --dport 22 -m state --state NEW,ESTABLISHED -j ACCEPT
192.168.2.17能够主动链接web02
[192.168.2.8-root@web02 ~]#iptables -I OUTPUT 1 -d 192.168.2.17 -p tcp --sport 22 -m state --state ESTABLISHED -j ACCEPT
192.168.2.17 是主动链接web02的,web02只能经过,这个192.168.2.17主动链接的关系,经过ESTABLESHED响应192.168.2.17实现不主动链接外面
[192.168.2.8-root@web02 ~]#iptables -vnL
Chain INPUT (policy DROP 667 packets, 78039 bytes)
pkts bytes target prot opt in out source destination
160 22561 ACCEPT tcp -- * * 192.168.2.17 0.0.0.0/0 tcp dpt:22 state NEW,ESTABLISHED
2220 162K ACCEPT all -- * * 192.168.2.105 0.0.0.0/0
Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
pkts bytes target prot opt in out source destination
Chain OUTPUT (policy DROP 115 packets, 6988 bytes)
pkts bytes target prot opt in out source destination
13915 1597K ACCEPT all -- * * 0.0.0.0/0 192.168.2.105
102 17868 ACCEPT tcp -- * * 0.0.0.0/0 192.168.2.17 tcp spt:22 state ESTABLISHED
实验结果: 192.168.2.17能连 web02 ,web02 链接不了别人
[192.168.2.17-root@web03 ~]#ssh 192.168.2.8
root@192.168.2.8's password:
[192.168.2.8-root@web02 ~]#ssh 192.168.2.17
查看下 链接追踪的数据库文件,有记录了
[192.168.2.8-root@web02 ~]#cat /proc/net/nf_conntrack
ipv4 2 tcp 6 299 ESTABLISHED src=192.168.2.105 dst=192.168.2.8 sport=14216 dport=22 src=192.168.2.8 dst=192.168.2.105 sport=22 dport=14216 [ASSURED] mark=0 zone=0
use=2ipv4 2 tcp 6 428037 ESTABLISHED src=192.168.2.17 dst=192.168.2.8 sport=38610 dport=22 src=192.168.2.8 dst=192.168.2.17 sport=22 dport=38610 [ASSURED] mark=0 zone=0
use=2
iptables命令总结
[root@web02 ~]#iptables -N drop_invalid_packets
自定义一条链,叫drop_invalid_packets
[root@web02 ~]#iptables -vnL
Chain INPUT (policy DROP 2 packets, 234 bytes)
pkts bytes target prot opt in out source destination
1512 110K ACCEPT all -- * * 192.168.2.105 0.0.0.0/0
Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
pkts bytes target prot opt in out source destination
Chain OUTPUT (policy DROP 0 packets, 0 bytes)
pkts bytes target prot opt in out source destination
975 99681 ACCEPT all -- * * 0.0.0.0/0 192.168.2.105
Chain drop_invalid_packets (0 references)
pkts bytes target prot opt in out source destination
添加规则
添加tcp协议的数据包全是1 数据包,全是0的,都拒绝掉
[root@web02 ~]#iptables -A drop_invalid_packets -p tcp --tcp-flags ALL ALL -j REJECT
[root@web02 ~]#iptables -A drop_invalid_packets -p tcp --tcp-flags ALL NONE -j REJECT
[root@web02 ~]#iptables -vnL
Chain INPUT (policy DROP 2 packets, 234 bytes)
pkts bytes target prot opt in out source destination
1784 129K ACCEPT all -- * * 192.168.2.105 0.0.0.0/0
Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
pkts bytes target prot opt in out source destination
Chain OUTPUT (policy DROP 0 packets, 0 bytes)
pkts bytes target prot opt in out source destination
1120 115K ACCEPT all -- * * 0.0.0.0/0 192.168.2.105
Chain drop_invalid_packets (1 references)
pkts bytes target prot opt in out source destination
0 0 REJECT tcp -- * * 0.0.0.0/0 0.0.0.0/0 tcp flags:0x3F/0x3F reject-with icmp-port-unreachable
0 0 REJECT tcp -- * * 0.0.0.0/0 0.0.0.0/0 tcp flags:0x3F/0x00 reject-with icmp-port-unreachable
自定义的链是孤立的须要关联到系统自带的链上,
[root@web02 ~]#iptables -A INPUT -s 192.168.2.17 -j drop_invalid_packets
来自192.168.2.17的 tcp包全是 1或者0的都走自定义链的规则 REJECT.强硬的拒绝掉
[root@web02 ~]#iptables -vnL
Chain INPUT (policy DROP 2 packets, 234 bytes)
pkts bytes target prot opt in out source destination
2214 160K ACCEPT all -- * * 192.168.2.105 0.0.0.0/0
2 168 drop_invalid_packets all -- * * 192.168.2.17 0.0.0.0/0
Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
pkts bytes target prot opt in out source destination
Chain OUTPUT (policy DROP 0 packets, 0 bytes)
pkts bytes target prot opt in out source destination
1383 140K ACCEPT all -- * * 0.0.0.0/0 192.168.2.105
Chain drop_invalid_packets (2 references)
pkts bytes target prot opt in out source destination
0 0 REJECT tcp -- * * 0.0.0.0/0 0.0.0.0/0 tcp flags:0x3F/0x3F reject-with icmp-port-unreachable
0 0 REJECT tcp -- * * 0.0.0.0/0 0.0.0.0/0 tcp flags:0x3F/0x00 reject-with icmp-port-unreachable
下面咱们就 实验如下这样的用法
[root@web02 ~]#iptables -N INTERNETACCESS
[root@web02 ~]#iptables -A INTERNETACCESS -p tcp --dport 80 -j ACCEPT
[root@web02 ~]#iptables -A INTERNETACCESS -p tcp --dport 443 -j ACCEPT
[root@web02 ~]#iptables -A INTERNETACCESS -p tcp --dport 22 -j ACCEPT
[root@web02 ~]#iptables -A INTERNETACCESS -p tcp --dport 53 -j ACCEPT
[root@web02 ~]#iptables -A INTERNETACCESS -p udp --dport 53 -j ACCEPT
[root@web02 ~]#iptables -A INPUT -s 192.168.2.0/24 -j INTERNETACCESS
[root@web02 ~]#iptables -vnL --line-numbers
Chain INPUT (policy DROP 27 packets, 3159 bytes)
num pkts bytes target prot opt in out source destination
1 3151 229K ACCEPT all -- * * 192.168.2.105 0.0.0.0/0
2 2 168 drop_invalid_packets all -- * * 192.168.2.17 0.0.0.0/0
3 27 3159 INTERNETACCESS all -- * * 192.168.2.0/24 0.0.0.0/0
Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
num pkts bytes target prot opt in out source destination
Chain OUTPUT (policy DROP 6 packets, 360 bytes)
num pkts bytes target prot opt in out source destination
1 1921 192K ACCEPT all -- * * 0.0.0.0/0 192.168.2.105
Chain INTERNETACCESS (1 references)
num pkts bytes target prot opt in out source destination
1 0 0 ACCEPT tcp -- * * 0.0.0.0/0 0.0.0.0/0 tcp dpt:80
2 0 0 ACCEPT tcp -- * * 0.0.0.0/0 0.0.0.0/0 tcp dpt:443
3 0 0 ACCEPT tcp -- * * 0.0.0.0/0 0.0.0.0/0 tcp dpt:22
4 0 0 ACCEPT tcp -- * * 0.0.0.0/0 0.0.0.0/0 tcp dpt:53
5 0 0 ACCEPT udp -- * * 0.0.0.0/0 0.0.0.0/0 udp dpt:53
[root@web02 ~]#iptables -D INPUT 3
查看已经删除了INPUT链里的INTENTERACCESS自定义链
[root@web02 ~]#iptables -vnL
Chain INPUT (policy DROP 2 packets, 234 bytes)
pkts bytes target prot opt in out source destination
3451 250K ACCEPT all -- * * 192.168.2.105 0.0.0.0/0
2 168 drop_invalid_packets all -- * * 192.168.2.17 0.0.0.0/0
[root@web02 ~]#iptables -D INTERNETACCESS 1
[root@web02 ~]#iptables -F INTERNETACCESS
查看已经把自定链都清空了
[root@web02 ~]#iptables -vnL --line-numbers
Chain INPUT (policy DROP 79 packets, 9243 bytes)
num pkts bytes target prot opt in out source destination
1 3510 254K ACCEPT all -- * * 192.168.2.105 0.0.0.0/0
2 2 168 drop_invalid_packets all -- * * 192.168.2.17 0.0.0.0/0
Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
num pkts bytes target prot opt in out source destination
Chain OUTPUT (policy DROP 14 packets, 840 bytes)
num pkts bytes target prot opt in out source destination
1 2136 217K ACCEPT all -- * * 0.0.0.0/0 192.168.2.105
Chain INTERNETACCESS (0 references) #下面没规则了,清空完成
num pkts bytes target prot opt in out source destination
Chain drop_invalid_packets (1 references)
num pkts bytes target prot opt in out source destination
1 0 0 REJECT tcp -- * * 0.0.0.0/0 0.0.0.0/0 tcp flags:0x3F/0x3F reject-with icmp-port-unreachable
2 0 0 REJECT tcp -- * * 0.0.0.0/0 0.0.0.0/0 tcp flags:0x3F/0x00 reject-with icmp-port-unreachable
INTERNETACCEPT 链已经删除
[root@web02 ~]#iptables -vnL --line-numbers
Chain drop_invalid_packets (1 references)
num pkts bytes target prot opt in out source destination
1 0 0 REJECT tcp -- * * 0.0.0.0/0 0.0.0.0/0 tcp flags:0x3F/0x3F reject-with icmp-port-unreachable
2 0 0 REJECT tcp -- * * 0.0.0.0/0 0.0.0.0/0 tcp flags:0x3F/0x00 reject-with icmp-port-unreachable
cat /etc/sysconfig/iptables
执行service iptables save 命令保存到 /etc/sysconfig/iptables里
iptables-save把策略保存到文件
[192.168.2.8-root@web02 ~]#iptables-save
# Generated by iptables-save v1.4.21 on Sun Jan 27 16:16:57 2019
*filter
:INPUT DROP [1020:119340]
:FORWARD ACCEPT [0:0]
:OUTPUT DROP [183:10850]
-A INPUT -s 192.168.2.105/32 -j ACCEPT
-A INPUT -s 192.168.2.106/32 -j ACCEPT
-A OUTPUT -d 192.168.2.106/32 -j ACCEPT
-A OUTPUT -d 192.168.2.17/32 -p tcp -m tcp --sport 22 -m state --state ESTABLISHED -j ACCEPT
MMIT
iptables-save
[192.168.2.8-root@web02 ~]#iptables-save > /etc/sysconfig/iptables-test
iptables-restore
[192.168.2.8-root@web02 ~]#iptables-restore < /etc/sysconfig/iptables-test
-n | --noflush :不清除原有的规则
-t test : 测试
[192.168.2.8-root@web02 ~]# cat /etc/rc.d/rc.locatl
iptables-save < /etc/sysconfig/iptables-test
SANT
DNAT
MASQUERADE
内网: 内网网卡:eth0 ip:172.16.1.17 不能上外网
路由: 外网网卡:eth0 ip 192.168.2.8 内网:eth1 ip:172.16.1.8 eth0能够上外网,eth1不能上外网
外网: 外网网卡:eth1 ip 192.168.2.7 不能上外网
[172.16.2.17-root@web03 ~]#cat /etc/sysconfig/network-scripts/ifcfg-eth0
TYPE=Ethernet
BOOTPROTO=static
DEFROUTE=yes
NAME=eth0
DEVICE=eth0
ONBOOT=yes
IPADDR=172.16.1.17
GATEWAY=172.16.1.8
PREFIX=24
DNS1=223.5.5.5
route 机器: eth0外网.桥接模式
eth1内网.仅主机模式
[root@route ~]# cat /etc/sysconfig/network-scripts/ifcfg-eth0
TYPE=Ethernet
BOOTPROTO=none
NAME=eth0
DEVICE=eth0
ONBOOT=yes
IPADDR=192.168.2.8
NETMASK=255.255.255.0
GATEWAY=192.168.2.1
DNS1=223.5.5.5
[root@route ~]# cat /etc/sysconfig/network-scripts/ifcfg-eth1
TYPE=Ethernet
BOOTPROTO=none
NAME=eth1
DEVICE=eth1
ONBOOT=yes
IPADDR=172.16.1.8
[192.168.2.7-root@web01~]#cat /etc/sysconfig/network-scripts/ifcfg-eth0
TYPE=Ethernet
DEFROUTE=yes
NAME=eth0
DEVICE=eth0
ONBOOT=yes
IPADDR=192.168.2.7
GATEWAY=192.168.2.8
PREFIX=24
DNS1=192.168.2.1
1, 充当网关
开启内核转发
[root@route ~]# iptables -A FORWARD -p icmp -d 192.168.2.7 -j REJECT
语法格式:
iptables -t nat -A POSTROUTING -s 172.16.1.0/24 -j SNAT --to-source 192.168.2.8-192.168.2.100
格式:-s 指定源地址, -j SNAT做源地址转换 --to-source指定由哪一个外网ip地址转换
192.168.2.8-192.168.2.100 能够是多个地址 加"-"
route直接添加规则
[root@route ~]# iptables -t nat -A POSTROUTING -s 172.16.1.0/24 -j SNAT --to-source 192.168.2.8
[root@route ~]# iptables -vnL -t nat
Chain PREROUTING (policy ACCEPT 30 packets, 3088 bytes)
pkts bytes target prot opt in out source destination
Chain INPUT (policy ACCEPT 5 packets, 1090 bytes)
pkts bytes target prot opt in out source destination
Chain OUTPUT (policy ACCEPT 4 packets, 304 bytes)
pkts bytes target prot opt in out source destination
Chain POSTROUTING (policy ACCEPT 4 packets, 304 bytes)
pkts bytes target prot opt in out source destination
6 360 SNAT all -- * * 172.16.1.0/24 0.0.0.0/0 to:192.168.2.8
snat 转换的只是IP地址,不转换端口,咱们能够证实一下
telnet 192.168.2.7 80端口
[172.16.2.17-root@web03 ~]#telnet 192.168.2.7 80
Trying 192.168.2.7...
Connected to 192.168.2.7.
Escape character is '^]'.
在内网机器上新开终端,ss 命令查看,端口是33352
[172.16.2.17-root@web03 ~]#ss -tn
State Recv-Q Send-Q Local Address:Port Peer Address:Port
ESTAB 0 0 172.16.1.17:33370 192.168.2.7:80
192.168.2.7机器查看 链接的ip地址是 防火墙的ip,证实了snat只转换ip不转换端口
[192.168.2.7-root@web01~]#ss -tn
[192.168.2.7-root@web01~]#ss -tn
State Recv-Q Send-Q Local Address:Port Peer Address:Port
ESTAB 0 0 192.168.2.7:80 192.168.2.8:33370
MASQUERADE 动态ip 适用于小型企业ADSL拨号,不固定的IP
[root@route ~]# iptables -t nat -A POSTROUTING -s 172.16.1.0/24 -j MASQUERADE
[root@route ~]# iptables -t nat -R POSTROUTING 1 -s 172.16.1.0/24 -j MASQUERADE
[root@route ~]# iptables -t nat -vnL
Chain PREROUTING (policy ACCEPT 0 packets, 0 bytes)
pkts bytes target prot opt in out source destination
Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
pkts bytes target prot opt in out source destination
Chain OUTPUT (policy ACCEPT 0 packets, 0 bytes)
pkts bytes target prot opt in out source destination
Chain POSTROUTING (policy ACCEPT 0 packets, 0 bytes)
pkts bytes target prot opt in out source destination
0 0 MASQUERADE all -- * * 172.16.1.0/24 0.0.0.0/0
[192.168.2.7-root@web01~]#ping 172.16.1.17
connect: Network is unreachable
[192.168.2.7-root@web01~]#route -n
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
169.254.0.0 0.0.0.0 255.255.0.0 U 1002 0 0 eth0
192.168.2.0 0.0.0.0 255.255.255.0 U 0 0 0 eth0
192.168.2.7 curl route
[192.168.2.7-root@web01~]# curl 192.168.2.8
防火墙的http
[root@route ~]# iptables -t nat -A PREROUTING -d 192.168.2.8 -p tcp --dport 80 -j DNAT --to-destination 172.16.1.17
还能够直接转换端口,把192.168.2.8的80端口,替换为 172.16.1.17的8080端口
[root@route ~]# iptables -t nat -A PREROUTING -d 192.168.2.8 -p tcp --dport 80 -j DNAT --to-destination 172.16.1.17:8080
[root@route ~]# iptables -t nat -vnL
Chain PREROUTING (policy ACCEPT 2 packets, 468 bytes)
pkts bytes target prot opt in out source destination
1 60 DNAT tcp -- * * 0.0.0.0/0 192.168.2.8 tcp dpt:80 to:172.16.1.17
Chain INPUT (policy ACCEPT 1 packets, 234 bytes)
pkts bytes target prot opt in out source destination
Chain OUTPUT (policy ACCEPT 3 packets, 228 bytes)
pkts bytes target prot opt in out source destination
Chain POSTROUTING (policy ACCEPT 4 packets, 288 bytes)
pkts bytes target prot opt in out source destination
[192.168.2.7-root@web01~]# curl 192.168.2.8
172.16.1.17
经过改变目标的IP和端口, 将接收的包转发到不一样地址,
把别人访问个人80端口,转换成其余端口
实验:
[192.168.2.7-root@web01~]# curl 192.168.2.8
curl: (7) Failed connect to 192.168.2.8:80; Connection refused
[172.16.2.17-root@web03 ~]#iptables -t nat -A PREROUTING -d 172.16.1.17 -p tcp --dport 80 -j REDIRECT --to-ports 8080
[192.168.2.7-root@web01~]# curl 192.168.2.8
172.16.1.17