Networking 使用Squid和/或iptables通过网桥共享ip地址

Networking 使用Squid和/或iptables通过网桥共享ip地址,networking,network-programming,iptables,squid,Networking,Network Programming,Iptables,Squid,编辑了以增加清晰度,并添加了指向其他尝试解决方案的链接 我已经和另一个开发者尝试了好几天了,但是我们一无所获,网上有很多评论说没有这样的例子(包括但不完全是这样)。我们也尝试实现上描述的解决方案,但到目前为止,本地http服务器似乎没有收到预期的任何请求 我们正在尝试做什么: 位于另一个设备(微型计算机)和网络之间的设备(测试设备)上。我们希望测试设备使用微型计算机的ip地址与控制服务器通信——换句话说,我们不希望它必须有自己的ip地址,而是使用微型计算机的ip地址来执行控制命令(例如,阻止网络

编辑了以增加清晰度,并添加了指向其他尝试解决方案的链接

我已经和另一个开发者尝试了好几天了,但是我们一无所获,网上有很多评论说没有这样的例子(包括但不完全是这样)。我们也尝试实现上描述的解决方案,但到目前为止,本地http服务器似乎没有收到预期的任何请求

我们正在尝试做什么:

位于另一个设备(微型计算机)和网络之间的设备(测试设备)上。我们希望测试设备使用微型计算机的ip地址与控制服务器通信——换句话说,我们不希望它必须有自己的ip地址,而是使用微型计算机的ip地址来执行控制命令(例如,阻止网络流量、恢复网络流量)。事情是这样安排的:

Mini Computer|    |            Test Device           |    | LAN
Ethernet     |<-->|eth_minicomp<-->br0<-->eth_network|<-->| Ethernet
我一直希望使用iptables/tproxy或者Squid通过将所需的TCP/IP流量路由到lo(127.0.0.1)来处理这个问题,但似乎无法实现。我最近的一次尝试是尝试使用

sysctl net.ipv4.ip_forward=1
sysctl net.ipv4.conf.lo.rp_filter=1
iptables -t mangle -F
iptables -t mangle -X
iptables -t mangle -N DIVERT
iptables -t mangle -A DIVERT -j MARK --set-mark 0x01/0x01
iptables -t mangle -A DIVERT -j ACCEPT
iptables -t mangle -A PREROUTING -p tcp -m socket -j DIVERT
iptables -t mangle -A PREROUTING -s $CONTROLLER_IP -p tcp -j TPROXY \
    --tproxy-mark 0x1/0x1 --on-port 80
ip route flush table 100
ip rule add fwmark 1 lookup 100
ip route add local 0.0.0.0/0 dev lo table 100
TPROXY似乎至少需要net.ipv4.ip_转发集,但是,似乎没有为此类解决方案设置上的过程

和<代码> > S/<代码>,>代码> -D ,<代码>——关于端口< /代码>等。看来我可以使用中间设置中的SuID人来做这样的事情,但我看不出是怎么做的。尝试搜索或继续搜索,会返回很多我不太想问的问题


那么,我们如何将这些数据包路由到测试设备上的本地服务器进行处理呢?RTFM响应非常受欢迎,我们只是找不到令人难以置信的手册。

在团队成员使用电子表格和iptables的帮助下工作

实现这一点的最大惊喜是发现,如果使用ebtables创建以太网桥,就必须删除以太帧,以便将其提升到网络层。我们都认为DROP实际上丢弃了以太网帧,因此丢弃了TCP/IP数据包。算了吧

我们现在有了一个设备,它可以共享所连接计算机的MAC和IP地址,并且仍然可以在不中断计算机的情况下进行通信

INT_IP=169.254.1.1
SRC_IP=192.168.1.2
DST_IP=192.168.1.3
EXT_PORT=80
INT_PORT=54321

# Bring interfaces to bridge down
ip link set dev eth1 down
ip link set dev eth2 down
# Remove any ip addresses on the interfaces
ip address flush dev eth1
ip address flush dev eth2
ip address add 0.0.0.0 dev eth1
ip address add 0.0.0.0 dev eth2
# Bring interfaces back up
ip link set dev eth1 up
ip link set dev eth2 up
# Set promiscuous on the interfaces
ip link set dev eth1 promisc on
ip link set dev eth2 promisc on
# Create bridge
ip link add name br0 type bridge
ip link set dev br0 up
# Add interfaces to bridge
ip link set dev eth1 master br0
ip link set dev eth2 master br0
# Add a local private IP to the bridge
ip address add $INT_IP dev "br0"
# Allow forwarding
sysctl -w net.ipv4.ip_forward=1
# Set up ethernet bridge with ebtables.
# NOTE the drop. Completely counterintuitive.

ebtables -t broute -A BROUTING -p IPv4 --ip-source $SRC_IP \
     --ip-destination $DST_IP --ip-proto tcp --ip-dport \
     $EXT_PORT -j redirect --redirect-target DROP
ebtables -t broute -A BROUTING -p IPv4 --ip-proto tcp \
     --ip-sport $INT_PORT -j redirect --redirect-target \
     DROP

# Set up iptables to handle diverting requests that originate
# from $SRC_IP destined for $DST_IP on port $EXT_PORT and send
# them to $INT_IP and $EXT_PORT in stead where you can have a
# service / thingy to handle them.
iptables -t nat -A PREROUTING -p tcp -s $SRC_IP -d $DST_IP \
     --dport $EXT_PORT -j DNAT \
     --to-destination $INT_IP:$INT_PORT
iptables -t nat -A POSTROUTING -p tcp -d $INT_IP \
     --dport $EXT_PORT -j SNAT --to-source \
     $DST_IP:$EXT_PORT
iptables -t nat -A POSTROUTING -j MASQUERADE
现在,如果您尝试从$SRC_IP到达端口$EXT_port上的$DST_IP,它将被路由到$INT_端口上的$INT_IP。相反,如果您尝试从配置此功能的系统向$INT_端口上的$INT_IP发送数据,则所有通信量都将转到$EXT_端口上的$SRC_IP


-2因果报应!呜呼

IP表在第三层(网络之间)工作,但桥接发生在第二层(在同一网络上,流量直接从主机传送到主机)。@RonMaupin感谢您的评论。我尝试了ebtables排列,只是想把它踢下一层,但是,应该可以在代理地址是本地地址(例如127.0.0.1或具有本地路由的任何地址)的情况下设置代理,但我只找到如何使用SSL MITM解决方案(例如和)实现这一点,而它们不提供“如何”选项在某种程度上,我可以打破它。
INT_IP=169.254.1.1
SRC_IP=192.168.1.2
DST_IP=192.168.1.3
EXT_PORT=80
INT_PORT=54321

# Bring interfaces to bridge down
ip link set dev eth1 down
ip link set dev eth2 down
# Remove any ip addresses on the interfaces
ip address flush dev eth1
ip address flush dev eth2
ip address add 0.0.0.0 dev eth1
ip address add 0.0.0.0 dev eth2
# Bring interfaces back up
ip link set dev eth1 up
ip link set dev eth2 up
# Set promiscuous on the interfaces
ip link set dev eth1 promisc on
ip link set dev eth2 promisc on
# Create bridge
ip link add name br0 type bridge
ip link set dev br0 up
# Add interfaces to bridge
ip link set dev eth1 master br0
ip link set dev eth2 master br0
# Add a local private IP to the bridge
ip address add $INT_IP dev "br0"
# Allow forwarding
sysctl -w net.ipv4.ip_forward=1
# Set up ethernet bridge with ebtables.
# NOTE the drop. Completely counterintuitive.

ebtables -t broute -A BROUTING -p IPv4 --ip-source $SRC_IP \
     --ip-destination $DST_IP --ip-proto tcp --ip-dport \
     $EXT_PORT -j redirect --redirect-target DROP
ebtables -t broute -A BROUTING -p IPv4 --ip-proto tcp \
     --ip-sport $INT_PORT -j redirect --redirect-target \
     DROP

# Set up iptables to handle diverting requests that originate
# from $SRC_IP destined for $DST_IP on port $EXT_PORT and send
# them to $INT_IP and $EXT_PORT in stead where you can have a
# service / thingy to handle them.
iptables -t nat -A PREROUTING -p tcp -s $SRC_IP -d $DST_IP \
     --dport $EXT_PORT -j DNAT \
     --to-destination $INT_IP:$INT_PORT
iptables -t nat -A POSTROUTING -p tcp -d $INT_IP \
     --dport $EXT_PORT -j SNAT --to-source \
     $DST_IP:$EXT_PORT
iptables -t nat -A POSTROUTING -j MASQUERADE