1. 程式人生 > >iptables雜記(2)

iptables雜記(2)

iptables 雜記 基礎

iptables擴展

使用擴展參數放通80,和22端口

[root@xx ~]# iptables -I INPUT -s 10.201.106.0/24 -d 10.201.106.130 -p tcp -m multiport --dports 22,80 -j ACCEPT
[root@xx ~]# iptables -I OUTPUT -s 10.201.106.130 -d 10.201.106.0/24 -p tcp -m multiport --sports 22,80 -j ACCEPT
[root@xx ~]# 

[root@xx ~]# iptables -L -n -v
Chain INPUT (policy DROP 1 packets, 229 bytes)
 pkts bytes target     prot opt in     out     source               destination         
  224 15988 ACCEPT     tcp  --  *      *       10.201.106.0/24      10.201.106.130       multiport dports 22,80
 3451  253K ACCEPT     tcp  --  *      *       0.0.0.0/0            10.201.106.130       tcp dpt:22
   27  2268 ACCEPT     icmp --  *      *       0.0.0.0/0            10.201.106.130       icmptype 0

Chain FORWARD (policy DROP 0 packets, 0 bytes)
 pkts bytes target     prot opt in     out     source               destination         

Chain OUTPUT (policy DROP 11 packets, 1160 bytes)
 pkts bytes target     prot opt in     out     source               destination         
   27  2304 ACCEPT     tcp  --  *      *       10.201.106.130       10.201.106.0/24      multiport sports 22,80
 2612  309K ACCEPT     tcp  --  *      *       10.201.106.130       0.0.0.0/0            tcp spt:22
   42  3528 ACCEPT     icmp --  *      *       10.201.106.130       0.0.0.0/0            icmptype 8
[root@xx ~]# 

刪除沒用的規則:
[root@xx ~]# iptables -D INPUT 2
[root@xx ~]# iptables -D OUTPUT 2
[root@xx ~]# iptables -L -n -v
Chain INPUT (policy DROP 0 packets, 0 bytes)
 pkts bytes target     prot opt in     out     source               destination         
  385 27742 ACCEPT     tcp  --  *      *       10.201.106.0/24      10.201.106.130       multiport dports 22,80
   27  2268 ACCEPT     icmp --  *      *       0.0.0.0/0            10.201.106.130       icmptype 0

