-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathejemplo33.py
executable file
·258 lines (222 loc) · 8.22 KB
/
ejemplo33.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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import time,sys,datetime,re,csv,os,errno
import ctypes,ctypes.util
import threading
from threading import Thread, Lock
from subprocess import Popen, PIPE
from signal import SIGINT, signal
import logging
import logging.handlers
from netaddr import *
from netaddr.core import NotRegisteredError
logging.getLogger("scapy.runtime").setLevel(logging.ERROR)
from scapy.all import *
# define variables
intfparent='wlan1'
intfmon='mon0'
workdir='./capture'
csvsummary= workdir + '/' + 'ap_summary.csv'
channel=''
clients = []
uni = 0
mach = []
manuf =''
ap_list = []
ap_plist = []
sysloglevel=4 ## (debug)7------0(not syslog)
first_pass = 1
lock = Lock()
DN = open(os.devnull, 'w')
# Scapy packet handler function
def PacketHandler(pkt):
global ap_plist, ap_list,csvwriter
if pkt.haslayer(Dot11):
if pkt.haslayer(Dot11ProbeReq): ## probe request
mac = str(pkt.addr2)
if pkt.haslayer(Dot11Elt):
if pkt.ID == 0:
ssid = pkt.info
if ssid and ssid not in clients:
manuf = get_oui(mac)
clients.append([mac,manuf,ssid])
print "CLIENT MAC: %s (%s) PROBING FOR AP: %s" %(mac,manuf,ssid)
elif pkt.haslayer(Dot11ProbeResp): ## probe responese
bssid = pkt.addr3
if bssid not in ap_plist:
ap_plist.append(bssid)
manuf = get_oui(bssid)
capability = pkt.sprintf("{Dot11Beacon:%Dot11Beacon.cap%}{Dot11ProbeResp:%Dot11ProbeResp.cap%}")
crypto = set()
p = pkt[Dot11Elt]
while isinstance(p, Dot11Elt):
if p.ID == 0:
essid = p.info
elif p.ID == 3:
if len(p.info) == 1:
channel = int(ord(p.info))
else:
channel=0
elif p.ID == 48:
crypto.add("WPA2")
elif p.ID == 221 and p.info.startswith('\x00P\xf2\x01\x01\x00'):
crypto.add("WPA")
p = p.payload
if not crypto:
if 'privacy' in capability:
crypto.add("WEP")
else:
crypto.add("OPN")
print "AP ESSID: %s BSSID: %s (%s) ENC: %s CHANNEL: %s - PROBE RESPONSE SAVED!" %(essid,bssid,manuf,' / '.join(crypto),channel)
filename=workdir + '/' + pkt.info + '_' + bssid.replace(':','') + '.cap'
writer = PcapWriter(filename, append=True)
writer.write(pkt)
writer.close()
elif pkt.type == 0 and pkt.subtype == 8: ## beacon
bssid = pkt.addr3
if bssid not in ap_list:
ap_list.append(bssid)
manuf = get_oui(bssid)
capability = pkt.sprintf("{Dot11Beacon:%Dot11Beacon.cap%}{Dot11ProbeResp:%Dot11ProbeResp.cap%}")
crypto = set()
p = pkt[Dot11Elt]
while isinstance(p, Dot11Elt):
if p.ID == 0:
essid = p.info
elif p.ID == 3:
if len(p.info) == 1:
channel = int(ord(p.info))
else:
channel=0
elif p.ID == 48:
crypto.add("WPA2")
elif p.ID == 221 and p.info.startswith('\x00P\xf2\x01\x01\x00'):
crypto.add("WPA")
p = p.payload
if not crypto:
if 'privacy' in capability:
crypto.add("WEP")
else:
crypto.add("OPN")
hidden_essid = (not essid)
if hidden_essid: essid = 'HiddenEssid!'
print "AP ESSID: %s BSSID: %s (%s) ENC: %s CHANNEL: %s - BEACON SAVED!" %(essid, bssid, manuf, ' / '.join(crypto), channel)
filename=workdir + '/' + pkt.info + '_' + bssid.replace(':','') + '.cap'
writer = PcapWriter(filename, append=True)
csvwriter.writerow([essid,bssid,manuf,'/'.join(crypto),channel])
writer.write(pkt) ; writer.close()
def endsniff(d=False):
return d
def get_oui(mac):
global manuf
maco = EUI(mac)
try:
manuf = maco.oui.registration().org.replace(',',' ')
except NotRegisteredError:
manuf = "Not available"
return manuf
def ProbeReqBroadcast():
sendp(RadioTap()/Dot11(addr1="ff:ff:ff:ff:ff:ff", addr2=RandMAC(), addr3="ff:ff:ff:ff:ff:ff")/Dot11ProbeReq()/Dot11Elt(ID="SSID", info=""), iface=intfmon, count=10)
def ProbeReq(probessid,dst,bssid):
src='00:00:de:ad:be:ef' ## source ip from packets
dst='ff:ff:ff:ff:ff:ff' ## Destination address for beacons and probes
bssid='00:11:22:33:44:55' ## BSSID MAC address for fake AP
count=10
param = Dot11ProbeReq()
essid = Dot11Elt(ID='SSID',info=probessid, len=len(probessid))
dsset = Dot11Elt(ID='DSset',info='\x01')
pkt = RadioTap()/Dot11(type=0,subtype=4,addr1=dst,addr2=src,addr3=bssid)/param/essid/Dot11EltRates()/dsset
print '[*] 802.11 Probe Request: SSID=[%s], count=%d' % (probessid,count)
try:
sendp(pkt,count=count,inter=0.1,verbose=0)
except:
raise
def InitMon():
# Check if monitor device exists
if not os.path.isdir("/sys/class/net/" + intfmon):
if not os.path.isdir("/sys/class/net/" + intfparent):
print "WiFi interface %s does not exist! Cannot continue!" %(intfparent)
exit()
else:
# create monitor interface using iw
cmd = 'iw dev %s interface add %s type monitor >/dev/null 2>&1' % (intfparent, intfmon)
cmd2 = 'ifconfig %s up >/dev/null 2>&1' % (intfmon)
try:
os.system(cmd)
time.sleep(0.3)
os.system(cmd2)
except:
raise
else:
print "Monitor %s exists! Nothing to do, just continuing..." %(intfmon)
def stop(signal, frame):
print ' CTRL+C pressed, exiting...'
endsniff(True)
sys.exit('Closing')
def LoadAPlist():
try:
ifile = open(csvsummary, "r")
csvreader = csv.reader(ifile, delimiter=',', quotechar='"', quoting=csv.QUOTE_NONE,escapechar='\\')
for row in csvreader:
ap_list.append(row[1])
ifile.close()
except Exception, e:
return
def channel_hop(channel=''):
global intfmon, first_pass
channelNum = 0
err = None
while 1:
if channel:
with lock:
monchannel = channel
else:
channelNum +=1
if channelNum > 14:
channelNum = 1
with lock:
first_pass = 0
with lock:
monchannel = str(channelNum)
try:
proc = Popen(['iw', 'dev', intfmon, 'set', 'channel', monchannel], stdout=DN, stderr=PIPE)
except OSError as e:
print '['+R+'-'+W+'] Could not execute "iw"'
os.kill(os.getpid(),SIGINT)
sys.exit(1)
for line in proc.communicate()[1].split('\n'):
if len(line) > 2: # iw dev shouldnt display output unless there's an error
err = 'Channel hopping failed: '+ line
if channel:
time.sleep(.05)
else:
if first_pass == 1:
time.sleep(1)
continue
def checkdir(dir):
try:
os.makedirs(dir)
except OSError as e:
if e.errno != errno.EEXIST:
raise
##### Main loop
# Init monitor mode device
InitMon()
# Check if workdir exists and create it
checkdir(workdir)
# Start channel hopping
hop = Thread(target=channel_hop, args=channel)
hop.daemon = True
hop.start()
# Signal handler init
signal(SIGINT, stop)
# We need a CSV file to save the summary of captured files
LoadAPlist()
ofile = open(csvsummary, "a")
csvwriter = csv.writer(ofile, delimiter=',', quotechar='"', quoting=csv.QUOTE_NONE,escapechar='\\')
# We begin to sniff and capture
try:
sniff(iface=intfmon, prn=PacketHandler, stop_filter=endsniff())
except:
print "Some error avoid sniffing with %s device!" %intfmon
ofile.close()