-
Notifications
You must be signed in to change notification settings - Fork 4
/
wifi.py
executable file
·205 lines (163 loc) · 6.34 KB
/
wifi.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
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
import threading
import time
from Peach.publisher import *
from Peach.Utilities.common import *
try:
from ctypes import *
class pcap_pkthdr(Structure):
"""
struct pcap_pkthdr {
struct timeval ts; /* time stamp */
bpf_u_int32 caplen; /* length of portion present */
bpf_u_int32 len; /* length this packet (off wire) */
};
"""
_fields_ = [
('ts', c_uint64),
('caplen', c_uint),
('len', c_uint)
]
except:
pass
class Wifi(Publisher):
"""
AirPcap I/O inteface. Supports sending beacons and standard I/O.
"""
PCAP_ERRBUF_SIZE = 256
AIRPCAP_LT_802_11 = 1 # plain 802.11 link type. Every packet in the buffer contains the raw 802.11 frame, including MAC FCS.
AIRPCAP_LT_802_11_PLUS_RADIO = 2 # 802.11 plus radiotap link type. Every packet in the buffer contains a radiotap header followed by the 802.11 frame. MAC FCS is included.
AIRPCAP_LT_UNKNOWN = 3 # Unknown link type. You should see it only in case of error.
AIRPCAP_LT_802_11_PLUS_PPI = 4 # 802.11 plus PPI header link type. Every packet in the buffer contains a PPI header followed by the 802.11 frame. MAC FCS is included.
def __init__(self, channel=5, mac="\xca\xce\xca\xce\xca\xce", device="\\\\.\\airpcap00"):
Publisher.__init__(self)
self.mac = mac
self.device = device
self.channel = channel
self.pcap = None
self.air = None
self.beacon = None
self.beaconThread = None
self.beaconStopEvent = None
self.probe = None
self.association = None
# Don't initalize here to avoid a deepcopy
# issue!
#self.beaconStopEvent = threading.Event()
#self.beaconStopEvent.clear()
def start(self):
if self.beaconStopEvent is None:
self.beaconStopEvent = threading.Event()
errbuff = c_char_p("A" * self.PCAP_ERRBUF_SIZE) # Must pre-alloc memory for error message
self.pcap = cdll.wpcap.pcap_open_live(self.device, 65536, 1, 1000, errbuff)
if self.pcap == 0:
raise Exception(errbuff.value)
self.air = cdll.wpcap.pcap_get_airpcap_handle(self.pcap)
cdll.airpcap.AirpcapSetDeviceChannel(self.air, self.channel)
cdll.airpcap.AirpcapSetLinkType(self.air, self.AIRPCAP_LT_802_11)
self.beaconStopEvent.clear()
def stop(self):
if self.pcap is not None:
cdll.wpcap.pcap_close(self.pcap)
self.pcap = None
if self.beaconThread is not None:
self.beaconStopEvent.set()
self.beaconThread.join()
self.beaconThread = None
self.beaconStopEvent.clear()
self.beacon = None
def send(self, data):
cdll.wpcap.pcap_sendpacket(self.pcap, data, len(data))
def receive(self, size=None):
"""
Receive some data.
@type size: integer
@param size: Number of bytes to return
@rtype: string
@return: data received
"""
cdll.wpcap.pcap_next_ex.argtypes = [
c_void_p,
POINTER(POINTER(pcap_pkthdr)),
POINTER(POINTER(c_ubyte))
]
header = pointer(pcap_pkthdr())
header.len = 0
header.caplen = 0
pkt_data = POINTER(c_ubyte)()
while True:
res = cdll.wpcap.pcap_next_ex(self.pcap, byref(header), byref(pkt_data))
if res < 0:
# error reading packets
error = cdll.wpcap.pcap_geterr(self.pcap)
raise Exception(error)
elif res == 0:
# timeout hit
continue
# Convert byte array to binary string
data = ""
cnt = 0
for b in pkt_data:
if cnt >= header.contents.len:
break
cnt += 1
data += chr(b)
# Check mac destination, assumes we want broadcasts
dest_mac = data[4:10]
src_mac = data[10:16]
if src_mac == self.mac:
continue
if not (dest_mac == '\xff\xff\xff\xff\xff\xff' or dest_mac == self.mac):
continue
#print ">> Pkt type: %2x" % ord(data[0])
# Is probe request?
if ord(data[0]) == 0x40 and self.probe is not None:
print(">> Sending probe %2x %2x %2x %2x" % (ord(self.probe[-4]),
ord(self.probe[-2]),
ord(self.probe[-3]),
ord(self.probe[-1])))
self.send(self.probe)
continue
# Is Association request?
if ord(data[0]) == 0x00 and self.association is not None:
print(">> Sending association")
self.send(self.association)
break
if hasattr(self, "publisherBuffer"):
self.publisherBuffer.haveAllData = True
#return data
def _sendBeacon(self):
while not self.beaconStopEvent.isSet():
cdll.wpcap.pcap_sendpacket(self.pcap, self.beacon, len(self.beacon))
time.sleep(0.1)
def _startBeacon(self):
if self.beacon is None:
return
if self.beaconThread is not None:
return
self.beaconThread = threading.Thread(target=self._sendBeacon)
self.beaconThread.start()
def call(self, method, args):
if method == 'beacon':
self.beacon = args[0]
self._startBeacon()
elif method == 'probe':
print(">> Setting probe")
self.probe = args[0]
printHex(self.probe)
elif method == 'association':
print(">> Setting association")
self.association = args[0]
printHex(self.association)
def connect(self):
"""
Called to connect or open a connection/file.
"""
pass
def close(self):
"""
Close current stream/connection.
"""
pass