/bash-scripting-for-hackers

Bash Scripting Cheatsheet for pen-testing!

Primary LanguageShell

Note: These notes are for my personal reference!

๐๐š๐ฌ๐ก ๐’๐œ๐ซ๐ข๐ฉ๐ญ๐ข๐ง๐  for Hackers & Bug Bounty Hunters

๐๐š๐ฌ๐ข๐œ๐ฌ

  • Telling interpreter that the file is bash file
!#/bin/bash
  • Make the file executable
$ chmod +x script.sh

๐‚๐จ๐ฆ๐ฆ๐ž๐ง๐ญ๐ฌ

# this is a single line comment 
# Multi line comment

# - Method I
<<COMMENTS
  This 
  is a 
  multiline
  comment
COMMENTS


# - Method II
:'
 This 
  is a 
  multiline
  comment
'

๐•๐š๐ซ๐ข๐š๐›๐ฅ๐ž๐ฌ

  • Syntax: variable_name=variable_value
  • Note: There should not be any white spaces on either side of the =
  • Single quotes (') helps to treat every character as it is
  • Double quotes (") helps to do the substitution

System Defined Variables Meaning
BASH represents the Shell Name.
BASH_VERSION specifies the shell version which the Bash holds.
COLUMNS specify the no. of columns for our screen
HOME specifies the home directory for the user
LOGNAME specifies the logging user name.
OSTYPE tells the type of OS.
PWD represents the current working directory.
USERNAME specifies the name of currently logged in user.
                                                                                      
#!/bin/bash

echo $BASH
echo $BASH_VERSION
echo $COLUMNS
echo $HOME
echo $LOGNAME
echo $OSTYPE
echo $PWD
echo $USERNAME
                                                                                       

๐‘๐ž๐š๐๐ข๐ง๐  ๐ˆ๐ง๐ฉ๐ฎ๐ญ

  • Syntax: read [flag] varname
    • -s : for silent mode
    • -p : for prompt mode
    • -a : for arrays
โ”Œโ”€โ”€(shreyasใ‰ฟkali)-[~/practise/bash_scripting]
โ””โ”€$ cat read.sh  
#!/bin/bash

echo "firstname: "
read firstname
echo "lastname: "
read lastname

echo "Hello Mr. $firstname $lastname"

โ”Œโ”€โ”€(shreyasใ‰ฟkali)-[~/practise/bash_scripting]
โ””โ”€$ ./read.sh  
firstname: 
shreyas
lastname: 
chavhan
Hello Mr. shreyas chavhan

โ”Œโ”€โ”€(shreyasใ‰ฟkali)-[~/practise/bash_scripting]
โ””โ”€$ vim read_prompt.sh
                                                                                       
โ”Œโ”€โ”€(shreyasใ‰ฟkali)-[~/practise/bash_scripting]
โ””โ”€$ chmod +x read_prompt.sh 
                                                                                       
โ”Œโ”€โ”€(shreyasใ‰ฟkali)-[~/practise/bash_scripting]
โ””โ”€$ cat read_prompt.sh     
#!/bin/bash

read -p "Your Name: " name

echo "Hello Mr. $name"
                                                                                       
โ”Œโ”€โ”€(shreyasใ‰ฟkali)-[~/practise/bash_scripting]
โ””โ”€$ ./read_prompt.sh 
Your Name: Shreyas
Hello Mr. Shreyas
                          
โ”Œโ”€โ”€(shreyasใ‰ฟkali)-[~/practise/bash_scripting]
โ””โ”€$ vim read_silent.sh
                                                                                       
โ”Œโ”€โ”€(shreyasใ‰ฟkali)-[~/practise/bash_scripting]
โ””โ”€$ ./read_silent.sh  
Your Password 
Your Password is mypassword, it's secret - don't tell anyone!
                                                                                       
โ”Œโ”€โ”€(shreyasใ‰ฟkali)-[~/practise/bash_scripting]
โ””โ”€$ cat read_silent.sh 
#!/bin/bash


read -sp "Your Password " password

echo ""
echo "Your Password is $password, it's secret - don't tell anyone!"

  • Multi-line input
while read -r line; do
   printf '%s\n' "$line"
done

๐๐š๐ฌ๐ก ๐ข๐Ÿ-๐ž๐ฅ๐ข๐Ÿ-๐ž๐ฅ๐ฌ๐ž

  • Syntax
if [condition]; then
  <blah blah>
elif [condition]; then
  <blah blah>
else
  <blah blah>
fi

  • || : OR
  • && : AND
if [] || []; then
  <blah blah>
elif [] && []; then
  <blah blah>
else
  <blah blah>
fi
Operators Description
! EXPRESSION To check if EXPRESSION is false.
-n STRING To check if the length of STRING is greater than zero.
-z STRING To check if the length of STRING is zero (i.e., it is empty)
STRING1 == STRING2 To check if STRING1 is equal to STRING2.
STRING1 != STRING2 To check if STRING1 is not equal to STRING2.
INTEGER1 -eq INTEGER2 To check if INTEGER1 is numerically equal to INTEGER2.
INTEGER1 -gt INTEGER2 To check if INTEGER1 is numerically greater than INTEGER2.
INTEGER1 -lt INTEGER2 To check if INTEGER1 is numerically less than INTEGER2.
-d FILE To check if FILE exists and it is a directory.
-e FILE To check if FILE exists.
-r FILE To check if FILE exists and the read permission is granted.
-s FILE To check if FILE exists and its size is greater than zero (which means that it is not empty).
-w FILE To check if FILE exists and the write permission is granted.
x FILE To check if FILE exists and the execute permission is granted.

๐๐š๐ฌ๐ก ๐‚๐š๐ฌ๐ž๐ฌ

  • Syntax:
#!/bin/bash  
  
echo "Which Operating System are you using?"  
echo "Windows, Android, Chrome, Linux, Others?"  
read -p "Type your OS Name:" OS  
  
case $OS in  
    Windows|windows)  
        echo "That's common. You should try something new."  
        echo  
        ;;  
    Android|android)  
        echo "This is my favorite. It has lots of applications."  
        echo  
        ;;  
    Chrome|chrome)  
        echo "Cool!!! It's for pro users. Amazing Choice."  
        echo  
        ;;  
    Linux|linux)  
        echo "You might be serious about security!!"  
        echo  
        ;;  
    *)  
        echo "Sounds interesting. I will try that."  
        echo  
        ;;  
