iptables相關(guān)以及sudo

(一)瞭吃、詳述iptables五鏈

iptables: 包過濾型的防火墻

firewall: 防火墻,隔離工具;工作于主機(jī)或網(wǎng)絡(luò)邊緣,對(duì)于進(jìn)出本主機(jī)或本網(wǎng)絡(luò)的報(bào)文根據(jù)事先定義的檢查規(guī)則作匹配檢測(cè),對(duì)于能夠被規(guī)則匹配的報(bào)文做出相應(yīng)處理的組件;

  • 主機(jī)防火墻
  • 網(wǎng)絡(luò)防火墻
  • 軟件防火墻(軟件邏輯)
  • 硬件防火墻(硬件和軟件邏輯)

四表五鏈概念

四表五鏈

四表:

  • raw: PREROUTING, OUTPUT
  • mangle: PREROUTING, INPUT, FORWARD, OUTPUT, POSTROUTING
  • nat: PREROUTING, INPUT, OUTPUT, POSTROUTING
  • filter: INPUT, FORWARD, OUTPUT

五鏈:

INPUT鏈——進(jìn)來的數(shù)據(jù)包應(yīng)用此規(guī)則鏈中的策略
OUTPUT鏈——外出的數(shù)據(jù)包應(yīng)用此規(guī)則鏈中的策略
FORWARD鏈——轉(zhuǎn)發(fā)數(shù)據(jù)包時(shí)應(yīng)用此規(guī)則鏈中的策略
PREROUTING鏈——對(duì)數(shù)據(jù)包作路由選擇前應(yīng)用此鏈中的規(guī)則(所有的數(shù)據(jù)包進(jìn)來的時(shí)侯都先由這個(gè)鏈處理)
POSTROUTING鏈——對(duì)數(shù)據(jù)包作路由選擇后應(yīng)用此鏈中的規(guī)則(所有的數(shù)據(jù)包出來的時(shí)侯都先由這個(gè)鏈處理)

報(bào)文流向

報(bào)文流向
  • 流入本機(jī): PREROUTING--->INPUT
  • 由本機(jī)流出: OUTPUT--->POSTROUTING
  • 轉(zhuǎn)發(fā): PREROUTING--->FORWARD--->POSTROUTING

(二)、舉例實(shí)現(xiàn)iptables多端口匹配、連接追蹤偶垮、字符串匹配窗宦、時(shí)間匹配余舶、并發(fā)連接限制芥玉、速率匹配塘装、報(bào)文狀態(tài)匹配等應(yīng)用

測(cè)試環(huán)境搭建

主機(jī)類別 ip地址
遠(yuǎn)程訪問的Client 192.168.30.105
服務(wù)器主機(jī)Server 192.168.30.104

第一: 服務(wù)器開啟iptables功能,且保證規(guī)則為空

[root@CentOS7_Server ~]#iptables -vnL
Chain INPUT (policy ACCEPT 6 packets, 428 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 4 packets, 448 bytes)
 pkts bytes target     prot opt in     out     source               destination

第二: 服務(wù)器端安裝測(cè)試軟件,包括但不限于(httpd,telnet,samba,ftp,vsftpd,mariadb等)

[root@CentOS7_Server ~]#yum install httpd telnet-server samba ftp vsftpd mariadb-server -y

第三: 啟動(dòng)服務(wù)器上iptables并加入開機(jī)啟動(dòng)項(xiàng)

[root@CentOS7_Server ~]#systemctl start firewalld.service
[root@CentOS7_Server ~]#systemctl status firewalld.service
[root@CentOS7_Server ~]#systemctl enable firewalld.service

第四: 配置服務(wù)器端iptables規(guī)則

[root@CentOS7_Server ~]#iptables -A INPUT -d 192.168.30.104 -p tcp --dport 22 -j ACCEPT  ###入站-放行SSH的22號(hào)端口
[root@CentOS7_Server ~]#iptables -A OUTPUT -s 192.168.30.104 -p tcp --sport 22 -j ACCEPT ###出站-放行SSH的22號(hào)端口

[root@CentOS7_Server ~]#iptables -A INPUT -i ens33 -j REJECT   ###入站-拒絕通過ens33網(wǎng)卡的連接
[root@CentOS7_Server ~]#iptables -A OUTPUT -o ens33 -j REJECT  ###出站-拒絕通過ens33網(wǎng)卡的連接

[root@CentOS7_Server ~]#iptables -I OUTPUT 2 -s 192.168.30.104 -p icmp --icmp-type 8 -j ACCEPT  ###出站-允許服務(wù)器ping外部主機(jī)
[root@CentOS7_Server ~]#iptables -I INPUT 2 -d 192.168.30.104 -p icmp --icmp-type 0 -j ACCEPT  ###進(jìn)站-允許服務(wù)器接收主機(jī)ping要求
[root@CentOS7_Server ~]#iptables -vnL --line-numbers
Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
num   pkts bytes target     prot opt in     out     source               destination         
1      907 69892 ACCEPT     tcp  --  *      *       0.0.0.0/0            192.168.30.104       tcp dpt:22
2        0     0 ACCEPT     icmp --  *      *       0.0.0.0/0            192.168.30.104       icmptype 0
3       22  2364 REJECT     all  --  ens33  *       0.0.0.0/0            0.0.0.0/0            reject-with icmp-port-unreachable

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

Chain OUTPUT (policy ACCEPT 0 packets, 0 bytes)
num   pkts bytes target     prot opt in     out     source               destination         
1      471 48640 ACCEPT     tcp  --  *      *       192.168.30.104       0.0.0.0/0            tcp spt:22
2        0     0 ACCEPT     icmp --  *      *       192.168.30.104       0.0.0.0/0            icmptype 8
3        0     0 REJECT     all  --  *      ens33   0.0.0.0/0            0.0.0.0/0            reject-with icmp-port-unreachable

