"The only true wisdom is in knowing you know nothing." - Socrates
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.
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.
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.
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
pwdls (List): Displays contents of directories
ls [options] [directory]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 subdirectoriesls -lah shows all files in long format with human-readable sizescd (Change Directory): Navigate between directories
cd [directory]cd ~: Go to home directorycd ..: Go up one directory levelcd -: Return to previous directorycd /: Go to root directoryFile and Directory Operations:
mkdir (Make Directory): Create new directories
mkdir [options] directory_namemkdir -p path/to/directory: Create parent directories as neededmkdir -m 755 directory: Create with specific permissionsrm (Remove): Delete files and directories
rm [options] file_or_directoryrm -r directory: Remove directory recursivelyrm -f file: Force removal without confirmationrm -rf directory: Force recursive removal (use with extreme caution!)cp (Copy): Copy files and directories
cp [options] source destinationcp -r source_dir dest_dir: Copy directory recursivelycp -p file dest: Preserve file attributescp -i file dest: Prompt before overwritingmv (Move): Move or rename files and directories
mv [options] source destinationmv oldname newname: Rename filemv file /new/location/: Move file to new locationmv -i file dest: Prompt before overwritingFile Viewing and Manipulation:
cat (Concatenate): Display file contents
cat file1 [file2 ...]cat file1 file2 > combined: Combine filesecho: Display messages or output text
echo "message"echo $VARIABLE: Display variable valueecho "text" > file: Write to file (overwrite)echo "text" >> file: Append to filegrep: Search for patterns in files
grep [options] pattern [files]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 regexAdvanced Text Processing:
cut: Extract specific fields from delimited text
cut [options] filecut -d':' -f1 /etc/passwd: Extract usernamescut -c1-10 file: Extract characters 1-10tr (Translate): Character-level substitution
tr [options] set1 set2tr 'a-z' 'A-Z': Convert lowercase to uppercasetr -d ',': Delete all commassed (Stream Editor): Text manipulation and search-replace
sed [options] 'command' filesed 's/old/new/g' file: Replace all occurrencessed '/pattern/d' file: Delete lines matching patternsed -n '1,10p' file: Print lines 1-10awk: Data extraction and reporting
awk 'pattern {action}' fileawk '{print $1}' file: Print first fieldawk -F':' '{print $1}' /etc/passwd: Print usernamesawk '$3 > 100' file: Print lines where 3rd field > 100Understanding and managing file permissions is crucial for both security testing and system administration.
chmod (Change Mode): Modify file permissions
chmod [options] mode filechmod 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)chmod u+x file: Add execute for userchmod g-w file: Remove write for groupchmod o=r file: Set others to read-onlychown (Change Owner): Modify file ownership
chown [options] user:group filechown user file: Change ownerchown user:group file: Change owner and groupchown -R user:group directory: Recursive changeManaging processes is essential for understanding system activity and controlling running applications.
ps (Process Status): Display running processes
ps [options]ps aux: Show all processes for all usersps -ef: Full-format listingps -u username: Show processes for specific userkill: Terminate processes
kill [signal] PIDkill PID: Graceful termination (SIGTERM)kill -9 PID: Force kill (SIGKILL)kill -HUP PID: Reload configuration (SIGHUP)find: Search for files and directories
find [path] [conditions]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 daysTo practice these commands, create a lab environment with this Bash script:
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:
Practical Exercises:
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
After mastering individual commands, the next step is combining them into scripts:
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.