Pentest Primer: Bash Scripting Basics
This lesson introduces essential Linux commands and scripting fundamentals crucial for penetration testing and cybersecurity work.
Scripting refers to the creation and execution of sequences of commands or instructions to perform specific tasks. In penetration testing, scripting empowers testers to automate various procedures, interact with systems, and analyze data efficiently. It encompasses a wide array of languages and tools tailored to address different aspects of security testing.
Why Scripting Still Matters in the AI Era
With the rise of AI bots and assistants, you may wonder why learning scripting is still necessary. However, AI is still far from perfect, and you still need to understand what AI is writing for you. Scripting remains important for several critical reasons:
Understanding and Verification: You must be able to read and verify AI-generated code for correctness and security. Blindly running code you don’t understand is dangerous in security contexts.
Customization: AI can provide templates, but you need scripting knowledge to customize solutions for specific penetration testing scenarios.
Debugging: When scripts fail or produce unexpected results, you need fundamental understanding to troubleshoot effectively.
Speed and Efficiency: Experienced practitioners can often write simple scripts faster than describing requirements to an AI and validating the output.
Professional Competency: Security professionals are expected to have scripting skills. It’s a fundamental competency in the field.
Scripting Languages for Penetration Testing
This series covers three essential scripting languages for penetration testing:
Bash: Linux shell scripting for automation, system interaction, and command chaining. Essential for working in Linux environments and automating reconnaissance tasks.
Python: Versatile, high-level language perfect for tool development, data analysis, and complex automation. Most popular language for security tool development.
PowerShell: Windows scripting and automation language. Essential for testing and exploiting Windows environments, Active Directory attacks, and post-exploitation activities.
Essential Linux File Management Commands
Before diving into scripting, mastering basic Linux commands is crucial. These commands form the foundation for navigation, file management, and system interaction.
Navigation and Information:
pwd (Print Working Directory): Displays your current location in the filesystem
- Usage:
pwd - Shows the full path of your current directory
ls (List): Displays contents of directories
- Usage:
ls [options] [directory] - Common options:
ls -l: Long format with permissions, owner, size, datels -a: Show all files including hidden files (starting with .)ls -lh: Long format with human-readable file sizesls -R: Recursively list subdirectories
- Example:
ls -lahshows all files in long format with human-readable sizes
cd (Change Directory): Navigate between directories
- Usage:
cd [directory] - Special directories:
cd ~: Go to home directorycd ..: Go up one directory levelcd -: Return to previous directorycd /: Go to root directory
File and Directory Operations:
mkdir (Make Directory): Create new directories
- Usage:
mkdir [options] directory_name - Options:
mkdir -p path/to/directory: Create parent directories as neededmkdir -m 755 directory: Create with specific permissions
rm (Remove): Delete files and directories
- Usage:
rm [options] file_or_directory - Options:
rm -r directory: Remove directory recursivelyrm -f file: Force removal without confirmationrm -rf directory: Force recursive removal (use with extreme caution!)
- Warning: rm is permanent - there’s no recycle bin
cp (Copy): Copy files and directories
- Usage:
cp [options] source destination - Options:
cp -r source_dir dest_dir: Copy directory recursivelycp -p file dest: Preserve file attributescp -i file dest: Prompt before overwriting
mv (Move): Move or rename files and directories
- Usage:
mv [options] source destination - Examples:
mv oldname newname: Rename filemv file /new/location/: Move file to new locationmv -i file dest: Prompt before overwriting
File Viewing and Manipulation:
cat (Concatenate): Display file contents
- Usage:
cat file1 [file2 ...] - Can concatenate multiple files
- Example:
cat file1 file2 > combined: Combine files
echo: Display messages or output text
- Usage:
echo "message" - Common uses:
echo $VARIABLE: Display variable valueecho "text" > file: Write to file (overwrite)echo "text" >> file: Append to file
grep: Search for patterns in files
- Usage:
grep [options] pattern [files] - Options:
grep -i pattern file: Case-insensitive searchgrep -r pattern directory: Recursive searchgrep -v pattern file: Invert match (show non-matching lines)grep -n pattern file: Show line numbersgrep -E pattern file: Use extended regex
- Essential for log analysis and finding specific content
Advanced Text Processing:
cut: Extract specific fields from delimited text
- Usage:
cut [options] file - Examples:
cut -d':' -f1 /etc/passwd: Extract usernamescut -c1-10 file: Extract characters 1-10
tr (Translate): Character-level substitution
- Usage:
tr [options] set1 set2 - Examples:
tr 'a-z' 'A-Z': Convert lowercase to uppercasetr -d ',': Delete all commas
sed (Stream Editor): Text manipulation and search-replace
- Usage:
sed [options] 'command' file - Examples:
sed 's/old/new/g' file: Replace all occurrencessed '/pattern/d' file: Delete lines matching patternsed -n '1,10p' file: Print lines 1-10
awk: Data extraction and reporting
- Usage:
awk 'pattern {action}' file - Examples:
awk '{print $1}' file: Print first fieldawk -F':' '{print $1}' /etc/passwd: Print usernamesawk '$3 > 100' file: Print lines where 3rd field > 100
File Permissions and Ownership
Understanding and managing file permissions is crucial for both security testing and system administration.
chmod (Change Mode): Modify file permissions
- Usage:
chmod [options] mode file - Numeric mode:
- Read (r) = 4, Write (w) = 2, Execute (x) = 1
chmod 755 file: rwxr-xr-x (owner: rwx, group: rx, others: rx)chmod 644 file: rw-r–r– (owner: rw, group: r, others: r)chmod 700 file: rwx—— (owner only)
- Symbolic mode:
chmod u+x file: Add execute for userchmod g-w file: Remove write for groupchmod o=r file: Set others to read-only
chown (Change Owner): Modify file ownership
- Usage:
chown [options] user:group file - Examples:
chown user file: Change ownerchown user:group file: Change owner and groupchown -R user:group directory: Recursive change
Process Management
Managing processes is essential for understanding system activity and controlling running applications.
ps (Process Status): Display running processes
- Usage:
ps [options] - Common options:
ps aux: Show all processes for all usersps -ef: Full-format listingps -u username: Show processes for specific user
kill: Terminate processes
- Usage:
kill [signal] PID - Common signals:
kill PID: Graceful termination (SIGTERM)kill -9 PID: Force kill (SIGKILL)kill -HUP PID: Reload configuration (SIGHUP)
File Search
find: Search for files and directories
- Usage:
find [path] [conditions] - Examples:
find / -name "*.log": Find all .log filesfind /home -user username: Find files owned by userfind / -perm 777: Find files with 777 permissionsfind / -type f -size +100M: Find files larger than 100MBfind / -mtime -7: Find files modified in last 7 days
Hands-On Practice Lab
To practice these commands, create a lab environment with this Bash script:
1
2
3
4
5
mkdir -p ~/bash_lab_1/file_management
touch ~/bash_lab_1/file_management/example{1,2,3}.log
touch ~/bash_lab_1/file_management/conf{1,2,3}.yml
touch ~/bash_lab_1/file_management/.hidden{1,2,3}
sudo cp /var/log/boot.log ~/bash_lab_1/file_management/boot.log
This creates:
- A directory structure for practicing
- Multiple log files to work with
- Configuration files
- Hidden files to discover
- A real system log to analyze
Practical Exercises:
- Navigate to the lab directory and list all files including hidden ones
- Search for specific patterns in log files using grep
- Use find to locate all .log files
- Change permissions on files using chmod
- Practice text manipulation with sed and awk
- Extract specific information from log files using cut and grep
Applying Commands to Penetration Testing
These commands are essential throughout penetration testing:
Reconnaissance: Using grep and awk to parse scan results, find to locate interesting files, ps to identify running services
Enumeration: Combining commands with pipes to filter and analyze data efficiently
Log Analysis: Using grep, sed, and awk to analyze system logs and identify security events
Privilege Escalation: Finding SUID binaries, world-writable files, and misconfigurations
Post-Exploitation: Navigating systems, finding sensitive data, understanding system configuration
Building Scripting Skills
After mastering individual commands, the next step is combining them into scripts:
- Start with simple one-liners combining commands with pipes
- Save useful command combinations as shell scripts
- Learn basic Bash scripting (variables, loops, conditionals)
- Progress to Python for more complex automation
- Add PowerShell for Windows environments
Remember: The goal isn’t memorizing every option for every command. Focus on understanding core functionality and knowing how to look up syntax when needed (man command or command --help).
The commands covered in this lesson form the foundation for all Linux-based penetration testing activities. Mastery of these fundamentals is essential before progressing to more advanced techniques and tools.