http://book.luffycity.com/linux-book/互联网服务基础/iptables.html
iptables 四表:
1. raw:高级功能,如 网址过滤。
2. mangle:数据包修改(QOS),用于实现服务质量
3. net:地址转换,用于网关路由器。
4. filter:包过滤,用于防火墙规则。
iptables 五链:
1. INPUT链:处理输入数据包。
2. OUTPUT:处理输出数据包。
3. FORWARD:处理转发数据包。
4. PREROUTING:用于目标地址转换(DNAT)。
5. POSTROUTING: 用于源地址转换(SNAT)。
动作包括:
1. ACCEPT
2. DROP
3. REDIRECT
4. SNAT
5. DNAT
6. MASQUSERADE
7. LOG
案例
1. 禁止服务器被ping
#给INPUT链添加规则,指定icmp协议,指定icmp类型 是8(回显请求), -s指定网段范围 -j 跳转的目标,即将做什么 iptables -A INPUT -p icmp --icmp-type 8 -s 0/0 -j REJECT #客户端机器 yumac: ~ yuchao$ping 123.206.16.61 PING 123.206.16.61 (123.206.16.61): 56 data bytes 92 bytes from pyyuc (123.206.16.61): Destination Port Unreachable Vr HL TOS Len ID Flg off TTL Pro cks Src Dst 4 5 00 5400 c4b6 0 0000 33 01 6b31 192.168.11.14 123.206.16.61
2. 服务器禁ping,直接丢弃。
[root@chaogelinux ~]# iptables -F [root@chaogelinux ~]# iptables -A INPUT -p icmp --icmp-type 8 -s 0/0 -j DROP yumac: ~ yuchao$ping 123.206.16.61 PING 123.206.16.61 (123.206.16.61): 56 data bytes Request timeout for icmp_seq 0 Request timeout for icmp_seq 1 Request timeout for icmp_seq 2
3. 检查防火墙规则。
[root@chaogelinux ~]# iptables -L Chain INPUT (policy ACCEPT) target prot opt source destination DROP icmp -- anywhere anywhere icmp echo-request Chain FORWARD (policy ACCEPT) target prot opt source destination Chain OUTPUT (policy ACCEPT) target prot opt source destination
4. 清空所有防火墙规则
[root@chaogelinux ~]# iptables -F [root@chaogelinux ~]# iptables -L Chain INPUT (policy ACCEPT) target prot opt source destination Chain FORWARD (policy ACCEPT) target prot opt source destination Chain OUTPUT (policy ACCEPT) target prot opt source destination
6. 删除第一条规则
[root@chaogelinux ~]# iptables -L Chain INPUT (policy ACCEPT) target prot opt source destination DROP icmp -- anywhere anywhere icmp echo-request Chain FORWARD (policy ACCEPT) target prot opt source destination Chain OUTPUT (policy ACCEPT) target prot opt source destination [root@chaogelinux ~]# [root@chaogelinux ~]# iptables -D INPUT 1
7. 禁止访问本机80端口
#禁止流量进入,指定tcp类型,拒绝的端口是80,动作是拒绝 iptables -A INPUT -p tcp --dport 80 -j DROP #客户端访问 pythonav.cn
8. 禁止访问本机FTP服务。
yumac: ~ yuchao$ftp 123.206.16.61 Connected to 123.206.16.61. 220 (vsFTPd 3.0.2) Name (123.206.16.61:yuchao): chaoge 331 Please specify the password. Password: 230 Login successful. ftp> ls 200 PORT command successful. Consider using PASV. 150 Here comes the directory listing. -rw-r--r-- 1 0 0 0 Jan 08 02:49 haha drwx------ 2 2003 2003 4096 Jan 08 02:50 超哥到此一游 226 Directory send OK. ftp> #服务器禁止21端口流量 [root@chaogelinux ~]# iptables -A INPUT -p tcp --dport 21 -j DROP #此时已经无法连接ftp yumac: ~ yuchao$ftp 123.206.16.61
9. 只允许指定的IP远程连接此服务器。
#iptables自上而下匹配 iptables -A INPUT -s 222.35.242.139/24 -p tcp --dport 22 -j ACCEPT iptables -A INPUT -p tcp --dport 22 -j REJECT [root@chaogelinux ~]# iptables -L Chain INPUT (policy ACCEPT) target prot opt source destination ACCEPT tcp -- 222.35.242.0/24 anywhere tcp dpt:ssh REJECT tcp -- anywhere anywhere tcp dpt:ssh reject-with icmp-port-unreachable Chain FORWARD (policy ACCEPT) target prot opt source destination Chain OUTPUT (policy ACCEPT) target prot opt source destination #换一台ip的机器,直接被拒绝 [root@web01 ~]# ssh root@123.206.16.61 ssh: connect to host 123.206.16.61 port 22: Connection refused #只要删除第二条拒绝的规则,即可 [root@chaogelinux ~]# iptables -D INPUT 2 #又可以连接了 [root@web01 ~]# ssh root@123.206.16.61
10. 禁止指定的IP,访问本机的80端口。
#此时的防火墙规则 [root@chaogelinux ~]# iptables -L Chain INPUT (policy ACCEPT) target prot opt source destination ACCEPT tcp -- 222.35.242.0/24 anywhere tcp dpt:ssh REJECT tcp -- anywhere anywhere tcp dpt:ssh reject-with icmp-port-unreachable Chain FORWARD (policy ACCEPT) target prot opt source destination Chain OUTPUT (policy ACCEPT) target prot opt source destination #在规则链开头,追加一个新规则,禁止某个ip地址,访问本机的80端口 [root@chaogelinux ~]# iptables -I INPUT -p tcp -s 222.35.242.139/24 --dport 80 -j REJECT [root@chaogelinux ~]# iptables -L Chain INPUT (policy ACCEPT) target prot opt source destination REJECT tcp -- 222.35.242.0/24 anywhere tcp dpt:http reject-with icmp-port-unreachable ACCEPT tcp -- 222.35.242.0/24 anywhere tcp dpt:ssh REJECT tcp -- anywhere anywhere tcp dpt:ssh reject-with icmp-port-unreachable Chain FORWARD (policy ACCEPT) target prot opt source destination Chain OUTPUT (policy ACCEPT) target prot opt source destination [root@chaogelinux ~]# #此时已经无法访问 yumac: ~ yuchao$curl 123.206.16.61 curl: (7) Failed to connect to 123.206.16.61 port 80: Connection refused
11. 禁止所有的主机网段,访问本机的8000-9000端口
[root@chaogelinux ~]# iptables -A INPUT -p tcp -s 0/0 --dport 8000:9000 -j REJECT [root@chaogelinux ~]# [root@chaogelinux ~]# [root@chaogelinux ~]# iptables -A INPUT -p udp -s 0/0 --dport 8000:9000 -j REJECT
# iptables 网络防火墙
http://book.luffycity.com/linux-book/%E4%BA%92%E8%81%94%E7%BD%91%E6%9C%8D%E5%8A%A1%E5%9F%BA%E7%A1%80/iptables%E8%BF%9B%E9%98%B6.html#iptables%E7%BD%91%E7%BB%9C%E9%98%B2%E7%81%AB%E5%A2%99