基于 ip 地址
=============================
匹配的标准
IP: -s 192.168.0.0/24
-d 192.168.0.1
NIC: -i eth0
-o eth1
!: -i eth0 ! -s 192.168.0.0/24
[root@localhost ~]# iptables -tfilter -A INPUT -s 192.168.2.0/24 -j DROP
[root@localhost ~]# iptables -I INPUT! -s 192.168.3.0/24 -j ACCEPT
[root@localhost ~]# iptables -I INPUT-i eth0 -j ACCEPT
[root@localhost ~]# service iptablessave
iptables:将防火墙规则保存到 /etc/sysconfig/iptables:
[确定]
基于服务端口号
==========================================================================
protocol port : -p tcp --dport 80
-p udp --sport 53
port range: 0:1024
ICMP: -p icmp --icmp-typehost-unreachable
[root@localhost ~]# iptables -A INPUT-s 192.168.2.1 -p tcp --dport 80 -j DROP
默认策略
-P <chain><ACCEPT|DROP|REJECT>
[root@localhost ~]# iptables -P INPUTACCEPT
刷新一个 table 的所有 rule
[root@localhost ~]# iptables -F
iptables -t filter -L
默认策略设置成 DROP
[root@localhost ~]# iptables -tfilter -P INPUT DROP
SSH 对方主机,要回包
[root@localhost ~]# iptables -tfilter -A INPUT -p tcp --sport 22 -j ACCEPT
默认策略 DROP 要放行自己 lo
[root@localhost ~]# iptables -tfilter -A INPUT -i lo -j ACCEPT
-v 详细信息 -n 不解析(ip 解析到主机名 port 解析到协议) --line-numbers 显示条目数
[root@localhost ~]# iptables -tfilter -L -n -v --line-numbers
MATCH EXTENSION 扩展匹配
===============================================================
icmp
[root@localhost ~]# iptables -tfilter -I INPUT -p icmp -h
[root@localhost ~]# iptables -tfilter -I INPUT -p icmp -m icmp --icmp-type echo-reply -j ACCEPT
[root@localhost ~]# iptables -tfilter -I INPUT -p icmp -m icmp --icmp-type echo-request -j DROP
(默认策略 ACCEPT)
================================================================
connlimit :Allowsyou to restrict the number of parallel TCP connections to a server per
client IP address (or address block).同一个 IP 同时发起,最多 2 个连接
[root@localhost ~]# iptables -tfilter -A INPUT -s 172.16.1.0/24 -p tcp --syn --dport 22 -m
connlimit -h
[root@localhost ~]# iptables -tfilter -A INPUT -s 172.16.1.0/24 -p tcp --syn --dport 22 -m
connlimit --connlimit-above 2 -jACCEPT
=================================================================
iprange
[root@localhost ~]# echo 1 >/proc/sys/net/ipv4/ip_forward
[root@localhost ~]# iptables -tfilter -A FORWARD -m iprange --src-range 172.16.1.10-172.16.1.20
-j DROP
[root@localhost ~]# iptables -tfilter -A INPUT -p tcp --dport 80 -m iprange --src-range
172.16.1.10-172.16.1.20 -j DROP
==================================================================
length
[root@localhost ~]# ping -c 1 node1
PING node1.uplooking.com (172.16.1.1)56(84) bytes of data.
64 bytes from node1.uplooking.com(172.16.1.1): icmp_seq=1 ttl=64 time=1.25 ms
--- node1.uplooking.com pingstatistics ---
1 packets transmitted, 1 received, 0%packet loss, time 0ms
rtt min/avg/max/mdev =1.259/1.259/1.259/0.000 ms
56(数据)+20(IP 首部)+8(ICMP 首部)
[root@localhost ~]# iptables -tfilter -A INPUT -p icmp -m length --length 50:100 -j DROP
[root@node1 ~]# ping 172.16.1.6
PING 172.16.1.6 (172.16.1.6) 56(84)bytes of data.
--- 172.16.1.6 ping statistics ---
5 packets transmitted, 0 received,100% packet loss, time 4010ms
[root@node1 ~]# ping -c 1 -s 50172.16.1.6
PING 172.16.1.6 (172.16.1.6) 50(78)bytes of data.
--- 172.16.1.6 ping statistics ---
1 packets transmitted, 0 received,100% packet loss, time 0ms
[root@node1 ~]# ping -c 1 -s 21172.16.1.6
PING 172.16.1.6 (172.16.1.6) 21(49)bytes of data.
29 bytes from 172.16.1.6: icmp_seq=1ttl=64 time=7.25 ms
-- 172.16.1.6 ping statistics ---
1 packets transmitted, 1 received, 0%packet loss, time 0ms
rtt min/avg/max/mdev = 7.255/7.255/7.255/0.000ms
==========================================================================
limit(洪水流量 洪水攻击)
[root@node1 ~]# ping 172.16.1.6
PING 172.16.1.6 (172.16.1.6) 56(84)bytes of data.
64 bytes from 172.16.1.6: icmp_seq=1ttl=64 time=0.304 m
64 bytes from 172.16.1.6: icmp_seq=2ttl=64 time=0.346 ms
64 bytes from 172.16.1.6: icmp_seq=3ttl=64 time=0.584 ms
3 packets transmitted, 3 received, 0%packet loss, time 2007ms
rtt min/avg/max/mdev =0.304/0.411/0.584/0.124 ms
[root@localhost]#iptables –t filter-A INPUT -s172.16.1.1 -p icmp -m limit --limit 10/minute -j
ACCEPT
[root@localhost ~]# iptables -tfilter -A INPUT -p icmp -j DROP
--limit-burst number number to match in a burst,default 5
6 秒一个
前 5 个不限制 后每
===========================================================================
mac (局域网用 广域网不用)
[root@localhost ~]# iptables -tfilter -A INPUT [ -p icmp ] -m mac --mac 00:0C:29:24:44:0A -j
DROP
[root@localhost ~]# arping 172.16.1.1
ARPING 172.16.1.1 from 172.16.1.6eth0
Unicast reply from 172.16.1.1[00:0C:29:24:44:0A] 0.979ms
Unicast reply from 172.16.1.1[00:0C:29:24:44:0A] 1.122ms
Unicast reply from 172.16.1.1[00:0C:29:24:44:0A] 1.102ms
Unicast reply from 172.16.1.1[00:0C:29:24:44:0A] 0.949ms
Sent 4 probes (1 broadcast(s))
Received 4 response(s)
