python嗅探dhcp服务,使用python scapy发送DHCP Discover

这篇博客介绍了作者在学习网络编程时尝试使用Python Scapy库通过tap接口发送DHCP Discover包以获取DHCP服务器响应的过程。尽管能成功发送包,但无法收到响应。作者后来发现Scapy的srp()函数在端口68上无法接收包,因此创建了一个新的线程来嗅探BOOTP消息。解决方案是结合sniff()和sendp()函数,并利用多线程机制同时发送和捕获DHCP响应。
摘要由CSDN通过智能技术生成

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'

`scapy`是一个强大的网络协议分析和自动化工具,在Python中被广泛用于网络通信、嗅探、数据包构造等方面。如果你想用`scapy`发送ICMP(Internet Control Message Protocol)消息,特别是回显请求(ping)或回显应答,你可以按照以下步骤操作: 1. 首先,确保已经安装了`scapy`库。如果没有安装,可以通过命令行输入`pip install scapy`来安装。 2. 导入必要的模块: ```python from scapy.all import * ``` 3. 创建一个ICMP回显请求报文(`ICMP Echo Request`),通常使用`IP`和`ICMP`层组合: ```python packet = IP(dst="目标IP地址")/ICMP(type=8, code=0, id=0, seq=0) # ICMP type 8代表Echo Request ``` 这里的参数可以根据实际需求调整,比如改变目标地址和序列号。 4. 发送这个报文: ```python send(packet, verbose=0) ``` `verbose=0`表示不显示发送过程的详细信息。 如果你想发送的是回显应答(即响应别人的ping请求),则可以捕获并处理收到的ICMP Echo Reply,然后发送对应的ICMP Echo Reply: ```python # 捕获ICMP Echo Requests sniff(filter="icmp and icmp[icmptype] == 8", prn=lambda packet: send_reply(packet)) def send_reply(packet): if packet.haslayer(ICMP) and packet[ICMP].type == 0: # 类型为0的是Echo Request reply = packet.reply() send(reply) ``` 这里定义了一个`send_reply`函数,它会检查接收到的ICMP报文是否是类型为0的Echo Request,并生成相应的Echo Reply。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值