Linux 防火牆

防火牆是一種網絡服務,它可以過濾、阻止不需要的網絡流量,允許正常的網絡流量通過。

iptablesip6tables 用於在Linux內核中設置、維護和檢查IPv4和IPv6包過濾規則表。可以定義幾個不同的表。每個表都包含許多內置鏈,還可以包含用戶定義的鏈。

每個鏈是一個規則列表,可以匹配一組數據包。每個規則都指定如何處理匹配的數據包。

命令格式:

iptables [-t table] {-A|-C|-D} chain rule-specification

ip6tables [-t table] {-A|-C|-D} chain rule-specification

iptables [-t table] -I chain [rulenum] rule-specification

iptables [-t table] -R chain rulenum rule-specification

iptables [-t table] -D chain rulenum

iptables [-t table] -S [chain [rulenum]]

iptables [-t table] {-F|-L|-Z} [chain [rulenum]] [options...]

iptables [-t table] -N chain

iptables [-t table] -X [chain]

iptables [-t table] -P chain target

iptables [-t table] -E old-chain-name new-chain-name
rule-specification = [matches...] [target]

match = -m matchname [per-match-options]

target = -j targetname [per-target-options]

target

  • 匹配防火牆規則時,如果數據包不匹配,則檢查鏈中的下一條規則;如果匹配,則由 target 值指定下一條規則。
  • target 值可以是用戶定義的鏈的名稱、iptables-extensions(8) 中描述的 iptables-extensions(8) 之一或特殊值 ACCEPT、DROP、RETURN 之一。
  • ACCEPT 是指讓包通過。DROP 是指拋棄包。RETURN 意味着停止遍歷此鏈,並在上一個(調用)鏈中的下一個規則處繼續。

table

-t, --table table :指定操作哪個表。目前共有五個表:

  • filter :默認的表。負責過濾。它包含內置的鏈 INPUT , FORWARD , OUTPUT .
  • nat :網絡地址轉換功能。它由四個內置部分組成:PREROUTING , INPUT , OUTPUT , POSTROUTING .
  • mangle :拆散報文,做出修改,並重新封裝。直到內核 2.4.17,它包含兩部分:PREROUTINGOUTPUT 。內核2.4.18 之後,它還包含了其他三部分:INPUT , FORWARD , POSTROUTING .
  • raw :關閉 nat 表上企業的連接追蹤機制。. 它以更高的優先級在netfilter鉤子上註冊,因此在ip_conntrack或任何其他ip表之前被調用。它提供以下內置鏈:REROUTING, OUTPUT .
  • security :此表用於強制訪問控制(MAC)網絡規則。強制訪問控制由Linux安全模塊(如SELinux)實現。security表 在 filter表之後調用。此表提供以下內置鏈:INPUT , OUTPUT , FORWARD .

在這裏插入圖片描述
在這裏插入圖片描述
截自:http://www.zsythink.net/archives/1199

命令選項

  • -A, --append chain rule-specification :將一個或多個規則附加到選定鏈的末尾。
  • -C, --check chain rule-specification :檢查指定的鏈中是否存在指定的規則。通過退出碼來表明查詢結果。
  • -D, --delete chain rule-specification
    -D, --delete chain rulenum :從指定的鏈中刪除指定的規則(通過匹配或序號指定,序號從 1開始)。
  • -I, --insert chain [rulenum] rule-specification :插入指定的規則到指定鏈中的指定位置(默認爲 1)。
  • -R, --replace chain rulenum rule-specification :用指定的規則替換指定鏈中指定位置的規則。
  • -L, --list [chain] :列出指定鏈中的所有規則(默認是所有鏈)。
  • -S, --list-rules [chain] :打印指定鏈中的所有規則(默認是所有鏈)。
  • -F, --flush [chain] :刪除指定鏈中的所有規則(默認是所有鏈)。
  • -Z, --zero [chain [rulenum]] :將所有鏈中的數據包和字節計數器清零,或者只清零給定的鏈,或者只清零鏈中的給定規則。
  • -N, --new-chain chain :創建新的用戶自定義鏈。
  • -X, --delete-chain [chain] :刪除指定的用戶自定義的鏈。 刪除時,必須確保沒有其他的鏈引用當前鏈;如果存在,則必須刪除或替換引用規則,然後才能刪除鏈。鏈必須爲空,即不包含任何規則。如果沒有給出參數,它將嘗試刪除表中的每個非內置鏈。
  • -P, --policy chain target :將內置(非用戶定義)鏈的策略設置爲給定目標。策略目標必須是 ACCEPT 或 DROP。
  • -E, --rename-chain old-chain new-chain :重命名用戶定義的鏈。

參數選項

  • -4, --ipv4 :結合 ip6tables-restore 使用插入一個規則時,將忽略該規則。
  • -6, --ipv6 :結合 iptables-restore 使用插入一個規則時,將忽略該規則。
  • [!] -p, --protocol protocol :要檢查的規則或數據包的協議。protocol 可取:tcp, udp, udplite, icmp, icmpv6,esp, ah, sctp, mh , all 。! 表示取反。
  • [!] -s, --source address[/mask][,...] :指定源地址。address 可取:網絡名、主機名、主機IP地址。! 表示取反。--src 是此選項的別名。
  • [!] -d, --destination address[/mask][,...] :指定目的地址。--dst 是此選項的別名。其他同上。
  • -m, --match match :指定要使用的匹配。匹配時遵從短路邏輯。
  • -j, --jump target :指定此規則的 target,即匹配時執行的動作。
  • -g, --goto chain :指定應該繼續執行的處理過程。在用戶鏈中使用。
  • [!] -i, --in-interface name :接收數據包的接口的名稱。如果接口名稱以“+”結尾,則以該名稱開頭的任何接口都將匹配。! 表示取反。
  • [!] -o, --out-interface name :將通過其發送數據包的接口的名稱。如果接口名稱以“+”結尾,則以該名稱開頭的任何接口都將匹配。! 表示取反。
  • [!] -f, --fragment :這意味着該規則只引用分片數據包的第二個或更後的IPv4片段。! 表示只查看第一個分片。
  • -c, --set-counters packets bytes :使管理員能夠初始化規則的數據包和字節計數器。