1.基本匹配條件: (無需加載任何模塊,由iptables/netfilter自行提供)

參數(shù)選項(xiàng) 解釋說明
-s, --source address[/mask][....] 檢查報(bào)文中的源ip地址是否符合此處指定的地址或范圍
-d, --destination address[/mask][...] 檢查報(bào)文中的目標(biāo)ip地址是否符合此處指定的地址或范圍
-p, --protocol protocol protocol: tcp,udp,udplite,icmp,icmpv6,esp,ah,sctp,mh or "all"
-i, --in-interface name 數(shù)據(jù)報(bào)文流入的接口;只能應(yīng)用于數(shù)據(jù)報(bào)文流入的環(huán)節(jié),只能應(yīng)用于PREROUTING,INPUT和FORWARD鏈
-o, --out-interface name 數(shù)據(jù)報(bào)文流出的接口;只能應(yīng)用于數(shù)據(jù)報(bào)文流出的環(huán)節(jié),只能應(yīng)用于FORWARD,OUTPUT和POSTROUTING鏈

2.擴(kuò)展匹配條件: (需要加載擴(kuò)展模塊,方可生效)

隱式擴(kuò)展: 在使用-p選項(xiàng)指明了特定的協(xié)議時(shí),無需再同時(shí)使用-m選項(xiàng)指明擴(kuò)展模塊的擴(kuò)展機(jī)制
顯式擴(kuò)展: 必須使用-m選項(xiàng)指明要調(diào)用的擴(kuò)展模塊的擴(kuò)展機(jī)制

Step1.隱式擴(kuò)展:

不需要手動(dòng)加載擴(kuò)展模塊,因?yàn)樗鼈兪菍?duì)協(xié)議的擴(kuò)展,所以,但凡使用-p指明了協(xié)議,就表示已經(jīng)指明了要擴(kuò)展的模塊

tcp參數(shù)選項(xiàng):
參數(shù)選項(xiàng) 解釋說明
--source-port, --sport port[:port] 匹配報(bào)文的源端口;可以是端口范圍
--destination-port, --dport port[:port] 匹配報(bào)文的目標(biāo)端口;可以是端口范圍
--tcp-flags mask comp
--tcp-flags SYN,ACK,FIN,RST SYN 要檢查的標(biāo)志位為SYN,ACK,FIN,RST四個(gè),其中SYN必須為1,余下的必須為0
[root@CentOS7_Server ~]#iptables -A INPUT -d 192.168.30.104 -p tcp --dport 22 -j ACCEPT
[root@CentOS7_Server ~]#iptables -A OUTPUT -s 192.168.30.104 -p tcp --sport 22 -j ACCEPT
udp參數(shù)選項(xiàng):
參數(shù)選項(xiàng) 解釋說明
--source-port, --sport port[:port] 匹配報(bào)文的源端口;可以是端口范圍
--destination-port, -dport port[:port] 匹配報(bào)文的目標(biāo)端口;可以是端口范圍
[root@CentOS7 ~]#iptables -I INPUT -d 192.168.30.104 -p udp --dport 137:138 -j ACCEPT
[root@CentOS7 ~]#iptables -I OUTPUT -s 192.168.30.104 -p udp --sport 137:138 -j ACCEPT
icmp參數(shù)選項(xiàng):
參數(shù)選項(xiàng) 解釋說明
--icmp-type {type[/code]typename}
echo-request: 8 ping請(qǐng)求報(bào)文(出站)
echo-reply: 0 ping響應(yīng)報(bào)文(進(jìn)站)
[root@CentOS7_Server ~]#iptables -I OUTPUT -s 192.168.30.104 -p icmp --icmp-type 8 -j ACCEPT
[root@CentOS7_Server ~]#iptables -I INPUT -d 192.168.30.104 -p icmp --icmp-type 0 -j ACCEPT

Step2.顯式擴(kuò)展:

必須使用-m選項(xiàng)指明要調(diào)用的擴(kuò)展模塊的擴(kuò)展機(jī)制

(1)急迂、mutliport,多端口匹配

以離散或連續(xù)的方式定義多端口匹配條件,最多15個(gè)

參數(shù)選項(xiàng) 解釋說明
--source-ports,--sports port[,port|,port:port] 指定多個(gè)源端口
--destination-ports,--dports port[,port|,port:port] 指定多個(gè)目標(biāo)端口

第一步: 啟動(dòng)所有服務(wù)器端測(cè)試服務(wù)(80,139,445)

[root@CentOS7_Server ~]#ss -tnl
State       Recv-Q Send-Q                           Local Address:Port                                          Peer Address:Port              
LISTEN      0      50                                           *:445                                                      *:*                  
LISTEN      0      128                                  127.0.0.1:9000                                                     *:*                  
LISTEN      0      50                                           *:3306                                                     *:*                  
LISTEN      0      50                                           *:139                                                      *:*                  
LISTEN      0      128                                          *:111                                                      *:*                  
LISTEN      0      5                                192.168.122.1:53                                                       *:*                  
LISTEN      0      128                                          *:22                                                       *:*                  
LISTEN      0      128                                  127.0.0.1:631                                                      *:*                  
LISTEN      0      100                                  127.0.0.1:25                                                       *:*                  
LISTEN      0      128                                  127.0.0.1:6010                                                     *:*                  
LISTEN      0      128                                  127.0.0.1:6011                                                     *:*                  
LISTEN      0      50                                          :::445                                                     :::*                  
LISTEN      0      50                                          :::139                                                     :::*                  
LISTEN      0      128                                         :::111                                                     :::*                  
LISTEN      0      128                                         :::80                                                      :::*                  
LISTEN      0      128                                         :::22                                                      :::*                  
LISTEN      0      128                                        ::1:631                                                     :::*                  
LISTEN      0      100                                        ::1:25                                                      :::*                  
LISTEN      0      128                                        ::1:6010                                                    :::*                  
LISTEN      0      128                                        ::1:6011                                                    :::* 

