blob: 64ae75344558fc704f2a83ecc0fdfe9ac98aa0ed [file] [log] [blame]
Chetan Gaonkerb2bd8242016-03-03 15:39:24 -08001from scapy.all import *
2
3conf.verb = 0 # Disable Scapy verbosity
4conf.checkIPaddr = 0 # Don't check response packets for matching destination IPs
5
6class DHCPTest:
7
8 def __init__(self, seed_ip = '192.168.1.1', iface = 'veth0'):
9 self.seed_ip = seed_ip
10 self.seed_mac = self.ipToMac(self.seed_ip)
11 self.iface = iface
12 self.mac_map = {}
13 self.mac_inverse_map = {}
14
15 def is_mcast(self, ip):
16 mcast_octet = (atol(ip) >> 24) & 0xff
17 return True if mcast_octet >= 224 and mcast_octet <= 239 else False
18
Chetan Gaonker1f7c3f82016-03-08 12:17:37 -080019 def discover(self, mac = None, update_seed = False):
Chetan Gaonkerb2bd8242016-03-03 15:39:24 -080020 '''Send a DHCP discover/offer'''
21
22 if mac is None:
23 mac = self.seed_mac
24 if update_seed:
25 self.seed_ip = self.incIP(self.seed_ip)
26 self.seed_mac = self.ipToMac(self.seed_ip)
27 mac = self.seed_mac
28
29 chmac = self.macToChaddr(mac)
30 L2 = Ether(dst="ff:ff:ff:ff:ff:ff", src=mac)
31 L3 = IP(src="0.0.0.0", dst="255.255.255.255")
32 L4 = UDP(sport=68, dport=67)
33 L5 = BOOTP(chaddr=chmac)
34 L6 = DHCP(options=[("message-type","discover"),"end"])
35 resp = srp1(L2/L3/L4/L5/L6, filter="udp and port 68", timeout=5, iface=self.iface)
36 try:
37 srcIP = resp.yiaddr
38 serverIP = resp.siaddr
39 except AttributeError:
40 print("Failed to acquire IP via DHCP for %s on interface %s" %(mac, self.iface))
41 return (None, None)
42
43 for x in resp.lastlayer().options:
44 if(x == 'end'):
45 break
46 op,val = x
47 if(op == "subnet_mask"):
48 subnet_mask = val
49 elif(op == 'server_id'):
50 server_id = val
51
52 L5 = BOOTP(chaddr=chmac, yiaddr=srcIP)
53 L6 = DHCP(options=[("message-type","request"), ("server_id",server_id),
54 ("subnet_mask",subnet_mask), ("requested_addr",srcIP), "end"])
55 srp1(L2/L3/L4/L5/L6, filter="udp and port 68", timeout=5, iface=self.iface)
56 self.mac_map[mac] = (srcIP, serverIP)
57 self.mac_inverse_map[srcIP] = (mac, serverIP)
58 return (srcIP, serverIP)
59
Chetan Gaonker1f7c3f82016-03-08 12:17:37 -080060 def discover_next(self):
Chetan Gaonkerb2bd8242016-03-03 15:39:24 -080061 '''Send next dhcp discover/request with updated mac'''
Chetan Gaonker1f7c3f82016-03-08 12:17:37 -080062 return self.discover(update_seed = True)
Chetan Gaonkerb2bd8242016-03-03 15:39:24 -080063
Chetan Gaonker1f7c3f82016-03-08 12:17:37 -080064 def release(self, ip):
65 '''Send a DHCP discover/offer'''
66 if ip is None:
67 return False
68 if not self.mac_inverse_map.has_key(ip):
69 return False
70 mac, server_ip = self.mac_inverse_map[ip]
71 chmac = self.macToChaddr(mac)
72 L2 = Ether(dst="ff:ff:ff:ff:ff:ff", src=mac)
73 L3 = IP(src="0.0.0.0", dst="255.255.255.255")
74 L4 = UDP(sport=68, dport=67)
75 L5 = BOOTP(chaddr=chmac, ciaddr = ip)
76 L6 = DHCP(options=[("message-type","release"), ("server_id", server_ip), "end"])
77 sendp(L2/L3/L4/L5/L6, iface = self.iface)
78 del self.mac_map[mac]
79 del self.mac_inverse_map[ip]
80 return True
Chetan Gaonkerb2bd8242016-03-03 15:39:24 -080081
82 def macToChaddr(self, mac):
83 rv = []
84 mac = mac.split(":")
85 for x in mac:
86 rv.append(chr(int(x, 16)))
87 return reduce(lambda x,y: x + y, rv)
88
89 def get_ip(self, mac):
90 if self.mac_map.has_key(mac):
91 return self.mac_map[mac]
92 return (None, None)
93
94 def get_mac(self, ip):
95 if self.mac_inverse_map.has_key(ip):
96 return self.mac_inverse_map[ip]
97 return (None, None)
98
99 def ipToMac(self, ip):
100 '''Generate a mac from a ip'''
101
102 mcast = self.is_mcast(ip)
103 mac = "01:00:5e" if mcast == True else "00:00:00"
104 octets = ip.split(".")
105 for x in range(1,4):
106 num = str(hex(int(octets[x])))
107 num = num.split("x")[1]
108 if len(num) < 2:
109 num = "0" + str(num)
110 mac += ":" + num
111 return mac
112
113 def incIP(self, ip, n=1):
114 '''Increment an IP'''
115
116 if n < 1:
117 return ip
118 o = ip.split(".")
119 for ii in range(3,-1,-1):
120 if int(o[ii]) < 255:
121 o[ii] = str(int(o[ii]) + 1)
122 break
123 else:
124 o[ii] = str(0)
125
126 n -= 1
127 return self.incIP(".".join(o), n)