blob: 5cf79fd2e8c92669cd581d51698df67e31869a48 [file] [log] [blame]
A R Karthick05d9b5f2016-06-08 11:53:54 -07001#
Chetan Gaonkercfcce782016-05-10 10:10:42 -07002# Copyright 2016-present Ciena Corporation
3#
4# Licensed under the Apache License, Version 2.0 (the "License");
5# you may not use this file except in compliance with the License.
6# You may obtain a copy of the License at
A R Karthick05d9b5f2016-06-08 11:53:54 -07007#
Chetan Gaonkercfcce782016-05-10 10:10:42 -07008# http://www.apache.org/licenses/LICENSE-2.0
A R Karthick05d9b5f2016-06-08 11:53:54 -07009#
Chetan Gaonkercfcce782016-05-10 10:10:42 -070010# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS,
12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13# See the License for the specific language governing permissions and
14# limitations under the License.
15#
A R Karthicka2e53d62016-02-19 17:38:30 -080016#### Authentication parameters
Chetan Gaonker35cb16f2016-03-02 03:05:28 -080017from scapy.all import *
A R Karthickaa10a202016-08-15 15:06:21 -070018from scapy_ssl_tls.ssl_tls import *
A R Karthicka2e53d62016-02-19 17:38:30 -080019from socket import *
20from struct import *
A R Karthicka2e53d62016-02-19 17:38:30 -080021import sys
22from nose.tools import assert_equal, assert_not_equal, assert_raises, assert_true
23
24USER = "raduser"
25PASS = "radpass"
26WRONG_USER = "XXXX"
27WRONG_PASS = "XXXX"
28NO_USER = ""
29NO_PASS = ""
30DEV = "tap0"
31ETHERTYPE_PAE = 0x888e
32PAE_GROUP_ADDR = "\xff\xff\xff\xff\xff\xff"
33EAPOL_VERSION = 1
34EAPOL_EAPPACKET = 0
35EAPOL_START = 1
36EAPOL_LOGOFF = 2
37EAPOL_KEY = 3
38EAPOL_ASF = 4
39EAP_REQUEST = 1
40EAP_RESPONSE = 2
41EAP_SUCCESS = 3
42EAP_FAILURE = 4
43EAP_TYPE_ID = 1
44EAP_TYPE_MD5 = 4
45EAP_TYPE_MSCHAP = 26
46EAP_TYPE_TLS = 13
47cCertMsg = '\x0b\x00\x00\x03\x00\x00\x00'
48TLS_LENGTH_INCLUDED = 0x80
A R Karthickaa10a202016-08-15 15:06:21 -070049TLS_MORE_FRAGMENTS = 0x40
A R Karthicka2e53d62016-02-19 17:38:30 -080050
A R Karthicka2e53d62016-02-19 17:38:30 -080051class EapolPacket(object):
A R Karthick05d9b5f2016-06-08 11:53:54 -070052
ChetanGaonker6138fcd2016-08-18 17:56:39 -070053 src_mac_map = { 'bcast': 'ff:ff:ff:ff:ff:ff',
54 'mcast': '01:80:C2:00:00:03',
55 'zeros': '00:00:00:00:00:00',
56 'default': None
57 }
58
A R Karthicka2e53d62016-02-19 17:38:30 -080059 def __init__(self, intf = 'veth0'):
60 self.intf = intf
61 self.s = None
A R Karthickaa10a202016-08-15 15:06:21 -070062 self.max_recv_size = 1600
A R Karthicka2e53d62016-02-19 17:38:30 -080063
ChetanGaonker6138fcd2016-08-18 17:56:39 -070064 def setup(self, src_mac = 'default'):
A R Karthicka2e53d62016-02-19 17:38:30 -080065 self.s = socket(AF_PACKET, SOCK_RAW, htons(ETHERTYPE_PAE))
66 self.s.bind((self.intf, ETHERTYPE_PAE))
67 self.mymac = self.s.getsockname()[4]
ChetanGaonker6138fcd2016-08-18 17:56:39 -070068 mac = None
69 if self.src_mac_map.has_key(src_mac):
70 mac = self.src_mac_map[src_mac]
71 if mac is None:
72 mac = self.mymac
73 self.llheader = Ether(dst = PAE_GROUP_ADDR, src = mac, type = ETHERTYPE_PAE)
74 log.info('llheader packet is %s'%self.llheader.show())
75 log.info('source mac of packet is %s'%mac)
Chetan Gaonker5b366302016-03-21 16:18:21 -070076 self.recv_sock = L2Socket(iface = self.intf, type = ETHERTYPE_PAE)
A R Karthicka2e53d62016-02-19 17:38:30 -080077
78 def cleanup(self):
79 if self.s is not None:
80 self.s.close()
81 self.s = None
A R Karthick05d9b5f2016-06-08 11:53:54 -070082
A R Karthicka2e53d62016-02-19 17:38:30 -080083 def eapol(self, req_type, payload=""):
Chetan Gaonker35cb16f2016-03-02 03:05:28 -080084 return EAPOL(version = EAPOL_VERSION, type = req_type)/payload
A R Karthicka2e53d62016-02-19 17:38:30 -080085
86 def eap(self, code, pkt_id, req_type=0, data=""):
Chetan Gaonker35cb16f2016-03-02 03:05:28 -080087 return EAP(code = code, id = pkt_id, type = req_type)/data
A R Karthicka2e53d62016-02-19 17:38:30 -080088
A R Karthickaa10a202016-08-15 15:06:21 -070089 def eapFragmentSend(self, code, pkt_id, flags = TLS_LENGTH_INCLUDED, payload = "", fragsize = 1024):
90 req_type = EAP_TYPE_TLS
91 if code in [ EAP_SUCCESS, EAP_FAILURE ]:
92 data = pack("!BBH", code, pkt_id, 4)
93 self.eapol_send(EAPOL_EAPPACKET, data)
94 return True
95
96 if len(payload) <= fragsize:
97 if flags & TLS_LENGTH_INCLUDED:
98 flags_dlen = pack("!BL", flags, len(payload))
99 data = pack("!BBHB", code, pkt_id, 5 + len(flags_dlen) + len(payload), req_type) \
100 + flags_dlen + payload
101 self.eapol_send(EAPOL_EAPPACKET, data)
102 return True
103 flags_str = pack("!B", flags)
104 data = pack("!BBHB", code, pkt_id, 5+len(flags_str)+len(payload), req_type) + flags_str + payload
105 self.eapol_send(EAPOL_EAPPACKET, data)
106 return True
107
108 fragments = []
109 data = payload[:]
110 frag = 0
111 def eapol_frag_cb(pkt):
112 r = str(pkt)
113 tls_data = r[self.TLS_OFFSET:]
114 frag_data = fragments[frag]
115 ##change packet id in response to match request
116 eap_payload = frag_data[:1] + pack("!B", pkt[EAP].id) + frag_data[2:]
117 self.eapol_send(EAPOL_EAPPACKET, eap_payload)
118
119 while len(data) > 0:
120 data_frag = data[:fragsize]
121 data = data[fragsize:]
122 if frag == 0:
123 ##first frag, include the total length
124 flags_dlen = pack("!BL", TLS_LENGTH_INCLUDED | TLS_MORE_FRAGMENTS, len(payload))
125 fragments.append(pack("!BBHB", code, pkt_id, 5 + len(flags_dlen) + len(data_frag), req_type) \
126 + flags_dlen + data_frag)
127 else:
128 if len(data) > 0:
129 flags = TLS_MORE_FRAGMENTS
130 else:
131 flags = 0
132 flags_str = pack("!B", flags)
133 fragments.append(pack("!BBHB", code, pkt_id, 5+len(flags_str)+len(data_frag), req_type) + \
134 flags_str + data_frag)
135 frag += 1
136
137 frag = 0
138 self.eapol_send(EAPOL_EAPPACKET, fragments[frag])
139 for frag in range(len(fragments)-1):
140 frag += 1
141 r = self.eapol_scapy_recv(cb = eapol_frag_cb,
142 lfilter = lambda pkt: EAP in pkt and pkt[EAP].type == EAP_TYPE_TLS and \
143 pkt[EAP].code == EAP.REQUEST)
144
145 return True
146
A R Karthicka2e53d62016-02-19 17:38:30 -0800147 def eapTLS(self, code, pkt_id, flags = TLS_LENGTH_INCLUDED, data=""):
148 req_type = EAP_TYPE_TLS
149 if code in [EAP_SUCCESS, EAP_FAILURE]:
150 return pack("!BBH", code, pkt_id, 4)
151 else:
152 if flags & TLS_LENGTH_INCLUDED:
153 flags_dlen = pack("!BL", flags, len(data))
154 return pack("!BBHB", code, pkt_id, 5+len(flags_dlen)+len(data), req_type) + flags_dlen + data
155 flags_str = pack("!B", flags)
156 return pack("!BBHB", code, pkt_id, 5+len(flags_str)+len(data), req_type) + flags_str + data
157
A R Karthickaa10a202016-08-15 15:06:21 -0700158 def eapTLSFragment(self, code, pkt_id, frag, data="", data_len = 0):
159 req_type = EAP_TYPE_TLS
160 if frag == 0:
161 flags = TLS_LENGTH_INCLUDED | TLS_MORE_FRAGMENTS
162 elif frag > 0:
163 flags = TLS_MORE_FRAGMENTS
164 else:
165 #last fragment
166 flags = 0
167 if data_len == 0:
168 data_len = len(data)
169 if flags & TLS_LENGTH_INCLUDED:
170 flags_dlen = pack("!BL", flags, data_len)
171 return pack("!BBHB", code, pkt_id, 5+len(flags_dlen)+len(data), req_type) + flags_dlen + data
172 flags_str = pack("!B", flags)
173 return pack("!BBHB", code, pkt_id, 5+len(flags_str)+len(data), req_type) + flags_str + data
174
A R Karthicka2e53d62016-02-19 17:38:30 -0800175 def eapol_send(self, eapol_type, eap_payload):
Chetan Gaonker35cb16f2016-03-02 03:05:28 -0800176 return sendp(self.llheader/self.eapol(eapol_type, eap_payload), iface=self.intf)
A R Karthicka2e53d62016-02-19 17:38:30 -0800177
178 def eapol_recv(self):
A R Karthickaa10a202016-08-15 15:06:21 -0700179 p = self.s.recv(self.max_recv_size)[14:]
A R Karthicka2e53d62016-02-19 17:38:30 -0800180 vers,pkt_type,eapollen = unpack("!BBH",p[:4])
181 print "Version %d, type %d, len %d" %(vers, pkt_type, eapollen)
182 assert_equal(pkt_type, EAPOL_EAPPACKET)
183 return p[4:]
184
A R Karthick05d9b5f2016-06-08 11:53:54 -0700185 def eapol_scapy_recv(self, cb = None, lfilter = None, count = 1, timeout = 5):
Chetan Gaonker5b366302016-03-21 16:18:21 -0700186 def eapol_default_cb(pkt): pass
187 if cb is None:
188 cb = eapol_default_cb
A R Karthick05d9b5f2016-06-08 11:53:54 -0700189 return sniff(prn = cb, lfilter = lfilter, count = count, timeout = timeout, opened_socket = self.recv_sock)
Chetan Gaonker5b366302016-03-21 16:18:21 -0700190
A R Karthicka2e53d62016-02-19 17:38:30 -0800191 def eapol_start(self):
192 eap_payload = self.eap(EAPOL_START, 2)
193 return self.eapol_send(EAPOL_START, eap_payload)
194
A R Karthick307483c2016-06-06 17:05:19 -0700195 def eapol_logoff(self):
196 eap_payload = self.eap(EAPOL_LOGOFF, 2)
197 return self.eapol_send(EAPOL_LOGOFF, eap_payload)
198
A R Karthicka2e53d62016-02-19 17:38:30 -0800199 def eapol_id_req(self, pkt_id = 0, user = USER):
200 eap_payload = self.eap(EAP_RESPONSE, pkt_id, EAP_TYPE_ID, user)
201 return self.eapol_send(EAPOL_EAPPACKET, eap_payload)
202
Chetan Gaonker4a25e2b2016-03-04 14:45:15 -0800203 def eap_md5_challenge_recv(self,rad_pwd):
204 PASS = rad_pwd
205 print 'Inside EAP MD5 Challenge Exchange'
A R Karthickaa10a202016-08-15 15:06:21 -0700206 p = self.s.recv(self.max_recv_size)[14:]
Chetan Gaonker4a25e2b2016-03-04 14:45:15 -0800207 vers,pkt_type,eapollen = unpack("!BBH",p[:4])
208 print "EAPOL Version %d, type %d, len %d" %(vers, pkt_type, eapollen)
209 code, pkt_id, eaplen = unpack("!BBH", p[4:8])
210 print "EAP Code %d, id %d, len %d" %(code, pkt_id, eaplen)
211 assert_equal(code, EAP_REQUEST)
212 reqtype = unpack("!B", p[8:9])[0]
213 reqdata = p[9:4+eaplen]
214 print 'Request type is %d' %(reqtype)
215 assert_equal(reqtype, EAP_TYPE_MD5)
216 challenge=pack("!B",pkt_id)+PASS+reqdata[1:]
217 print "Generating md5 challenge for %s" % challenge
218 return (challenge,pkt_id)
219
220 def eap_Status(self):
221 print 'Inside EAP Status'
A R Karthickaa10a202016-08-15 15:06:21 -0700222 p = self.s.recv(self.max_recv_size)[14:]
Chetan Gaonker4a25e2b2016-03-04 14:45:15 -0800223 code, id, eaplen = unpack("!BBH", p[4:8])
224 return code
225