如何正確記錄和查看 nftables 活動?
在 Debian 10 buster 上,我遇到了 docker 容器無法 ping docker 主機甚至 docker bridge 介面但能夠訪問網際網路的問題。
允許在此處的相關問題中訪問,在我的情況下並不能解決它。似乎與 iptables/nftables 相關,如果我能先弄清楚如何記錄錯誤,我可能會弄清楚該怎麼做。
我在
DOCKER-USER
和中都輸入了日誌規則INPUT
,nft insert rule ip filter DOCKER-USER counter log
但它們都顯示記錄了 0 個數據包。
/var/log/kern.log
不顯示任何防火牆相關資訊,journalctl -k
.使用該系統查看防火牆活動的新方法如何
nftables
?nft list ip table filter table ip filter { chain INPUT { type filter hook input priority 0; policy drop; ct state invalid counter packets 80 bytes 3200 drop iifname "vif*" meta l4proto udp udp dport 68 counter packets 0 bytes 0 drop ct state related,established counter packets 9479197 bytes 17035404271 accept iifname "vif*" meta l4proto icmp counter packets 0 bytes 0 accept iifname "lo" counter packets 9167 bytes 477120 accept iifname "vif*" counter packets 0 bytes 0 reject with icmp type host-prohibited counter packets 28575 bytes 1717278 drop counter packets 0 bytes 0 log counter packets 0 bytes 0 log iifname "docker0" counter packets 0 bytes 0 accept } chain FORWARD { type filter hook forward priority 0; policy drop; counter packets 880249 bytes 851779418 jump DOCKER-ISOLATION-STAGE-1 oifname "br-cc7b89b40bee" ct state related,established counter packets 7586 bytes 14719677 accept oifname "br-cc7b89b40bee" counter packets 0 bytes 0 jump DOCKER iifname "br-cc7b89b40bee" oifname != "br-cc7b89b40bee" counter packets 5312 bytes 2458488 accept iifname "br-cc7b89b40bee" oifname "br-cc7b89b40bee" counter packets 0 bytes 0 accept oifname "br-d41d1510d330" ct state related,established counter packets 8330 bytes 7303256 accept oifname "br-d41d1510d330" counter packets 0 bytes 0 jump DOCKER iifname "br-d41d1510d330" oifname != "br-d41d1510d330" counter packets 7750 bytes 7569465 accept iifname "br-d41d1510d330" oifname "br-d41d1510d330" counter packets 0 bytes 0 accept oifname "br-79fccb9a0478" ct state related,established counter packets 11828 bytes 474832 accept oifname "br-79fccb9a0478" counter packets 11796 bytes 707760 jump DOCKER iifname "br-79fccb9a0478" oifname != "br-79fccb9a0478" counter packets 7 bytes 526 accept iifname "br-79fccb9a0478" oifname "br-79fccb9a0478" counter packets 11796 bytes 707760 accept counter packets 1756295 bytes 1727495359 jump DOCKER-USER oifname "docker0" ct state related,established counter packets 1010328 bytes 1597833795 accept oifname "docker0" counter packets 0 bytes 0 jump DOCKER iifname "docker0" oifname != "docker0" counter packets 284235 bytes 16037499 accept iifname "docker0" oifname "docker0" counter packets 0 bytes 0 accept ct state invalid counter packets 0 bytes 0 drop ct state related,established counter packets 0 bytes 0 accept counter packets 0 bytes 0 jump QBS-FORWARD iifname "vif*" oifname "vif*" counter packets 0 bytes 0 drop iifname "vif*" counter packets 0 bytes 0 accept counter packets 0 bytes 0 drop } chain OUTPUT { type filter hook output priority 0; policy accept; } chain QBS-FORWARD { } chain DOCKER { } chain DOCKER-ISOLATION-STAGE-1 { iifname "br-cc7b89b40bee" oifname != "br-cc7b89b40bee" counter packets 5312 bytes 2458488 jump DOCKER-ISOLATION-STAGE-2 iifname "br-d41d1510d330" oifname != "br-d41d1510d330" counter packets 7750 bytes 7569465 jump DOCKER-ISOLATION-STAGE-2 iifname "br-79fccb9a0478" oifname != "br-79fccb9a0478" counter packets 7 bytes 526 jump DOCKER-ISOLATION-STAGE-2 iifname "docker0" oifname != "docker0" counter packets 590138 bytes 34612496 jump DOCKER-ISOLATION-STAGE-2 counter packets 1808904 bytes 1760729363 return } chain DOCKER-ISOLATION-STAGE-2 { oifname "br-cc7b89b40bee" counter packets 0 bytes 0 drop oifname "br-d41d1510d330" counter packets 0 bytes 0 drop oifname "br-79fccb9a0478" counter packets 0 bytes 0 drop oifname "docker0" counter packets 0 bytes 0 drop counter packets 644929 bytes 74784737 return } chain DOCKER-USER { counter packets 0 bytes 0 log iifname "docker0" counter packets 305903 bytes 18574997 accept counter packets 1450392 bytes 1708920362 return } }
您可以使用
nftrace
跟踪數據包流。它非常冗長,但不會進入核心日誌,而是通過多播網路連結套接字分發(即,如果沒有人監聽它們,則跟踪只會轉到“/dev/null”)。如果您真的想跟踪所有內容,請以低優先級從預路由和輸出進行跟踪。最好使用單獨的表,因為您顯示
nft list ip table filter
的實際上是iptables-over-nftables與兼容性 xt 匹配層 API,不應被篡改(但可以安全地用於跟踪)。您還應該知道 iptables 可能還有其他表,例如nat表。因此,使用
traceall.nft
載入的文件中的規則集nft -f traceall.nft
:table ip traceall delete table ip traceall table ip traceall { chain prerouting { type filter hook prerouting priority -350; policy accept; meta nftrace set 1 } chain output { type filter hook output priority -350; policy accept; meta nftrace set 1 } }
您現在可以使用以下(非常詳細的)IPv4 跟踪:
nft monitor trace
如果在容器內執行此操作,這甚至會起作用(日誌目標通常不是這種情況)。
您可以在其他地方啟動這些跟踪,或者在啟動它們之前設置條件,也可以
meta nftrace set 0
在稍後優先級的規則中再次停用它們()以避免跟踪所有鉤子/鏈。遵循此示意圖將有助於理解事件的順序:Netfilter 和 General Networking 中的數據包流。如果選擇在iptables
-j TRACE
中使用等效的目標,還請諮詢 man for ,因為iptables-over-nftables改變了它的行為(與iptables-legacy相比)。xtables-monitor
雖然我回答了 OP 的問題,但以下是關於問題和日誌問題的瘋狂猜測:
- 如果 Docker 本身在容器中執行,則日誌可能不可用。它們可以提供給主機,以及所有允許查詢核心消息的容器
sysctl -w net.netfilter.nf_log_all_netns=1
,使用,僅僅是因為核心消息沒有命名空間實例。- ip filter INPUT中**日誌規則的計數器為零,而前一個帶有drop語句的規則的計數器不是。這意味著日誌規則制定得太晚了:在drop之後。日誌規則(或者更確切地說是iptables 的*)應該插入到最後的drop語句**之前*,而不是附加在它永遠不會到達的地方之後。
-j LOG
- 關於 Docker 的唯一 INPUT 規則是
iifname "docker0" counter packets 0 bytes 0 accept
. 如果容器不在預設的 Docker 網路上,則沒有規則允許它們到達主機。嘗試添加一個規則來測試它。確保在刪除規則之前插入結果。使用iptables,避免使用 nftables 添加可能與iptables-over-nftables不兼容的規則:
iptables -I INPUT 8 -i "br-*" -j ACCEPT