A R Karthick | a2e53d6 | 2016-02-19 17:38:30 -0800 | [diff] [blame] | 1 | #### Authentication parameters |
Chetan Gaonker | 35cb16f | 2016-03-02 03:05:28 -0800 | [diff] [blame] | 2 | from scapy.all import * |
A R Karthick | a2e53d6 | 2016-02-19 17:38:30 -0800 | [diff] [blame] | 3 | from socket import * |
| 4 | from struct import * |
A R Karthick | a2e53d6 | 2016-02-19 17:38:30 -0800 | [diff] [blame] | 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 | |
A R Karthick | a2e53d6 | 2016-02-19 17:38:30 -0800 | [diff] [blame] | 34 | class EapolPacket(object): |
| 35 | |
| 36 | def __init__(self, intf = 'veth0'): |
| 37 | self.intf = intf |
| 38 | self.s = None |
| 39 | self.max_payload_size = 1600 |
| 40 | |
| 41 | def setup(self): |
| 42 | self.s = socket(AF_PACKET, SOCK_RAW, htons(ETHERTYPE_PAE)) |
| 43 | self.s.bind((self.intf, ETHERTYPE_PAE)) |
| 44 | self.mymac = self.s.getsockname()[4] |
Chetan Gaonker | 35cb16f | 2016-03-02 03:05:28 -0800 | [diff] [blame] | 45 | self.llheader = Ether(dst = PAE_GROUP_ADDR, src = self.mymac, type = ETHERTYPE_PAE) |
A R Karthick | a2e53d6 | 2016-02-19 17:38:30 -0800 | [diff] [blame] | 46 | |
| 47 | def cleanup(self): |
| 48 | if self.s is not None: |
| 49 | self.s.close() |
| 50 | self.s = None |
| 51 | |
| 52 | def eapol(self, req_type, payload=""): |
Chetan Gaonker | 35cb16f | 2016-03-02 03:05:28 -0800 | [diff] [blame] | 53 | return EAPOL(version = EAPOL_VERSION, type = req_type)/payload |
A R Karthick | a2e53d6 | 2016-02-19 17:38:30 -0800 | [diff] [blame] | 54 | |
| 55 | def eap(self, code, pkt_id, req_type=0, data=""): |
Chetan Gaonker | 35cb16f | 2016-03-02 03:05:28 -0800 | [diff] [blame] | 56 | return EAP(code = code, id = pkt_id, type = req_type)/data |
A R Karthick | a2e53d6 | 2016-02-19 17:38:30 -0800 | [diff] [blame] | 57 | |
| 58 | def eapTLS(self, code, pkt_id, flags = TLS_LENGTH_INCLUDED, data=""): |
| 59 | req_type = EAP_TYPE_TLS |
| 60 | if code in [EAP_SUCCESS, EAP_FAILURE]: |
| 61 | return pack("!BBH", code, pkt_id, 4) |
| 62 | else: |
| 63 | if flags & TLS_LENGTH_INCLUDED: |
| 64 | flags_dlen = pack("!BL", flags, len(data)) |
| 65 | return pack("!BBHB", code, pkt_id, 5+len(flags_dlen)+len(data), req_type) + flags_dlen + data |
| 66 | flags_str = pack("!B", flags) |
| 67 | return pack("!BBHB", code, pkt_id, 5+len(flags_str)+len(data), req_type) + flags_str + data |
| 68 | |
| 69 | def eapol_send(self, eapol_type, eap_payload): |
Chetan Gaonker | 35cb16f | 2016-03-02 03:05:28 -0800 | [diff] [blame] | 70 | return sendp(self.llheader/self.eapol(eapol_type, eap_payload), iface=self.intf) |
A R Karthick | a2e53d6 | 2016-02-19 17:38:30 -0800 | [diff] [blame] | 71 | |
| 72 | def eapol_recv(self): |
| 73 | p = self.s.recv(self.max_payload_size)[14:] |
| 74 | vers,pkt_type,eapollen = unpack("!BBH",p[:4]) |
| 75 | print "Version %d, type %d, len %d" %(vers, pkt_type, eapollen) |
| 76 | assert_equal(pkt_type, EAPOL_EAPPACKET) |
| 77 | return p[4:] |
| 78 | |
| 79 | def eapol_start(self): |
| 80 | eap_payload = self.eap(EAPOL_START, 2) |
| 81 | return self.eapol_send(EAPOL_START, eap_payload) |
| 82 | |
| 83 | def eapol_id_req(self, pkt_id = 0, user = USER): |
| 84 | eap_payload = self.eap(EAP_RESPONSE, pkt_id, EAP_TYPE_ID, user) |
| 85 | return self.eapol_send(EAPOL_EAPPACKET, eap_payload) |
| 86 | |
Chetan Gaonker | 4a25e2b | 2016-03-04 14:45:15 -0800 | [diff] [blame] | 87 | def eap_md5_challenge_recv(self,rad_pwd): |
| 88 | PASS = rad_pwd |
| 89 | print 'Inside EAP MD5 Challenge Exchange' |
| 90 | p = self.s.recv(self.max_payload_size)[14:] |
| 91 | vers,pkt_type,eapollen = unpack("!BBH",p[:4]) |
| 92 | print "EAPOL Version %d, type %d, len %d" %(vers, pkt_type, eapollen) |
| 93 | code, pkt_id, eaplen = unpack("!BBH", p[4:8]) |
| 94 | print "EAP Code %d, id %d, len %d" %(code, pkt_id, eaplen) |
| 95 | assert_equal(code, EAP_REQUEST) |
| 96 | reqtype = unpack("!B", p[8:9])[0] |
| 97 | reqdata = p[9:4+eaplen] |
| 98 | print 'Request type is %d' %(reqtype) |
| 99 | assert_equal(reqtype, EAP_TYPE_MD5) |
| 100 | challenge=pack("!B",pkt_id)+PASS+reqdata[1:] |
| 101 | print "Generating md5 challenge for %s" % challenge |
| 102 | return (challenge,pkt_id) |
| 103 | |
| 104 | def eap_Status(self): |
| 105 | print 'Inside EAP Status' |
| 106 | p = self.s.recv(self.max_payload_size)[14:] |
| 107 | code, id, eaplen = unpack("!BBH", p[4:8]) |
| 108 | return code |
| 109 | |