linux iptables:安全應用,防火墻
阿新 • • 發佈:2018-02-18
onf ont wall 如果 source cmp 開源 .sh image
iptables:安全應用,防火墻
windows和linux都有防火墻,企業的邊緣會部署防火墻保證企業內部的局域網是安全的。針對個人電腦會有防火墻保證系統是安全的。
防火墻是唯一通道。
防火墻分類(這裏的分類只是一個簡單的分類,還有很多種): 1.包過濾防火墻,速度快,但是只檢查包頭,不檢查數據區,不建立連接狀態表,安全性能低。 2.應用代理防火墻。安全高,性能高。只檢查包頭,不檢查數據區,建立連接狀態表。 防火墻還可以分為硬件和軟件防火墻。硬件防火墻就是一個硬件設備,軟件防火墻比如windows的360和linux的開源的iptables。
iptables是linux自帶的小型的防火墻。
table有4張表,filter過濾表,寫的是防火墻的策略。nat表做網絡相關的。mangle和raw是內核部分,不用管。
command:過濾表的策略。不允許哪個網卡進去或者出來,
match:匹配條件。匹配數據包來做。
target:匹配的數據包進行操作。就2種可能性,通過和不通過。如果沒問題就通過,有問題就不通過。
表 filter表:針對本機,itables的默認表有:input,forward,output鏈。 nat表:轉換數據包的源地址和目的地址。包括:prerouting,postrouting,out鏈。 https://www.linuxidc.com/Linux/2017-02/140556.htm
以下是centos的:
(ubuntu沒有這些命令)
service iptables stop
service iptables start
service iptables restart
root@ubuntu:/# iptables -L (查看iptables防火墻的默認策略) Chain INPUT (policy DROP) target prot opt source destination ufw-before-logging-input all -- anywhere anywhereufw-before-input all -- anywhere anywhere ufw-after-input all -- anywhere anywhere ufw-after-logging-input all -- anywhere anywhere ufw-reject-input all -- anywhere anywhere ufw-track-input all -- anywhere anywhere Chain FORWARD (policy DROP) target prot opt source destination DOCKER-ISOLATION all -- anywhere anywhere DOCKER all -- anywhere anywhere ACCEPT all -- anywhere anywhere ctstate RELATED,ESTABLISHED ACCEPT all -- anywhere anywhere ACCEPT all -- anywhere anywhere
以下是centos的: vim sysconfig/iptables :防火墻主配置文件,這裏面存儲的都是防火墻的策略,如果要添加策略,建議通過命令行來添加,而不是修改這個文件來添加。 *filter : 表示是一個過濾表 :INPUT ACCEPT [0:0] :這3個鏈都是開的 :FORWARD ACCEPT [0:0] :OUTPUT ACCEPT [0:0] : RH_Firewall-1-INPUT - [0:0] : RH_Firewall-1-INPUT是防火墻當前策略的一個名字 -A INPUT -j RH_Firewall-1-INPUT -A FORWARD -j RH_Firewall-1-INPUT -A RH_Firewall-1-INPUT -p lo -j ACCEPT (lo同意) -A RH_Firewall-1-INPUT -p icmp --icmp-type any -j ACCEPT (icmp同意) -A RH_Firewall-1-INPUT -p 50 -j ACCEPT (50.51端口可以訪問) -A RH_Firewall-1-INPUT -p 51 -j ACCEPT -A RH_Firewall-1-INPUT -p udp --dport 5353 -d 224.0.0251 -j ACCEPT (udp可以訪問)
寫策略: 查看規則-L:iptables -L ,iptables -L -n ,iptables -L -n --line-number 修改默認規則-P:iptables -P INPUT DROP (備註:為鏈設置默認的target可用的是DROP和ACCEPT,作為最後一條規則被執行) 追加規則-A:作為最後一條規則,iptables -A INPUT -p tcp --dport 22 -j ACCEPT 刪除規則-D:iptables -D INPUT 3 ,iptables -D INPUT -s 192.168.0.1 DROP 修改規則-R:iptables -R INPUT 3 -s 192.168.0.1 -j ACCEPT 清空規則-F:iptables -F ,iptables -F INPUT ,iptables -t nat -F
root@ubuntu:/# iptables -L (默認策略,和iptables配置文件的信息是一致的) Chain INPUT (policy DROP) target prot opt source destination ufw-before-logging-input all -- anywhere anywhere ufw-before-input all -- anywhere anywhere ufw-after-input all -- anywhere anywhere root@ubuntu:/# iptables -L -n (會把目標信息以地址的形式顯示出來) iptables -L -n --line-number (顯示行號,刪除規則是基於行號刪除的)
INPUT屬於進入的概念,iptables -P INPUT DROP,-P是修改規則,規則只有3種inout,output,forward, -P表示針對inout或output或forward進行調整。vim sysconfig/iptables文件裏面的:INPUT ACCEPT [0:0] :FORWARD ACCEPT [0:0] : OUTPUT ACCEPT [0:0] 默認都是ACCEPT的,iptables -P INPUT DROP,iptables-save(修改了要保存),這樣INPUT就是DROP了。
root@ubuntu:/# iptables -L Chain INPUT (policy DROP) :input鏈,都進不來 target prot opt source destination ufw-before-logging-input all -- anywhere anywhere Chain FORWARD (policy DROP) :forward鏈,都不能跳轉 target prot opt source destination DOCKER-ISOLATION all -- anywhere anywhere DOCKER all -- anywhere anywhere ACCEPT all -- anywhere anywhere ctstate RELATED,ESTABLISHED ACCEPT all -- anywhere anywhere ACCEPT all -- anywhere anywhere ufw-before-logging-forward all -- anywhere anywhere Chain OUTPUT (policy ACCEPT) :output鏈,都出得去 target prot opt source destination ufw-before-logging-output all -- anywhere anywhere ufw-before-output all -- anywhere anywhere Chain DOCKER (1 references) target prot opt source destination Chain DOCKER-ISOLATION (1 references) target prot opt source destination RETURN all -- anywhere anywhere Chain ufw-after-input (1 references) target prot opt source destination ufw-skip-to-policy-input udp -- anywhere anywhere udp dpt:netbios-ns ufw-skip-to-policy-input udp -- anywhere anywhere udp dpt:netbios-dgm ufw-skip-to-policy-input tcp -- anywhere anywhere tcp dpt:netbios-ssn
Chain是鏈,input和output是輸入和輸出,input鏈是從外面進來到主機這裏來,針對進來的信息做什麽策略,output鏈是從主機出去,針對出去的做什麽策略,forward是指當要訪問資源的時候必須通過我這臺計算機轉向,A要訪問C必須經過B主機,就要通過B主機轉向,B直接重定向到C上面去。 input和output是針對當前主機而言的,forward是針對其他電腦的。 iptables -L是打印當前3個鏈的策略信息。INPUT是DROP表示任何人訪問這臺電腦都是禁止的,OUTPUT是DROP表示當前電腦不能訪問外部。 root@ubuntu:/# iptables -P INPUT ACCEPT (-P修改input鏈) root@ubuntu:/# iptables -L Chain INPUT (policy ACCEPT) target prot opt source destination ufw-before-logging-input all -- anywhere anywhere ufw-before-input all -- anywhere anywhere
追加規則:要往input鏈或者output鏈或者forward鏈裏面來追加規則。 root@ubuntu:/# iptables -A INPUT -p tcp --dport 22 -j ACCEPT (-p表示要使用tcp相關的協議,) root@ubuntu:/# iptables -L --line-number Chain INPUT (policy DROP) num target prot opt source destination 1 ufw-before-logging-input all -- anywhere anywhere 7 ACCEPT tcp -- anywhere anywhere tcp dpt:ssh (通過tcp協議在任意地方連接ssh都是同意)
修改規則-R:iptables -R INPUT 3 -s 192.168.0.1 -j ACCEPT root@ubuntu:/# iptables -R INPUT 7 -s 192.168.0.1 -j DROP (7表示input鏈裏面的第7行,-s表示不是原來的任意地址了,192.168.0.1進來的要拒絕) root@ubuntu:/# iptables -L --line-number Chain INPUT (policy DROP) num target prot opt source destination 7 DROP all -- 192.168.0.1 anywhere
刪除規則-D:iptables -D INPUT 3 ,iptables -D INPUT -s 192.168.0.1 DROP root@ubuntu:/# iptables -D INPUT 7 (根據行號刪除) root@ubuntu:/# iptables -L --line-number Chain INPUT (policy DROP) :就沒有7了 num target prot opt source destination
root@ubuntu:/# iptables -A INPUT -p SFTP --dport 22 -j ACCEPT root@ubuntu:/# iptables -A INPUT -p FTP --dport 21 -j ACCEPT root@ubuntu:/# iptables -L --line-number Chain INPUT (policy ACCEPT) num target prot opt source destination 7 ACCEPT tcp -- anywhere anywhere tcp dpt:ssh 8 ACCEPT tcp -- anywhere anywhere tcp dpt:ftp 清空規則-F:iptables -F (清空所有鏈),iptables -F INPUT (清空input鏈),iptables -t nat -F
匹配條件:是從最大的範圍到最小範圍劃分的。計算機上網是要通過網卡上網的,網卡連接的是網線,出入接口是從網卡出去進來,對應的是input和output鏈。源地址和目的地址是不允許某個ip進來或者訪問某個ip。
-A追加規則,output鏈,將從網卡eth0出去和進來的都拒絕。 root@ubuntu:/# iptables -A OUTPUT -o ens33 -j DROP root@ubuntu:/# iptables -L --line-number Chain OUTPUT (policy ACCEPT) num target prot opt source destination 7 DROP all -- anywhere anywhere root@ubuntu:/# ping www.baidu.com (出不去了)(刪除後就可以ping了) PING www.a.shifen.com (163.177.151.110) 56(84) bytes of data. ping: sendmsg: Operation not permitted ping: sendmsg: Operation not permitted
root@ubuntu:/# iptables -A INPUT -s 192.168.2.114 -j DROP root@ubuntu:/# iptables -L Chain INPUT (policy ACCEPT) target prot opt source destination DROP all -- 192.168.2.114 anywhere root@ubuntu:/# iptables -A OUTPUT -d www.baidu.com -j DROP root@ubuntu:/# iptables -L Chain OUTPUT (policy ACCEPT) num target prot opt source destination 7 DROP all -- anywhere 163.177.151.110 8 DROP all -- anywhere 163.177.151.109 root@ubuntu:/# ping www.baidu.com PING www.a.shifen.com (163.177.151.109) 56(84) bytes of data. ping: sendmsg: Operation not permitted root@ubuntu:/# iptables -D OUTPUT 7 root@ubuntu:/# iptables -D OUTPUT 8 iptables: Index of deletion too big. root@ubuntu:/# iptables -D OUTPUT 7 root@ubuntu:/# ping www.baidu.com PING www.a.shifen.com (163.177.151.110) 56(84) bytes of data. 64 bytes from 163.177.151.110: icmp_seq=1 ttl=128 time=13.7 ms
-A追加一個規則,來自於192.168.0.1網段的,tcp協議的22端口的,都禁止。
按照ip地址加端口加協議用的比較多。禁網卡用的少。
iptables的匹配規則:
不建議修改配置文件:iptables命令方式改的規則,如果不保存到配置文件,則重啟後就沒有了,service iptables save就是將命令行方式改的規則保存到配置文件。
linux:如果既有配置文件又有命令參數,如果通過命令方式執行的都是臨時的,通過配置文件都是永久的。
linux iptables:安全應用,防火墻