第二步: 創(chuàng)建index測(cè)試頁面

[root@CentOS7_Server ~]#cd /data/www/html/
[root@CentOS7_Server html]#vim index.html
[root@CentOS7_Server html]#curl http://192.168.30.104
<h1> Multiport Test !! </h1>
防火墻阻擋httpd
[root@CentOS7_Server html]#iptables -I INPUT -d 192.168.30.104 -p udp --dport 137:138 -j ACCEPT
[root@CentOS7_Server html]#iptables -I OUTPUT -s 192.168.30.104 -p udp --sport 137:138 -j ACCEPT

第三步: 添加多端口規(guī)則,然后測(cè)試80訪問頁面以及samba服務(wù)

[root@CentOS7_Server html]#iptables -R INPUT 3 -d 192.168.30.104 -p tcp -m multiport --dports 22,80,139,445 -j ACCEPT 
[root@CentOS7_Server html]#iptables -R OUTPUT 3 -s 192.168.30.104 -p tcp -m multiport --sports 22,80,139,445 -j ACCEPT 
[root@CentOS7_Server html]#iptables -vnL --line-numbers
Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
num   pkts bytes target     prot opt in     out     source               destination         
1        0     0 ACCEPT     udp  --  *      *       0.0.0.0/0            192.168.30.104       udp dpts:137:138
2        0     0 ACCEPT     icmp --  *      *       0.0.0.0/0            192.168.30.104       icmptype 0
3      209 16884 ACCEPT     tcp  --  *      *       0.0.0.0/0            192.168.30.104       multiport dports 22,80,139,445
4      116  9327 REJECT     all  --  ens33  *       0.0.0.0/0            0.0.0.0/0            reject-with icmp-port-unreachable

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

Chain OUTPUT (policy ACCEPT 0 packets, 0 bytes)
num   pkts bytes target     prot opt in     out     source               destination         
1        5   552 ACCEPT     udp  --  *      *       192.168.30.104       0.0.0.0/0            udp spts:137:138
2        0     0 ACCEPT     icmp --  *      *       192.168.30.104       0.0.0.0/0            icmptype 8
3       39  3700 ACCEPT     tcp  --  *      *       192.168.30.104       0.0.0.0/0            multiport sports 22,80,139,445
4       65  6525 REJECT     all  --  *      ens33   0.0.0.0/0            0.0.0.0/0            reject-with icmp-port-unreachable

<測(cè)試samba服務(wù)>

[root@CentOS7_Client1 ~]#smbclient -L 192.168.30.104 
Enter SAMBA\root's password: 
Anonymous login successful

    Sharename       Type      Comment
    ---------       ----      -------
    print$          Disk      Printer Drivers
    IPC$            IPC       IPC Service (Samba 4.7.1)
Reconnecting with SMB1 for workgroup listing.
Anonymous login successful

    Server               Comment
    ---------            -------

    Workgroup            Master
    ---------            -------
    SAMBA   
測(cè)試httpd正常
(2)、iprange,ip范圍匹配

以聯(lián)系地址塊的方式來指明多ip地址匹配條件

參數(shù)選項(xiàng) 解釋說明
--src-range from 匹配源地址
--dst-range from 匹配目標(biāo)地址

第一步:添加一個(gè)test賬戶并啟動(dòng)telnet進(jìn)程

[root@CentOS7_Server ~]#useradd usertest
[root@CentOS7_Server ~]#echo "usertest"|passwd --stdin usertest
[root@CentOS7_Server ~]#systemctl start telnet.socket 
[root@CentOS7_Server ~]#ss -tnl|grep 23
LISTEN     0      128         :::23                      :::*

第二步:添加規(guī)則允許IP范圍連接23端口

[root@CentOS7_Server ~]#iptables -I INPUT 4 -d 192.168.30.104 -p tcp --dport 23 -m iprange --src-range 192.168.30.104-192.168.30.106 -j ACCEPT
[root@CentOS7_Server ~]#iptables -I OUTPUT 4 -s 192.168.30.104 -p tcp --sport 23 -m iprange --dst-range 192.168.30.104-192.168.30.106 -j ACCEPT
[root@CentOS7_Server ~]#iptables -vnL --line-numbers
Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
num   pkts bytes target     prot opt in     out     source               destination         
1        0     0 ACCEPT     udp  --  *      *       0.0.0.0/0            192.168.30.104       udp dpts:137:138
2        0     0 ACCEPT     icmp --  *      *       0.0.0.0/0            192.168.30.104       icmptype 0
3     1172 94005 ACCEPT     tcp  --  *      *       0.0.0.0/0            192.168.30.104       multiport dports 22,80,139,445
4        0     0 ACCEPT     tcp  --  *      *       0.0.0.0/0            192.168.30.104       tcp dpt:23 source IP range 192.168.30.104-192.168.30.106
5      595 59003 REJECT     all  --  ens33  *       0.0.0.0/0            0.0.0.0/0            reject-with icmp-port-unreachable

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

Chain OUTPUT (policy ACCEPT 0 packets, 0 bytes)
num   pkts bytes target     prot opt in     out     source               destination         
1       29  3072 ACCEPT     udp  --  *      *       192.168.30.104       0.0.0.0/0            udp spts:137:138
2        0     0 ACCEPT     icmp --  *      *       192.168.30.104       0.0.0.0/0            icmptype 8
3      685 78044 ACCEPT     tcp  --  *      *       192.168.30.104       0.0.0.0/0            multiport sports 22,80,139,445
4        0     0 ACCEPT     tcp  --  *      *       192.168.30.104       0.0.0.0/0            tcp spt:23 destination IP range 192.168.30.104-192.168.30.106
5       68  6705 REJECT     all  --  *      ens33   0.0.0.0/0            0.0.0.0/0            reject-with icmp-port-unreachable