其他選項

  • -v, --verbose :輸出更多信息。
  • -w, --wait [seconds] :等待 xtables 鎖(爲了防止併發訪問),默認不等待。
  • -W, --wait-interval microseconds :每次迭代等待的間隔。結合 -w 使用。
  • -n, --numeric :以數字形式輸出端口號、主機地址。
  • -x, --exact :顯示數據包和字節計數器的確切值。
  • --line-numbers :羅列規則時,顯示行號。
  • --modprobe=command :往鏈中添加或插入規則時,使用 command 加載任何必需的模塊。

擴展

ip6tables [-m name [module-options...]]  [-j  target-name  [target-options...]

iptables  [-m  name  [module-options...]]  [-j target-name [target-options...]

iptables 可以通過 -m-match 選項使用擴展包匹配模塊。

如果指定了 -p--protocol,並且僅當遇到未知選項時,iptables 將嘗試加載與協議同名的匹配模塊,以嘗試使該選項可用。

如果想查看具體模塊的使用方法,可以使用:iptables -m MODULE -h 查看。如,

ming@ubuntu:~$ iptables -m tcp -h
iptables v1.6.1

Usage: iptables -[ACD] chain rule-specification [options]
       iptables -I chain [rulenum] rule-specification [options]
       iptables -R chain rulenum rule-specification [options]
       iptables -D chain rulenum [options]
       iptables -[LS] [chain [rulenum]] [options]
...

MATCH 擴展

可用模塊有:(可用 man iptables-extensions 查看詳細信息)

addrtype
ah
bpf
cgroup
cluster
comment
connbytes
connlabel
connlimit
connmark
conntrack
cpu
dccp
devgroup
dscp
dst
ecn
esp
eui64
frag
hashlimit
hbh
helper
hl
icmp
icmp6
iprange
ipv6header
ipvs
length
limit
mac
mark
mh
multiport
nfacct
osf
owner
physdev
pkttype
policy
quota
rateest
realm
recent
rpfilter
rt
sctp
set
socket
state
statistic
string
tcp
tcpmss
time
tos
ttl
u32
udp

TARGET 擴展

(可用 man iptables-extensions 查看詳細信息)

AUDIT
CHECKSUM
CLASSIFY
CLUSTERIP
CONNMARK
CONNSECMARK
CT
DNAT
DNPT
DSCP
ECN
HL
HMARK
IDLETIMER
LED
LOG
MARK
MASQUERADE
NETMAP
NFLOG
NFQUEUE
NOTRACK
RATEEST
REDIRECT
REJECT
REJECT
SECMARK
SET
SNAT
SNPT
SYNPROXY
TCPMSS
TCPOPTSTRIP
TEE
TOS
TPROXY
TRACE
TTL
ULOG

例子

# 禁止訪問 8.8.8.8
ming@ubuntu:~$ ping 8.8.8.8
PING 8.8.8.8 (8.8.8.8) 56(84) bytes of data.
64 bytes from 8.8.8.8: icmp_seq=1 ttl=128 time=17.1 ms
64 bytes from 8.8.8.8: icmp_seq=2 ttl=128 time=17.0 ms
^C
--- 8.8.8.8 ping statistics ---
2 packets transmitted, 2 received, 0% packet loss, time 1002ms
rtt min/avg/max/mdev = 17.084/17.135/17.186/0.051 ms

root@ubuntu:/home/ming# iptables -A OUTPUT -d 8.8.8.8 -j DROP

root@ubuntu:/home/ming# ping 8.8.8.8
PING 8.8.8.8 (8.8.8.8) 56(84) bytes of data.
ping: sendmsg: Operation not permitted
ping: sendmsg: Operation not permitted
^C
--- 8.8.8.8 ping statistics ---
2 packets transmitted, 0 received, 100% packet loss, time 1005ms
# 禁止 HTTP 
root@ubuntu:/home/ming# wget http://www.baidu.com/
--2019-12-25 03:42:20--  http://www.baidu.com/
Resolving www.baidu.com (www.baidu.com)... 182.61.200.6, 182.61.200.7
Connecting to www.baidu.com (www.baidu.com)|182.61.200.6|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 2381 (2.3K) [text/html]
Saving to: ‘index.html’

index.html         100%[================>]   2.33K  --.-KB/s    in 0s      

2019-12-25 03:42:20 (167 MB/s) - ‘index.html’ saved [2381/2381]

root@ubuntu:/home/ming# iptables -A OUTPUT -p tcp --dport 80 -j DROP

root@ubuntu:/home/ming# wget http://www.baidu.com/
--2019-12-25 03:42:32--  http://www.baidu.com/
Resolving www.baidu.com (www.baidu.com)... 182.61.200.6, 182.61.200.7
Connecting to www.baidu.com (www.baidu.com)|182.61.200.6|:80... 
^C
發佈了118 篇原創文章 · 獲贊 18 · 訪問量 6萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章