gpt4 book ai didi

python - 使用 python scapy 发送 DHCP 发现

转载 作者:太空狗 更新时间:2023-10-29 21:39:53 24 4
gpt4 key购买 nike

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

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()

还有其他方法可以实现吗?如果是这样,请也提及它们。提前致谢。

最佳答案

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

我认为问题出在 srp() 函数上,它无法在端口 68 上接收数据包,但我创建了一个新函数,其中包含一个新线程,用于嗅探 BOOTP 消息并显示数据包字段。 你可以模拟它:

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

然后使用 srp() 或 sendp() 函数发送数据包 :)

注意:我使用了多线程机制导致我的程序在网络上存在流氓 DHCP 服务器时发送消息和嗅探

关于python - 使用 python scapy 发送 DHCP 发现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25124500/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com