Chain FORWARD (policy DROP 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         
  132 70357 ACCEPT     tcp  --  *      *       10.201.106.130       10.201.106.0/24      multiport sports 22,80
   42  3528 ACCEPT     icmp --  *      *       10.201.106.130       0.0.0.0/0            icmptype 8
[root@xx ~]# 

根據IP地址範圍放通端口

[root@xx ~]# iptables -I INPUT -d 10.201.106.130 -p tcp -m multiport --dports 22:23,80 -m iprange --src-range 10.201.106.1-10.201.106.130 -j ACCEPT

[root@xx ~]# iptables -I OUTPUT -s 10.201.106.130 -p tcp -m multiport --sports 22:33,80 -m iprange --dst-range 10.201.106.1-10.201.106.130 -jACCEPT

[root@xx ~]# iptables -L -n -v
Chain INPUT (policy DROP 0 packets, 0 bytes)
 pkts bytes target     prot opt in     out     source               destination         
  514 38968 ACCEPT     tcp  --  *      *       0.0.0.0/0            10.201.106.130       multiport dports 22:23,80 source IP range 10.201.106.1-10.201.106.130
 1166 86322 ACCEPT     tcp  --  *      *       10.201.106.0/24      10.201.106.130       multiport dports 22,80
   27  2268 ACCEPT     icmp --  *      *       0.0.0.0/0            10.201.106.130       icmptype 0

Chain FORWARD (policy DROP 0 packets, 0 bytes)
 pkts bytes target     prot opt in     out     source               destination         

Chain OUTPUT (policy DROP 11 packets, 1160 bytes)
 pkts bytes target     prot opt in     out     source               destination         
   22  3176 ACCEPT     tcp  --  *      *       10.201.106.130       0.0.0.0/0            multiport sports 22:33,80 destination IP range 10.201.106.1-10.201.106.130
 1153  154K ACCEPT     tcp  --  *      *       10.201.106.130       10.201.106.0/24      multiport sports 22,80
   42  3528 ACCEPT     icmp --  *      *       10.201.106.130       0.0.0.0/0            icmptype 8
[root@xx ~]# 

刪除多余規則:
[root@xx ~]# iptables -D INPUT 2
[root@xx ~]# iptables -D OUTPUT 2
[root@xx ~]# iptables -L -n
Chain INPUT (policy DROP)
target     prot opt source               destination         
ACCEPT     tcp  --  0.0.0.0/0            10.201.106.130       multiport dports 22:23,80 source IP range 10.201.106.1-10.201.106.130
ACCEPT     icmp --  0.0.0.0/0            10.201.106.130       icmptype 0

Chain FORWARD (policy DROP)
target     prot opt source               destination         

Chain OUTPUT (policy DROP)
target     prot opt source               destination         
ACCEPT     tcp  --  10.201.106.130       0.0.0.0/0            multiport sports 22:33,80 destination IP range 10.201.106.1-10.201.106.130
ACCEPT     icmp --  10.201.106.130       0.0.0.0/0            icmptype 8
[root@xx ~]# 

根據報文中的字符串過濾內容

[root@xx ~]# vim /www/htdocs/bad.html

This is a  movie page.

過濾前
技術分享圖片

過濾後:

[root@xx ~]# iptables -I OUTPUT -m string --algo bm --string "movie" -j REJECT

[root@xx ~]# iptables -L -n -v --line-number
Chain INPUT (policy DROP 345 packets, 37324 bytes)
num   pkts bytes target     prot opt in     out     source               destination         
1     1742  132K ACCEPT     tcp  --  *      *       0.0.0.0/0            10.201.106.130       multiport dports 22:23,80 source IP range 10.201.106.1-10.201.106.130
2       27  2268 ACCEPT     icmp --  *      *       0.0.0.0/0            10.201.106.130       icmptype 0

Chain FORWARD (policy DROP 0 packets, 0 bytes)
num   pkts bytes target     prot opt in     out     source               destination         

Chain OUTPUT (policy DROP 53 packets, 5010 bytes)
num   pkts bytes target     prot opt in     out     source               destination         
1        6  2310 REJECT     all  --  *      *       0.0.0.0/0            0.0.0.0/0            STRING match  "movie" ALGO name bm TO 65535 reject-with icmp-port-unreachable
2      982  265K ACCEPT     tcp  --  *      *       10.201.106.130       0.0.0.0/0            multiport sports 22:33,80 destination IP range 10.201.106.1-10.201.106.130
3       42  3528 ACCEPT     icmp --  *      *       10.201.106.130       0.0.0.0/0            icmptype 8

技術分享圖片
技術分享圖片

iptables時間段內過濾

1、刪除掉之前的string過濾條目
[root@xx ~]# iptables -D OUTPUT 1

2、本來是設置14-18點無法訪問WEB服務,需要策略的時間和系統時間時區一模一樣,所以改成了0:00-23:59,為了讓策略匹配
[root@xx ~]# iptables -I INPUT -d 10.201.106.130 -p tcp --dport 80 -m time --timestart 00:00 --timestop 23:59 -j REJECT
[root@xx ~]# iptables -L -n -v
Chain INPUT (policy DROP 0 packets, 0 bytes)
 pkts bytes target     prot opt in     out     source               destination         
    6   304 REJECT     tcp  --  *      *       0.0.0.0/0            10.201.106.130       tcp dpt:80 TIME from 00:00:00 to 23:59:00 UTC reject-with icmp-port-unreachable
 2584  203K ACCEPT     tcp  --  *      *       0.0.0.0/0            10.201.106.130       multiport dports 22:23,80 source IP range 10.201.106.1-10.201.106.130
   27  2268 ACCEPT     icmp --  *      *       0.0.0.0/0            10.201.106.130       icmptype 0

Chain FORWARD (policy DROP 0 packets, 0 bytes)
 pkts bytes target     prot opt in     out     source               destination         

Chain OUTPUT (policy DROP 6 packets, 472 bytes)
 pkts bytes target     prot opt in     out     source               destination         
 1595  511K ACCEPT     tcp  --  *      *       10.201.106.130       0.0.0.0/0            multiport sports 22:33,80 destination IP range 10.201.106.1-10.201.106.130
   42  3528 ACCEPT     icmp --  *      *       10.201.106.130       0.0.0.0/0            icmptype 8
[root@xx ~]# 

3、登錄測試

技術分享圖片
技術分享圖片

指明某個協議端口的並發連接數限制數量

[root@xx ~]# iptables -I INPUT -p tcp --dport 22 -m connlimit --connlimit-above 3 -j REJECT

[root@xx ~]# iptables -L -n -v
Chain INPUT (policy DROP 0 packets, 0 bytes)
   41  5548 REJECT     tcp  --  *      *       0.0.0.0/0            0.0.0.0/0            tcp dpt:22 #conn src/32 > 3 reject-with icmp-port-unreachable
   33  1672 REJECT     tcp  --  *      *       0.0.0.0/0            10.201.106.130       tcp dpt:80 TIME from 00:00:00 to 23:59:00 UTC reject-with icmp-port-unreachable
 3309  267K ACCEPT     tcp  --  *      *       0.0.0.0/0            10.201.106.130       multiport dports 22:23,80 source IP range 10.201.106.1-10.201.106.130
   27  2268 ACCEPT     icmp --  *      *       0.0.0.0/0            10.201.106.130       icmptype 0

ping速率限制

[root@xx ~]# iptables -A INPUT -d 10.201.106.130 -p icmp --icmp-type 8 -m limit --limit-burst 5 --limit 30/minute -j ACCEPT

[root@xx ~]# iptables -A OUTPUT -s 10.201.106.130 -p icmp --icmp-type 0 -j ACCEPT

[root@xx ~]# iptables -L -n -v
Chain INPUT (policy DROP 0 packets, 0 bytes)
 pkts bytes target     prot opt in     out     source               destination         
   41  5548 REJECT     tcp  --  *      *       0.0.0.0/0            0.0.0.0/0            tcp dpt:22 #conn src/32 > 3 reject-with icmp-port-unreachable
   33  1672 REJECT     tcp  --  *      *       0.0.0.0/0            10.201.106.130       tcp dpt:80 TIME from 00:00:00 to 23:59:00 UTC reject-with icmp-port-unreachable
 4108  325K ACCEPT     tcp  --  *      *       0.0.0.0/0            10.201.106.130       multiport dports 22:23,80 source IP range 10.201.106.1-10.201.106.130
   27  2268 ACCEPT     icmp --  *      *       0.0.0.0/0            10.201.106.130       icmptype 0
    0     0 ACCEPT     icmp --  *      *       0.0.0.0/0            10.201.106.130       icmptype 8 limit: avg 30/min burst 5

Chain FORWARD (policy DROP 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         
 2856  676K ACCEPT     tcp  --  *      *       10.201.106.130       0.0.0.0/0            multiport sports 22:33,80 destination IP range 10.201.106.1-10.201.106.130
   42  3528 ACCEPT     icmp --  *      *       10.201.106.130       0.0.0.0/0            icmptype 8
    0     0 ACCEPT     icmp --  *      *       10.201.106.130       0.0.0.0/0            icmptype 0
[root@xx ~]# 

查看最大並發連接數

[root@xx ~]# cat /proc/sys/net/nf_conntrack_max 
15628

查看已經追蹤到的所有連接

[root@xx ~]# cat /proc/net/nf_conntrack
ipv4     2 tcp      6 299 ESTABLISHED src=10.201.106.1 dst=10.201.106.130 sport=49630 dport=22 src=10.201.106.130 dst=10.201.106.1 sport=22 dport=49630 [ASSURED] mark=0 zone=0 use=2
[root@xx ~]# 

設置SSH進入只允許新連接和已建立連接,出去只允許已建立連接

[root@xx ~]# iptables -I INPUT -d 10.201.106.130 -p tcp --dport 22 -m state --state NEW,ESTABLISHED -j ACCEPT
[root@xx ~]# 

[root@xx ~]# iptables -I OUTPUT -s 10.201.106.130 -p tcp --sport 22 -m state --state ESTABLISHED -j ACCEPT

[root@xx ~]# iptables -L -n
Chain INPUT (policy DROP)
target     prot opt source               destination         
ACCEPT     tcp  --  0.0.0.0/0            10.201.106.130       tcp dpt:22 state NEW,ESTABLISHED

Chain FORWARD (policy DROP)
target     prot opt source               destination         

Chain OUTPUT (policy DROP)
target     prot opt source               destination         
ACCEPT     tcp  --  10.201.106.130       0.0.0.0/0            tcp spt:22 state ESTABLISHED
[root@xx ~]# 

設置http進入只允許新連接和已建立連接,出去只允許已建立連接

[root@xx ~]# iptables -I INPUT -d 10.201.106.130 -p tcp -dport 80 -m state --state NEW,ESTABLISHED -j ACCEPT
iptables v1.4.21: multiple -d flags not allowed
Try `iptables -h‘ or ‘iptables --help‘ for more information.
[root@xx ~]# 
[root@xx ~]# 
[root@xx ~]# 
[root@xx ~]# iptables -I INPUT -d 10.201.106.130 -p tcp --dport 80 -m state --state NEW,ESTABLISHED -j ACCEPT
[root@xx ~]# 
[root@xx ~]# iptables -I OUTPUT -s 10.201.106.130 -p tcp --sport 80 -m state --state ESTABLISHED -j ACCEPT
[root@xx ~]# 
[root@xx ~]# 
[root@xx ~]# iptables -L -n
Chain INPUT (policy DROP)
target     prot opt source               destination         
ACCEPT     tcp  --  0.0.0.0/0            10.201.106.130       tcp dpt:80 state NEW,ESTABLISHED
ACCEPT     tcp  --  0.0.0.0/0            10.201.106.130       tcp dpt:22 state NEW,ESTABLISHED

Chain FORWARD (policy DROP)
target     prot opt source               destination         

Chain OUTPUT (policy DROP)
target     prot opt source               destination         
ACCEPT     tcp  --  10.201.106.130       0.0.0.0/0            tcp spt:80 state ESTABLISHED
ACCEPT     tcp  --  10.201.106.130       0.0.0.0/0            tcp spt:22 state ESTABLISHED
[root@xx ~]# 

測試:
技術分享圖片

放通ping

[root@xx ~]# iptables -A INPUT -d 10.201.106.130 -p icmp --icmp-type 8 -m state --state NEW,ESTABLISHED -j ACCEPT
[root@xx ~]# 
[root@xx ~]# iptables -A OUTPUT -s 10.201.106.130 -p icmp --icmp-type 0 -m state --state ESTABLISHED -j ACCEPT
[root@xx ~]# iptables -L -n
Chain INPUT (policy DROP)
target     prot opt source               destination         
ACCEPT     tcp  --  0.0.0.0/0            10.201.106.130       tcp dpt:80 state NEW,ESTABLISHED
ACCEPT     tcp  --  0.0.0.0/0            10.201.106.130       tcp dpt:22 state NEW,ESTABLISHED
ACCEPT     icmp --  0.0.0.0/0            10.201.106.130       icmptype 8 state NEW,ESTABLISHED

Chain FORWARD (policy DROP)
target     prot opt source               destination         

Chain OUTPUT (policy DROP)
target     prot opt source               destination         
ACCEPT     tcp  --  10.201.106.130       0.0.0.0/0            tcp spt:80 state ESTABLISHED
ACCEPT     tcp  --  10.201.106.130       0.0.0.0/0            tcp spt:22 state ESTABLISHED
ACCEPT     icmp --  10.201.106.130       0.0.0.0/0            icmptype 0 state ESTABLISHED
[root@xx ~]# 

技術分享圖片

對所有已建立的進程出站放通

[root@xx ~]# iptables -I OUTPUT -m state --state ESTABLISHED -j ACCEPT

[root@xx ~]# iptables -L -n
Chain INPUT (policy DROP)
target     prot opt source               destination         
ACCEPT     tcp  --  0.0.0.0/0            10.201.106.130       tcp dpt:80 state NEW,ESTABLISHED
ACCEPT     tcp  --  0.0.0.0/0            10.201.106.130       tcp dpt:22 state NEW,ESTABLISHED
ACCEPT     icmp --  0.0.0.0/0            10.201.106.130       icmptype 8 state NEW,ESTABLISHED

Chain FORWARD (policy DROP)
target     prot opt source               destination         

Chain OUTPUT (policy DROP)
target     prot opt source               destination         
ACCEPT     all  --  0.0.0.0/0            0.0.0.0/0            state ESTABLISHED
[root@xx ~]# 

放通進站

[root@xx ~]# iptables -I INPUT -m state --state ESTABLISHED -j ACCEPT

再合並80和22端口的策略

[root@xx ~]# iptables -I INPUT 2 -d 10.201.106.130 -p tcp -m multiport --dports 22,80 -m state --state NEW -j ACCEPT

[root@xx ~]# iptables -D INPUT 3
[root@xx ~]# iptables -D INPUT 3
[root@xx ~]# iptables -L -n -v
Chain INPUT (policy DROP 0 packets, 0 bytes)
 pkts bytes target     prot opt in     out     source               destination         
  465 35120 ACCEPT     all  --  *      *       0.0.0.0/0            0.0.0.0/0            state ESTABLISHED
    0     0 ACCEPT     tcp  --  *      *       0.0.0.0/0            10.201.106.130       multiport dports 22,80 state NEW
   18  1080 ACCEPT     icmp --  *      *       0.0.0.0/0            10.201.106.130       icmptype 8 state NEW,ESTABLISHED

Chain FORWARD (policy DROP 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         
  575  116K ACCEPT     all  --  *      *       0.0.0.0/0            0.0.0.0/0            state ESTABLISHED
[root@xx ~]# 

iptables雜記(2)