blob: f7a8a2d0ce826857cdaeba95d930b972c4623ab2 [file] [log] [blame]
A R Karthicka2e53d62016-02-19 17:38:30 -08001#### Authentication parameters
Chetan Gaonker35cb16f2016-03-02 03:05:28 -08002from scapy.all import *
A R Karthicka2e53d62016-02-19 17:38:30 -08003from socket import *
4from struct import *
A R Karthicka2e53d62016-02-19 17:38:30 -08005import sys
6from nose.tools import assert_equal, assert_not_equal, assert_raises, assert_true
7
8USER = "raduser"
9PASS = "radpass"
10WRONG_USER = "XXXX"
11WRONG_PASS = "XXXX"
12NO_USER = ""
13NO_PASS = ""
14DEV = "tap0"
15ETHERTYPE_PAE = 0x888e
16PAE_GROUP_ADDR = "\xff\xff\xff\xff\xff\xff"
17EAPOL_VERSION = 1
18EAPOL_EAPPACKET = 0
19EAPOL_START = 1
20EAPOL_LOGOFF = 2
21EAPOL_KEY = 3
22EAPOL_ASF = 4
23EAP_REQUEST = 1
24EAP_RESPONSE = 2
25EAP_SUCCESS = 3
26EAP_FAILURE = 4
27EAP_TYPE_ID = 1
28EAP_TYPE_MD5 = 4
29EAP_TYPE_MSCHAP = 26
30EAP_TYPE_TLS = 13
31cCertMsg = '\x0b\x00\x00\x03\x00\x00\x00'
32TLS_LENGTH_INCLUDED = 0x80
33
A R Karthicka2e53d62016-02-19 17:38:30 -080034class 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 Gaonker35cb16f2016-03-02 03:05:28 -080045 self.llheader = Ether(dst = PAE_GROUP_ADDR, src = self.mymac, type = ETHERTYPE_PAE)
A R Karthicka2e53d62016-02-19 17:38:30 -080046
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 Gaonker35cb16f2016-03-02 03:05:28 -080053 return EAPOL(version = EAPOL_VERSION, type = req_type)/payload
A R Karthicka2e53d62016-02-19 17:38:30 -080054
55 def eap(self, code, pkt_id, req_type=0, data=""):
Chetan Gaonker35cb16f2016-03-02 03:05:28 -080056 return EAP(code = code, id = pkt_id, type = req_type)/data
A R Karthicka2e53d62016-02-19 17:38:30 -080057
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 Gaonker35cb16f2016-03-02 03:05:28 -080070 return sendp(self.llheader/self.eapol(eapol_type, eap_payload), iface=self.intf)
A R Karthicka2e53d62016-02-19 17:38:30 -080071
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