Bash for \x00's
The Ultimate Bash Scripting Handbook for CTFs: Conquer the Command Line
Bash scripting is an invaluable skill for CTF participants. Automating tasks, interacting with tools, parsing data, and manipulating files – Bash empowers you to conquer challenges with efficiency and precision. This comprehensive handbook explores the depths of Bash scripting for CTFs, covering a wide range of techniques and scenarios.
I. Core Concepts:
Shell: The command-line interpreter (e.g., Bash).
Commands: Instructions executed by the shell (e.g.,
ls,grep,awk).Variables: Store data (strings, numbers, etc.).
Control Flow:
if,for,while,case– control the execution of your script.Functions: Reusable blocks of code.
Input/Output Redirection:
<(input),>(output),>>(append),|(pipe).Pipes: Connect the output of one command to the input of another.
II. Essential Bash Commands for CTFs:
File Manipulation:
ls: List files and directories.cd: Change directory.mkdir: Create directory.rm: Remove files or directories.cp: Copy files or directories.mv: Move or rename files or directories.cat: Display 1 file contents.head: Display the first few lines of a file.tail: Display the last few lines of a file.less: View file contents one page at a time.touch: Create an empty file or update file timestamp.
Text Processing:
grep: Search for patterns in text.awk: Powerful text processing tool.sed: Stream editor for text manipulation.cut: Extract parts of lines.sort: Sort lines of text.uniq: Remove duplicate lines.tr: Translate or delete characters.
Networking:
ping: Test network connectivity.curl: Transfer data with URLs.wget: Download files from the web.netcat (nc): Network utility for reading and writing data across network connections.nmap: Network scanner. (Covered in a separate manual)
System Information:
uname: Display system information.whoami: Display current user.pwd: Print working directory.ps: List processes.toporhtop: Display system resource usage.
Encoding/Decoding:
base64: Base64 encode/decode.urlencode: URL encode.
Cryptography:
openssl: Command-line tool for cryptographic operations.
Automation:
find: Search for files and directories.xargs: Build and execute command lines from standard input.
III. Bash Scripting Basics:
Shebang:
#!/bin/bash(tells the system which interpreter to use).Variables:
name="John" echo "Hello, $name!"Command Substitution:
date=$(date) echo "The date is: $date"Arithmetic Expansion:
x=10 y=20 z=$((x + y)) echo "The sum is: $z"Conditional Statements:
if [ $x -gt $y ]; then echo "$x is greater than $y" elif [ $x -lt $y ]; then echo "$x is less than $y" else echo "$x is equal to $y" fiLoops:
forloop:for i in {1..10}; do echo $i done for file in *.txt; do echo $file donewhileloop:i=0 while [ $i -lt 10 ]; do echo $i i=$((i + 1)) done
Functions:
greet() { echo "Hello, $1!" } greet "John"Input/Output Redirection:
cat input.txt # Read from input.txt ls -l > output.txt # Write to output.txt ls -l >> output.txt # Append to output.txt command1 | command2 # Pipe output of command1 to command2Case Statements:
case $variable in value1) echo "Value 1" ;; value2) echo "Value 2" ;; *) echo "Default value" ;; esac
IV. Advanced Bash Scripting for CTFs:
Regular Expressions:
grep "pattern" file.txt awk '/pattern/ {print $1}' file.txt sed 's/old/new/g' file.txtWorking with URLs:
url="http://example.com/path?param1=value1¶m2=value2" echo $url | cut -d '?' -f 2 # Extract query string echo $url | sed 's/\?.*//' # Extract base URLEncoding/Decoding:
echo "data" | base64 # Base64 encode echo "ZGF0YQ==" | base64 -d # Base64 decode echo "value with spaces" | urlencode # URL encode (requires `urlencode` command)Networking:
nc -nvlp 8080 # Listen on port 8080 nc 192.168.1.1 80 # Connect to port 80 curl http://example.com # Make a web requestAutomation:
Bash
for i in {1..100}; do curl "http://example.com/page/$i" doneFile Processing:
while read line; do echo "Processing: $line" done < input.txtSubshells:
( cd /tmp; ls ) # Changes directory in a subshellSignal Handling:
trap "echo 'Exiting...'" INT # Handle Ctrl+C
V. Examples for CTFs:
Brute-forcing a web directory:
for dir in $(cat directories.txt); do curl "http://example.com/$dir" 2>/dev/null | grep "200 OK" doneDecoding a base64 encoded string:
echo "encoded_string" | base64 -dFinding a specific string in a large file:
grep "flag" large_file.txtAutomating interactions with a program:
./vulnerable_program << EOF input1 input2 EOFPort scanning with Nmap and processing the output:
nmap 192.168.1.1 -oG - | grep "open" | awk '{print $2}'
VI. Tips for CTFs:
Practice: Write lots of scripts! The more you practice, the more comfortable you'll become.
Read Writeups: Learn from other people's solutions.
Use Functions: Break down your scripts into reusable functions.
Be Modular: Make your scripts easy to modify and extend.
Comment Your Code: Explain what your script does. This will help you and others understand it later.
Use
set -xfor Debugging: This will
Last updated
Was this helpful?