Bibliotheque
DiscordHackTheBoxTryHackMeGitHub
  • Welcome wanderer
    • Bibliotheque
    • Hacking Philosophy
    • Useful Links
  • PENTESTING
    • Methodology
    • Protocols
      • FTP
      • SMB
      • NFS
      • SSH
      • RDP
      • SMTP
      • IMAP / POP3
      • RSYNC
      • SNMP
      • IPMI
      • R-Services
      • WinRM
      • WMI
      • LDAP
    • Databases
      • MySQL
      • MSSQL
      • Oracle TNS
      • PostgreSQL
    • File Transfers
      • Windows
      • Linux
      • Code
      • Misc
    • Password Attacks
      • John The Ripper
      • Hashcat
    • Docker
  • TOOLS
    • Nmap
    • Metasploit
    • BloodHound
    • Other
  • Linux
    • Theory
    • Commands and Utilities
      • Useful Commands
    • Bash Scripting
    • Post-Exploitation
      • Cred Hunting
      • Pivoting
  • WINDOWS
    • Theory
      • Security
    • Commands and Utilities
    • PowerShell
    • Post-Exploitation
      • Tools
      • Enumeration
        • System
        • Network
        • Users
        • Groups
        • Processes / Services
        • Permissions
        • Defence
        • Programs
        • Files
      • Access
      • Pivoting
      • Cred Hunting
    • Privilege Escalation
      • Privileges
      • Built-In Groups
        • Backup Operators
        • Server Operators
        • Print Operators
        • DnsAdmins
        • Event Log Readers
      • Privilege Abuse
        • Potatoes
        • SeDebugPrivilege
        • SeTakeOwnershipPrivilege
      • MISC
        • UAC Bypass
        • User-Interaction Attacks
        • Weak Permissions
  • ACTIVE DIRECTORY
    • Theory
      • Terminology
    • Reconnaissance
      • Responder
      • Password Policies
      • DNS
      • Enumeration
        • Users
        • Groups
          • GPO's
        • Shares
        • Domain
        • Trusts
        • ACL
    • Movement
      • Credentials
        • Dumping
          • DCSync
          • DPAPI Secrets
        • Making a Target List
        • Spraying
        • Powershell Remoting
      • Kerberos
        • Kerbrute
        • Kerberoasting
          • Semi-Manual Way
          • Targeted Kerberoasting
        • ASREProasting
        • Forging
          • Golden Ticket
        • Overpass The Hash
        • Pass The Ticket
        • RBCD
        • noPAC
      • MITM / Coerced Auths
        • LLMNR, NBT-NS Poisoning
        • PetitPotam
      • DACL Abuse
        • AddMember
        • ForceChangePassword
      • Trust Abuse
        • ExtraSIDs
      • ADCS
        • ESC1
      • Printers
        • PrintNightmare
    • Tools
  • Networking
    • Theory
      • Types / Topologies
      • OSI & TCP/IP Models
      • TCP / UDP
      • MAC Addresses
      • IP / Subnetting
      • Proxies
      • ARP
    • Pivoting
      • Port-Forwarding
    • Commands and Utilities
    • Techniques
  • WEB
    • Web Recon
      • Fuzzing
    • DNS
  • CLOUD
    • Google GKE/GCP
      • Theory
Powered by GitBook
On this page
  • What it is?
  • Script Execution - Examples
  • Shebang
  • Conditional Execution
  • If-Else
  • If-Only
  • If-Elif-Else
  • Case
  • Comparison Operators
  • String Operators
  • File Operators
  • Special Variables
  • Regular Variables
  • Arrays
  • Loops
  • For
  • While
  • Until
  • Script Termination
  • Wildcards
  • Tips & Tricks
  1. Linux

Bash Scripting

What it is?

Bash is the scripting language we use to communicate with Unix-based OS and give commands to the system.


Script Execution - Examples

venator17@kali[/kali]$ bash script.sh <optional arguments>
venator17@kali[/kali]$ sh script.sh <optional arguments>
venator17@kali[/kali]$ ./script.sh <optional arguments>

Shebang

The #!/bin/bash at the beginning of a Bash script is known as a shebang or hashbang. It serves as a directive to the operating system, indicating which interpreter should be used to execute the script.

