dhcp_server.py revision 0b237f3c
1from scapy.all import *
2from scapy.layers import *
3
4server_ip="6.0.2.2"
5client_ip="6.0.1.2"
6server_mac="00:0B:CD:AE:9F:C6"
7client_mac="aa:a2:a5:ea:54:20"
8subnet_mask="255.255.255.0"
9gateway="6.0.1.1"
10
11# suboption 1 Agent circuit ID; len:4; val:0x00000001
12# suboption 5 Link selection; len:4; val:6.0.1.1
13option82 = '\x01\x04\x00\x00\x00\x01\x05\x04\x06\00\x01\x01'
14
15def detect_dhcp(pkt):
16  # check if we get DHCP discover and send offer message
17  if pkt[DHCP] and pkt[DHCP].options[0][1] == 1:
18    sendp(Ether(src=server_mac,dst="ff:ff:ff:ff:ff:ff")/
19          IP(src=server_ip,dst="6.0.1.1")/
20          UDP(sport=67,dport=68)/
21          BOOTP(op=2, yiaddr=client_ip, siaddr=server_ip, giaddr=gateway,
22                chaddr=client_mac, xid=pkt[BOOTP].xid)/
23          DHCP(options=[('message_type', 'offer')])/
24          DHCP(options=[('subnet_mask',subnet_mask)])/
25          DHCP(options=[('server_id',server_ip)])/
26          DHCP(options=[('relay_agent_Information', option82), ('end')]))
27
28#sniff DHCP requests
29def start():
30    sniff(filter="udp and (port 67 or 68)", prn=detect_dhcp, store=0)
31
32start()
33