使用python scapy发送DHCP Discover [英] Sending DHCP Discover using python scapy

查看:1248
本文介绍了使用python scapy发送DHCP Discover的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是python的新手,正在学习一些网络编程,我希望通过我的Tap接口将DHCP数据包发送到我的DHCP服务器,并期望从中获得一些响应.我尝试了几种数据包构建技术,例如structs和ctypes,最终使用了Scapy.在这里,我能够发送DHCP数据包,但无法从DHCP服务器获得任何响应(使用wireshark和tcpdump分析).我的数据包看起来与原始DHCP数据包相同,但未能获得响应.这是我的代码

I am new to python and learning some network programming, I wish to send an DHCP Packet through my tap interface to my DHCP server and expecting some response from it. I tried with several packet building techniques such a structs and ctypes and ended up with using scapy. Here I am able to send DHCP Packet but unable to get any response from the DHCP server(Analyzed using wireshark and tcpdump)..My packet looked like same as original DHCP packet but failed to get response. Here is my code

import socket
from scapy.all import *

def main():

 if len(sys.argv)<3:
   print " fewer arguments."
   sys.exit(1)
 else:
   tap_interface = sys.argv[1]
   src_mac_address = sys.argv[2]

 ethernet = Ether(dst='ff:ff:ff:ff:ff:ff',src=src_mac_address,type=0x800)
 ip = IP(src ='0.0.0.0',dst='255.255.255.255')
 udp =UDP (sport=68,dport=67)
 fam,hw = get_if_raw_hwaddr(tap_interface)
 bootp = BOOTP(chaddr = hw, ciaddr = '0.0.0.0',xid =  0x01020304,flags= 1)
 dhcp = DHCP(options=[("message-type","discover"),"end"])
 packet = ethernet / ip / udp / bootp / dhcp

 fd = open('/dev/net/tun','r+')
 TUNSETIFF = 0x400454ca
 IFF_TAP = 0x0002
 IFF_NO_PI = 0x1000
 mode = IFF_TAP | IFF_NO_PI
 ifr = struct.pack('16sH', tap_interface, IFF_TAP | IFF_NO_PI)
 fcntl.ioctl(fd,TUNSETIFF,ifr)


 while True:
    sendp(packet, iface = tap_interface)
    time.sleep(10)


 if __name__ == '__main__':
     main()

还有其他方法可以实现这一目标吗?如果是这样,请也提及它们. 预先感谢.

Is there any other ways of achieving this? If so please do mention them as well. Thanks in Advance.

推荐答案

已解决!我遇到了同样的问题,

Solved ! I had the same problem,

我认为问题出在srp()函数上,它无法在端口68上接收数据包,但是我创建了一个具有新线程的新函数,该线程可以嗅探BOOTP消息并显示数据包字段. 您可以模拟它:

嗅探(iface = myiface,filter =端口68和端口67")

sniff(iface=myiface, filter="port 68 and port 67")

然后使用srp()或sendp()func :)

then send the packet using srp() or sendp() func :)

注意: 我使用了多线程机制,如果流氓DHCP服务器在网络上,我的程序会发送消息并进行嗅探

NOTE: I have used multithreading mechanism cause my program sends messages and sniffs if a rogue DHCP Server is on the network

这篇关于使用python scapy发送DHCP Discover的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