Skip to main content

DAMN BASH

Bash Scripting: From Basic to Advanced

Bash Scripting: From Basic to Advanced

Bash (Bourne Again SHell) is a powerful command-line interpreter and scripting language commonly used in Linux and macOS environments. This post covers Bash scripting from basic commands to more advanced techniques.

I. Basic Commands

These commands are the building blocks of Bash scripting:

CommandDescription
lsLists files and directories.
cdChanges the current directory.
pwdPrints the current working directory.
mkdirCreates a new directory.
rmRemoves files or directories (use with caution!).
cpCopies files or directories.
mvMoves or renames files or directories.
catDisplays file content.
echoPrints text to the console.

II. Variables

Variables store data that can be used in your scripts:

name="John Doe"
echo "Hello, $name!"
age=30
echo $((age + 5)) # Arithmetic operations

III. Input/Output Redirection

Redirect input and output of commands:

OperatorDescription
>Redirects output to a file (overwrites existing content).
>>Appends output to a file.
<Redirects input from a file.
2>&1Redirects standard error to standard output.
ls -l > filelist.txt   # Save the output of ls -l to filelist.txt
cat < input.txt       # Read content from input.txt
command 2>/dev/null    # Suppress error messages

IV. Control Flow

Control the execution flow of your scripts:

If/Else Statements

if [ $age -gt 18 ]; then
  echo "You are an adult."
elif [ $age -gt 12 ]; then
    echo "You are a teenager."
else
  echo "You are a child."
fi

For Loops

for i in {1..5}; do
  echo "Number: $i"
done

for file in *.txt; do
    echo "Processing $file"
done

While Loops

count=0
while [ $count -lt 5 ]; do
  echo "Count: $count"
  count=$((count + 1))
done

V. Functions

Group code into reusable blocks:

greet() {
  echo "Hello, $1!"
}

greet "Alice"

VI. Advanced Topics

Regular Expressions (Regex)

Powerful pattern matching for text processing (using grep, sed, awk):

grep "^a.*z$" file.txt # Lines starting with 'a' and ending with 'z'

Process Management

Controlling running processes:

ps aux | grep process_name # Find a process
kill <PID>                  # Kill a process

Working with Arrays

my_array=("apple" "banana" "cherry")
echo ${my_array[0]}    # Access the first element
echo ${my_array[@]}    # Access all elements

VII. Example Script

#!/bin/bash

# A simple script to backup a directory

backup_dir="/path/to/backup"
target_dir="/path/to/target"
timestamp=$(date +%Y%m%d%H%M%S)
archive_file="$backup_dir/backup_$timestamp.tar.gz"

tar -czvf "$archive_file" "$target_dir"

echo "Backup created: $archive_file"

This is just a starting point. There are many more advanced features in Bash scripting. Explore the man bash pages for the full documentation.

Comments

Popular posts from this blog

Thread Modelling Cheatsheet: Know Your Weaknesses Before Attackers Do!

Threat Modelling Part - 1 What is Threat Modelling?      Threat modelling is the process of identifying, assessing, and mitigating potential security threats before they happen. It helps teams anticipate how systems can be attacked and build defences proactively , not reactively. Key Concepts Threat: Something (like a hacker or malware) that could exploit a weakness. Vulnerability: A flaw in your system that can be exploited. Risk: The chance that a threat will exploit a vulnerability to cause damage. Analogy : Threat = Burglar Vulnerability = Unlocked door Risk = Getting robbed because the door is open in a bad neighborhood  Threat Modelling Process (High-Level) Define the Scope – What systems/apps are you evaluating? Identify Assets – What needs protection? (e.g. data, services) Identify Threats – Think like an attacker. What could go wrong? Analyze Vulnerabilities – What weaknesses exist? Prioritize Ri...

DNS Pentest - Port 53

Recon Banner Grabbing - Identify DNS Server Versions # Use dig to determine DNS server versions dig version.bind CHAOS TXT @DNS # Alternatively, use nmap script to grab the banner nmap --script dns-nsid <DNS_IP> # Alternatively, use telnet to grab the banner nc -nv -u <DNS_IP> 53 DNS Server Discovery # Using dig dig NS <target-domain> # Using nslookup nslookup -type=NS <target-domain> Enumeration Using DNS enum dnsenum --dnsserver <DNS_IP> --enum -p 0 -s 0 -o subdomains.txt -f <WORDLIST> <DOMAIN> Using dig # Query DNS records dig hackviser.com # Query specific type of DNS records (e.g., A record) dig A hackviser.com # Perform a reverse DNS lookup dig -x <IP_ADDRESS> # Query a specific DNS server dig @<DNS_SERVER_IP> hackviser.com Using nslookup # Perform DNS queries nslookup hackviser.com # Query a specific type of DNS record (e.g., MX record) nslookup -type=MX hackviser.com # Query a specific DNS server nslookup ha...

SMTP (Simple Mail Transfer Protocol) Pentesting - Port 25

 SMTP (Simple Mail Transfer Protocol) is a communication protocol for electronic mail transmission.it is used for sending e-mail. POP3 or IMAP are used for receiving e-mail. Default ports are 25 (SMTP), 465 (SMTPS), 587 (SMTPS)   Connect We can use Telnet to connect to the remote server. Here is a command using Telnet: telnet example.com 25 Enumeration Identifying a SMTP Server You can use Nmap to check if there's an Telnet server on a target host like this: nmap -p25,465,587 -sV -Pn target.com Additional Nmap commands for enumeration nmap --script smtp-brute -p 25,465,587 "target-ip" nmap --script smtp-commands -p 25,465,587 "target-ip" nmap --script smtp-enum-users -p 25,465,587 "target-ip" nmap --script smtp-ntlm-info --script-args smtp-ntlm-info.domain=example.com -p 25,465,587 "target-ip" nmap --script smtp-vuln-cve2011-1764 -p 25,465,587 "target-ip" nmap --script smtp-* -p 25,465,587 "target-ip" Enumer...