第三步測(cè)試telnet服務(wù)(client 192.168.30.105): 連接成功

[root@CentOS7_Client1 ~]#telnet 192.168.30.104
Trying 192.168.30.104...
Connected to 192.168.30.104.
Escape character is '^]'.
Kernel 3.10.0-862.el7.x86_64 on an x86_64
CentOS7_Server login: usertest  
Password: 

[usertest@CentOS7_Server ~]$ ifconfig
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.30.104  netmask 255.255.255.0  broadcast 192.168.30.255

測(cè)試telnet服務(wù)(client 192.168.30.107): 連接失敗

[root@ftp-server ~]#telnet 192.168.30.104
Trying 192.168.30.104...
telnet: connect to address 192.168.30.104: Connection timed out
(3)蹦肴、time, 匹配訪問網(wǎng)絡(luò)的時(shí)間
參數(shù)選項(xiàng) 解釋說明
--timestart hh:mm[:ss] 時(shí)間開始點(diǎn)
--timestop hh:mm[:ss] 時(shí)間結(jié)束點(diǎn)
--weekdays day[,day...] 一周的第幾天
--monthdays day[,day...] 一月的第幾天
--datestart YYYY[-MM[-DD[Thh[:mm[:ss]]]]] 日期開始時(shí)間點(diǎn)
--datestop YYYY[-MM[-DD[Thh[:mm[:ss]]]]] 日期結(jié)束時(shí)間點(diǎn)
--kerneltz: 使用內(nèi)核配置的時(shí)區(qū)而非默認(rèn)的UTC CentOS7才需要操作

第一: 開放進(jìn)出站對(duì)時(shí)間的端口
ntp端口123
chrony端口323

[root@CentOS7_Server ~]#iptables -I INPUT 5 -d 192.168.30.104 -p udp -m multiport --dports 123,323 -j ACCEPT
[root@CentOS7_Server ~]#iptables -I OUTPUT 5 -s 192.168.30.104 -p udp -m multiport --sports 123,323 -j ACCEPT

第二: 配置IP端在什么時(shí)間段可以訪問

[root@CentOS7_Server ~]#iptables -I INPUT 3 -d 192.168.30.104 -p tcp --dport 23 -m time --timestart 09:00:00 --timestop 18:00:00 --weekdays 1,2,3,4,5,6 --kerneltz -j ACCEPT
[root@CentOS7_Server ~]#iptables -I OUTPUT 3 -s 192.168.30.104 -p tcp --sport 23 -m time --timestart 09:00:00 --timestop 18:00:00 --weekdays 1,2,3,4,5,6 --kerneltz -j ACCEPT
###周一至周五早9點(diǎn)到晚18點(diǎn)允許訪問,周六日全天可以訪問,--kerneltz使用內(nèi)核中的時(shí)間
(4)僚碎、string, 對(duì)應(yīng)字符串匹配
參數(shù)選項(xiàng) 解釋說明
--algo {bm|kmp} 字符串選擇的類型
--string pattern 匹配模式
--hex-string pattern
--from offset
to offset
[root@CentOS7_Server html]#iptables -I INPUT 5 -d 192.168.30.104 -p tcp -m string --algo bm --string "linux" -j REJECT
[root@CentOS7_Server html]#iptables -I OUTPUT 5 -s 192.168.30.104 -p tcp -m string --algo bm --string "linux" -j REJECT
(5)、connlimit, 并發(fā)連接數(shù)限制
參數(shù)選項(xiàng) 解釋說明
--connlimit-upto n 小于n被允許
--connlimit-above n 大于n被拒絕
[root@CentOS7_Server html]#iptables -R INPUT 4 -d 192.168.30.104 -p tcp -m multiport --dports 80,139,445,3306 -j ACCEPT
[root@CentOS7_Server html]#iptables -R OUTPUT 4 -s 192.168.30.104 -p tcp -m multiport --sports 80,139,445,3306 -j ACCEPT
###開啟對(duì)3306端口的放行
[root@CentOS7_Server html]#iptables -I INPUT 6 -d 192.168.30.104 -p tcp --dport 3306 -m connlimit --connlimit-upto 2 -j ACCEPT
[root@CentOS7_Server html]#iptables -I OUTPUT 6 -s 192.168.30.104 -p tcp --sport 3306 -m connlimit --connlimit-upto 2 -j ACCEPT
###設(shè)置對(duì)訪問mariadb并發(fā)連接數(shù)小于2個(gè)的時(shí)候被允許
[root@CentOS7_Server html]#iptables -vnL --line-numbers
Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
num   pkts bytes target     prot opt in     out     source               destination         
1        0     0 ACCEPT     icmp --  *      *       0.0.0.0/0            192.168.30.104       icmptype 0
2     2696  198K ACCEPT     tcp  --  *      *       0.0.0.0/0            192.168.30.104       tcp dpt:22
3        0     0 ACCEPT     tcp  --  *      *       0.0.0.0/0            192.168.30.104       tcp dpt:23 TIME from 09:00:00 to 18:00:00 on Mon,Tue,Wed,Thu,Fri,Sat
4        0     0 ACCEPT     tcp  --  *      *       0.0.0.0/0            192.168.30.104       multiport dports 80,139,445,3306
5        0     0 REJECT     tcp  --  *      *       0.0.0.0/0            192.168.30.104       STRING match  "linux" ALGO name bm TO 65535 reject-with icmp-port-unreachable
6        0     0 ACCEPT     tcp  --  *      *       0.0.0.0/0            192.168.30.104       tcp dpt:3306 #conn src/32 <= 2
7       86  7883 REJECT     all  --  ens33  *       0.0.0.0/0            0.0.0.0/0            reject-with icmp-port-unreachable

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