Conditional Execution

If-Else

if [condition]
then [execution]
else [what would be executed if condition would fail]
fi [closing]

If-Only

if [condition]
then [execution]
fi [closing]

If-Elif-Else

if [first condition]
then [execution]
elif [second condition]
then [execution]
else [what would be executed if conditions would fail]
fi [closing]

Case

case <expression> in
	pattern_1 ) statements ;;
	pattern_2 ) statements ;;
	pattern_3 ) statements ;;
esac

Example::

case $opt in 
	"1") network_range ;;
	"2") ping_host ;;
	"3") network_range && ping_host ;;
	"*") exit 0 ;;
esac

Comparison Operators

Operator
Explanation

-eq

equal to

-ne

not equal to

-lt

less than

-le

less than or equal to

-gt

greater than

-ge

greater than or equal to

String Operators

Operator
Description

==

is equal to

!=

is not equal to

<

is less than in ASCII alphabetical order

>

is greater than in ASCII alphabetical order

-z

if the string is empty (null)

-n

if the string is not null

File Operators

Operator
Description

-e

if the file exist

-f

tests if it is a file

-d

tests if it is a directory

-L

tests if it is if a symbolic link

-N

checks if the file was modified after it was last read

-O

if the current user owns the file

-G

if the file’s group id matches the current user’s

-s

tests if the file has a size greater than 0

-r

tests if the file has read permission

-w

tests if the file has write permission

-x

tests if the file has execute permission

Special Variables

$#

Number of arguments passed to the script.

$@

List of command-line arguments.

$n

n is number of argument

$$

Id of executing process

$?

Success of command. 0 is success, 1 is a failure

Regular Variables

> variable="Declared without an error."
> echo $variable
> Declared without an error.

Arrays

> domains=(shadow wizard money gang)
> echo ${domains[0]}
> shadow

OR

> domains=(shadow wizard "money gang")
> echo ${domains[2]}
> money gang

Loops

For

for variable in list
do
    # Commands to be executed for each item in the list
done

Example:

fruits=("apple" "orange" "banana")
for fruit in "${fruits[@]}"
do
    echo "I like $fruit"
done

While

while [ condition ]
do
    # Commands to be executed while the condition is true
done

Example:

count=1 # Count from 1 to 5
while [ $count -le 5 ]
do
    echo $count
    ((count++))
done

Until

until [ condition ]
do
    # Commands to be executed until the condition becomes true
done

Example:

count=1 # Count from 1 to 5
until [ $count -gt 5 ]
do
    echo $count
    ((count++))
done

Script Termination

Exit Status
Explanation

exit 0

Succesful execution

exit 1

General error condition

exit 2

Specific error condition

Wildcards

In Bash, a wildcard refers to a character or a set of characters that can be used to represent a group of filenames or strings. Wildcards are often used in commands to perform operations on multiple files or strings that match a specified pattern.

Wildcard
Example Usage
Explanation

* (Asterisk)

echo *.txt

Matches all files ending with ".txt".

? (Question Mark)

ls file?.txt

Matches files like "file1.txt", "fileA.txt", etc.

[ ] (Square Brackets)

ls [aeiou]*.txt

Matches any file starting with a vowel and ending with ".txt".

{ } (Brace Expansion)

cp file{1,2,3}.txt dest/

Expands to "file1.txt", "file2.txt", and "file3.txt" and copies to the destination.

!(pattern) (Extended Pattern Matching)

ls !(file*.txt)

Matches all files except those starting with "file" and ending with ".txt".

?(pattern) (Zero or One Occurrence)

ls file?(1).txt

Matches "file.txt" or "file1.txt".

+(pattern) (One or More Occurrences)

ls file+(1).txt

Matches "file1.txt", "file11.txt", etc.

*(pattern) (Zero or More Occurrences)

ls file*(1).txt

Matches "file.txt", "file1.txt", "file11.txt", etc.

Tips & Tricks

  1. You Could use tee command for writing output to both standard output and file. If you would use tee -a, it would

  2. Use bash -x -v to verbose debugging

PreviousUseful CommandsNextPost-Exploitation

Last updated 6 months ago