hlrauc.py (7851B)
1 import socket 2 import os 3 import threading 4 import sys 5 import signal 6 from Crypto.Cipher import AES 7 8 class AuthCenter: 9 ''' 10 Home Location Register (HLR) and Authentication Center (AuC) used for 11 for retrieving SIM/AKA values. This provides a UDP server that 12 hostapd can communicate with to obtain SIM values. 13 ''' 14 def __init__(self, sock_path, config_file): 15 self._sock_path = sock_path 16 self._read_config(config_file) 17 self._socket = socket.socket(socket.AF_UNIX, socket.SOCK_DGRAM) 18 if os.path.exists(sock_path): 19 os.unlink(sock_path) 20 self._socket.bind(sock_path) 21 22 self._rxhandle = threading.Thread(target=self._rx_thread) 23 self._rxhandle.ready = threading.Event() 24 self._rxhandle.start() 25 26 # wait for rx thread to start 27 self._rxhandle.ready.wait() 28 29 def __del__(self): 30 os.remove(self._sock_path) 31 self._socket.close() 32 33 def _rx_thread(self): 34 self._rxhandle.ready.set() 35 while (True): 36 try: 37 data, addr = self._socket.recvfrom(1000) 38 data = data.decode('ascii') 39 resp = self._process_data(data) 40 except OSError: 41 break 42 except: 43 print("Exception:", sys.exc_info()[0]) 44 break 45 if resp: 46 self._socket.sendto(bytearray(resp, 'UTF-8'), addr) 47 48 def _read_config(self, file): 49 self._database = {} 50 with open(file) as f: 51 for line in f: 52 if line[0] == '#': 53 continue 54 else: 55 data = line.strip('\n').split(':') 56 self._database[data[0]] = data[1:] 57 58 def _process_data(self, data): 59 if data[:12] == "SIM-REQ-AUTH": 60 # SIM requests just return the stored values for the IMSI 61 imsi, num_chals = data[13:].split(' ') 62 if not imsi or not num_chals: 63 return "ERROR" 64 65 data = self._database.get(imsi, None) 66 if not data: 67 return "ERROR" 68 69 response = "SIM-RESP-AUTH %s" % imsi 70 response += (' ' + ':'.join(data))*int(num_chals) 71 72 return response 73 elif data[:12] == "AKA-REQ-AUTH": 74 # AKA requests must compute the milenage parameters for the IMSI 75 imsi = data.split(' ')[1] 76 data = self._database.get(imsi, None) 77 if not data: 78 return "ERROR" 79 80 # make sure this is an AKA entry 81 if len(data) < 4: 82 return "ERROR" 83 84 k, opc, amf, sqn = data 85 86 rand = self._bytetostring(os.urandom(16)) 87 88 response = "AKA-RESP-AUTH %s " % imsi 89 90 return response + self._get_milenage(opc, k, rand, sqn, amf) 91 elif data[:8] == "AKA-AUTS": 92 # sync error, parse out SQN and reset in database 93 imsi, auts, rand = data[9:].split(' ') 94 95 entry = self._database.get(imsi, None) 96 if not entry: 97 return "ERROR" 98 99 # make sure this is an AKA entry 100 if len(entry) < 4: 101 return "ERROR" 102 103 k, opc, amf, sqn = entry 104 105 # calculate/set new sequence number 106 entry[3] = self._resync_autn(opc, k, rand, auts) 107 self._database[imsi] = entry 108 109 return None 110 111 def _bytetostring(self, b): 112 return ''.join(format(x, '02x') for x in b) 113 114 def _xor(self, a, b): 115 ret = bytearray(16) 116 for i in range(len(a)): 117 ret[i] = a[i] ^ b[i] 118 return ret 119 120 def _resync_autn(self, opc, k, rand, auts): 121 opc = bytearray.fromhex(opc) 122 k = bytearray.fromhex(k) 123 rand = bytearray.fromhex(rand) 124 auts = bytearray.fromhex(auts) 125 new_sqn = bytearray(6) 126 ak_star = bytearray(6) 127 128 temp = self._xor(rand, opc) 129 aes1 = AES.new(bytes(k), AES.MODE_ECB) 130 temp = aes1.encrypt(bytes(temp)) 131 temp = bytearray(temp) 132 133 out5 = bytearray(16) 134 for i in range(16): 135 out5[(i + 4) % 16] = temp[i] ^ opc[i]; 136 137 out5[15] ^= 8 138 139 aes2 = AES.new(bytes(k), AES.MODE_ECB) 140 out5 = aes2.encrypt(bytes(out5)) 141 out5 = bytearray(out5) 142 143 for i in range(6): 144 ak_star[i] = out5[i] ^ opc[i] 145 146 for i in range(6): 147 new_sqn[i] = auts[i] ^ ak_star[i] 148 149 return self._bytetostring(new_sqn) 150 151 def _get_milenage(self, opc, k, rand, sqn, amf): 152 ''' 153 Computes milenage values from OPc, K, RAND, SQN and AMF 154 Returns a concatenated list (RAND + AUTN + IK + CK + RES) that 155 will be sent back as the response to the client (hostapd). This 156 is a python re-write of the function eap_aka_get_milenage() from 157 src/simutil.c 158 ''' 159 opc = bytearray.fromhex(opc) 160 k = bytearray.fromhex(k) 161 # rand gets returned, so it should be left as a hex string 162 _rand = bytearray.fromhex(rand) 163 sqn = bytearray.fromhex(sqn) 164 amf = bytearray.fromhex(amf) 165 166 aes1 = AES.new(bytes(k), AES.MODE_ECB) 167 tmp1 = self._xor(_rand, opc) 168 tmp1 = aes1.encrypt(bytes(tmp1)) 169 tmp1 = bytearray(tmp1) 170 171 tmp2 = bytearray() 172 tmp2[0:6] = sqn 173 tmp2[6:2] = amf 174 tmp2[9:6] = sqn 175 tmp2[15:2] = amf 176 177 tmp3 = bytearray(16) 178 for i in range(len(tmp1)): 179 tmp3[(i + 8) % 16] = tmp2[i] ^ opc[i] 180 181 tmp3 = self._xor(tmp3, tmp1) 182 183 aes2 = AES.new(bytes(k), AES.MODE_ECB) 184 tmp1 = aes2.encrypt(bytes(tmp3)) 185 tmp1 = bytearray(tmp1) 186 187 tmp1 = self._xor(tmp1, opc) 188 maca = self._bytetostring(tmp1[0:8]) 189 190 tmp1 = self._xor(_rand, opc) 191 aes3 = AES.new(bytes(k), AES.MODE_ECB) 192 tmp2 = aes3.encrypt(bytes(tmp1)) 193 tmp2 = bytearray(tmp2) 194 195 tmp1 = self._xor(tmp2, opc) 196 tmp1[15] ^= 1 197 198 aes4 = AES.new(bytes(k), AES.MODE_ECB) 199 tmp3 = aes4.encrypt(bytes(tmp1)) 200 tmp3 = bytearray(tmp3) 201 202 tmp3 = self._xor(tmp3, opc) 203 204 res = self._bytetostring(tmp3[8:16]) 205 ak = self._bytetostring(tmp3[0:6]) 206 207 for i in range(len(tmp1)): 208 tmp1[(i + 12) % 16] = tmp2[i] ^ opc[i] 209 210 tmp1[15] ^= 1 << 1 211 aes5 = AES.new(bytes(k), AES.MODE_ECB) 212 tmp1 = aes5.encrypt(bytes(tmp1)) 213 tmp1 = bytearray(tmp1) 214 215 tmp1 = self._xor(tmp1, opc) 216 ck = self._bytetostring(tmp1) 217 218 for i in range(len(tmp1)): 219 tmp1[(i + 8) % 16] = tmp2[i] ^ opc[i] 220 221 tmp1[15] ^= 1 << 2 222 aes6 = AES.new(bytes(k), AES.MODE_ECB) 223 tmp1 = aes6.encrypt(bytes(tmp1)) 224 tmp1 = bytearray(tmp1) 225 tmp1 = self._xor(tmp1, opc) 226 ik = self._bytetostring(tmp1) 227 228 tmp1 = bytearray.fromhex(ak) 229 autn = bytearray(6) 230 for i in range(0, 6): 231 autn[i] = sqn[i] ^ tmp1[i] 232 233 autn[6:2] = amf 234 autn[8:8] = bytearray.fromhex(maca)[0:8] 235 236 autn = self._bytetostring(autn) 237 238 return rand + ' ' + autn + ' ' + ik + ' ' + ck + ' ' + res 239 240 def stop(self): 241 ''' 242 Stop the Authentication server and close the socket 243 ''' 244 self._socket.shutdown(socket.SHUT_RDWR) 245 self._socket.close() 246 self._rxhandle.join() 247 248 if __name__ == '__main__': 249 ''' 250 This will run in a stand-alone mode for testing 251 ''' 252 if len(sys.argv) < 3: 253 print('Usage: ./hlrauc.py <sock_path> <config>') 254 sys.exit() 255 256 hlrauc = AuthCenter(sys.argv[1], sys.argv[2]) 257 258 def signal_handler(signal, frame): 259 print('Exiting...') 260 hlrauc.stop() 261 sys.exit() 262 263 signal.signal(signal.SIGINT, signal_handler) 264 265 signal.pause()