英文:
How to add domain name in DHCP offer packet with scapy?
问题
我用Scapy在Python中编写了一个DHCP服务器代码,可以嗅探发现消息并向客户端发送一个带有IP地址的适当提供消息。我尝试在数据包中插入关于DNS服务器IP的另一个选项,但无论如何,在数据包中的DHCP选项中都不显示。有人能帮助吗?
DHCP服务器代码
from time import sleep
from scapy.all import *
from scapy.layers.dhcp import BOOTP, DHCP
from scapy.layers.inet import UDP, IP
from scapy.layers.l2 import Ether, ARP
import random
requested_ips = {}
assigned_ips = {}
domain_ip = ""
# 检查IP地址是否已被使用的函数
def ip_in_use(ip):
arp_request = Ether(dst="ff:ff:ff:ff:ff:ff")/ARP(op="who-has", pdst=ip)
arp_response = srp1(arp_request, timeout=1, verbose=0)
if arp_response is not None:
return True
else:
return False
# 定义处理DHCP请求的函数
def handle_dhcp_request(pkt):
if DHCP in pkt and pkt[DHCP].options[0][1] == 1:
print("收到DHCP Discover消息")
client_mac = pkt[Ether].src
# 如果没有特定的IP地址请求,生成一个随机IP地址
random_ip = "192.168.1." + str(random.randint(2, 254))
# 检查请求的IP地址是否已在网络上使用
while ip_in_use(random_ip):
print(f"请求的IP {random_ip} 已被使用,提供随机IP")
random_ip = "192.168.1." + str(random.randint(2, 254))
# 检查DHCP服务器是否已分配IP地址
while assigned_ips and random_ip in assigned_ips.values():
random_ip = "192.168.1." + str(random.randint(2, 254))
requested_ips[client_mac] = random_ip
offer = Ether(src=get_if_hwaddr(conf.iface), dst=client_mac)/ \
IP(src="192.168.1.1", dst=random_ip)/ \
UDP(sport=67, dport=68)/ \
BOOTP(op=2, yiaddr=random_ip, siaddr="192.168.1.1", giaddr="0.0.0.0", xid=pkt[BOOTP].xid)/ \
DHCP(options=[("message-type", "offer"),
("subnet_mask", "255.255.255.0"),
("router", "192.168.1.1"),
("lease_time", 86400),
"end"])
# 添加DNS服务器IP到选项
offer[DHCP].options.insert(4, ("domain_name_server", domain_ip))
# 发送数据包
print(offer[DHCP])
sleep(1)
sendp(offer, iface=conf.iface)
print(f"发送DHCP Offer消息,IP地址为:{random_ip}")
# 为DNS服务器创建一个IP地址
domain_ip = "192.168.1." + str(random.randint(2, 254))
# 设置用于DHCP请求的嗅探过滤器
sniff_filter = "udp and (port 67 or 68)"
# 开始嗅探DHCP请求
print("DHCP服务器已启动。")
print(f"域名IP地址:{domain_ip}")
sniff(filter=sniff_filter, prn=handle_dhcp_request)
我尝试使用"offer[DHCP].options.insert(4, ("domain_name_server", domain_ip))"或在options中添加("domain_name_server", domain_ip),但都没有成功。然后我尝试执行"print(offer[DHCP])",但其中没有表示域名的字节。
英文:
I have written a DHCP server code in python using Scapy that can sniff discovery messages and send an appropriate offer message with an IP for a client. I tried to insert another option in the packet regarding the DNS server IP, but it doesn't show up no matter what in the DHCP options in the packet. Can someone help?
DHCP Server Code
from time import sleep
from scapy.all import *
from scapy.layers.dhcp import BOOTP, DHCP
from scapy.layers.inet import UDP, IP
from scapy.layers.l2 import Ether, ARP
import random
requested_ips = {}
assigned_ips = {}
domain_ip = ""
# Function to check if an IP address is in use
def ip_in_use(ip):
arp_request = Ether(dst="ff:ff:ff:ff:ff:ff")/ARP(op="who-has", pdst=ip)
arp_response = srp1(arp_request, timeout=1, verbose=0)
if arp_response is not None:
return True
else:
return False
# Define a function to handle DHCP requests
def handle_dhcp_request(pkt):
if DHCP in pkt and pkt[DHCP].options[0][1] == 1:
print("DHCP Discover received")
client_mac = pkt[Ether].src
# Generate a random IP address if no specific IP was requested
random_ip = "192.168.1." + str(random.randint(2, 254))
# Check if the requested IP address is already in use on network
while ip_in_use(random_ip):
print(f"Requested IP {random_ip} is already in use, offering random IP")
random_ip = "192.168.1." + str(random.randint(2, 254))
# Check if the IP address was already assigned by the DHCP server
while assigned_ips and random_ip in assigned_ips.values():
random_ip = "192.168.1." + str(random.randint(2, 254))
requested_ips[client_mac] = random_ip
offer = Ether(src=get_if_hwaddr(conf.iface), dst=client_mac)/ \
IP(src="192.168.1.1", dst=random_ip)/ \
UDP(sport=67, dport=68)/ \
BOOTP(op=2, yiaddr=random_ip, siaddr="192.168.1.1", giaddr="0.0.0.0", xid=pkt[BOOTP].xid)/ \
DHCP(options=[("message-type", "offer"),
("subnet_mask", "255.255.255.0"),
("router", "192.168.1.1"),
("lease_time", 86400),
"end"])
# Add DNS server IP to options
offer[DHCP].options.insert(4, ("domain_name_server", domain_ip))
# Send packet
print(offer[DHCP])
sleep(1)
sendp(offer, iface=conf.iface)
print(f"DHCP Offer sent with IP: {random_ip}")
# Create an IP address for the DNS server
domain_ip = "192.168.1." + str(random.randint(2, 254))
# Set up a sniffing filter for DHCP requests
sniff_filter = "udp and (port 67 or 68)"
# Start sniffing for DHCP requests
print("DHCP Server started.")
print(f"Domain IP: {domain_ip}")
sniff(filter=sniff_filter, prn=handle_dhcp_request)
I tried using "offer[DHCP].options.insert(4, ("domain_name_server", domain_ip))" or add ("domain_name_server", domain_ip) in the (options=[("message-type", "offer"), ("subnet_mask", "255.255.255.0"),("router", "192.168.1.1"),("lease_time", 86400), "end"]) but it didn't. I right after try to do "print(offer[DHCP])" and there are no bytes representing the domain name...
答案1
得分: 0
根据Scapy DHCP模块(BOOTP类),参数名为name_server
,而不是domain_name_server
。
英文:
According to the Scapy DHCP module (BOOTP Class),
the parameter name is name_server
,
and not domain_name_server
.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论