To save and get out of anything press Ctrl+D
You get can all the referencial scripts in here.
run these commands in root terminal else it might give you some weird error
apt-get update && apt-get upgrade -y
apt-get -y install open-vm-tools-desktop && reboot
apt-get -y install fuse
First of all create a directory in windows that you want to share
Then Right-Click on the folder -> Properties -> Sharing -> Advanced sharing -> Check the 'Share this folder' Option -> Permissions -> Check 'Full -control' for 'Everyone'
Enable folder sharing in the VMWARE as 'Always Enabled'
after that run the following command no matter how long it takes run this command(make sure you are in sudo su) in kali:
Post that run the following command
cd /mnt
Use the above command to move to the mnt folder then use the command
sudo mkdir hgfs
After that use the command
sudo mount -t fuse.vmhgfs-fuse .host:/ /mnt/hgfs -o allow_other
cat /etc/shells
This command gives you the list of all the available shells Checkout Command 2
which bash
This command gives you the location of bash which you have to include in the bash script
Checkout Command 3 and its output
First Script! Script Traversing and storing it in the Desktop and gaving it the execution command and executing it Commands
To write the output of a file into another file. Here is how you do it! Example
To write into another file with scripting, here is how you do it! Example
Output for the same OutputTo append the output of the script in the file use '>>' . Example
Check out the output of the script here
Checkout the script for the if-else and its respective output as well.
Checkout the script for the if-else-and-operator and its respective output as well.
Checkout the script for the if-else-or-operator and its respective output as well.
Checkout the SWITCH CASE and its respective output as well.
Script 1
Script 2
While loops run until the condition is false.
To use while loops Example
Until Loops run until the condition is true.
To use until loops Example
There are multiple ways of runnning a FOR Loop:
Normal loop. Example 1
Normal loop with a step function. Example 2
Traditional For LoopExample 3
Break Statement stops the entire execution of the loop and exits the loop.
Example
Where the Break Statement stops the entire execution of the loops and exits the loop, the continue statement only skips that one specific iteration of the loops and continues it execution further.
Example
Bounded number of inputs can taken from the user through terminal.
Exameple
Bounded number of inputs can taken from the user through terminal.
Example
To find out the length of the array we use the command 'echo #$'.
Example
To read a file content as an argument from the terminal '${1:-/dev/stdin}'.
Example 2 here file.txt is given as an argument
NOTE: If any file name is not passed as an argument then the script acts as a cat for the terminal
Example 1 where there is no filename passed as an argument
To read a file content without the use of '${1:-/dev/stdin}', just replace '${1:-/dev/stdin}' with the filename.
Example where file.txt has been replaced at the location of the file
ls -al 1>fileOUTPUT.txt 2>fileErr.txt
For this refer this VIDEO from time 57:04-1:02:00
Pipes are also known as - "Send inputs of one script to another script"
Sending a message from one scripts output as an input to another script.
Example
Checking if the Strings are equal or not.
Example
'/>' and '</' are the two operators required for checking if the string is of the smaller or greater size.
Example
declare <Variable-name> = <value>
declare command is used to declare a variable. Example
declare -p
This command gives you the output and shows you all the previously declared variables. Example
declare -r psswdLocation = /etc/passwd
declaring the variable with -r converts a traditional variable to a read-only variable Example
Syntax for declaring an Array
variableName = ('Value1','Value2','Value3')
Example
At line number 6 we use the '!' operator to print the index of the elements
Line 7 shows how to find out the size of the array
We use unset method to discard the value at the index 1
First function example shows how to define and call a function
Second function example shows how to define and call an argumentative function
mkdir -p <directory-name>
Using mkdir command in the script to create a new directory in the current directory
Example
url="https://github.com/sagar98cyber/bash-scripting-cheatsheet.git"
CURL ${url}
the above command is used to download different stuff from the URL
url="https://github.com/sagar98cyber/bash-scripting-cheatsheet.git"
curl ${url} -O
The above command with flag -O is used to download the file with the orginal filename as stored in the remote location
url="https://github.com/sagar98cyber/bash-scripting-cheatsheet.git"
curl ${url} -o Filename
The above command with flag -o: where o represents options, with -o we append the filename which would be given to the file being downloaded.
An Alternative to the above command is:
url="https://github.com/sagar98cyber/bash-scripting-cheatsheet.git"
curl ${url} > Filename
The below command is used to download the header(if you want to check if a 10GB of file is the exact one that you are looking for or not then you dont necessarily have to download the entire file to check, you can just download the header of the file) of the file.
url="https://github.com/sagar98cyber/bash-scripting-cheatsheet.git"
curl ${url} -I Filename
First we look at the select loop
#! /usr/bin/bash
select car in TOYOTA TESLA MARUTI TATAMOTORS
do
echo "You have selected $car as your favorite option!!"
done
Now instead of echoing out the output we can write switch statements
#! /usr/bin/bash
select car in TOYOTA TESLA MARUTI TATAMOTORS
do
case $car in
TOYOTA)
echo "You selected $car as your option!!";;
TESLA)
echo "You selected $car as your option!!";;
MARUTI)
echo "You selected $car as your option!!";;
TATAMOTORS)
echo "You selected $car as your option!!";;
*)
echo "Please enter a valid choice!!";;
esac
done
In the below code after the function body we see that there is a while loop
#! /usr/bin/bash
function FuncCar(){
select car in TOYOTA TESLA MARUTI TATAMOTORS
do
case $car in
TOYOTA)
echo "You selected $car as your option!!";;
TESLA)
echo "You selected $car as your option!!";;
MARUTI)
echo "You selected $car as your option!!";;
TATAMOTORS)
echo "You selected $car as your option!!";;
*)
echo "Please enter a valid choice!!";;
esac
done
}
echo "press any ket to continue"
while [ true ]
do
read -t 3 -n 1
if [ $? = 0 ]
then
echo "you have terminated the script"
FuncCar
exit;
else
echo "Waiting for you to press a key!!!"
fi
done
After the while loop we in the while body we have read command with a flag -t which stands for the time
That is "-t 3 -n" together means after 3 seconds interval go to a new line and execute the code in the loop.
It is basically a notification facility that alerts you on changes in files and directories
In other words it is used to monitor files and directories.
First of all we have to install iNotify with:
sudo apt install inotify-tools
NOTE: We can either monitor the directory or file that has been created or for learning purpose include a file or a directory that has already been created
After the installation we create a script that will be used to monitor a file or a directory:
inotify -m directory/file-name
grep stringToBeSearched fileInWhichStringIsToBeSearched
The above command is used to search for a string
We can also accomplish other tasks for example counting the occurences of the string in the file or to cat everything except that string that is to be searched
All this can be accomplished with the help of different flags
Let's take an example, first we need a file filled with raw data that is to be examined, then we run grep in the file by giving the appropriate locations in the script
grep stringToBeSearched fileInWhichStringIsToBeSearched
With no flags the grep just lists out all the occurences of a particular string
grep -i stringToBeSearched fileInWhichStringIsToBeSearched
Grep with the flag -i works just as same as the above command
grep -i -n stringToBeSearched fileInWhichStringIsToBeSearched
Grep with the -n command tells you the numbers at which lines the string resides
grep -i -n -c stringToBeSearched fileInWhichStringIsToBeSearched
-c flag is used to count the number of occurences of the string in the file
grep -i -v stringToBeSearched fileInWhichStringIsToBeSearched
-v flag is used to give the output of everything except the searched string
AWK is a scripting language that is used to manupilate data and generating reports
It makes it easier for string manupilation and arthimetic operations
Just like bash which has an extension of .sh awk has na extension of .awk
Utility that is tiny but powerful
Writing our First Program to print everything in the raw file
Second program is to print the lines in which Linux exists!
Third Program is if we want to display 2 word of a line or 3 word of a line then we use this script.
Also used for string and data manupilation
In first program we substitute where we replace 'i' with 'I'. 's/i/I/g' where s is for substitute 'i' is to replace with the 'I' and 'g' stands for global
Second program is just another way to achieve the same thing
If noticed carefully it does not really make any changes in the raw.txt file so instead of making changes in the existing file it is recommended that we save the output in a different file
Following code of the third program will help you for that.
bash +x ./scriptName.sh
use the above command to debugg your script
#! /usr/bin/bash -x
Appending the '-x' flag to your bash location at the start of the script will make you debugg your script as well
set-x
---------------
---------------
---------------
---------------
set +x
The above method is used to set a start point and an end point which basically means that the program between the set -x and set +x will be debugged
Splunk is a SIEM TOOL that helps in making 'data in to moving' which means, that it follows a client-server model and collects logs from different client machines and forwards it to the reciever.
We have a centralized reciever that recieves the data from the variety of clients.
Note: Make sure to perform below commands using the 'sudo su' which is using the root previlege:
wget -O splunk-8.2.4-87e2dda940d1-Linux-x86_64.tgz 'https://download.splunk.com/products/splunk/releases/8.2.4/linux/splunk-8.2.4-87e2dda940d1-Linux-x86_64.tgz'
After the dowload use the below command to install
Make sure that you have the root previleges and that you are in the '/' folder while using this command
tar -zxvf yourTGZfile.gz
After installation check if you have the splunk installed and have all its respective directories like 'bin', 'etc'
cd /splunk
ls
cd bin
ls
If everything is all good till here the use the following command to start the splunk
Make sure that you are in /splunk/bin directory while using this command
./splunk start --accept-license
After that enter the admin creation id and passward: for example for me it is user:admin and password:admin@143
After installing the splunk in your kali, you can configure it to recieve the data from the monitoring system.
We download a splunk universal forwarder which gathers all the data needed and forwards it to the splunk reciever.
For reference walkthrough use the following tutorial.