blob: 6c151ca26e446c7c2b17e685a22b5b36c938486d [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 = {}
Chetan Gaonkerf72ca402016-05-02 16:29:32 -070014 self.bootpmac = None
15 self.dhcpresp = None
Chetan Gaonkerb2bd8242016-03-03 15:39:24 -080016
17 def is_mcast(self, ip):
18 mcast_octet = (atol(ip) >> 24) & 0xff
19 return True if mcast_octet >= 224 and mcast_octet <= 239 else False
20
Chetan Gaonker1f7c3f82016-03-08 12:17:37 -080021 def discover(self, mac = None, update_seed = False):
Chetan Gaonkerb2bd8242016-03-03 15:39:24 -080022 '''Send a DHCP discover/offer'''
23
24 if mac is None:
25 mac = self.seed_mac
26 if update_seed:
27 self.seed_ip = self.incIP(self.seed_ip)
28 self.seed_mac = self.ipToMac(self.seed_ip)
29 mac = self.seed_mac
30
31 chmac = self.macToChaddr(mac)
Chetan Gaonkerf72ca402016-05-02 16:29:32 -070032 self.bootpmac = chmac
Chetan Gaonkerb2bd8242016-03-03 15:39:24 -080033 L2 = Ether(dst="ff:ff:ff:ff:ff:ff", src=mac)
34 L3 = IP(src="0.0.0.0", dst="255.255.255.255")
35 L4 = UDP(sport=68, dport=67)
36 L5 = BOOTP(chaddr=chmac)
37 L6 = DHCP(options=[("message-type","discover"),"end"])
Chetan Gaonker49ef0852016-03-23 15:06:18 -070038 resp = srp1(L2/L3/L4/L5/L6, filter="udp and port 68", timeout=10, iface=self.iface)
Chetan Gaonkerf72ca402016-05-02 16:29:32 -070039 self.dhcpresp = resp
Chetan Gaonkerb2bd8242016-03-03 15:39:24 -080040 try:
41 srcIP = resp.yiaddr
42 serverIP = resp.siaddr
43 except AttributeError:
44 print("Failed to acquire IP via DHCP for %s on interface %s" %(mac, self.iface))
45 return (None, None)
46
Chetan Gaonkercd86bdd2016-03-17 00:08:12 -070047 subnet_mask = "0.0.0.0"
Chetan Gaonkerb2bd8242016-03-03 15:39:24 -080048 for x in resp.lastlayer().options:
49 if(x == 'end'):
50 break
51 op,val = x
52 if(op == "subnet_mask"):
53 subnet_mask = val
54 elif(op == 'server_id'):
55 server_id = val
56
57 L5 = BOOTP(chaddr=chmac, yiaddr=srcIP)
58 L6 = DHCP(options=[("message-type","request"), ("server_id",server_id),
59 ("subnet_mask",subnet_mask), ("requested_addr",srcIP), "end"])
Chetan Gaonker49ef0852016-03-23 15:06:18 -070060 srp(L2/L3/L4/L5/L6, filter="udp and port 68", timeout=10, iface=self.iface)
Chetan Gaonkerb2bd8242016-03-03 15:39:24 -080061 self.mac_map[mac] = (srcIP, serverIP)
62 self.mac_inverse_map[srcIP] = (mac, serverIP)
63 return (srcIP, serverIP)
64
Chetan Gaonkerf72ca402016-05-02 16:29:32 -070065 def only_discover(self, mac = None, desired = False):
66 '''Send a DHCP discover'''
67
68 if mac is None:
69 mac = self.seed_mac
70
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(ciaddr=self.seed_ip, chaddr=chmac)
76 if desired:
77 L6 = DHCP(options=[("message-type","discover"),("requested_addr",self.seed_ip),"end"])
78 else:
79 L6 = DHCP(options=[("message-type","discover"),"end"])
80
81 resp = srp1(L2/L3/L4/L5/L6, filter="udp and port 68", timeout=10, iface=self.iface)
82
83 if(resp.lastlayer().options [0][1] == 2):
84 try:
85 srcIP = resp.yiaddr
86 serverIP = resp.siaddr
87 except AttributeError:
88 print "In Attribute error."
89 print("Failed to acquire IP via DHCP for %s on interface %s" %(mac, self.iface))
90 return (None, None, None)
91 return (srcIP, serverIP, mac)
92
93 elif(resp.lastlayer().options [0][1] == 5):
94
95 return (None, None, None)
96
97
98 def only_request(self, cip, mac):
99 '''Send a DHCP offer'''
100
101
102 subnet_mask = "0.0.0.0"
103 for x in self.dhcpresp.lastlayer().options:
104 if(x == 'end'):
105 break
106 op,val = x
107 if(op == "subnet_mask"):
108 subnet_mask = val
109 elif(op == 'server_id'):
110 server_id = val
111
112 L2 = Ether(dst="ff:ff:ff:ff:ff:ff", src=mac)
113 L3 = IP(src="0.0.0.0", dst="255.255.255.255")
114 L4 = UDP(sport=68, dport=67)
115 L5 = BOOTP(chaddr=self.bootpmac, yiaddr=cip)
116 L6 = DHCP(options=[("message-type","request"), ("server_id",server_id),
117 ("subnet_mask",subnet_mask), ("requested_addr",cip), "end"])
118 resp=srp1(L2/L3/L4/L5/L6, filter="udp and port 68", timeout=10, iface=self.iface)
119 if(resp.lastlayer().options [0][1] == 5):
120 try:
121 srcIP = resp.yiaddr
122 serverIP = resp.siaddr
123 except AttributeError:
124 print "In Attribute error."
125 print("Failed to acquire IP via DHCP for %s on interface %s" %(mac, self.iface))
126 return (None, None)
127 return (srcIP, serverIP)
128 else:
129 return (None, None)
130
131
Chetan Gaonker1f7c3f82016-03-08 12:17:37 -0800132 def discover_next(self):
Chetan Gaonkerb2bd8242016-03-03 15:39:24 -0800133 '''Send next dhcp discover/request with updated mac'''
Chetan Gaonker1f7c3f82016-03-08 12:17:37 -0800134 return self.discover(update_seed = True)
Chetan Gaonkerb2bd8242016-03-03 15:39:24 -0800135
Chetan Gaonker1f7c3f82016-03-08 12:17:37 -0800136 def release(self, ip):
137 '''Send a DHCP discover/offer'''
138 if ip is None:
139 return False
140 if not self.mac_inverse_map.has_key(ip):
141 return False
142 mac, server_ip = self.mac_inverse_map[ip]
143 chmac = self.macToChaddr(mac)
144 L2 = Ether(dst="ff:ff:ff:ff:ff:ff", src=mac)
145 L3 = IP(src="0.0.0.0", dst="255.255.255.255")
146 L4 = UDP(sport=68, dport=67)
147 L5 = BOOTP(chaddr=chmac, ciaddr = ip)
148 L6 = DHCP(options=[("message-type","release"), ("server_id", server_ip), "end"])
149 sendp(L2/L3/L4/L5/L6, iface = self.iface)
150 del self.mac_map[mac]
151 del self.mac_inverse_map[ip]
152 return True
Chetan Gaonkerb2bd8242016-03-03 15:39:24 -0800153
154 def macToChaddr(self, mac):
155 rv = []
156 mac = mac.split(":")
157 for x in mac:
158 rv.append(chr(int(x, 16)))
159 return reduce(lambda x,y: x + y, rv)
160
161 def get_ip(self, mac):
162 if self.mac_map.has_key(mac):
163 return self.mac_map[mac]
164 return (None, None)
165
166 def get_mac(self, ip):
167 if self.mac_inverse_map.has_key(ip):
168 return self.mac_inverse_map[ip]
169 return (None, None)
170
171 def ipToMac(self, ip):
172 '''Generate a mac from a ip'''
173
174 mcast = self.is_mcast(ip)
175 mac = "01:00:5e" if mcast == True else "00:00:00"
176 octets = ip.split(".")
177 for x in range(1,4):
178 num = str(hex(int(octets[x])))
179 num = num.split("x")[1]
180 if len(num) < 2:
181 num = "0" + str(num)
182 mac += ":" + num
183 return mac
184
185 def incIP(self, ip, n=1):
186 '''Increment an IP'''
187
188 if n < 1:
189 return ip
190 o = ip.split(".")
191 for ii in range(3,-1,-1):
192 if int(o[ii]) < 255:
193 o[ii] = str(int(o[ii]) + 1)
194 break
195 else:
196 o[ii] = str(0)
197
198 n -= 1
199 return self.incIP(".".join(o), n)