forked from violentshell/rollmac
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrollmac.py
More file actions
212 lines (163 loc) · 6.19 KB
/
rollmac.py
File metadata and controls
212 lines (163 loc) · 6.19 KB
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
import bs4 as soup
import datetime
import json
import logging
import psutil
import requests
import spoofmac
import subprocess
import sys
import time
# noinspection PyAttributeOutsideInit
class RollMac:
def __init__(self):
self.setup_logging()
self.get_config()
self.info = {'domain': self.domain}
self.over_limit = True
self.old_t_mbytes = 0
def run(self):
# Will hit on first loop, then on over limit
if self.over_limit:
# Spoof Mac
self.spoof()
# Let interface come back up
time.sleep(1)
# Re-connect
self.connect()
# Get redirect url from wpad
self.get_redirect()
# login to page with post values
if self.login() == 200:
self.log.info('Reconnected to AP with new mac. Resetting data limit')
# Reset limits ( Data is done automatically by flapping interface)
self.over_limit = False
self.start_time = datetime.datetime.now()
elif not self.over_limit:
time.sleep(15)
# Print output and check limits
self.over_limit = self.limit_check()
def spoof(self):
"""
:return: Nothing. Sets mac on interface
"""
self.mac = spoofmac.random_mac_address()
spoofmac.set_interface_mac(self.interface, self.mac)
self.log.debug('Changed mac of {} to: {}'.format(self.interface, self.mac))
def limit_check(self):
"""
:return: True if within 5% of time/data limit; False if outside 5% of time/data limit
"""
# Check time limit and MB limit
if self.time_check() or self.data_check():
return True
return False
def time_check(self):
"""
:return: True if within 5 min of limit; False if outside 5 min of limit
"""
try:
self.start_time
except NameError:
self.start_time = datetime.datetime.now()
self.log.info("Connected at {}".format(datetime.datetime.now().strftime('%H:%M:%S')))
# Online time in minutes
online_time = (datetime.datetime.now() - self.start_time).seconds / 60
# For info
self.log.info('{} minutes remaining'.format(round(self.time_limit - online_time - 5)))
# Return True if over limit threshold
if self.time_limit - online_time <= 5:
self.log.warn('Over time limit.')
return True
return False
def data_check(self):
"""
:return: True if within 5% of MB limit; False if outside 5% of MB limit
"""
# Raw interface bytes TODO Linux compatible?
netbytes = psutil.net_io_counters(True)[self.interface]
# Convert total to MB
t_mbytes = (netbytes.bytes_recv + netbytes.bytes_sent) >> 20
# For info
self.log.debug('{} MB remaining'.format(self.limit - t_mbytes))
# Return True if over limit threshold
if t_mbytes >= (self.limit - (5 / 100 * self.limit)):
self.log.warn('Over data limit.')
return True
return False
def connect(self):
"""
:return: True if connected, False for fail
"""
netsh = subprocess.check_output('netsh wlan connect name="{}"'.format(self.ssid))
if b'Connection request was completed successfully.' in netsh:
return self.log.debug('Re-assosiated with {}'.format(self.ssid))
def retry_get(self, url):
"""
:param url: string of a url to fetch
:return: data if it works, raises error if not
"""
for i in range(5):
try:
data = requests.get(url)
return data
except:
self.log.warn('Get failed for {} try number {}'.format(url, i))
time.sleep(3)
raise ConnectionError
def get_redirect(self):
"""
:return: url of the page redirected to from wpad
"""
wpad_data = self.retry_get('http://wpad.{}'.format(self.info['domain']))
if wpad_data.status_code != 200:
self.log.error('Failed to retrieve wpad from {}'.format(self.info['domain']))
sys.exit()
so = soup.BeautifulSoup(wpad_data.content, 'lxml')
self.info['url'] = so.find('meta', attrs={'http-equiv': 'refresh'})['content'].partition('=')[2]
self.log.info('Found redirect url: {}'.format(self.info['url']))
def login(self):
"""
:return: Status code for our post to the login page
"""
logon_page = self.retry_get(self.info['url'])
so2 = soup.BeautifulSoup(logon_page.content, 'lxml')
form = so2.find('form')
post_data = form.find_all('input', attrs={'type': 'hidden'})
post_dict = {}
for entry in post_data:
post_dict[entry['name']] = entry['value']
self.log.debug('Login form data: {}'.format(post_dict))
return requests.post(form['action'], data=post_dict).status_code
def get_config(self):
try:
with open('conf.json', 'r') as f:
self.config = json.load(f)
self.ssid = self.config['ssid']
self.limit = self.config['MB_limit']
self.time_limit = self.config['TIME_limit']
self.interface = self.config['interface']
self.domain = self.config['domain']
except Exception as e:
sys.exit(e)
def setup_logging(self):
# Silence request module
logging.getLogger("requests").setLevel(logging.WARNING)
# Setup log vars
self.log = logging.getLogger('')
self.format = logging.Formatter("%(asctime)s - %(name)s - %(levelname)s - %(message)s")
# Main logger
rootlogger = logging.getLogger()
rootlogger.setLevel(logging.DEBUG)
# Print to console
consolehandler = logging.StreamHandler()
consolehandler.setFormatter(self.format)
rootlogger.addHandler(consolehandler)
# Good to go
self.log.debug('Logging setup complete')
if __name__ == '__main__':
# Init class
main = RollMac()
# Loop forever
while True:
main.run()