scapy.layers.dhcp

DHCP (Dynamic Host Configuration Protocol) and BOOTP

Implements: - rfc951 - BOOTSTRAP PROTOCOL (BOOTP) - rfc1542 - Clarifications and Extensions for the Bootstrap Protocol - rfc1533 - DHCP Options and BOOTP Vendor Extensions

class scapy.layers.dhcp.BOOTP(_pkt, /, *, op=1, htype=1, hlen=6, hops=0, xid=0, secs=0, flags=<Flag 0 ()>, ciaddr='0.0.0.0', yiaddr='0.0.0.0', siaddr='0.0.0.0', giaddr='0.0.0.0', chaddr=b'', sname=b'', file=b'', options=b'')[source]

Bases: Packet

aliastypes = [<class 'scapy.layers.dhcp.BOOTP'>, <class 'scapy.packet.Packet'>]
answers(other)[source]
extract_padding(s)[source]
fields_desc: ClassVar[List[Field[Any, Any] | _FieldContainer]] = [<ByteEnumField (BOOTP).op>, <ByteEnumField (BOOTP).htype>, <ByteField (BOOTP).hlen>, <ByteField (BOOTP).hops>, <XIntField (BOOTP).xid>, <ShortField (BOOTP).secs>, <FlagsField (BOOTP).flags>, <IPField (BOOTP).ciaddr>, <IPField (BOOTP).yiaddr>, <IPField (BOOTP).siaddr>, <IPField (BOOTP).giaddr>, <_BOOTP_chaddr (BOOTP).chaddr>, <StrFixedLenField (BOOTP).sname>, <StrFixedLenField (BOOTP).file>, <StrEnumField (BOOTP).options>]
guess_payload_class(payload)[source]
hashret()[source]
payload_guess: List[Tuple[Dict[str, Any], Type[Packet]]] = [({'options': b'c\x82Sc'}, <class 'scapy.layers.dhcp.DHCP'>)]
class scapy.layers.dhcp.BOOTP_am(self, pool: Net | List[str] = Net('192.168.1.128/25'), network: str = '192.168.1.0/24', gw: str = '192.168.1.1', nameserver: str | List[str] = None, domain: str | None = None, renewal_time: int = 60, lease_time: int = 1800, **kwargs)[source]

Bases: AnsweringMachine

filter: str | None = 'udp and port 68 and port 67'
function_name = 'bootpd'
is_request(req)[source]
make_reply(req)[source]
parse_options(pool: Net | List[str] = Net('192.168.1.128/25'), network: str = '192.168.1.0/24', gw: str = '192.168.1.1', nameserver: str | List[str] = None, domain: str | None = None, renewal_time: int = 60, lease_time: int = 1800, **kwargs)[source]
Parameters:
  • pool – the range of addresses to distribute. Can be a Net, a list of IPs or a string (always gives the same IP).

  • network – the subnet range

  • gw – the gateway IP (can be None)

  • nameserver – the DNS server IP (by default, same than gw). This can also be a list.

  • domain – the domain to advertise (can be None)

Other DHCP parameters can be passed as kwargs. See DHCPOptions in dhcp.py. For instance:

dhcpd(pool=Net("10.0.10.0/24"), network="10.0.0.0/8", gw="10.0.10.1",
      classless_static_routes=["1.2.3.4/32:9.8.7.6"])

Other example with different options:

dhcpd(pool=Net("10.0.10.0/24"), network="10.0.0.0/8", gw="10.0.10.1",
      nameserver=["8.8.8.8", "4.4.4.4"], domain="DOMAIN.LOCAL")
print_reply(_, reply)[source]
class scapy.layers.dhcp.ClasslessFieldListField(name: str, default: List[AnyField] | None, field: AnyField, length_from: Callable[[Packet], int] | None = None, count_from: Callable[[Packet], int] | None = None, max_count: int | None = None)[source]

Bases: FieldListField

randval()[source]
class scapy.layers.dhcp.ClasslessStaticRoutesField(name: str, default: Any, fmt: str = 'H')[source]

Bases: Field

RFC 3442 defines classless static routes as up to 9 bytes per entry:

# Code Len Destination 1 Router 1 +—–+—+—-+—–+—-+—-+—-+—-+—-+ | 121 | n | d1 | … | dN | r1 | r2 | r3 | r4 | +—–+—+—-+—–+—-+—-+—-+—-+—-+

Destination first byte contains one octet describing the width followed by all the significant octets of the subnet.

addfield(pkt, s, val)[source]
getfield(pkt, s)[source]
i2m(pkt: Packet, x: str) bytes[source]
m2i(pkt: Packet, x: bytes) str[source]
randval()[source]
class scapy.layers.dhcp.DHCP(_pkt, /, *, options=b'')[source]

Bases: Packet

aliastypes = [<class 'scapy.layers.dhcp.DHCP'>, <class 'scapy.packet.Packet'>]
fields_desc: ClassVar[List[Field[Any, Any] | _FieldContainer]] = [<DHCPOptionsField (DHCP).options>]
mysummary()[source]
class scapy.layers.dhcp.DHCPOptionsField(name: str, default: I | None, fmt: str = 'H', remain: int = 0)[source]

Bases: StrField

A field that builds and dissects DHCP options. The internal value is a list of tuples with the format [(“option_name”, <option_value>), …] Where expected names and values can be found using DHCPOptions

getfield(pkt, s)[source]
i2m(pkt, x)[source]
i2repr(pkt, x)[source]
islist = 1
m2i(pkt, x)[source]
randval()[source]
class scapy.layers.dhcp.DHCP_am(self, pool: Net | List[str] = Net('192.168.1.128/25'), network: str = '192.168.1.0/24', gw: str = '192.168.1.1', nameserver: str | List[str] = None, domain: str | None = None, renewal_time: int = 60, lease_time: int = 1800, **kwargs)[source]

Bases: BOOTP_am

function_name = 'dhcpd'
make_reply(req)[source]
class scapy.layers.dhcp.RandClasslessStaticRoutesField[source]

Bases: RandField

A RandValue for classless static routes

class scapy.layers.dhcp.RandDHCPOptions(size=None, rndstr=None)[source]

Bases: RandField

scapy.layers.dhcp.dhcp_request(hw=None, req_type='discover', server_id=None, requested_addr=None, hostname=None, iface=None, **kargs)[source]

Send a DHCP discover request and return the answer.

Usage:

>>> dhcp_request()  # send DHCP discover
>>> dhcp_request(req_type='request',
...              requested_addr='10.53.4.34')  # send DHCP request