A R Karthick | a2e53d6 | 2016-02-19 17:38:30 -0800 | [diff] [blame] | 1 | #### Authentication parameters |
| 2 | from socket import * |
| 3 | from struct import * |
| 4 | import scapy |
| 5 | import sys |
| 6 | from nose.tools import assert_equal, assert_not_equal, assert_raises, assert_true |
| 7 | |
| 8 | USER = "raduser" |
| 9 | PASS = "radpass" |
| 10 | WRONG_USER = "XXXX" |
| 11 | WRONG_PASS = "XXXX" |
| 12 | NO_USER = "" |
| 13 | NO_PASS = "" |
| 14 | DEV = "tap0" |
| 15 | ETHERTYPE_PAE = 0x888e |
| 16 | PAE_GROUP_ADDR = "\xff\xff\xff\xff\xff\xff" |
| 17 | EAPOL_VERSION = 1 |
| 18 | EAPOL_EAPPACKET = 0 |
| 19 | EAPOL_START = 1 |
| 20 | EAPOL_LOGOFF = 2 |
| 21 | EAPOL_KEY = 3 |
| 22 | EAPOL_ASF = 4 |
| 23 | EAP_REQUEST = 1 |
| 24 | EAP_RESPONSE = 2 |
| 25 | EAP_SUCCESS = 3 |
| 26 | EAP_FAILURE = 4 |
| 27 | EAP_TYPE_ID = 1 |
| 28 | EAP_TYPE_MD5 = 4 |
| 29 | EAP_TYPE_MSCHAP = 26 |
| 30 | EAP_TYPE_TLS = 13 |
| 31 | cCertMsg = '\x0b\x00\x00\x03\x00\x00\x00' |
| 32 | TLS_LENGTH_INCLUDED = 0x80 |
| 33 | |
| 34 | def ethernet_header(src, dst, req_type): |
| 35 | return dst+src+pack("!H", req_type) |
| 36 | |
| 37 | class EapolPacket(object): |
| 38 | |
| 39 | def __init__(self, intf = 'veth0'): |
| 40 | self.intf = intf |
| 41 | self.s = None |
| 42 | self.max_payload_size = 1600 |
| 43 | |
| 44 | def setup(self): |
| 45 | self.s = socket(AF_PACKET, SOCK_RAW, htons(ETHERTYPE_PAE)) |
| 46 | self.s.bind((self.intf, ETHERTYPE_PAE)) |
| 47 | self.mymac = self.s.getsockname()[4] |
| 48 | self.llheader = ethernet_header(self.mymac, PAE_GROUP_ADDR, ETHERTYPE_PAE) |
| 49 | |
| 50 | def cleanup(self): |
| 51 | if self.s is not None: |
| 52 | self.s.close() |
| 53 | self.s = None |
| 54 | |
| 55 | def eapol(self, req_type, payload=""): |
| 56 | return pack("!BBH", EAPOL_VERSION, req_type, len(payload))+payload |
| 57 | |
| 58 | def eap(self, code, pkt_id, req_type=0, data=""): |
| 59 | if code in [EAP_SUCCESS, EAP_FAILURE]: |
| 60 | return pack("!BBH", code, pkt_id, 4) |
| 61 | else: |
| 62 | return pack("!BBHB", code, pkt_id, 5+len(data), req_type)+data |
| 63 | |
| 64 | def eapTLS(self, code, pkt_id, flags = TLS_LENGTH_INCLUDED, data=""): |
| 65 | req_type = EAP_TYPE_TLS |
| 66 | if code in [EAP_SUCCESS, EAP_FAILURE]: |
| 67 | return pack("!BBH", code, pkt_id, 4) |
| 68 | else: |
| 69 | if flags & TLS_LENGTH_INCLUDED: |
| 70 | flags_dlen = pack("!BL", flags, len(data)) |
| 71 | return pack("!BBHB", code, pkt_id, 5+len(flags_dlen)+len(data), req_type) + flags_dlen + data |
| 72 | flags_str = pack("!B", flags) |
| 73 | return pack("!BBHB", code, pkt_id, 5+len(flags_str)+len(data), req_type) + flags_str + data |
| 74 | |
| 75 | def eapol_send(self, eapol_type, eap_payload): |
| 76 | return self.s.send(self.llheader + self.eapol(eapol_type, eap_payload)) |
| 77 | |
| 78 | def eapol_recv(self): |
| 79 | p = self.s.recv(self.max_payload_size)[14:] |
| 80 | vers,pkt_type,eapollen = unpack("!BBH",p[:4]) |
| 81 | print "Version %d, type %d, len %d" %(vers, pkt_type, eapollen) |
| 82 | assert_equal(pkt_type, EAPOL_EAPPACKET) |
| 83 | return p[4:] |
| 84 | |
| 85 | def eapol_start(self): |
| 86 | eap_payload = self.eap(EAPOL_START, 2) |
| 87 | return self.eapol_send(EAPOL_START, eap_payload) |
| 88 | |
| 89 | def eapol_id_req(self, pkt_id = 0, user = USER): |
| 90 | eap_payload = self.eap(EAP_RESPONSE, pkt_id, EAP_TYPE_ID, user) |
| 91 | return self.eapol_send(EAPOL_EAPPACKET, eap_payload) |
| 92 | |