contributed by < ZhuMon
>
linux2020
sysprog2020
final project
儘管我們可在網頁瀏覽器中透過像是 AdBlock 這類的 extension 來過濾廣告,但需要額外的設定和佔用更多系統資源,倘若我們能透過 netfilter,直接在核心層級過濾網路廣告,那所有應用程式都有機會受益。
參考資訊:
光使用 IP 無法正確阻擋廣告
若將上述 block list 的網域皆 mapping 到 localhost,是否便能阻檔廣告,並且不用使用到 netfilter?
/etc/hosts
依照下列格式將 adblock.txt 的網域 map 到 127.0.0.1
記得要重開瀏覽器,因為瀏覽器會暫存廣告
參考 https://netfilter.org/documentation/HOWTO/netfilter-hacking-HOWTO.html
hooks
,開發者可以向 hooks
定義行為,封包在通過該點時,就會進行該行為
hooks
包含「進行 routing 前」、「進行 routing 後」、「送出封包前」等等hook
,以便對封包進行操作(通過、捨棄、存入 Queue…)參考 Use the iptables firewall to block ads on your Linux machine
簡介
$ man iptables
iptables/ip6tables — administration tool for IPv4/IPv6 packet filtering and NAT
iptables 藉由 kernel 內的 netfilter 模組,在 User Space 管理網路封包的處理和轉發
Lab
/etc/ads
使用
wget
下載需要注意檔案一開始的 html,需先將其刪除
ifconfig
觀察自己的網路介面卡名字為何,因為我的名字是 eno1
,所以要將 ads
內的 eth0
換為 eno1
$ iptables-restore < /etc/ads
,將 iptables 的 rule 寫入Commands / Options | Description |
---|---|
-A, –append chain | 將 rule 加到指定 chain 的最後 |
-o, –out-interface output name[+] | 選擇封包出去的網路介面卡 |
-d, –destination address[/mask][…] | 封包的目的地 |
-j, –jump target | 此 rule 要執行的動作 |
使用的 rule 竟然是禁止往外的封包?
$ iptables-restore < /etc/ads
將 rules 放入,因此直接使用 $ sh /etc/ads
執行簡介
安裝環境
https://github.com/xdp-project/xdp-tutorial/blob/master/setup_dependencies.org
basic01-xdp-pass
https://github.com/xdp-project/xdp-tutorial/tree/master/basic01-xdp-pass
Compile
make
應該會過可以使用 llvm-objdump 看一下編譯後的組合語言
使用 ip
command load 我們的 BPF-program
Permission denied,加上 sudo
試試
使用 ip link show
查看有沒有成功
刪除