Courses-Study

My notes for studying courses

View on GitHub

BASH SCRIPTING

1 - INTRO TO BASH SCRIPTING

#!/bin/bash
# Hello World Bash Script
echo "Hello World!"
  1. First line has characters #! called shebang and a binary file /bin/bash. This line tells that this file should be run with /bin/bash when executing
  2. Second line starts with #. Every line starting with # is known as comment and is ignored by the interpreter
  3. Third line is the command that is going to be executed which prints ‘Hello World!’ to the terminal

For execution we should do several things

  1. Give execution permission with chmod command
  2. Then execute it with ./filename notation. ./ indicates the current path
# Give execution permission
chmod +x hello-world.sh
# chmod is a command to change file permission
# +x is for adding execution permission to a file

# Executing the bash script
./hello-world.sh

01.png

2 - VARIABLES

first_name=Good
last_name=Hacker
echo $first_name $last_name
Good Hacker
greeting=Hello World
  1. Declare varialbe with single quotes
greeting='Hello World'
echo $greeting
Hello World

02.png

  1. Declare variable with double quotes
greeting2="New $greeting"
echo $greeting2
New Hello World

03.png

  1. We can store a command’s output into a a varialbe by putting the command in $()
user=$(whoami)
echo $user
kali

04.png

  1. We can so example number 3 with backtick character ` however it is an older and deprecated method.
user=`whoami`
echo $user
kali

05.png

  1. Let’s see this in example number 5
#!/bin/bash -x
# -x adds additional debug output when executing

var1=value1
echo $var1

var2=value2
echo $var2

$(var1=newvar1)
echo $var1

`var2=newvar2`
echo $var2

06.png

07.png

2.1 - ARGUMENTS

ls -l /var/log
# both -l and /var/log are arguments to ls command
#!/bin/bash

echo "The first two arguments are $1 and $2"
# $1 and $2 refers to the first and second argument of the script
# $0 refer to the bash script file name itselft

08.png

$? # which show exit status of last process
$RANDOM # which generates a random number

09.png

2.2 - READING USER INPUT

#!/bin/bash

echo "Hello there, would you like to learn how to hack: Y/N?"

read answer

echo "Your answer was $answer"

10.png

#!/bin/bash
# Prompt the user for credentials

read -p 'Username: ' username
read -sp 'Password: ' password
# -p is for prompt a text
# -s is for silent mode (not shows the characters)

echo "Thanks, your creds are as follows: " $username " and " $password

11.png

3 - IF, ELSE, ELIF STATEMENTS

12.png

13.png

14.png

15.png

16.png

17.png

18.png

19.png

20.png

21.png

4 - BOOLEAN LOGICAL OPERATIONS

  1. First of all the && operator, we use it when we want to execute second command only if the first command executes successfully, Lets see it in an example

22.png

  1. Let’s see another example with a command that returns false when executing (Here grep returns false because user2 does not exist in /etc/passwd)

23.png

  1. Let’s see another example with or || operator, This operator is opposite of && and it executes second command only if the first command fails. Here the first command failed to find user2 and instead the second command runs

24.png

  1. We can use this in test commands or conditions to use multiple commands to meet a specific condition. Let’s see an example that checks two conditions and runs a third command when both commands return true:

25.png

26.png

  1. Let’s see previous example with or || operator and use it in if statement, Here only one of them has to be true to meet the condition and enter the if branch

27.png

28.png

5 - LOOPS

5.1 - FOR LOOPS

29.png

30.png

31.png

5.1 - WHLIE LOOPS

32.png

33.png

34.png

35.png

36.png

6 - FUNCTIONS

37.png

38.png 39.png

40.png 41.png

42.png 43.png

Variables Scope

local name="Kourosh"

44.png

45.png

7 - PRACTICAL EXAMPLES

7.1 - PRACTICAL BASH USAGE – EXAMPLE 1

wget www.megacorp.com

46.png

grep "href=" index.html

47.png

grep "href=" index.html | grep "\.megacorpone" | grep -v "www\.megacorpone\.com" | head

48.png

grep "href=" index.html | grep "\.megacorpone" | grep -v "www\.megacorpone\.com" | awk -F "http://" 'print {$2}'

49.png

grep -o "[^/]*\.megacorpone\.com" index.html | sort -u > list.txt
# grep -o only returns strings defined in reqex
# [^/]* means every character except /
# \. means we wanna treat . as normal character not regex syntax

51.png

for subdomain in $(cat list.txt); do host $subdomain; done

52.png

for subdomain in $(cat list.txt); do host $subdomain; done | grep "has address" | cut -d " " -f 4 | sort -u
# grep "hass address" searchs for lines which has ip address
# cut -f " " -f 4 separates by " " and pick forth element which is IP address
# sort -u sorts the results

53.png

7.2 - PRACTICAL BASH USAGE – EXAMPLE 2

searchsploit afd windows -w -t | grep http | cut -f 2 -d "|"

54.png

for e in $(searchsploit afd windows -w -t | grep http | cut -f 2 -d "|"); do exp_name=$(echo $e | cut -d "/" -f 5) && url=$(echo $e | sed 's/exploits/raw/') && wget -q --no-check-certificate $url -O $exp_name; done

# for iterates inside exploits
# exp_name extracts the exploit name/ID from the link
# sed replaces exploit with raw to generate raw exploit link
# && wget downloads the exploit if the previous command exists with success

55.png

#!/bin/bash 
# Bash Script to search for a given exploit and download all matches.

for e in $(searchsploit afd windows -w -t | grep http | cut -f 2 -d "|")

do
	exp_name=$(echo $e | cut -d "/" -f 5)
	url=$(echo $e | sed 's/exploits/raw/')
	wget -q --no-check-certificate $url -O $exp_name
done

56.png

7.3 - PRACTICAL BASH USAGE – EXAMPLE 3

sudo nmap -A -p80 --open 10.11.1.0/24 -oG nmap-scan_10.11.1.1-254 
# -A is for aggressive mode
# -p80 scan just for port 80
# --open only shows open ports
# -oG is for write output to file

57.png

cat nmap-scan_10.11.1.1-254 | grep 80 | grep -v "Nmap"

58.png

cat nmap-scan_10.11.1.1-254 | grep 80 | grep -v "Nmap" | awk '{print $2}'

59.png

for ip in $(cat nmap-scan_10.11.1.1-254 | grep 80 | grep -v "Nmap" | awk '{print $2}'); do cutycapt --url=$ip --out=$ip.png; done
# --url specify target website
# --out specify the output png file

60.png

#!/bin/bash
# Bash Script to examine the scan results through HTML.

echo "<HTML><BODY><BR>" > web.html

ls -l *.png | awk -F : '{ print $1":\n<BR><IMG SRC=\""$1""$2"\" width=600><BR>"}' >> web.html

echo "</BODY></HTML>" >> web.html

61.png

Learning to use bash effectively allows you to do large amount of tasks and tests automatically