Chain OUTPUT (policy ACCEPT 0 packets, 0 bytes)
num   pkts bytes target     prot opt in     out     source               destination         
1        0     0 ACCEPT     icmp --  *      *       192.168.30.104       0.0.0.0/0            icmptype 8
2     1647  200K ACCEPT     tcp  --  *      *       192.168.30.104       0.0.0.0/0            tcp spt:22
3        0     0 ACCEPT     tcp  --  *      *       192.168.30.104       0.0.0.0/0            tcp spt:23 TIME from 09:00:00 to 18:00:00 on Mon,Tue,Wed,Thu,Fri,Sat
4        0     0 ACCEPT     tcp  --  *      *       192.168.30.104       0.0.0.0/0            multiport sports 80,139,445,3306
5        0     0 REJECT     tcp  --  *      *       192.168.30.104       0.0.0.0/0            STRING match  "linux" ALGO name bm TO 65535 reject-with icmp-port-unreachable
6        0     0 ACCEPT     tcp  --  *      *       192.168.30.104       0.0.0.0/0            tcp spt:3306 #conn src/32 <= 2
7       31  2516 REJECT     all  --  *      ens33   0.0.0.0/0            0.0.0.0/0            reject-with icmp-port-unreachable
(6)阴幌、limit, 速率限制

這里的限制是指報(bào)文的發(fā)包速率限制听盖。使用的是令牌桶算法,每拿一個(gè)令牌才能相應(yīng)的發(fā)一個(gè)報(bào)文裂七,而令牌按照固定頻率發(fā)放皆看,在沒有報(bào)文發(fā)送的時(shí)候,會(huì)像桶一樣把令牌攢起來背零,在需要發(fā)送報(bào)文的時(shí)候會(huì)一次性把桶里的報(bào)文都發(fā)出去腰吟,這就叫做令牌桶算法

參數(shù)選項(xiàng) 解釋說明
--limit rate[/second|/minute|/hour|/day] 按秒/分/時(shí)/天限制速率
--limit-burst number 所擁有的令牌桶數(shù)量
--syn, -m limit 限制本機(jī)某tcp服務(wù)接收新請(qǐng)求的速率
[root@CentOS7_Server html]#iptables -I INPUT 6 -d 192.168.30.104 -p icmp --icmp-type 8 -m limit --limit-burst 5 --limit 15/minute -j ACCEPT
[root@CentOS7_Server html]#iptables -I OUTPUT 6 -s 192.168.30.104 -p icmp --icmp-type 0 -j ACCEPT
令牌桶測(cè)試
7、state(報(bào)文狀態(tài)匹配)

主要參數(shù): --state state

參數(shù) 解釋說明
NEW 新連接請(qǐng)求
ESTABLISHED 已經(jīng)建立的連接
INVALID 無法識(shí)別的連接
RELATED 相關(guān)聯(lián)的連接,當(dāng)前連接是一個(gè)新請(qǐng)求,但附屬于某個(gè)已經(jīng)存在的連接
UNTRACKED 未追蹤的連接

追蹤到的連接:/proc/net/nf_conntrack
調(diào)整可記錄的連接數(shù)量最大值:/proc/sys/net/nf_conntrack_max
超時(shí)時(shí)長:/proc/sys/net/netfilter/timeout

[root@CentOS7_Server html]#iptables -F
[root@CentOS7_Server html]#iptables -A INPUT -d 192.168.30.104 -p tcp -m multiport --dports 22:23,80,139,445,3306 -m state --state NEW -j ACCEPT
###清空之前規(guī)則,添加目前服務(wù)所有tcp端口
[root@CentOS7_Server html]#iptables -I INPUT -d 192.168.30.104 -m state --state ESTABLISHED -j ACCEPT
[root@CentOS7_Server html]#iptables -A OUTPUT -s 192.168.30.104 -m state --state ESTABLISHED -j ACCEPT
###進(jìn)站和出站只要是ESTABLISHED都允許放行
[root@CentOS7_Server html]#iptables -I INPUT 2 -d 192.168.30.104 -p udp --dport 137:138 -m state --state NEW -j ACCEPT
###第一次訪問udp137和138端口的NEW允許放行
[root@CentOS7_Server html]#iptables -A INPUT -d 192.168.30.104 -j REJECT
[root@CentOS7_Server html]#iptables -A OUTPUT -s 192.168.30.104 -j REJECT
###默認(rèn)規(guī)則調(diào)整為全部拒絕
httpd測(cè)試訪問

samba測(cè)試訪問

RELATED狀態(tài)的追蹤:

需要手動(dòng)裝載狀態(tài)的追蹤模塊:nf_conntrack_ftp

iptables -I OUTPUT -s 192.168.30.104 -p tcp -m multiport --sports 22,80,139,445,3306 -m state --state ESTABLISHED,RELATED -j ACCEPT
處理動(dòng)作(跳轉(zhuǎn)目標(biāo))
- -j targetname [per-target-options]
    - 簡單target:
        - ACCEPT, DROP
    - 擴(kuò)展target:
        - REJECT
            - --reject-with type
        - LOG
            - --log-level
            - --log-prefix
            - 默認(rèn)日志保存于/var/log/messages
    - 自定義鏈做為target:

保存和載入規(guī)則:

