Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sockets/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 正在使用scapy将DNS标头作为原始数据发送_Python_Sockets_Dns_Scapy - Fatal编程技术网

Python 正在使用scapy将DNS标头作为原始数据发送

Python 正在使用scapy将DNS标头作为原始数据发送,python,sockets,dns,scapy,Python,Sockets,Dns,Scapy,我正在尝试使用python在linux机器上设置一个套接字服务器,作为我的网络团队的一个附带项目。 机器将侦听端口53,并使用scapy响应dns查询 现在,询问部分工作得很好,至少我认为,当我将服务器设置为我的dns服务器并尝试使用internet时,我还没有真正达到这一部分,但当我尝试tcpdump时,它看起来像一个正常的数据包 无论如何,当我从服务器响应到客户机时,数据包将与我需要的所有头一起发送,包括DNS、DNSQR和DNSRR,正如我在tcpdump中看到的那样。 但是,当客户机接收

我正在尝试使用python在linux机器上设置一个套接字服务器,作为我的网络团队的一个附带项目。 机器将侦听端口53,并使用scapy响应dns查询

现在,询问部分工作得很好,至少我认为,当我将服务器设置为我的dns服务器并尝试使用internet时,我还没有真正达到这一部分,但当我尝试tcpdump时,它看起来像一个正常的数据包

无论如何,当我从服务器响应到客户机时,数据包将与我需要的所有头一起发送,包括DNS、DNSQR和DNSRR,正如我在tcpdump中看到的那样。 但是,当客户机接收到数据包时,它只有IP和UDP报头,其余的作为原始数据发送

正如我前面提到的,我试着查看wireshark和tcpdump,看到了服务器和客户端之间发送的差异,并且从服务器端看到,应答数据包被标记为
格式错误的数据包

我不完全确定这是不是真的,因为我在网上看到,这只是因为我没有完全欺骗它

代码:

客户端数据包:

pkt = IP(dst='<Server IP>')/UDP(dport=53,sport=RandShort())/DNS(rd=1,qd=DNSQR(qname='dns.google'))
pkt=IP(dst='')/UDP(dport=53,sport=RandShort())/DNS(rd=1,qd=DNSQR(qname='DNS.google'))
服务器数据包:

def build_dns_response(src_addr,og_sport,dns_qtype,dns_qname,response_type=True,response_name=None):
    '''
    a function that will build a DNS response packet back to the sender, according to the parameters that are recieved.
    dns_qname = url/ip that the sender asked for.
    og_sport = original source port, will be used as the dport of this packet
    src_addr = source address of the original packet, will be used as the destination of this packet.
    dns_qtype = the question type of the original request, 1 stands for type A, and 12 for PTR
    response_type = will indicate if the server responds with an answer of 'ok' or 'name-error' on the 'rcode' field,True for ok,False for name-error
    response_name = the response that the server will send as a result of the dns request, in case that a request matches to a database query.
    '''
    if response_type is True:
        pkt = IP(src='<Server IP>',dst=src_addr)/\
                                UDP(sport=53,dport=og_sport)/\
                                DNS(opcode=0,rd=1,rcode='ok',nscount=1,ancount=1,qd=DNSQR(qname=dns_qname,qtype=dns_qtype),an=DNSRR(rrname=dns_qname,type=dns_qtype,rdata=response_name))

def build_dns_response(src_addr,og_sport,dns_qtype,dns_qname,response_type=True,response_name=None):
'''
根据接收到的参数,将DNS响应数据包构建回发送方的函数。
dns_qname=发件人请求的url/ip。
og_sport=原始源端口,将用作此数据包的dport
src_addr=原始数据包的源地址,将用作此数据包的目的地。
dns_qtype=原始请求的问题类型,1表示类型A,12表示PTR
response_type=将指示服务器是否在“rcode”字段上回答“ok”或“name error”,True表示ok,False表示name error
response_name=如果请求与数据库查询匹配,服务器将作为dns请求的结果发送的响应。
'''
如果响应类型为True:
pkt=IP(src='',dst=src_addr)/\
UDP(运动=53,dport=og_运动)/\
DNS(操作码=0,rd=1,rcode='ok',nscount=1,ancount=1,qd=DNSQR(qname=DNS\u qname,qtype=DNS\u qtype),an=DNSRR(rrname=DNS\u qname,type=DNS\u qtype,rdata=response\u name))
服务器正在发送的数据包:

