blob: ea26164acfeca389a9ee104460cc76cb7b0959d2 [file] [log] [blame]
A R Karthicka2e53d62016-02-19 17:38:30 -08001#### Authentication parameters
2from socket import *
3from struct import *
4import scapy
5import 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
34def ethernet_header(src, dst, req_type):
35 return dst+src+pack("!H", req_type)
36
37class 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