保存: iptables-save >/PATH/TO/SOME_RULE_FILE
重載: iptables-restore </PATH/TO/SOME_RULE_FILE
-n, --noflush: 不清楚原有規(guī)則
-t, --test: 僅僅分析生成規(guī)則集,但不提交
CentOS 6:
  保存規(guī)則:
    service iptables save
      保存規(guī)則于/etc/sysconfig/iptables文件,覆蓋保存
  重載規(guī)則:
    service iptables restart
      默認(rèn)重載/etc/sysconfig/iptables文件中的規(guī)則
配置文件: /etc/sysconfig/iptables-config
CentOS 7:
(1)自定義Unit File,進(jìn)行iptables-restore
(2)firewalld服務(wù)
(3)自定義腳本

規(guī)則優(yōu)化的思路:

  • 使用自定義鏈管理特定應(yīng)用的相關(guān)規(guī)則,模塊化管理規(guī)則:
  • (1)優(yōu)先放行雙方向狀態(tài)為ESTABLISHED的報(bào)文
  • (2)服務(wù)于不同類別的功能的規(guī)則,匹配到報(bào)文可能性更大的放前面
  • (3)服務(wù)于同一類別的功能的規(guī)則,匹配條件較嚴(yán)格的放在前面
  • (4)設(shè)置默認(rèn)策略: 白名單機(jī)制
    • (a)iptables -P, 不建議
    • (b)建議在規(guī)則的最后定義規(guī)則做為默認(rèn)策略

(三)徙瓶、舉例實(shí)現(xiàn)iptables之SNAT源地址修改及DNAT目標(biāo)地址修改和PNAT端口修改等應(yīng)用

SNAT是source network address translation的縮寫毛雇,即源地址目標(biāo)轉(zhuǎn)換。
比如侦镇,多個(gè)PC機(jī)使用ADSL路由器共享上網(wǎng)灵疮,每個(gè)PC機(jī)都配置了內(nèi)網(wǎng)IP,PC機(jī)訪問外部網(wǎng)絡(luò)的時(shí)候壳繁,路由器將數(shù)據(jù)包的報(bào)頭中的源地址替換成路由器的ip震捣,當(dāng)外部網(wǎng)絡(luò)的服務(wù)器比如網(wǎng)站web服務(wù)器接到訪問請(qǐng)求的時(shí)候荔棉,他的日志記錄下來的是路由器的ip地址,而不是pc機(jī)的內(nèi)網(wǎng)ip蒿赢;
這是因?yàn)槿笥#@個(gè)服務(wù)器收到的數(shù)據(jù)包的報(bào)頭里邊的“源地址”,已經(jīng)被替換了所以叫做SNAT羡棵,基于源地址的地址轉(zhuǎn)換.
簡單來說: SNAT是指在數(shù)據(jù)包從網(wǎng)卡發(fā)送出去的時(shí)候壹若,把數(shù)據(jù)包中的源地址部分替換為指定的IP,這樣皂冰,接收方就認(rèn)為數(shù)據(jù)包的來源是被替換的那個(gè)IP的主機(jī)

Step1.搭建環(huán)境

設(shè)備名稱 ip地址
Client客戶端 172.16.0.0/16
SNAT服務(wù)器 ens33: 192.168.30.105, ens36: 172.16.8.100
遠(yuǎn)程云端服務(wù)器 192.168.30.103
SNAT

Step2.SNAT轉(zhuǎn)發(fā)服務(wù)器配置雙網(wǎng)卡

[root@SNAT-Server ~]#ifconfig
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.30.105  netmask 255.255.255.0  broadcast 192.168.30.255
        inet6 fe80::ebca:5abc:36ce:599c  prefixlen 64  scopeid 0x20<link>
        inet6 240e:82:f00d:95c6:2e01:fc37:1f2e:8c41  prefixlen 64  scopeid 0x0<global>
        inet6 fe80::ba8f:a1a3:c743:29eb  prefixlen 64  scopeid 0x20<link>
        inet6 fe80::a5b9:2f8e:5a1a:78fd  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:38:0b:78  txqueuelen 1000  (Ethernet)
        RX packets 2845  bytes 254075 (248.1 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 1770  bytes 251538 (245.6 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

ens36: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 172.168.0.254  netmask 255.255.255.255  broadcast 0.0.0.0
        inet6 240e:82:f00d:95c6:44f:4c38:4b14:7  prefixlen 128  scopeid 0x0<global>
        inet6 fe80::ed88:92f2:476d:be7d  prefixlen 64  scopeid 0x20<link>
        inet6 240e:82:f00d:95c6:4353:9518:a38c:247d  prefixlen 64  scopeid 0x0<global>
        ether 00:0c:29:38:0b:82  txqueuelen 1000  (Ethernet)
        RX packets 887  bytes 97703 (95.4 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 97  bytes 14111 (13.7 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

Step3. 在SNAT轉(zhuǎn)發(fā)服務(wù)器開啟核心轉(zhuǎn)發(fā)功能

[root@SNAT-Server ~]#sysctl -w net.ipv4.ip_forward=1   ###開啟服務(wù)器的核心轉(zhuǎn)發(fā)功能
net.ipv4.ip_forward = 1
[root@SNAT-Server ~]#systemctl start firewalld.service
[root@SNAT-Server ~]#systemctl enable firewalld.service

Step4.配置iptables規(guī)則,使得192.168.30.0/24網(wǎng)段能夠正常訪問

[root@SNAT-Server ~]#iptables -F
[root@SNAT-Server ~]#iptables -t nat -A POSTROUTING -s 172.16.0.0/16 -o ens33 -j SNAT --to-source 192.168.30.105

Step5.如果是動(dòng)態(tài)ip環(huán)境,配置如下規(guī)則:

[root@SNAT-Server ~]#iptables -t nat -A POSTROUTING -s 172.16.0.0/16 -j MASQUERADE

DNAT是destination network address translation的縮寫店展,即目標(biāo)網(wǎng)絡(luò)地址轉(zhuǎn)換。
典型的應(yīng)用是秃流,有個(gè)web服務(wù)器放在內(nèi)網(wǎng)配置內(nèi)網(wǎng)ip赂蕴,前端有個(gè)防火墻配置公網(wǎng)ip,互聯(lián)網(wǎng)上的訪問者使用公網(wǎng)ip來訪問這個(gè)網(wǎng)站當(dāng)訪問的時(shí)候剔应,客戶端發(fā)出一個(gè)數(shù)據(jù)包,這個(gè)數(shù)據(jù)包的報(bào)頭里邊语御,目標(biāo)地址寫的是防火墻的公網(wǎng)ip峻贮,防火墻會(huì)把這個(gè)數(shù)據(jù)包的報(bào)頭改寫一次,將目標(biāo)地址改寫成web服務(wù)器的內(nèi)網(wǎng)ip应闯,然后再把這個(gè)數(shù)據(jù)包發(fā)送到內(nèi)網(wǎng)的web服務(wù)器上纤控,這樣,數(shù)據(jù)包就穿透了防火墻碉纺,并從公網(wǎng)ip變成了一個(gè)對(duì)內(nèi)網(wǎng)地址的訪問了船万,即DNAT,基于目標(biāo)的網(wǎng)絡(luò)地址轉(zhuǎn)換.
簡單來說:DNAT骨田,就是指數(shù)據(jù)包從網(wǎng)卡發(fā)送出去的時(shí)候耿导,修改數(shù)據(jù)包中的目的IP,表現(xiàn)為如果你想訪問A态贤,可是因?yàn)榫W(wǎng)關(guān)做了DNAT舱呻,把所有訪問A的數(shù)據(jù)包的目的IP全部修改為B,那么悠汽,你實(shí)際上訪問的是B箱吕。


DNAT
[root@SNAT-Server ~]#iptables -t nat -A PREROUTING -d 172.16.0.254 -p tcp  --dport 80 -j DNAT --to-destination 192.168.30.103

PNAT端口修改

PNAT
[root@SNAT-Server ~]#iptables -t nat -A PREROUTING -d 192.168.30.103 -p tcp --dport 80 -j REDIRECT --to-ports 8080

(四)、簡述sudo安全切換工具柿冲,及詳細(xì)講解visudoer

sudo

su: switch user

  • 用戶切換
  • (1) su -l user
  • (2) su -l user -c 'COMMAND'

sudo:

  • 能夠讓獲得授權(quán)的用戶以另外一個(gè)用戶的身份運(yùn)行指定的命令
- 授權(quán)機(jī)制: 授權(quán)文件 /etc/sudoers
    - root    ALL=(ALL)   ALL
    - %wheel  ALL=(ALL)   ALL
- 編譯此文件的專用命令: visudo
    - 授權(quán)項(xiàng):
        - who    where=(whom)  commands
        - user   hosts=(runas) commands
                       - users:
                - username
                - #uid
                - %groupname
                - %#gid
                - user_alias
                - 支持將多個(gè)用戶定義為一組用戶,稱之為用戶別名,即user_alias
            - hosts:
                - ip
                - hostname
                - NetAddr
                - host_alias
            - runas:
                - ...
                - runas_alias
            - commands:
                - command
                - directory
                - sudoedit: 特殊權(quán)限,可用于向其他用戶授予sudo權(quán)限
                - cmnd_alias

sudo命令的使用示例:

1.創(chuàng)建和刪除

###創(chuàng)建系統(tǒng)用戶并設(shè)置密碼
[root@CentOS7_Client1 ~]#useradd hello
[root@CentOS7_Client1 ~]#echo "hello" |passwd --stdin hello
###su到對(duì)應(yīng)用戶下無法創(chuàng)建新用戶或者刪除用戶,提示權(quán)限不夠
[root@CentOS7_Client1 ~]#su - hello
[hello@CentOS7_Client1 ~]$ useradd linux
-bash: /usr/sbin/useradd: Permission denied
[hello@CentOS7_Client1 ~]$ userdel hello
-bash: /usr/sbin/userdel: Permission denied
###visudo編輯添加對(duì)應(yīng)的權(quán)限操作
[root@CentOS7_Client1 ~]#visudo
## Same thing without a password
# %wheel        ALL=(ALL)       NOPASSWD: ALL
hello   ALL=(ALL)  /usr/sbin/useradd,/usr/sbin/userdel
###重新su到hello賬戶,添加和刪除創(chuàng)建的用戶
[root@CentOS7_Client1 ~]#su - hello
Last login: Fri Nov 16 14:38:52 CST 2018 on pts/0

[hello@CentOS7_Client1 ~]$ sudo useradd linuxtest
[hello@CentOS7_Client1 ~]$ tail /etc/passwd
hello:x:1002:1002::/home/hello:/bin/bash
linuxtest:x:1003:1003::/home/linuxtest:/bin/bash

[hello@CentOS7_Client1 ~]$ userdel linuxtest
-bash: /usr/sbin/userdel: Permission denied
[hello@CentOS7_Client1 ~]$ sudo userdel linuxtest

注意:一般sudo命令會(huì)驗(yàn)證su賬號(hào)的密碼才能使用,默認(rèn)時(shí)間是5分鐘,超過時(shí)間后執(zhí)行會(huì)再次提示輸入密碼

2.自定義組實(shí)現(xiàn)sudo

[root@CentOS7_Client1 ~]#usermod -a -G wheel hello
[root@CentOS7_Client1 ~]#newgrp wheel
[root@CentOS7_Client1 ~]#id hello
uid=1002(hello) gid=1002(hello) groups=1002(hello),10(wheel)

[root@CentOS7_Client1 ~]#visudo
## Same thing without a password
%wheel  ALL=(ALL)       /usr/sbin/useradd,/usr/sbin/userdel

[root@CentOS7_Client1 ~]#su - hello
Last login: Fri Nov 16 14:41:44 CST 2018 on pts/0
[hello@CentOS7_Client1 ~]$ sudo -k
[hello@CentOS7_Client1 ~]$ sudo useradd hellouser1
[sudo] password for hello: 
[hello@CentOS7_Client1 ~]$ id hellouser1
uid=1004(hellouser1) gid=1004(hellouser1) groups=1004(hellouser1)

注意: 使用sudo su -root可以無密碼切換到管理員賬戶,風(fēng)險(xiǎn)很大,所以需要做對(duì)應(yīng)的修改

## Same thing without a password
%wheel  ALL=(ALL)       ALL, !/bin/su, !/usr/bin/passwd root

3.定義別名實(shí)現(xiàn)sudo

[root@CentOS7_Client1 ~]#visudo
## Allows people in group wheel to run all commands
#%wheel ALL=(ALL)       ALL
User_Alias USERADMIN=fedora,centos
Cmnd_Alias NETADMINCMD=/sbin/ip, /sbin/ifconfig, /sbin/route
Cmnd_Alias USERADMINCMD=/sbin/useradd, /sbin/userdel, /bin/passwd, !/bin/passwd root
fedora  ALL=(ALL)       NETADMINCMD  
centos  ALL=(ALL)       USERADMINCMD
[root@CentOS7_Client1 ~]#su - fedora
Last login: Fri Nov 16 15:08:38 CST 2018 on pts/0
[fedora@CentOS7_Client1 ~]$ sudo -l
User fedora may run the following commands on CentOS7_Client1:
    (ALL) /sbin/ip, /sbin/ifconfig, /sbin/route
[fedora@CentOS7_Client1 ~]$ su - centos
Password: 
[centos@CentOS7_Client1 ~]$ sudo -l
User centos may run the following commands on CentOS7_Client1:
    (ALL) /sbin/useradd, /sbin/userdel, /bin/passwd, !/bin/passwd root

注意:每次用戶操作的時(shí)候都要輸入用戶密碼茬高,只要在規(guī)則的別名前面加一個(gè)標(biāo)簽NOPASSWD就可以不需要密碼操作。每次需要用戶輸入密碼則加PASSWD假抄。

[root@CentOS7_Client1 ~]# visudo
 USERADMIN ALL=(ALL) NOPASSWD:NETADMINCMD,PASSWD:USERADMINCMD
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末怎栽,一起剝皮案震驚了整個(gè)濱河市丽猬,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌婚瓜,老刑警劉巖宝鼓,帶你破解...
    沈念sama閱讀 221,273評(píng)論 6 515
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異巴刻,居然都是意外死亡愚铡,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,349評(píng)論 3 398
  • 文/潘曉璐 我一進(jìn)店門胡陪,熙熙樓的掌柜王于貴愁眉苦臉地迎上來沥寥,“玉大人,你說我怎么就攤上這事柠座∫匮牛” “怎么了?”我有些...
    開封第一講書人閱讀 167,709評(píng)論 0 360
  • 文/不壞的土叔 我叫張陵妈经,是天一觀的道長淮野。 經(jīng)常有香客問我,道長吹泡,這世上最難降的妖魔是什么骤星? 我笑而不...
    開封第一講書人閱讀 59,520評(píng)論 1 296
  • 正文 為了忘掉前任,我火速辦了婚禮爆哑,結(jié)果婚禮上洞难,老公的妹妹穿的比我還像新娘。我一直安慰自己揭朝,他們只是感情好队贱,可當(dāng)我...
    茶點(diǎn)故事閱讀 68,515評(píng)論 6 397
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著潭袱,像睡著了一般柱嫌。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上屯换,一...
    開封第一講書人閱讀 52,158評(píng)論 1 308
  • 那天慎式,我揣著相機(jī)與錄音,去河邊找鬼趟径。 笑死瘪吏,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的蜗巧。 我是一名探鬼主播掌眠,決...
    沈念sama閱讀 40,755評(píng)論 3 421
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢(mèng)啊……” “哼幕屹!你這毒婦竟也來了蓝丙?” 一聲冷哼從身側(cè)響起级遭,我...
    開封第一講書人閱讀 39,660評(píng)論 0 276
  • 序言:老撾萬榮一對(duì)情侶失蹤,失蹤者是張志新(化名)和其女友劉穎渺尘,沒想到半個(gè)月后挫鸽,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 46,203評(píng)論 1 319
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡鸥跟,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 38,287評(píng)論 3 340
  • 正文 我和宋清朗相戀三年丢郊,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片医咨。...
    茶點(diǎn)故事閱讀 40,427評(píng)論 1 352
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡枫匾,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出拟淮,到底是詐尸還是另有隱情干茉,我是刑警寧澤,帶...
    沈念sama閱讀 36,122評(píng)論 5 349
  • 正文 年R本政府宣布很泊,位于F島的核電站角虫,受9級(jí)特大地震影響,放射性物質(zhì)發(fā)生泄漏委造。R本人自食惡果不足惜戳鹅,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,801評(píng)論 3 333
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望争涌。 院中可真熱鬧粉楚,春花似錦辣恋、人聲如沸亮垫。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,272評(píng)論 0 23
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽饮潦。三九已至,卻和暖如春携狭,著一層夾襖步出監(jiān)牢的瞬間继蜡,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,393評(píng)論 1 272
  • 我被黑心中介騙來泰國打工逛腿, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留稀并,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 48,808評(píng)論 3 376
  • 正文 我出身青樓单默,卻偏偏與公主長得像碘举,于是被迫代替她去往敵國和親。 傳聞我的和親對(duì)象是個(gè)殘疾皇子搁廓,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,440評(píng)論 2 359