Want to code faster? Our Python Code Generator lets you create Python scripts with just a few clicks. Try it now!
A brute-force attack is an activity that involves repetitive attempts of trying many password combinations to break into a system that requires authentication. There are a lot of open-source tools to brute-force SSH in Linux, such as Hydra, Nmap, and Metasploit. However, in this tutorial, you will learn how you can make an SSH brute-force script in the Python programming language.
Read Also: How to Make a Subdomain Scanner in Python.
We'll use the paramiko library that provides us with an easy SSH client interface. Let's install it:
We're using colorama just for printing in colors, nothing else.
Open up a new Python file and import the required modules:
Defining some colors we gonna use:
Now let's build a function that given hostname, username, and password, it tells us whether the combination is correct:
Master Ethical Hacking with Python by building 35+ Tools from scratch. Get your copy now!
Download EBookA lot to cover here. First, we initialize our SSH Client using paramiko.SSHClient()
class that is a high-level representation of a session with an SSH server.
Second, we set the policy to use when connecting to servers without a known host key; we used paramiko.AutoAddPolicy()
, which is a policy for automatically adding the hostname and new host key to the local host keys and saving it.
Finally, we try to connect to the SSH server and authenticate to it using the client.connect()
method with 3 seconds of a timeout, this method raises:
socket.timeout
: when the host is unreachable during the 3 seconds.paramiko.AuthenticationException
: when the username and password combination is incorrect.paramiko.SSHException
: when a lot of logging attempts were performed in a short period of time, in other words, the server detects it is some kind of brute-force, we will know that and sleep for a minute and recursively call the function again with the same parameters.If none of the above exceptions were raised, then the connection is successfully established, and the credentials are correct; we return True in this case.
Since this is a command-line script, we will parse arguments passed in the command line using argparse
:
We basically parsed arguments to retrieve the hostname, username, and password list file and then iterate over all the passwords in the wordlist, I ran this on my local SSH server. Here is a screenshot:
wordlist.txt is a Nmap password list file that contains more than 5000 passwords. I've essentially grabbed it from Kali Linux OS under the path "/usr/share/wordlists/nmap.lst".However, if you want to generate your own custom wordlist, I encourage you to use the Crunch tool.
DISCLAIMER: Test this with a server or machine you have permission to test on. Otherwise, it isn't our responsibility.
Alright, we are basically done with this tutorial. See how you can extend this script to use multi-threading for fast brute-forcing.
If you wish to brute force FTP servers instead, check this tutorial.
Finally, we have an Ethical Hacking with Python Ebook, where we build over 35 hacking tools and scripts with Python from scratch! Make sure to check it out if you're interested.
RELATED: How to Make a Port Scanner in Python.
Happy Brute-forcing ♥
Found the article interesting? You'll love our Python Code Generator! Give AI a chance to do the heavy lifting for you. Check it out!
View Full Code Generate Python Code
Got a coding query or need some guidance before you comment? Check out this Python Code Assistant for expert advice and handy tips. It's like having a coding tutor right in your fingertips!