esac  

๐๐š๐ฌ๐ก ๐Ÿ๐จ๐ซ ๐‹๐จ๐จ๐ฉ

  • C++ like for loop
for ((i = 0 ; i < 100 ; i++)); do
  echo $i
done
  • To read a range
for num in {1..10}  
  do  
  echo $num  
done  
  • a range with increment
for num in {1..10..1}  
  do  
  echo $num  
done  
  • a range with decrement
for num in {10..0..1}  
  do  
  echo $num  
done  
  • Array variables
array=(  "element1" "element 2" .  .  "elementN" )  
  
for i in "${arr[@]}"  
  do  
  echo $i  
done  
  • white spaces in String as word separators
#!/bin/bash  
  
for word in $str;  
do  
  <Statements>  
done  
  • Each line in string as a word
#!/bin/bash  
  
for word in "$str";  
  do  
  <Statements>  
done  
  • Infinite loop
i=1;  
for (( ; ; ))  
  do  
  sleep 1s  
  echo "Current Number: $((i++))"  
done  

๐๐š๐ฌ๐ก ๐ฐ๐ก๐ข๐ฅ๐ž ๐ฅ๐จ๐จ๐ฉ

  • C++ Style while loop
i=1  
while((i <= 10))  
  do  
  echo $i  
  let i++  
done  

๐‚๐ฎ๐ญ ๐‚๐ก๐ž๐š๐ญ๐ฌ๐ก๐ž๐ž๐ญ

Command Explanation
cut -c 3 display 3rd character from each line of text
cut -c 2,7 display the 2nd and 7th character from each line of text
cut -c 2-7 display a range of characters starting at the 2nd position of a string and ending at the 7th position(both positions included)
cut -c -4 display the first four characters from each line of text
cut -d $'\t' -f -3 display first three fields of a tab delimited file
cut -c 13- display the characters from 13th position to the end
cut -d ' ' -f 4 display 4th word with space ' ' as a delimiter
cut -d ' ' -f -3 display first three words with space ' ' used as a delimiter
cut -d $'\t' -f 2- given a tab delimited file, display the fields from second fields to last field

๐Ž๐ญ๐ก๐ž๐ซ ๐๐จ๐ญ๐ž๐ฌ

  • Chop off the arithmetic operations to decimal points: bc <<< "scale=3; $expression"

  • Round of the arithmetic operation result: printf %.3f $(echo $expression | bc -l)

  • Performing Arithmatic Operations (add, subtract, multiply divide) on two variables: $((EXPR))

read -s X;
read -s Y;

echo $(($X + $Y));
echo $(($X - $Y));
echo $(($X * $Y));
echo $(($X / $Y));
  • head : output the first part of files
head -n <number of lines>       # display first n lines from a text file
head -c <number of characters>  # display first n characters from a text file
  • tail : output the last part of files
tail -n <number of lines>      # display last n lines from a text file
tail -c <number of characters> # display last n characters from a text file
  • Reading a file line by line
while read -r line; do <command> "$line" done < filename

Multi-threading or Parallel Execution in bash

  • We make use of parallel command to execute multiple threads in parallel
  • Installing parallel
sudo apt install parallel
  • Reading Man Page
man parallel
  • Usage
parallel [options] [command [arguments]] < list_of_arguments

parallel [options] [command [arguments]] ( ::: arguments | :::+ arguments | :::: argfile(s) | ::::+ argfile(s) ) ...

parallel --semaphore [options] command

#!/usr/bin/parallel --shebang [options] [command [arguments]]

#!/usr/bin/parallel --shebang-wrap [options] [command [arguments]]

Done