[Link].
com
FOR RED TEAMS
[Link]
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
[Link]
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
1. Getting Started with Shell Scripting
○ Writing and Executing Bash Scripts
2. Basic Bash Scripting
○ Variables
○ Operators
○ Conditional Statements
3. Intermediate and Advanced Bash Scripting
○ Loops (for, while, until)
○ Functions
○ Working with Strings
○ Arrays
4. Task-Specific Bash Scripts
○ File Operations
○ Network Management
○ Process Management
○ System Monitoring
5. 50 Shell Scripts for Red Teamers
Getting Started with Shell Scripting
Shell scripts are executed line by line by the Bash interpreter. To begin scripting, you need to
write a Bash program and ensure it is executable. While there are multiple ways to create
scripts, the following method is the most straightforward.
Shebang (#!) in Shell Scripting
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
[Link]
#!/bin/bash
This ensures that the script is interpreted using Bash. It must always be placed on the first line
of the script file.
How to Write and Run a Bash Script in Linux
Step 1: Creating a Shell Script
To create a basic shell script, follow these steps:
1. Open a terminal using CTRL + ALT + T.
2. Create a directory (if it doesn’t exist) for storing your scripts:
mkdir bin
3. Inside the bin directory, create a new script file:
nano bin/hello_world.sh
4. Write the following script in the file:
#!/bin/bash
echo "Hello, World!"
5. Save the file using CTRL + S , then exit with CTRL + X.
6. Make the script executable:
chmod u+rwx bin/hello_world.sh
7. Restart your system to ensure the script directory is recognized in the $PATHvariable.
Step 2: Executing the Bash Script
After restarting your system, you can run the script by opening a terminal and entering:
3
[Link]
bash hello_world.sh
This will display:
Hello, World!
Basic Bash Scripting
Just like other programming languages, Bash scripting follows a specific structure with key
components like variables, operators, and conditionals. The first line of a Bash script must
always begin with the Shebang (#!), followed by the path to the Bash interpreter (/bin/bash).
Besides regular Linux commands, Bash scripting involves core elements such as variables,
operators, and conditionals, which will be covered in this section.
Variables in Shell Scripting
Variables are essential in Bash scripting as they store and manage data. A variable represents a
memory location where values (numbers, strings, etc.) can be stored and manipulated. In Bash,
variables are referenced using the dollar sign ($).
Syntax for Declaring a Variable:
VARIABLE_NAME=VALUE
Rules for Using Variables in Bash:
● Assign values using the = operator.
● Variable names are case-sensitive (e.g., VAR and var are different).
● To reference a variable, use the dollar sign ($) before the name.
● When updating a variable, use the assignment operator (=) without re-declaring the type.
● Enclose multi-word strings in single quotes (') to ensure they are treated as a single
unit.
4
[Link]
Example 1: Defining Variables in a Bash Script
#!/bin/bash
# Declaring variables
name="Tom"
age=12
# Displaying variable values
echo "Name: $name, Age: $age"
Output:
Name: Tom, Age: 12
Example 2: Taking User Input and Storing it in a Variable
You can take user input using the read command and store it in a variable.
#!/bin/bash
echo "Enter a number:"
read num
echo "The number you entered is: $num"
Output:
Enter a number:
12
The number you entered is: 12
Example 3: Prompting a User for Input Using -p Option
5
[Link]
The read command, when used with the -p flag, allows displaying a message alongside the
input prompt.
#!/bin/bash
read -p "Enter your name: " username
echo "Welcome, $username!"
Output:
Enter your name: Alice
Welcome, Alice!
Example 4: Concatenating Multiple Variables
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!
Example 5: Passing Values as Command-Line Arguments
6
[Link]
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."
Executing the Script:
bash [Link] Alice 25
Output:
My name is Alice and I am 25 years old.
Example 6: Printing Environment Variables
You can also access system environment variables using ${!} syntax.
#!/bin/bash
read -p "Enter an environment variable name: " var
echo "Environment Variable Value: ${!var}"
Output:
Enter an environment variable name: HOME
Environment Variable Value: /home/user
Operators in Shell Scripting
7
[Link]
Bash provides various operators for performing calculations and comparisons. They are
grouped into the following categories:
Example 1: Adding Two Numbers
#!/bin/bash
num1=10
num2=20
sum=$((num1 + num2))
echo "Sum: $sum"
Output:
Sum: 30
Example 2: Subtracting Two Numbers
#!/bin/bash
num1=30
num2=20
diff=$((num1 - num2))
8
[Link]
echo "Difference: $diff"
Output:
Difference: 10
Example 3: Multiplication and Division
#!/bin/bash
num1=6
num2=3
prod=$((num1 * num2))
div=$((num1 / num2))
echo "Product: $prod"
echo "Quotient: $div"
Output:
Product: 18
Quotient: 2
Example 4: Generating a Random Number Between 1 and 50
#!/bin/bash
echo $((1 + RANDOM % 50))
Output: