iptables實戰筆記一

iptables實戰

1.開啟防火墻

systemctl start firewalld

2.清空所有的默認規則,我們自己定義自己的規則

iptables -F

查看此時的iptables
iptables -nL

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         

Chain FORWARD_IN_ZONES (0 references)
target     prot opt source               destination         

Chain FORWARD_IN_ZONES_SOURCE (0 references)
target     prot opt source               destination         

Chain FORWARD_OUT_ZONES (0 references)
target     prot opt source               destination         

Chain FORWARD_OUT_ZONES_SOURCE (0 references)
target     prot opt source               destination         

Chain FORWARD_direct (0 references)
target     prot opt source               destination         

Chain FWDI_public (0 references)
target     prot opt source               destination         

Chain FWDI_public_allow (0 references)
target     prot opt source               destination         

Chain FWDI_public_deny (0 references)
target     prot opt source               destination         

Chain FWDI_public_log (0 references)
target     prot opt source               destination         

Chain FWDO_public (0 references)
target     prot opt source               destination         

Chain FWDO_public_allow (0 references)
target     prot opt source               destination         

Chain FWDO_public_deny (0 references)
target     prot opt source               destination         

Chain FWDO_public_log (0 references)
target     prot opt source               destination         

Chain INPUT_ZONES (0 references)
target     prot opt source               destination         

Chain INPUT_ZONES_SOURCE (0 references)
target     prot opt source               destination         

Chain INPUT_direct (0 references)
target     prot opt source               destination         

Chain IN_public (0 references)
target     prot opt source               destination         

Chain IN_public_allow (0 references)
target     prot opt source               destination         

Chain IN_public_deny (0 references)
target     prot opt source               destination         

Chain IN_public_log (0 references)
target     prot opt source               destination         

Chain OUTPUT_direct (0 references)
target     prot opt source               destination

3.我們準備建立自己的規則

(1) 放行ssh (端口:22)

iptables -t filter -A INPUT  -s 0/0  -d 192.168.42.153  -p tcp --dport 22 -j ACCEPT 或者
iptables -A INPUT -d 192.168.42.153 -p tcp --dport 22 -j ACCEPT
iptables -t filter -A OUTPUT  -s 192.168.42.153  -d 0/0   -p tcp   --sport 22  -j ACCEPT 或者
iptables -A  OUTPUT -s  192.168.42.153  -p tcp  --sport  22 -j ACCEPT

(2)修改默認規則鏈(關閉所有端口)

iptables -P INPUT DROP
iptables -P OUTPUT DROP
iptables -P FORWARD DROP

(3)放行web(80)端口 httpd nginx

iptables -I INPUT -d 192.168.42.153 -p tcp --dport 80 -j ACCEPT 或者
iptables -t filter -I INPUT -d 192.168.42.153 -p tcp --dport 80 -j ACCEPT

iptables -I OUTPUT -s 192.168.42.153 -p tcp --sport 80 -j ACCEPT 或者
iptables -t filter -I OUTPUT -s 192.168.42.153  -d 0/0 -p tcp --sport 80 -j ACCEPT

(4)修改默認規則鏈后,我們發現ping不通自己,也ping不通別的主機

iptables -t filter -I INPUT -s 127.0.0.1 -d 127.0.0.1 -i lo  -j ACCEPT 
iptables -t filter -I OUTPUT -s 127.0.0.1 -d 127.0.0.1 -o lo  -j ACCEPT

(5)允許自己ping別的主機

iptables -t filter -I OUTPUT -s 192.168.42.153 -d 0/0  -p icmp --icmp-type 8 -j ACCEPT
iptables -t filter -I INPUT -s 0/0 -d 192.168.42.153 -p icmp --icmp-type 0 -j ACCEPT

(6)允許任何人來ping本機

iptables -t filter -I INPUT -s 0/0 -d 192.168.42.153 -p icmp --icmp-type 8 -j ACCEPT
iptables -t filter -I OUTPUT -s 192.168.42.153 -d 0/0  -p icmp --icmp-type 0 -j ACCEPT

(7)同時開發多個端口(多端口匹配)

iptables -I INPUT -s 0/0 -d 192.168.42.153 -p tcp -m multiport --dports 22,80,3306 -j ACCEPT
iptables -I INPUT -d 0/0 -s 192.168.42.153 -p tcp -m multiport --sports 22,80,3306 -j ACCEPT

(8)iptables -vnL –line-numbers #顯示數字

iptables  -vnL INPUT  --line-numbers 
Chain INPUT (policy DROP 1 packets, 229 bytes)
num   pkts bytes target     prot opt in     out     source               destination         
1        8   576 ACCEPT     icmp --  *      *       0.0.0.0/0            192.168.42.153       icmptype 8
2       12  1008 ACCEPT     icmp --  *      *       0.0.0.0/0            192.168.42.153       icmptype 0
3       16  1226 ACCEPT     all  --  lo     *       127.0.0.1            127.0.0.1           
4       88  7565 ACCEPT     tcp  --  *      *       0.0.0.0/0            192.168.42.153       tcp dpt:80
5     2135  163K ACCEPT     tcp  --  *      *       0.0.0.0/0            192.168.42.153       tcp dpt:22

(9) 源地址,目的地址范圍匹配

iptables -I INPUT -d 192.168.42.153 -p tcp --dport 23 -m iprange --src-range 192.168.42.150-192.168.42.158 -j ACCEPT

iptables -I OUTPUT -s 192.168.42.153 -p tcp --dport 23 -m iprange --dst-range  192.168.42.150-192.168.42.158 -j ACCEPT

(10)禁止包含”old”字符的頁面出來

iptables -I OUTPUT -s 192.168.42.153 -d 0/0 -p tcp --sport 80 -m string --algo bm --string "old" -j DROP

(11)基于時間限定,9點到19點,禁止訪問80端口

iptables -I INPUT -s 0/0  -d 192.168.42.153 -p tcp --dport 80  -m time --timestart 09:00:00 --timestop 19:00:00 --kerneltz  -j DROP

(12)周一到周五9點到19點禁止訪問80端口

iptables -I INPUT  -d 192.168.42.153 -p tcp --dport 80  -m time --timestart 09:00:00 --timestop 19:00:00 --kerneltz --weekdays 1,2,3,4,5  -j DROP

(13)端口大于2個并發連接(禁止)

iptables -I INPUT -s 0/0 -d 192.168.42.153 -p tcp  --dport 22 -m connlimit --connlimit-above 2 -j DROP

(14)端口同一個客戶端小于3個并發連接

iptables -I INPUT -s 0/0 -d 192.168.42.153 -p tcp  --dport 22 -m connlimit ! --connlimit-above 3 -j DROP

4.新建自定義鏈 ,開放80

iptables -F
iptables -A INPUT -d 192.168.42.153 -p tcp --dport 22 -j ACCEPT
iptables -A  OUTPUT -s  192.168.42.153  -p tcp  --sport  22 -j ACCEPT
iptables -P INPUT DROP
iptables -P OUTPUT DROP
iptables -N webinput
iptables -N weboutput
iptables -I webinput -d 192.168.42.153 -p tcp --dport 80 -j ACCEPT 
iptables -I weboutput -s 192.168.42.153 -p tcp --sport 80 -j ACCEPT
iptables -A INPUT -p tcp -j webinput 
iptables -A OUTPUT -p tcp -j weboutput

“`

原創文章,作者:srayban,如若轉載,請注明出處:http://www.www58058.com/77980

(0)
sraybansrayban
上一篇 2017-06-12 21:15
下一篇 2017-06-13 11:34

相關推薦

  • 18-系統啟動故障修復-實踐

    說明:重啟時可以選擇性在vmlinuz所在行末尾添加 selinux=0;或者直接編輯/etc/selinus/config文件,更改 SELINUX=disabled 關閉SELINUX??梢员苊獯驑撕灒澥訒r間 以下操作都需要進入bootloader引導加載項修改內核啟動參數,在vmlinuz所在行末尾添加一個啟動選項 如何進入bootloader引…

    2017-04-02
  • 小白學習Linux系統一周總結

         告別平凡,安定而又沒什么前途的工作,我選擇了報了門熱門的計算機課程--python運維開發。我不知道為什么一開始就學Linux系統,我早預料到這是一個艱難的開始,還好我不是完全沒有基礎,以前有學過iOS開發。看到著終端中的“\”表示根目錄,我輕微有點親切感,不像第一次看到終端時的恐懼。我總結這周的學習感受,?!?/p>

    Linux干貨 2017-09-04
  • Linux網絡相關概念及bash腳本編程練習

    馬哥教育網絡班第23期+第八周課堂練習 Linux網絡相關概念及bash腳本編程練習 習題: 1.請描述網橋、集線器、二層交換機、三層交換機、路由器的功能、使用場景與區別 (1)網橋: 橋接器(英語:network bridge),又稱網橋,一種網路裝置,負責網路橋接(network bridging)之用。 橋接器將網絡的多個網段在數據鏈路層(O…

    Linux干貨 2016-11-15
  • centos進程管理

    一 、進程的概念和分類1.進程的概念      Linux是一個多用戶多任務的操作系統。多用戶是指多個用戶可以在同一時間使用同一個linux系統;多任務是指在Linux下可以同時執行多個任務,更詳細的說,linux采用了分時管理的方法,所有的任務都放在一個隊列中,操作系統根據每個任務的優先級為每個任務分配合適…

    Linux干貨 2016-09-19
  • Linux下硬連接和軟連接的區別

    Linux下硬鏈接與軟鏈接的區別   鏈接,是指在計算機文件之間傳遞參數和控制命令,并把它們組成一個可執行的整體的過程。例如我們常見的windows系統下的“快捷方式”,它是一種鏈接,人們可以通過它來直接訪問計算機上的某個文件,不必逐級在硬盤目錄下尋找;互聯網上的“超文本鏈接”,它用文字鏈接的形式來指向一個頁面,人們可以通過此鏈接快速訪問網站的頁面…

    Linux干貨 2016-10-21
  • LVM練習及quota和dd命令

    練習 1、創建一個至少有兩個PV組成的大小為20G的名為testvg的 VG;要求PE大小為16MB, 而后在卷組中創建大小為5G的邏 輯卷testlv;掛載至/users目錄 第一步!首先準備足夠大的硬盤來實驗 [root@Cloud /]#lsblk  #添加了/dev/sdb/c硬盤  NAME &nb…

    Linux干貨 2016-09-09
欧美性久久久久