A R Karthick | 05d9b5f | 2016-06-08 11:53:54 -0700 | [diff] [blame] | 1 | # |
Chetan Gaonker | cfcce78 | 2016-05-10 10:10:42 -0700 | [diff] [blame] | 2 | # 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 Karthick | 05d9b5f | 2016-06-08 11:53:54 -0700 | [diff] [blame] | 7 | # |
Chetan Gaonker | cfcce78 | 2016-05-10 10:10:42 -0700 | [diff] [blame] | 8 | # http://www.apache.org/licenses/LICENSE-2.0 |
A R Karthick | 05d9b5f | 2016-06-08 11:53:54 -0700 | [diff] [blame] | 9 | # |
Chetan Gaonker | cfcce78 | 2016-05-10 10:10:42 -0700 | [diff] [blame] | 10 | # 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 Karthick | a2e53d6 | 2016-02-19 17:38:30 -0800 | [diff] [blame] | 16 | #### Authentication parameters |
Chetan Gaonker | 35cb16f | 2016-03-02 03:05:28 -0800 | [diff] [blame] | 17 | from scapy.all import * |
A R Karthick | aa10a20 | 2016-08-15 15:06:21 -0700 | [diff] [blame] | 18 | from scapy_ssl_tls.ssl_tls import * |
A R Karthick | a2e53d6 | 2016-02-19 17:38:30 -0800 | [diff] [blame] | 19 | from socket import * |
| 20 | from struct import * |
A R Karthick | a2e53d6 | 2016-02-19 17:38:30 -0800 | [diff] [blame] | 21 | import sys |
| 22 | from nose.tools import assert_equal, assert_not_equal, assert_raises, assert_true |
| 23 | |
| 24 | USER = "raduser" |
| 25 | PASS = "radpass" |
| 26 | WRONG_USER = "XXXX" |
| 27 | WRONG_PASS = "XXXX" |
| 28 | NO_USER = "" |
| 29 | NO_PASS = "" |
| 30 | DEV = "tap0" |
| 31 | ETHERTYPE_PAE = 0x888e |
| 32 | PAE_GROUP_ADDR = "\xff\xff\xff\xff\xff\xff" |
| 33 | EAPOL_VERSION = 1 |
| 34 | EAPOL_EAPPACKET = 0 |
| 35 | EAPOL_START = 1 |
| 36 | EAPOL_LOGOFF = 2 |
| 37 | EAPOL_KEY = 3 |
| 38 | EAPOL_ASF = 4 |
| 39 | EAP_REQUEST = 1 |
| 40 | EAP_RESPONSE = 2 |
| 41 | EAP_SUCCESS = 3 |
| 42 | EAP_FAILURE = 4 |
| 43 | EAP_TYPE_ID = 1 |
| 44 | EAP_TYPE_MD5 = 4 |
| 45 | EAP_TYPE_MSCHAP = 26 |
| 46 | EAP_TYPE_TLS = 13 |
| 47 | cCertMsg = '\x0b\x00\x00\x03\x00\x00\x00' |
| 48 | TLS_LENGTH_INCLUDED = 0x80 |
A R Karthick | aa10a20 | 2016-08-15 15:06:21 -0700 | [diff] [blame] | 49 | TLS_MORE_FRAGMENTS = 0x40 |
A R Karthick | a2e53d6 | 2016-02-19 17:38:30 -0800 | [diff] [blame] | 50 | |
A R Karthick | a2e53d6 | 2016-02-19 17:38:30 -0800 | [diff] [blame] | 51 | class EapolPacket(object): |
A R Karthick | 05d9b5f | 2016-06-08 11:53:54 -0700 | [diff] [blame] | 52 | |
ChetanGaonker | 6138fcd | 2016-08-18 17:56:39 -0700 | [diff] [blame] | 53 | 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 Karthick | a2e53d6 | 2016-02-19 17:38:30 -0800 | [diff] [blame] | 59 | def __init__(self, intf = 'veth0'): |
| 60 | self.intf = intf |
| 61 | self.s = None |
A R Karthick | aa10a20 | 2016-08-15 15:06:21 -0700 | [diff] [blame] | 62 | self.max_recv_size = 1600 |
A R Karthick | a2e53d6 | 2016-02-19 17:38:30 -0800 | [diff] [blame] | 63 | |
ChetanGaonker | 6138fcd | 2016-08-18 17:56:39 -0700 | [diff] [blame] | 64 | def setup(self, src_mac = 'default'): |
A R Karthick | a2e53d6 | 2016-02-19 17:38:30 -0800 | [diff] [blame] | 65 | 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] |
ChetanGaonker | 6138fcd | 2016-08-18 17:56:39 -0700 | [diff] [blame] | 68 | 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 Gaonker | 5b36630 | 2016-03-21 16:18:21 -0700 | [diff] [blame] | 76 | self.recv_sock = L2Socket(iface = self.intf, type = ETHERTYPE_PAE) |
A R Karthick | a2e53d6 | 2016-02-19 17:38:30 -0800 | [diff] [blame] | 77 | |
| 78 | def cleanup(self): |
| 79 | if self.s is not None: |
| 80 | self.s.close() |
| 81 | self.s = None |
A R Karthick | 05d9b5f | 2016-06-08 11:53:54 -0700 | [diff] [blame] | 82 | |
A R Karthick | a2e53d6 | 2016-02-19 17:38:30 -0800 | [diff] [blame] | 83 | def eapol(self, req_type, payload=""): |
Chetan Gaonker | 35cb16f | 2016-03-02 03:05:28 -0800 | [diff] [blame] | 84 | return EAPOL(version = EAPOL_VERSION, type = req_type)/payload |
A R Karthick | a2e53d6 | 2016-02-19 17:38:30 -0800 | [diff] [blame] | 85 | |
| 86 | def eap(self, code, pkt_id, req_type=0, data=""): |
Chetan Gaonker | 35cb16f | 2016-03-02 03:05:28 -0800 | [diff] [blame] | 87 | return EAP(code = code, id = pkt_id, type = req_type)/data |
A R Karthick | a2e53d6 | 2016-02-19 17:38:30 -0800 | [diff] [blame] | 88 | |
A R Karthick | aa10a20 | 2016-08-15 15:06:21 -0700 | [diff] [blame] | 89 | 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 Karthick | a2e53d6 | 2016-02-19 17:38:30 -0800 | [diff] [blame] | 147 | 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 Karthick | aa10a20 | 2016-08-15 15:06:21 -0700 | [diff] [blame] | 158 | 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 Karthick | a2e53d6 | 2016-02-19 17:38:30 -0800 | [diff] [blame] | 175 | def eapol_send(self, eapol_type, eap_payload): |
Chetan Gaonker | 35cb16f | 2016-03-02 03:05:28 -0800 | [diff] [blame] | 176 | 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] | 177 | |
| 178 | def eapol_recv(self): |
A R Karthick | aa10a20 | 2016-08-15 15:06:21 -0700 | [diff] [blame] | 179 | p = self.s.recv(self.max_recv_size)[14:] |
A R Karthick | a2e53d6 | 2016-02-19 17:38:30 -0800 | [diff] [blame] | 180 | 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 Karthick | 05d9b5f | 2016-06-08 11:53:54 -0700 | [diff] [blame] | 185 | def eapol_scapy_recv(self, cb = None, lfilter = None, count = 1, timeout = 5): |
Chetan Gaonker | 5b36630 | 2016-03-21 16:18:21 -0700 | [diff] [blame] | 186 | def eapol_default_cb(pkt): pass |
| 187 | if cb is None: |
| 188 | cb = eapol_default_cb |
A R Karthick | 05d9b5f | 2016-06-08 11:53:54 -0700 | [diff] [blame] | 189 | return sniff(prn = cb, lfilter = lfilter, count = count, timeout = timeout, opened_socket = self.recv_sock) |
Chetan Gaonker | 5b36630 | 2016-03-21 16:18:21 -0700 | [diff] [blame] | 190 | |
A R Karthick | a2e53d6 | 2016-02-19 17:38:30 -0800 | [diff] [blame] | 191 | def eapol_start(self): |
| 192 | eap_payload = self.eap(EAPOL_START, 2) |
| 193 | return self.eapol_send(EAPOL_START, eap_payload) |
| 194 | |
A R Karthick | 307483c | 2016-06-06 17:05:19 -0700 | [diff] [blame] | 195 | def eapol_logoff(self): |
| 196 | eap_payload = self.eap(EAPOL_LOGOFF, 2) |
| 197 | return self.eapol_send(EAPOL_LOGOFF, eap_payload) |
| 198 | |
A R Karthick | a2e53d6 | 2016-02-19 17:38:30 -0800 | [diff] [blame] | 199 | 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 Gaonker | 4a25e2b | 2016-03-04 14:45:15 -0800 | [diff] [blame] | 203 | def eap_md5_challenge_recv(self,rad_pwd): |
| 204 | PASS = rad_pwd |
| 205 | print 'Inside EAP MD5 Challenge Exchange' |
A R Karthick | aa10a20 | 2016-08-15 15:06:21 -0700 | [diff] [blame] | 206 | p = self.s.recv(self.max_recv_size)[14:] |
Chetan Gaonker | 4a25e2b | 2016-03-04 14:45:15 -0800 | [diff] [blame] | 207 | 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 Karthick | aa10a20 | 2016-08-15 15:06:21 -0700 | [diff] [blame] | 222 | p = self.s.recv(self.max_recv_size)[14:] |
Chetan Gaonker | 4a25e2b | 2016-03-04 14:45:15 -0800 | [diff] [blame] | 223 | code, id, eaplen = unpack("!BBH", p[4:8]) |
| 224 | return code |
| 225 | |