这就是我想要做的:
我发送 DNS请求(使用dig或其他方法),然后在Python中使用 socket 进行拦截。然后,我想将此相同的请求发送到网关,并等待网关的响应。
import socket
#I redirected all DNS requests from port 53 to port 2525 so I am listening here
port = 2525
ip = '127.0.0.1'
sock = socket.socket(socket.AF_INET,socket.SOCK_DGRAM)
sock.bind((ip,port))
#Gateway part
gateway = '192.168.1.1'
port_gateway = 9090
sock_gateway = socket.socket(socket.AF_INET,socket.SOCK_DGRAM)
sock_gateway.connect((gateway,port_gateway))
while 1:
data,addr = sock.recvfrom(512)
print(data)
#send the request from User to the gateway :
sock_gateway.sendto(data,(gateway,53))
reply = sock_gateway.recv(512)
print(reply)
以下是输出:b'7\xa3\x01 \x00\x01\x00\x00\x00\x00\x00\x01\x06google\x03com\x00\x00\x01\x00\x01\x00\x00)\x10\x00\x00\x00\x00\x00\x00\x0c\x00\n\x00\x08\x9b+\xc1\xfa\xf4\xbc\x1c\x14'
这是我发出的DNS请求中的数据,但是,我想将其转发到网关,并侦听响应,但是没有任何响应。
我该怎么做?