###[ IP ]### 
  version   = 4
  ihl       = None
  tos       = 0x0
  len       = None
  id        = 1
  flags     = 
  frag      = 0
  ttl       = 64
  proto     = udp
  chksum    = None
  src       = <Server IP>
  dst       = <client IP >
  \options   \
###[ UDP ]### 
     sport     = domain
     dport     = 25151
     len       = None
     chksum    = None
###[ DNS ]### 
        id        = 0
        qr        = 0
        opcode    = QUERY
        aa        = 0
        tc        = 0
        rd        = 1
        ra        = 0
        z         = 0
        ad        = 0
        cd        = 0
        rcode     = ok
        qdcount   = 1
        ancount   = 1
        nscount   = 1
        arcount   = 0
        \qd        \
         |###[ DNS Question Record ]### 
         |  qname     = 'dns.google.'
         |  qtype     = A
         |  qclass    = IN
        \an        \
         |###[ DNS Resource Record ]### 
         |  rrname    = 'dns.google.'
         |  type      = A
         |  rclass    = IN
         |  ttl       = 0
         |  rdlen     = None
         |  rdata     = 8.8.8.8
        ns        = None
        ar        = None

####[IP]###
版本=4
ihl=无
tos=0x0
len=无
id=1
标志=
frag=0
ttl=64
proto=udp
chksum=无
src=
dst=
\选择权\
###[UDP]###
运动=领域
dport=25151
len=无
chksum=无
###[DNS]###
id=0
qr=0
操作码=查询
aa=0
tc=0
rd=1
ra=0
z=0
ad=0
cd=0
rcode=ok
qdcount=1
安count=1
nscount=1
arcount=0
\量子点\
|###[DNS问题记录]###
|qname='dns.google'
|qtype=A
|qclass=IN
\一个\
|###[DNS资源记录]###
|rrname='dns.google'
|类型=A
|rclass=IN
|ttl=0
|rdlen=无
|rdata=8.8.8.8
ns=无
ar=无
客户端接收的数据包:

###[ IP ]###
  version   = 4
  ihl       = 5
  tos       = 0x0
  len       = 82
  id        = 1
  flags     =
  frag      = 0
  ttl       = 64
  proto     = udp
  chksum    = 0x6686
  src       = <Server IP>
  dst       = <Client IP>
  \options   \
###[ UDP ]###
     sport     = domain
     dport     = 25151
     len       = 62
     chksum    = 0xf99
###[ Raw ]###
        load      = '\x00\x00\x01\x00\x00\x01\x00\x01\x00\x01\x00\x00\x03dns\x06google\x00\x00\x01\x00\x01\x03dns\x06google\x00\x00\x01\x00\x01\x00\x00\x00\x00\x00\x04\x08\x08\x08\x08'

###[IP]###
版本=4
国际人道主义法=5
tos=0x0
len=82
id=1
旗帜=
frag=0
ttl=64
proto=udp
chksum=0x6686
src=
dst=
\选择权\
###[UDP]###
运动=领域
dport=25151
len=62
chksum=0xf99
###[原始]###
load='\x00\x00\x01\x00\x00\x00\x01\x00\x01\x00\x01\x00\x00\x03dns\x06google\x00\x00\x00\x01\x00\x03dns\x06google\x00\x00\x01\x00\x00\x00\x00\x00\x04\x08\x08\x08'
我想值得一提的是,整个对话都是从主机PC到虚拟机的


提前感谢您的帮助

您能否为您的
数据包.show()
旁边的两个数据包提供
原始(数据包)
的输出?您裁剪了太多的信息。你如何发送这些包?您使用2级还是3级插座?格式错误的数据包可能是校验和、失败的长度字段或数据包生成的更普遍问题。确保Scapy计算校验和和和长度,确保计算正确。您是否尝试使用scapy源代码中的
DNS_am
,或从中获得灵感?查一下,这是DNS的应答机