A few days, maybe a week ago I started trying to script out an automated recon scanner in Python 3, based off ideas I’m getting from a TheCyberMentor course.
The idea:
Create one python script to somehow do the following
nmap scan of a host
Based off the results of nmap scan, do a gobuster scan of only the http ports
Based off the results of nmap scan, do a subdir wfuzz of only the http ports
Save results of each scan in a new folder, named by me, so that I can use it with HTB
This morphed into:
Name a folder to dump scans into
Ask for a hostname/IP
Run two nmap scans to save time. One -p-, then feed the open ports into a longer -A –script=vuln scan.
Run a gobuster scan
Run a wfuzz scan
In the end I made it work. Code below with brief explanations. Might be useful if you’re rolling your own.
The Code
I made a cool splash in MOTD ASCII art format. Script clears the terminal, then presents motd.
First Tasks
The first thing the script does is check to see if it was run as root. If not, it exits. It then asks you for a name for a new folder, then makes that folder. It then asks for a hostname/ip, then saves that input as target_IP for later use.
Initial faster nmap scan
The next thing the script does is start a fast all-port nmap scan and saves the output under the new folder. At get_ports_open, it then reads the nmap scan results, greps through the following pipeline: Line starts with a number, contains the word “open”, prints the first column of that line, removes “/tcp”, saves to ports_open.txt.
It then basically does the same thing for http ports.
Nmap aggressive scan
The script then starts the second, longer, more aggressive nmap scan of only the ports listed in ports_open.txt. I found a cool “-p $(tr ‘\n’ , <./ports_open.txt)” line on stack overflow which tells nmap to scan ports from a file line by line. This works very well.
Gobuster scan
The script then moves on to gobuster. It uses the seclists raft-large wordlist to fuzz for directories found in ports_http.txt.
This one was tricky for me to figure out.
I had to open ports.http.txt, then start a FOR loop: for each line in the file, do a gobuster scan.
Python was opening the lines in ports_http.txt as an array (list), and I couldn’t just concatenate the lines with the os.system commands because the os.system commands are strings. I kept getting a “you can’t concatenate list with str” error and had to figure out how to make both the list and string values either all list, or all strings. It was easier to re-declare everything as strings, then concatenate each of these individual declarations.
I also ran into the issue of having line breaks in the ports_http.txt file, from back where I told nmap to pull the http ports from the initial scan and paste them into a new ports_http.txt file. So, I also had to google and implement a line replace in the for loop, replacing “\n” for “” in each line iteration.
The loop then renames and moves the scan result at the end of each for loop, placing the actual port # in the name of the resulting scan so that you don’t just end up with multiple copies of “3_gobuster_scan.txt”.
Wfuzz scan
So the wfuzz scan does the exact same thing as the gobuster scan above, in the same manner.
A tricky bit that I discovered after running a few wfuzz scans, that I didn’t think of because I have rarely used it for HackTheBox so far, is that the wfuzz output gives you the http response code for every single fuzz done, that you just grep through or sort afterward. So, I looked up all the response codes, figured that ehhh, maybe I don’t need any of the 500 responses, which is perhaps a mistake, and added “–sc 200,201,202,203,204,205,206” to tell wfuzz to only output the 200 responses.
Cleanup
The last part of the script just deletes the ports_open.txt and ports_http.txt files.
I thought maybe I should move them into the folder the the script creates, like the other scan outputs, but decided that this is stupid if I’m already going to be reading the scan results anyway.
In the future, I still plan on parsing through results and combining them into some sort of quick reference textfile. That’s for later, not now, I guess.
Final Thoughts
This was interesting, and moved on much faster than I thought it would. While I do review python code for CTF stuff, and do read about it, I have never made anything in python before in my life, not even a “Hello World”. This was a smooth-enough first project.
Now that I know for a fact how some of the loops work, and have some code to look back on that will jog my memory, I intend to start leveraging python more in practice, not just in mental games and “oh what-if” scenarios.
It’s just too simple to not do. I mean it’s basically just C++ psuedocode, and finding/using the “while” method was cool, I bet there’s more stuff out there than while and for loops.
python
I feedback.
Let me know what you think of this article on twitter @cpardue09 or leave a comment below!