【发布时间】:2015-06-28 14:36:02
【问题描述】:
我有一个充当代理的小型 python 脚本。除了 DNS 请求外,脚本似乎一切正常。当我的脚本收到 DNS 请求时,我会执行请求,然后将响应转发回发出 DNS 请求的原始用户。但是,当发起 DNS 请求的人收到响应时,它被认为是格式错误的。我知道旧版本的 scapy 存在 DNS 问题,所以我更新到 scapy 2.3.1 但仍然有问题。
#!/usr/bin/env python
from tornado.websocket import WebSocketHandler
from tornado.httpserver import HTTPServer
from tornado.web import Application
from tornado.ioloop import IOLoop
from collections import defaultdict
from scapy.all import *
import threading
# Warning: Not thread-safe.
# Dictionary mapping (outbound.dst, outbound.dport) -> count of IP packets awaiting reply
outbound_packets = defaultdict(int)
outbound_udp = defaultdict(int)
connection = None
class PacketSniffer(threading.Thread):
def __init__(self):
threading.Thread.__init__(self)
def run(self):
global connection
while (True):
pkt = sniff(iface="eth0", count=1)
if pkt[0].haslayer(IP):
pkt = pkt[0][IP]
if outbound_packets[(pkt.src, pkt.sport)] > 0:
outbound_packets[(pkt.src, pkt.sport)] -= 1
if pkt[0].haslayer(UDP):
# Modify the destination address back to the address of the TUN on the host.
pkt.dst = "10.0.0.1"
try:
del pkt[UDP].chksum
del pkt[IP].chksum
pkt.show2() # Force recompute the checksum
except IndexError:
print "error deleting"
if connection:
connection.write_message(str(pkt).encode('base64'))
elif pkt[0].haslayer(TCP):
print "TCP packet"
# Modify the destination address back to the address of the TUN on the host.
pkt.dst = "10.0.0.1"
try:
del pkt[TCP].chksum
del pkt[IP].chksum
pkt.show2() # Force recompute the checksum
except IndexError:
print "error deleting"
if connection:
connection.write_message(str(pkt).encode('base64'))
我不是 DNS 专家,但据我所知,响应有 Answer RRs: 2,但查看实际的 DNS 答案,我只看到 1 个条目。假设 Answer RRs 值应该与实际答案的数量相匹配是否安全?如果是这种情况,您知道如何/为什么从 DNS 条目中删除答案吗?
【问题讨论】:
标签: python networking proxy dns scapy