將流量從網橋重新導向到 http 代理

將流量從網橋重新導向到 http 代理

如果我創建乙太網路橋接器的新實例:

# brctl addbr br1
# ip link set dev br1 up
# ip addr add 10.100.100.1/24 dev br1

開始tinyproxy監聽localhost其預設連接埠 8888:

# tinyproxy

建立firejail一個新的網路命名空間並將其連接到網橋:

# firejail --net=br1 /bin/bash

然後,我如何將流量透過網橋路由到tinyproxy,以便curl從沙箱內取得網頁firejail

# curl --proxy http://10.100.100.1:8888 http://wtfismyip.com/text

答案1

以下命令對於刷新/刪除鍊和停用很有用ufw

# /lib/ufw/ufw-init flush-all

建立乙太網路橋接器:

ext_if="enp8s8"
bridge="brtp8888"
proxy_port="8888"  # tinyproxy default

brctl addbr "${bridge}"
ip link set dev "${bridge}" up
ip addr add 10.100.100.1/24 dev "${bridge}"
# Allow the bridge to route traffic to localhost
sysctl net.ipv4.conf."${bridge}".route_localnet=1

將定向至網橋連接埠 8888 的 TCP 流量路由至tinyproxy

iptables -t nat -A PREROUTING -i "${bridge}" -p tcp -j DNAT --to-destination 127.0.0.1:"${proxy_port}"
iptables -t nat -A POSTROUTING -s 10.100.100.0/24 -o eth0 -j MASQUERADE

(註:以上內容改編自使用 Tor HOWTO 進行 Firejail.)

Tinyproxy 限制連接,localhost除非有設定行,否則編輯/etc/tinyproxy.conf

Allow 10.100.100.0/24

一套比較完整的iptables規則:

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

iptables -A INPUT -i lo -j ACCEPT
iptables -A INPUT -m state --state INVALID -j DROP
iptables -A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT

iptables -A INPUT -i "${bridge}" -p tcp --dport "${proxy_port}" -j ACCEPT
iptables -t nat -A PREROUTING -i "${bridge}" -p tcp -j DNAT --to-destination 127.0.0.1:"${proxy_port}"  # tinyproxy default
iptables -t nat -A POSTROUTING -s 10.100.100.0/24 -o eth0 -j MASQUERADE

等價ufw的:

## Copy the following into /etc/ufw/before.rules (see man ufw-framework, 'Port Redirections')
# *nat
# :PREROUTING ACCEPT [0:0]
# -A PREROUTING -p tcp -i brtp8888 --dport 8888 -j DNAT \
#   --to-destination 127.0.0.1:8888
# COMMIT
# *nat
# :POSTROUTING ACCEPT [0:0]
# -A POSTROUTING -s 10.100.100.0/24 -o eth0 -j MASQUERADE
# COMMIT

ufw allow in on "${bridge}" from 10.100.100.0/24 proto tcp

另請參閱這篇文章Firejail 並透過主機 OpenVPN 用戶端連接到互聯網

如果有人可以解釋為什麼創建如上所述的橋,打開運行 Firefox 的沙箱並將--net=br1Firefox 的 HTTP 代理設置為網關 IP(即,br1任何端口)也可以工作,我很想知道。

相關內容