tproxy_Linux TPROXY

利用iptables TPROXY target,可以在skb进入到协议栈之前,将skb关联到一个本地监听的socket,并且设置skb的fwmark。可以利用fwmark配置高级路由功能将非本地流量送到本地lo interface,从而能进入本地协议栈的处理。skb随后通过ip_rcv进入本地协议栈处理后,可以直接利用已关联的socket进行处理,而不需像普通的处理流程那样,使用skb中的tcp 4-tuples来查找listening socket或者established socket。这样可以把去往任意目的IP和目的端口的skb,关联到本地代理程序监听的socket上。本地代理程序通过accept返回的socket中包含的local address和local port仍然为原始连接的信息。关联到哪个本地监听的socket,可以通过on-port和on-ip option来指定,并且本地监听的socket需要设置IP_TRANSPARENT option,否则TPROXY target会当做找不到指定的本地socket,将skb丢弃。

使用TPROXY的三个步骤:

配置代理程序,确定程序监听的IP地址和端口,确保监听socket已打开IP_TRANSPARENT选项。这里程序监听的IP地址可以是

A:0.0.0.0

B: 127.0.0.1

C: 任意本地接口的IP地址

D: 其它IP地址

选择使用哪种IP来监听,都是可以的,只要TPROXY target配置中的on-ip配置匹配就好了。

配置iptables规则,将需要透明代理的流量使用TPROXY target。使用on-port,on-ip参数,指定透明代理监听的socket;使用--tproxy-mark设置使用透明代理流量的fwmark。

配置高级路由,将使用透明代理的流量送到本地lo接口。

FAQ

Question1:已匹配iptables规则,使用TPROXY target的报文,还会继续匹配后续规则么?

Answer1:不会。TPROXY target返回NF_DROP(报文不能匹配找到本地已设置IP_TRANSPARENT option的established或者listening socket)或者NF_ACCEPT(报文可以匹配找到本地已设置IP_TRANSPARENT option的established或者listening socket)

Question2:TPROXY target对已建立连接的报文生效么?

Answer2:生效。TPROXY target checks whether there is an established socket for the incoming skb (using the tcp 4-tuples in the skb to search the established sockets), if there is a such socket and the socket has set IP_TRANSPARENT, it sets the fwmark for the skb and associates the skb with the socket and return NF_ACCEPT.  If no established socket found, it looks for listening socket using laddr and lport. It checks the configured ip address with --on-ip option, the dev ip, and the destination ip of the skb one by one, and the first non-zero one will be used as the laddr. The port configured with --on-port option will be used as lport, if not configured, the destination port of the skb is used as lport. If a listening socket is matched, and the socket has enabled IP_TRANSPARENT, it sets the mark/mask for the skb and associates the skb with the socket and returns NF_ACCEPT. Otherwise it returns NF_DROP.

Question3:既然TPROXY target对已经建立连接的报文也生效,那么Linux kernel文档https://www.kernel.org/doc/Documentation/networking/tproxy.txt中,下面这段配置是必须的么?

# iptables -t mangle -N DIVERT

# iptables -t mangle -A PREROUTING -p tcp -m socket -j DIVERT

# iptables -t mangle -A DIVERT -j MARK --set-mark 1

# iptables -t mangle -A DIVERT -j ACCEPT

Answer3:不是必须的,但加些配置性能稍微会提高些。对于可以匹配本地Established的报文,设置fwmark,配合高级路由配置确保这些报文在路由结束时可以送到本地的lo接口。这些报文不会再经过TPROXY target处理。(MARK target性能会略高于TPROXY target)

另外这个配置在调试过程中也会更安全一些,对于报文属于某个已经建立的连接(包括到本地的连接,如ssh)将直接accept,不会走到TPROXY的规则。比如通过ssh登录到一台机器增加规则使用TPROXY target,但是配置时忘记exclude本地流量,这样包括ssh到这台机器的报文也将使用TPROXY target,而TPROXY target发现ssh的报文能匹配上本地established的socket,但是该socket未设置IP_TRANSPARENT,则会返回NF_DROP,导致ssh连接断开。

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值