Why bash on CTF if there is Python and AutoRecon
Bash is everywhere. There are no stripped CTF containers and minimal Python images, but sh, grep and wget There are even BusyBox. I met tasks where there was nothing in a remote car except sh and awk - and that was enough for full file system exploration. More details in our article on creating ctf tasks.
The second point is the speed of creation. Glue nmap, Ports extraction and gobuster five lines on the bash. The same in Python with argparse and processing of exceptions - a project for 50+ lines. When you have two hours for ten tasks, this difference decides.
And the third: bash scripts for the pentest — direct implementation of techniques from MITRE ATT&CK. Launch shell scripts on the target system is mapped on the technique T1059.004 Unix Shell (Execution tactics). File System Intelligence Through find and ls T1083 (File and Directory Discovery). Collection of system data through uname, id, cat /etc/os-release T1082 (System Information Discovery) Overtaking passwords through curl in the cycle - T1110.001 (Password Guessing). The Atomic Red Team tests for T1059.004 have "Create and Execute Bash Shell Script" and "Harvest SUID executable files" scenarios - both are executed through sh on Linux. This is exactly what CTF members do: write a script and run to gather information.
Automation of CTF through bash saves cognitive resource. Instead of stuffing the same teams, you focus on the logic of the task – and this is what distinguishes the participant who closes 12 tasks per competition from who closes 4.
Preparing the Environment for Writing Bash Scripts
Before the first script, check three things.
The bash version. bash --version – suitable any 4.x and above. On macOS by default costs bash 3.2 due to licensing restrictions - update through brew install bash. On Windows – WSL2 with any Linux distribution.
The utilities. The scripts from this article work with nmap, gobuster, curl, grep, awk, sed and wc. In Kali Linux and Parrot OS everything is out of the box. On minimal distributions deliver through apt install nmap curl gobuster. Specific versions of value do not have - basic flags are used, stable for years.
Working directory and shebang. Create a folder: mkdir ~/ctf && cd ~/ctf. Each script starts with a line #!/bin/bash - it indicates the interpreter system. After creating the file, make it executable: chmod +x script.sh. Without it get Permission denied – a trap through which every beginner goes without exception. The alternative is to launch through bash script.sh, but it is less convenient with frequent use.
The editor is any: nano for beginners, vim for those who are willing to spend an hour getting used to the modal mode. On CTF scripts are often written directly in the terminal through nano script.sh – raise the GUI editor once.
Reconnaissance automation: from manual commands to one script
The most common routine on CTF is to scan ports, identify services, check web directories. The three teams that always go after each other are the ideal candidate for automating Linux routine tasks. Logic: the script accepts the IP address of the target machine, creates a folder for the results, runs nmap. If HTTP is found on a port, it automatically pits gobuster to re-search the directories. If no HTTP port sticks out, the script will only be limited to the nmap stage.
#!/bin/bash
[ -z "$1" ] && echo "Использование: $0 <IP>" && exit 1
IP="$1"; DIR="recon_$IP"; mkdir -p "$DIR"
echo "[*] Сканирую $IP..."
nmap -sV -sC -oN "$DIR/nmap.txt" "$IP"
WEBPORT=$(grep '/tcp.*http' "$DIR/nmap.txt" | grep -oE '^[0-9]+' | head -1)
[ -n "$WEBPORT" ] && echo "[*] HTTP на :$WEBPORT, gobuster..." \
&& gobuster dir -u "http://$IP:$WEBPORT" \
-w /usr/share/wordlists/dirb/common.txt -o "$DIR/dirs.txt" 2>/dev/null
echo "[+] Результаты в ./$DIR/"
Line analysis of the intelligence script
Construction [ -z "$1" ] checks whether the command line argument is transmitted. Without it, the script will start nmap without a goal and hangs in anticipation of entry - it is unacceptable to spend time on the CTF.
Variable DIR generates the name of the folder of the view recon_10.10.10.5, so that the results of each machine are stored separately and do not overwrite each other. mkdir -p creates a directory without a mistake if it already exists.
Flags nmap -sV -sC include defining service versions and running default NSE scripts. Without them, nmap will only show open port numbers without context – this is not enough for CTF. Flag -oN saves the result in a text format suitable for parsing files by bash tools.
Next is the most interesting reception. Construction grep '/tcp.*http' finds strings of kind 80/tcp open http in the nmap output, then grep -oE '^[0-9]+' Pulls the port number from the beginning of the line. GNU grep flags are used; in BusyBox flag -oE available only if grep is assembled with the EXTENDED_REGEXP option – on the stripped-down systems without it, replace with grep -o '[0-9]\+' (BRE-syntax). If and -o unavailable, use grep '/tcp.*http' "$DIR/nmap.txt" | awk -F/ '{print $1}' | head -1 – a more portable option for minimal BusyBox builds. Through head -1 Take the first HTTP port. If nmap has not found HTTP, variable WEBPORT remains empty, block with gobuster will not be fulfilled. No mistakes, the script is quietly completed.
Redirect 2>/dev/null suppresses the progress bar gobuster, which clogs the terminal at automated start. The results are saved to the file dirs.txt.
To run for multiple purposes, wrap the call into a loop: while read -r ip; do ./recon.sh "$ip"; done < targets.txt. File targets.txt contains one IP per line. On the CTF with a dozen machines, start the command - and go to read the conditions of the tasks while the automation ploughs in the background.
Scripts for overtaking passwords on CTF
Web-form authorization without rate-limiting and CSRF protection is an entry-level CTF classic. Four-digit PIN or password from a short wordlist. According to OWASP, this is A07:2021 (Identification and Authentication Failures). On the CTF, this weakness is a deliberate part of the task. In production, there is a hole that is hit by the hands.
Preparing data before starting the brutforce
Before writing the script, disassemble the request manually. Open Developer Tools in the browser (Network), enter a deliberately incorrect password and fix four parameters: URL forms (for example, http://target:8080/login), method (POST or GET), field names (user, pass) and the text of the answer in case of error (Access denied). Without this manual preparation, the script shoots blind, and you won't know if it worked or not.
#!/bin/bash
URL="http://target:8080/login"; FAIL="Access denied"
WORDLIST="${1:-/usr/share/wordlists/rockyou.txt}"
while read -r pass; do
resp=$(curl -s -d "user=admin" --data-urlencode "pass=${pass}" "$URL")
if ! echo "$resp" | grep -q "$FAIL"; then
echo "[+] Пароль найден: $pass"; exit 0
fi
done < "$WORDLIST"
echo "[-] Совпадений нет"
Variable FAIL contains the wrong password marker – the same line that you have fixed in Developer Tools. Construction ${1:-/usr/share/wordlists/rockyou.txt} takes the path to the wordlist from the first argument, and if it is not transmitted, it puts the default path to rockyou.txt, which in Kali stands out of the box.
Cycle while read -r pass reads the wordlist line by line, without uploading the file to the memory in its entirety. This is critical: rockyou.txt weighs 140+MB, and design for pass in $(cat rockyou.txt) Dumbly kill the shell. Flag -r prevents the interpretation of reverse slacks – without it, the password pa\ss will turn into pass.
curl -s -d ... --data-urlencode sends a POST request: curl automatically combines several -d/--data-urlencode into a single body through &, at the same time --data-urlencode encoding special symbols (&, =, spaces) in the meaning of the password. Flag -s suppresses the progress bar. The server response hits the variable resp, and grep -q quietly checks for an error string. If Access denied not found — password has come up, the script outputs the result and concludes through exit 0.
To accelerate, you can parallelize through xargs -P: cat wordlist.txt | xargs -P 10 -I {} curl -s -d "user=admin&pass={}" "$URL" start 10 streams at a time. But on CTF it is rarely necessary - tasks are designed for short wordlists, where the linear overboard is laid in seconds.
If the task requires a selection of SSH or FTP, it is easier to take hydra: hydra -l admin -P wordlist.txt ssh://target. One line, no scripts. Writing bash scripts for web form hacking is necessary for non-standard cases where hydra does not cope due to the custom response format or the multi-step authentication process.
Bash is everywhere. There are no stripped CTF containers and minimal Python images, but sh, grep and wget There are even BusyBox. I met tasks where there was nothing in a remote car except sh and awk - and that was enough for full file system exploration. More details in our article on creating ctf tasks.
The second point is the speed of creation. Glue nmap, Ports extraction and gobuster five lines on the bash. The same in Python with argparse and processing of exceptions - a project for 50+ lines. When you have two hours for ten tasks, this difference decides.
And the third: bash scripts for the pentest — direct implementation of techniques from MITRE ATT&CK. Launch shell scripts on the target system is mapped on the technique T1059.004 Unix Shell (Execution tactics). File System Intelligence Through find and ls T1083 (File and Directory Discovery). Collection of system data through uname, id, cat /etc/os-release T1082 (System Information Discovery) Overtaking passwords through curl in the cycle - T1110.001 (Password Guessing). The Atomic Red Team tests for T1059.004 have "Create and Execute Bash Shell Script" and "Harvest SUID executable files" scenarios - both are executed through sh on Linux. This is exactly what CTF members do: write a script and run to gather information.
Automation of CTF through bash saves cognitive resource. Instead of stuffing the same teams, you focus on the logic of the task – and this is what distinguishes the participant who closes 12 tasks per competition from who closes 4.
Preparing the Environment for Writing Bash Scripts
Before the first script, check three things.
The bash version. bash --version – suitable any 4.x and above. On macOS by default costs bash 3.2 due to licensing restrictions - update through brew install bash. On Windows – WSL2 with any Linux distribution.
The utilities. The scripts from this article work with nmap, gobuster, curl, grep, awk, sed and wc. In Kali Linux and Parrot OS everything is out of the box. On minimal distributions deliver through apt install nmap curl gobuster. Specific versions of value do not have - basic flags are used, stable for years.
Working directory and shebang. Create a folder: mkdir ~/ctf && cd ~/ctf. Each script starts with a line #!/bin/bash - it indicates the interpreter system. After creating the file, make it executable: chmod +x script.sh. Without it get Permission denied – a trap through which every beginner goes without exception. The alternative is to launch through bash script.sh, but it is less convenient with frequent use.
The editor is any: nano for beginners, vim for those who are willing to spend an hour getting used to the modal mode. On CTF scripts are often written directly in the terminal through nano script.sh – raise the GUI editor once.
Reconnaissance automation: from manual commands to one script
The most common routine on CTF is to scan ports, identify services, check web directories. The three teams that always go after each other are the ideal candidate for automating Linux routine tasks. Logic: the script accepts the IP address of the target machine, creates a folder for the results, runs nmap. If HTTP is found on a port, it automatically pits gobuster to re-search the directories. If no HTTP port sticks out, the script will only be limited to the nmap stage.
#!/bin/bash
[ -z "$1" ] && echo "Использование: $0 <IP>" && exit 1
IP="$1"; DIR="recon_$IP"; mkdir -p "$DIR"
echo "[*] Сканирую $IP..."
nmap -sV -sC -oN "$DIR/nmap.txt" "$IP"
WEBPORT=$(grep '/tcp.*http' "$DIR/nmap.txt" | grep -oE '^[0-9]+' | head -1)
[ -n "$WEBPORT" ] && echo "[*] HTTP на :$WEBPORT, gobuster..." \
&& gobuster dir -u "http://$IP:$WEBPORT" \
-w /usr/share/wordlists/dirb/common.txt -o "$DIR/dirs.txt" 2>/dev/null
echo "[+] Результаты в ./$DIR/"
Line analysis of the intelligence script
Construction [ -z "$1" ] checks whether the command line argument is transmitted. Without it, the script will start nmap without a goal and hangs in anticipation of entry - it is unacceptable to spend time on the CTF.
Variable DIR generates the name of the folder of the view recon_10.10.10.5, so that the results of each machine are stored separately and do not overwrite each other. mkdir -p creates a directory without a mistake if it already exists.
Flags nmap -sV -sC include defining service versions and running default NSE scripts. Without them, nmap will only show open port numbers without context – this is not enough for CTF. Flag -oN saves the result in a text format suitable for parsing files by bash tools.
Next is the most interesting reception. Construction grep '/tcp.*http' finds strings of kind 80/tcp open http in the nmap output, then grep -oE '^[0-9]+' Pulls the port number from the beginning of the line. GNU grep flags are used; in BusyBox flag -oE available only if grep is assembled with the EXTENDED_REGEXP option – on the stripped-down systems without it, replace with grep -o '[0-9]\+' (BRE-syntax). If and -o unavailable, use grep '/tcp.*http' "$DIR/nmap.txt" | awk -F/ '{print $1}' | head -1 – a more portable option for minimal BusyBox builds. Through head -1 Take the first HTTP port. If nmap has not found HTTP, variable WEBPORT remains empty, block with gobuster will not be fulfilled. No mistakes, the script is quietly completed.
Redirect 2>/dev/null suppresses the progress bar gobuster, which clogs the terminal at automated start. The results are saved to the file dirs.txt.
To run for multiple purposes, wrap the call into a loop: while read -r ip; do ./recon.sh "$ip"; done < targets.txt. File targets.txt contains one IP per line. On the CTF with a dozen machines, start the command - and go to read the conditions of the tasks while the automation ploughs in the background.
Scripts for overtaking passwords on CTF
Web-form authorization without rate-limiting and CSRF protection is an entry-level CTF classic. Four-digit PIN or password from a short wordlist. According to OWASP, this is A07:2021 (Identification and Authentication Failures). On the CTF, this weakness is a deliberate part of the task. In production, there is a hole that is hit by the hands.
Preparing data before starting the brutforce
Before writing the script, disassemble the request manually. Open Developer Tools in the browser (Network), enter a deliberately incorrect password and fix four parameters: URL forms (for example, http://target:8080/login), method (POST or GET), field names (user, pass) and the text of the answer in case of error (Access denied). Without this manual preparation, the script shoots blind, and you won't know if it worked or not.
#!/bin/bash
URL="http://target:8080/login"; FAIL="Access denied"
WORDLIST="${1:-/usr/share/wordlists/rockyou.txt}"
while read -r pass; do
resp=$(curl -s -d "user=admin" --data-urlencode "pass=${pass}" "$URL")
if ! echo "$resp" | grep -q "$FAIL"; then
echo "[+] Пароль найден: $pass"; exit 0
fi
done < "$WORDLIST"
echo "[-] Совпадений нет"
Variable FAIL contains the wrong password marker – the same line that you have fixed in Developer Tools. Construction ${1:-/usr/share/wordlists/rockyou.txt} takes the path to the wordlist from the first argument, and if it is not transmitted, it puts the default path to rockyou.txt, which in Kali stands out of the box.
Cycle while read -r pass reads the wordlist line by line, without uploading the file to the memory in its entirety. This is critical: rockyou.txt weighs 140+MB, and design for pass in $(cat rockyou.txt) Dumbly kill the shell. Flag -r prevents the interpretation of reverse slacks – without it, the password pa\ss will turn into pass.
curl -s -d ... --data-urlencode sends a POST request: curl automatically combines several -d/--data-urlencode into a single body through &, at the same time --data-urlencode encoding special symbols (&, =, spaces) in the meaning of the password. Flag -s suppresses the progress bar. The server response hits the variable resp, and grep -q quietly checks for an error string. If Access denied not found — password has come up, the script outputs the result and concludes through exit 0.
To accelerate, you can parallelize through xargs -P: cat wordlist.txt | xargs -P 10 -I {} curl -s -d "user=admin&pass={}" "$URL" start 10 streams at a time. But on CTF it is rarely necessary - tasks are designed for short wordlists, where the linear overboard is laid in seconds.
If the task requires a selection of SSH or FTP, it is easier to take hydra: hydra -l admin -P wordlist.txt ssh://target. One line, no scripts. Writing bash scripts for web form hacking is necessary for non-standard cases where hydra does not cope due to the custom response format or the multi-step authentication process.