-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwhosopen
205 lines (167 loc) · 7.77 KB
/
whosopen
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
#!/bin/python3
# Look for open wifi hotspots, connect, harvest ip
# Main issue: open spots are not efficiently found (bot grabs hidden too).
# Future update: ensure only open are grabbed
from os import system as sys # Allows me to control network manager
from os import geteuid as usertype # Allows me to prevent non-root use
from sys import exit as terminate # Allows me to generate exit codes
from sys import argv # Allows me to read arguments
from termcolor import colored as color # Allows me to make the terminal display colorful
from re import findall as find # Allows me to use regular expressions
from urllib import request # Allows me to send a web request to get the ip
from time import sleep # Allows me to limit the speed, reducing CPU demand
# ============ Functions ============
def info(content, bad=False):
'''
This prints info to the terminal in a fancy way
'''
if not bad:
print(color('[i] ', 'blue') + color(content, 'white'))
else:
print(color('[X] ', 'red') + color(content, 'white'))
def scan(): # Function 1
'''
This orders a scan of the environment and displays the results
if no hotspots are located. This is the main function of the script.
'''
sys('sudo nmcli dev wifi rescan') # Scan environment now
sys('sudo nmcli dev wifi > hotspots') # Send info over environment to file
sys('sudo chmod 755 hotspots') # Change this file's permissions
with open('hotspots', 'r') as file: # Open the new file in Python
spots = file.read() # Save the file's content to spots
sys('clear') # Clears the terminal
print(color(spots, 'white')) # Prints wifi hotspots around you
# 1. Split file by newline
spots = spots.split('\n')
# 2. Determine if there's an open spot
for spot in spots:
try:
bssid = spot.split(' ')[8] # At [8] lies the BSSID
except: # Something is going wrong
with open('blackbox', 'a') as blackbox: # Open bug record file
blackbox.write(str(spot.split(' ')) + '\n') # Add the info for later review
continue
if '--' in spot and not inBlacklist(bssid): # If the hotspot is open and not in the blacklist
# 3. Connect to hotspot
if connect(bssid) and internetAccess():
ip = harvestIP()
if not ip == 'err': # If the error code was not returned
saveInfo(ip) # Write the info to the file
continue
else: # If for some reason an error popped up
addToBlacklist(bssid)
continue
else:
addToBlacklist(bssid)
continue
def connect(bssid): # Function 2
'''
This connects to a hotspot
:Param: string BSSID
:Return: bool indicating status
'''
# 1. Connect to wifi hotspot
info('Attempting to connect to hotspot')
sys('sudo nmcli dev wifi rescan')
sys('sudo nmcli dev wifi connect %s > status' % bssid) # Connect to BSSID and output to file
sys('sudo chmod 755 status')
with open('status', 'r') as file:
if 'successfully activated' in file.read():
sys('sudo rm status') # Delete temporary file
info('Connected')
return True # True represents that the connection was successful
else:
sys('sudo rm status')
info('Failed to connect', bad=True)
return False # False represents that the connection failed
def internetAccess(): # Function 3
'''
This function determines if the connection has internet by pinging
Google's DNS
:Return: bool representing the ability to access internet
'''
info('Testing internet access')
sys('ping -c4 8.8.8.8 > pingInfo') # Pinging Google's DNS server should work to identify access; does this 4x
sys('sudo chmod 755 pingInfo')
with open('pingInfo', 'r') as file:
try:
content = file.read().split('\n')[7].split(' ')[3] # This gets the value associated with recieved responses
if int(content) > 0: # As long as one ICMP ping was recieved
info('Internet access confirmed')
return True # True represents internet access
else:
info('No internet access', bad=True)
return False
except Exception as e: # Something has gone wrong. Report failure
info('Error: %s' % str(e)) # These failures dont tend to be an issue; may indicate authentication requirement
return False
def inBlacklist(bssid):
'''
Determines if bssid is in blacklist
:Param bssid: string BSSID
:Return: bool representing membership; true if in, false otherwise
'''
try:
with open('blacklist.txt', 'r') as file: # Open blacklist file
content = file.read() # Get content of blacklist
if bssid in content: # If you can find the requested BSSID, it has been blacklisted
return True # True represents that it was found
else:
return False
except: # Likely cause is that the file does not exist as it hasn't been created yet
return False
def addToBlacklist(bssid):
'''
This function adds a bssid to the blacklist
:Param: string bssid
:Return: None
'''
with open('blacklist.txt', 'a') as file: # Create or append to the blacklist
file.write(bssid + '\n') # Write the bad BSSID to blacklist file
info('Added %s to blacklist' % bssid, bad=True)
def harvestIP(): # Function 4
'''
This retrieves an IP address
:Return: string ip
'''
info('Trying to get IP address')
try:
# The HTML variable is obtained by asking duckduckgo what's the ip and decoding the response
html = request.urlopen('https://duckduckgo.com/?q=whats+my+ip&ia=answer').read().decode('utf-8')
visIP = find('[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}', string=html)[0] # Find IP in request HTTP response
with open('bufferIP', 'a') as file: # Potentially unnecessary premature saving of found data (will be removed later)
file.write(str(visIP) + '\n')
return visIP # Return IP
except:
info('Faild to acquire IP address', bad=True)
return 'err' # If err, the bot failed to get a correct response
def saveInfo(ip): # Function 5
'''
This saves the ip to the output list
:Param ip: string ip address
:Return: None
'''
try:
with open('ips.txt', 'a') as file: # This is the file that will contain the sweet sweet IP addresses
file.write(ip + '\n') # Wirte each address and a newline character
info('Saved IP address to file')
except Exception as e:
info('Error: %s' % str(e)) # This will help me debug if something goes wrong
# ============ START HUNT ============
try: # Get sleep time argument if there
wait = float(argv[1]) # This must be a numerical value
except:
wait = 3 # If no numercial value was inputted, default wait is three seconds
# 1. Determine user type, require root
if not usertype() == 0: # If the user is not root
info('Must run script as root', bad=True) # Scold user
terminate(1) # Report failure
# 2. Start hunting loop
ct = 0
while True:
scan()
info('Current iteration: %s' % str(ct))
info('Waiting for %s seconds ...' % str(wait))
ct += 1
sleep(wait)
terminate(0) # CTRL+C to end. This is the normal way of exiting