Chetan Gaonker | b2bd824 | 2016-03-03 15:39:24 -0800 | [diff] [blame] | 1 | from scapy.all import * |
| 2 | |
| 3 | conf.verb = 0 # Disable Scapy verbosity |
| 4 | conf.checkIPaddr = 0 # Don't check response packets for matching destination IPs |
| 5 | |
| 6 | class 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 Gaonker | f72ca40 | 2016-05-02 16:29:32 -0700 | [diff] [blame] | 14 | self.bootpmac = None |
| 15 | self.dhcpresp = None |
Chetan Gaonker | b2bd824 | 2016-03-03 15:39:24 -0800 | [diff] [blame] | 16 | |
| 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 Gaonker | 1f7c3f8 | 2016-03-08 12:17:37 -0800 | [diff] [blame] | 21 | def discover(self, mac = None, update_seed = False): |
Chetan Gaonker | b2bd824 | 2016-03-03 15:39:24 -0800 | [diff] [blame] | 22 | '''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 Gaonker | f72ca40 | 2016-05-02 16:29:32 -0700 | [diff] [blame] | 32 | self.bootpmac = chmac |
Chetan Gaonker | b2bd824 | 2016-03-03 15:39:24 -0800 | [diff] [blame] | 33 | 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 Gaonker | 49ef085 | 2016-03-23 15:06:18 -0700 | [diff] [blame] | 38 | resp = srp1(L2/L3/L4/L5/L6, filter="udp and port 68", timeout=10, iface=self.iface) |
Chetan Gaonker | f72ca40 | 2016-05-02 16:29:32 -0700 | [diff] [blame] | 39 | self.dhcpresp = resp |
Chetan Gaonker | b2bd824 | 2016-03-03 15:39:24 -0800 | [diff] [blame] | 40 | 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 Gaonker | cd86bdd | 2016-03-17 00:08:12 -0700 | [diff] [blame] | 47 | subnet_mask = "0.0.0.0" |
Chetan Gaonker | b2bd824 | 2016-03-03 15:39:24 -0800 | [diff] [blame] | 48 | 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 Gaonker | 49ef085 | 2016-03-23 15:06:18 -0700 | [diff] [blame] | 60 | srp(L2/L3/L4/L5/L6, filter="udp and port 68", timeout=10, iface=self.iface) |
Chetan Gaonker | b2bd824 | 2016-03-03 15:39:24 -0800 | [diff] [blame] | 61 | self.mac_map[mac] = (srcIP, serverIP) |
| 62 | self.mac_inverse_map[srcIP] = (mac, serverIP) |
| 63 | return (srcIP, serverIP) |
| 64 | |
Chetan Gaonker | f72ca40 | 2016-05-02 16:29:32 -0700 | [diff] [blame] | 65 | 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 Gaonker | 1f7c3f8 | 2016-03-08 12:17:37 -0800 | [diff] [blame] | 132 | def discover_next(self): |
Chetan Gaonker | b2bd824 | 2016-03-03 15:39:24 -0800 | [diff] [blame] | 133 | '''Send next dhcp discover/request with updated mac''' |
Chetan Gaonker | 1f7c3f8 | 2016-03-08 12:17:37 -0800 | [diff] [blame] | 134 | return self.discover(update_seed = True) |
Chetan Gaonker | b2bd824 | 2016-03-03 15:39:24 -0800 | [diff] [blame] | 135 | |
Chetan Gaonker | 1f7c3f8 | 2016-03-08 12:17:37 -0800 | [diff] [blame] | 136 | 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 Gaonker | b2bd824 | 2016-03-03 15:39:24 -0800 | [diff] [blame] | 153 | |
| 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) |