如何使用 scapy 在 DHCP offer 数据包中添加域名?

问题描述 投票:0回答:1

我使用 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 = ""

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

我尝试使用“offer[DHCP].options.insert(4, ("domain_name_server", domain_ip))" 或在 (options=[("message-type", "offer" ), ("subnet_mask", "255.255.255.0"),("router", "192.168.1.1"),("lease_time", 86400), "end"]) 但它没有。我在尝试执行“print(offer[DHCP])”后立即发现没有代表域名的字节...

python networking scapy dhcp
1个回答
0
投票

根据Scapy DHCP模块(BOOTP类), 参数名称是

name_server
, 而不是
domain_name_server

© www.soinside.com 2019 - 2024. All rights reserved.