Skip to content

Commit a982da5

Browse files
committed
DHCPAM: Make_reply to use raw to inject IP.
1 parent 92925da commit a982da5

File tree

1 file changed

+23
-1
lines changed

1 file changed

+23
-1
lines changed

scapy/layers/dhcp.py

+23-1
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
StrField,
4040
StrFixedLenField,
4141
XIntField,
42+
RawVal
4243
)
4344
from scapy.layers.inet import UDP, IP
4445
from scapy.layers.l2 import Ether, HARDWARE_TYPES
@@ -669,6 +670,24 @@ def make_reply(self, req):
669670
class DHCP_am(BOOTP_am):
670671
function_name = "dhcpd"
671672

673+
def ip_to_bytes(self, ip_string):
674+
"""Concat a IP str of form IP,IP,IP and returns it as bytes
675+
Backcompatible if IP is a single IP.
676+
:param ip_string: String of the IP to be packed"""
677+
ip_string = ip_string.replace(" ", "")
678+
679+
# Split IPs by commas and filter out empty strings
680+
ip_list = [ip.strip() for ip in ip_string.split(',') if ip.strip()]
681+
682+
# Convert each IP to packed format
683+
packed_ips = []
684+
for ip in ip_list:
685+
packed_ips.append(socket.inet_aton(ip))
686+
687+
# Concatenate packed IPs into a single byte string
688+
return b''.join(packed_ips)
689+
690+
672691
def make_reply(self, req):
673692
resp = BOOTP_am.make_reply(self, req)
674693
if DHCP in req:
@@ -677,12 +696,15 @@ def make_reply(self, req):
677696
for op in req[DHCP].options
678697
if isinstance(op, tuple) and op[0] == "message-type"
679698
]
699+
700+
nameserver_list = self.ip_to_bytes(self.nameserver)
701+
680702
dhcp_options += [
681703
x for x in [
682704
("server_id", self.gw),
683705
("domain", self.domain),
684706
("router", self.gw),
685-
("name_server", self.nameserver),
707+
("name_server", IP(len = RawVal(nameserver_list))),
686708
("broadcast_address", self.broadcast),
687709
("subnet_mask", self.netmask),
688710
("renewal_time", self.renewal_time),

0 commit comments

Comments
 (0)