【问题标题】:Python/iptables: Original Destination IPPython/iptables:原始目标 IP
【发布时间】:2015-08-14 19:15:21
【问题描述】:

我正在尝试获取使用 iptables 重定向的数据包的原始目标信息(最终目标是将所有网络流量重定向到 localhost,同时保留原始目标 ip)。
我正在使用以下代码发送数据包:

import socket  
HOST = '192.168.10.1'
PORT = 50007
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((HOST, PORT))
s.send(b'whatever')
s.close()

然后重定向它:

iptables -t nat -A OUTPUT -d 192.168.10.1 -j DNAT --to 127.0.0.1

然后接收它们:

import socket
HOST = ''
PORT = 50007
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind((HOST, PORT))
while True:
    s.listen(5)
    conn, addr = s.accept()
    print('Connected by', addr)
    data = conn.recv(1024)
    if(data):
        print(data)
conn.close()

我尝试使用类似的东西

dst = conn.getsockopt(socket.SOL_IP, socket.SO_ORIGINAL_DST, 16)

但这会导致

AttributeError: 'module' object has no attribute 'SO_ORIGINAL_DST'

【问题讨论】:

    标签: python linux python-3.x iptables


    【解决方案1】:

    一些进一步的阅读和尝试导致我犯了错误。我对我读到的各种方法感到有些困惑并迷失了方向。线索在于定义 SO_ORIGINAL_DST(在本例中为 TCP)。
    这段代码(取自here)正是我想要的:

    SO_ORIGINAL_DST = 80
    sockaddr_in = conn.getsockopt(socket.SOL_IP,
                                  SO_ORIGINAL_DST, 16)
    (proto, port, a, b, c, d) = struct.unpack('!HHBBBB', sockaddr_in[:8])
    print('Original destination was: %d.%d.%d.%d:%d' % (a, b, c, d, port))
    

    【讨论】:

      猜你喜欢
      • 2015-06-17
      • 2011-09-25
      • 1970-01-01
      • 1970-01-01
      • 2011-07-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-03-29
      相关资源
      最近更新 更多