-
Notifications
You must be signed in to change notification settings - Fork 20
/
ccx_scanner.py
236 lines (210 loc) · 10.5 KB
/
ccx_scanner.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
#!/usr/bin/env python
# -*- coding: iso-8859-15 -*-
#
# BSD 3-Clause License
#
# Copyright (c) 2016, Andrés Blanco ([email protected])
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# * Redistributions of source code must retain the above copyright notice, this
# list of conditions and the following disclaimer.
#
# * Redistributions in binary form must reproduce the above copyright notice,
# this list of conditions and the following disclaimer in the documentation
# and/or other materials provided with the distribution.
#
# * Neither the name of WIG nor the names of its
# contributors may be used to endorse or promote products derived from
# this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
import sys
import time
import pprint
import random
import struct
import pcapy
from impacket import ImpactDecoder
from impacket import dot11
import helpers
from Extensions import ccx
BSSID_KEY = "BSSID"
SSID_KEY = "SSID"
CTRL_IP_ADDR_KEY = "Controller IP Address"
AP_NAME_KEY = "Access Point Name"
ASSOCIATED_CLIENTS_KEY = "Associated Clients"
CHANNEL_KEY = "Channel"
SECURITY_KEY = "Security"
TIMESTAMP_KEY = "Timestamp"
class Scanner(object):
def __init__(self, iface):
self.iface = iface
mac_address = "00:de:ad:be:ef:00"
Receiver(iface, mac_address)
class Receiver(object):
"""
CCX (Cisco Client Extension) Receiver class handles CCX beacon and reassociation response frame reception and
parsing.
"""
def __init__(self, iface, mac_address):
self.devices = dict()
self.iface = iface
self.pd = pcapy.open_live(iface, helpers.PCAP_SNAPLEN, helpers.PCAP_PROMISCOUS, helpers.PCAP_TIMEOUT)
self.mac_address = helpers.get_buffer_from_string_mac_address(mac_address)
# We need to capture beacon and probe response frames to get BSSID, SSID and CCX 85 IE.
# But we also need to get reassociation response frames with CCX 95 IE.
bpf_filter = "(type mgt subtype beacon) or (type mgt subtype probe-resp) or (type mgt subtype reassoc-resp)"
self.pd.setfilter(bpf_filter)
datalink = self.pd.datalink()
if datalink == helpers.PCAP_DLT_IEEE802_11:
self.decoder = ImpactDecoder.Dot11Decoder()
elif datalink == helpers.PCAP_DLT_IEEE802_11_RADIOTAP:
self.decoder = ImpactDecoder.RadioTapDecoder()
else:
raise Exception("Invalid datalink.")
self.run()
def print_report(self):
"""Print all information."""
print "Report"
print "-" * 70
for device in sorted(self.devices.values(), key=lambda d: d[TIMESTAMP_KEY]):
if BSSID_KEY in device:
print "BSSID: %s" % device[BSSID_KEY]
if SSID_KEY in device:
print "SSID: %s" % device[SSID_KEY]
if CHANNEL_KEY in device:
print "Channel: %d" % device[CHANNEL_KEY]
if SECURITY_KEY in device:
print "Security: %s" % device[SECURITY_KEY]
if AP_NAME_KEY in device:
print "Access Point Name: %s" % device[AP_NAME_KEY]
if ASSOCIATED_CLIENTS_KEY in device:
print "Associated Clients: %d" % device[ASSOCIATED_CLIENTS_KEY]
if CTRL_IP_ADDR_KEY in device:
print "Controller IP Address: %s" % device[CTRL_IP_ADDR_KEY]
print "-" * 70
def run(self):
"""Receive and process frames forever."""
while True:
try:
hdr, frame = self.pd.next()
if frame:
self.process_probe_response_frame(frame)
except Exception, e:
print "Exception: %s" % str(e)
except KeyboardInterrupt:
print "Sniffing Process: Caught CTRL+C. Exiting..."
self.print_report()
break
def get_radiotap_header(self):
"""Returns a radiotap header buffer for frame injection."""
buff = str()
buff += "\x00\x00" # Version
buff += "\x0b\x00" # Header length
buff += "\x04\x0c\x00\x00" # Bitmap
buff += "\x6c" # Rate
buff += "\x0c" # TX Power
buff += "\x01" # Antenna
return buff
def get_reassociation_request_frame(self, destination, seq, data):
"""Returns management reassociation request frame header."""
buff = str()
buff += self.get_radiotap_header()
buff += "\x20\x00" # Frame Control - Management - Reassociation Request
buff += "\x28\x00" # Duration
buff += destination # Destination Address- Broadcast
buff += self.mac_address # Source Address
buff += destination # BSSID Address - Broadcast
buff += "\x00" + struct.pack("B", seq)[0] # Sequence Control
# Capabilities
buff += data
return buff
def transmit_reassociation_request(self, bssid, data):
"""Transmit reassociation request frame."""
seq = random.randint(1, 254) # TODO: Fix how we are handling sequence numbers
frame = self.get_reassociation_request_frame(bssid, seq, data)
self.pd.sendpacket(frame)
def process_probe_response_frame(self, data):
"""Process Beacon and Reassociation Response frame searching for CCX IEs and storing information."""
self.decoder.decode(data)
frame_control = self.decoder.get_protocol(dot11.Dot11)
if frame_control.get_type() != dot11.Dot11Types.DOT11_TYPE_MANAGEMENT and \
(frame_control.get_subtype() != dot11.Dot11Types.DOT11_SUBTYPE_MANAGEMENT_BEACON or
frame_control.get_subtype() != dot11.Dot11Types.DOT11_SUBTYPE_MANAGEMENT_REASSOCIATION_RESPONSE):
return
mgt_frame = self.decoder.get_protocol(dot11.Dot11ManagementFrame)
bssid = helpers.get_string_mac_address_from_array(mgt_frame.get_bssid())
if frame_control.get_subtype() == dot11.Dot11Types.DOT11_SUBTYPE_MANAGEMENT_REASSOCIATION_RESPONSE:
reassociation_response = self.decoder.get_protocol(dot11.Dot11ManagementReassociationResponse)
data = reassociation_response._get_element(ccx.CISCO_CCX_IE_IP_ADDRESS_ID)
if data:
if bssid not in self.devices:
self.devices[bssid] = dict()
self.devices[bssid][BSSID_KEY] = bssid
self.devices[bssid][TIMESTAMP_KEY] = int(time.time())
ssid = reassociation_response._get_element(dot11.DOT11_MANAGEMENT_ELEMENTS.SSID)
if ssid and SSID_KEY not in self.devices[bssid]:
self.devices[bssid][SSID_KEY] = ssid
print "UPDATED:"
pprint.pprint(self.devices[bssid])
if CTRL_IP_ADDR_KEY not in self.devices[bssid]:
ccx95 = chr(ccx.CISCO_CCX_IE_IP_ADDRESS_ID) + chr(len(data)) + data
self.devices[bssid][CTRL_IP_ADDR_KEY] = ccx.CiscoCCX95InformationElement(ccx95).get_ip_address()
print "UPDATED:"
pprint.pprint(self.devices[bssid])
else:
if frame_control.get_subtype() == dot11.Dot11Types.DOT11_SUBTYPE_MANAGEMENT_BEACON:
frame = self.decoder.get_protocol(dot11.Dot11ManagementBeacon)
elif frame_control.get_subtype() == dot11.Dot11Types.DOT11_SUBTYPE_MANAGEMENT_PROBE_RESPONSE:
frame = self.decoder.get_protocol(dot11.Dot11ManagementProbeResponse)
else:
return
security = helpers.get_security(frame)
data = frame._get_element(ccx.CISCO_CCX_IE_DEVICE_NAME_ID)
if bssid not in self.devices and data:
self.devices[bssid] = dict()
self.devices[bssid][BSSID_KEY] = bssid
self.devices[bssid][TIMESTAMP_KEY] = int(time.time())
ccx85 = chr(ccx.CISCO_CCX_IE_DEVICE_NAME_ID) + chr(len(data)) + data
ssid = frame.get_ssid().replace("\x00", "")
channel = frame.get_ds_parameter_set()
device_name = ccx.CiscoCCX85InformationElement(ccx85).get_device_name()
associated_clients = ccx.CiscoCCX85InformationElement(ccx85).get_associated_clients()
self.devices[bssid][SSID_KEY] = ssid
self.devices[bssid][CHANNEL_KEY] = channel
self.devices[bssid][AP_NAME_KEY] = device_name
self.devices[bssid][ASSOCIATED_CLIENTS_KEY] = associated_clients
self.devices[bssid][SECURITY_KEY] = security
pprint.pprint(self.devices[bssid])
if CTRL_IP_ADDR_KEY not in self.devices[bssid]:
data = str()
data += struct.pack("H", frame.get_capabilities()) # capabilities
data += "\x5a\x00" # listen intervals
data += helpers.get_buffer_from_string_mac_address(bssid)
data += frame.get_header_as_string()[12:]
self.transmit_reassociation_request(helpers.get_buffer_from_string_mac_address(bssid), data)
elif bssid in self.devices and data:
if CTRL_IP_ADDR_KEY not in self.devices[bssid]:
data = str()
data += struct.pack("H", frame.get_capabilities()) # capabilities
data += "\x5a\x00" # listen intervals
data += helpers.get_buffer_from_string_mac_address(bssid)
data += frame.get_header_as_string()[12:]
self.transmit_reassociation_request(helpers.get_buffer_from_string_mac_address(bssid), data)
if __name__ == "__main__":
if len(sys.argv) != 2:
sys.exit(-1)
interface = sys.argv[1]
Scanner(interface)