Notes: Linux Shells | Cyber Security 101 | THM
- solbergtonje
- 10 dec. 2024
- 4 min läsning
Uppdaterat: 25 dec. 2024
Writing commands in CLI - more efficient and resource-friendly
How To Interact With a Shell
When interacting with a shell, you must be in the directory where you want to perform operations.
Default = your home directory
Commands
pwd: (Print Working Directory) current working directory
cd: (Change Directory) change directory
ls: show contents of a directory
cat: show contents of a file
grep: search for word or pattern inside a file
Example: grep THM dictionary.txt (searching for THM in dictionary.txt)
Types of Linux Shells
All the different types of shells have their own features and characteristics
Multiple shells are installed in different Linux distributions
Commands
echo $SHELL: see which shell in use
/etc/shells: list all available shells in that Linux OS
Example: cat /etc/shells
Change shell by typing the shell name
Example: zsh
Change default shell: chsh -s
Example: chsh -s /usr/bin/zsh (sets zsh as default shell)
Different Shells
Bash (Bourne Again Shell)
Default shell for most Linux distributions.
Key features:
- widely used with scripting capabilities with extensive documentation available
- basic tab completion feature
- basic level of customization
- less user-friendly (but widely used = familiarity)
- syntax highlighting NOT available
- keeps a history file and logs all of your commands = use up/down arrow
- command: history = display all your previous commands
Fish (Friendly Interactive Shell)
Not installed by default in most Linux distributions.
Focus on user-friendliness
Key features:
- limited scripting features compared to Bash and Zsh
- advanced tab completion
- most user-friends shell - very simple syntax, good for beginner users
- syntax highlighting built-in - improved readability - different colours - helps spot errors
- auto spell correction for writing commands
- customize command prompt with different themes through interactive tools
- command history
Zsh (Z Shell)
Not installed by default in most Linux distributions.
Considered a modern shell.
Key features:
- excellent level of scripting
- advanced tab completion - extended by using plugins
- advanced customisation through oh-my-zsh framework - make it slower than other shells
- can be highly user-friendly with proper customization
- syntax highlighting can be used with plugins
- auto spell correction for commands
- tab completion, and command history
Selecting the best shell depends on your usage and the shell's features.
Shell Scripting and Components
A shell script = set of commands
Scripting helps to automate tasks.
.sh = default extension for bash scripts
create file: nano first:script.sh
Start every script with shebang
shebang = #! + name of interpreter to use while executing the script
Example: #!/bin/bash
Fundamental building blocks of a script (=an efficient script)
Variables
A variable stores a value inside
Then the variable name can be used wherever you need it
Example:
# Defining the Interpreter
#!/bin/bash
echo "Hey, what's your name?"
read name
echo "Welcome, $name"
echo - display the string "Hey, what's your name?"
read - takes input from the used
name - variable to store input from user
Give script execution permissions:
Example: chmod +x scriptname.sh
Use ./ before script name to execute the script in the current directory
(otherwise it will search for the script in the PATH environment variable)
Loops
loop = repeating something
EXAMPLE
# Defining the Interpreter
#!/bin/bash
for i in {1..10};
do
echo $i
done
this is a 'for loop'
i = variable
do = start loop
echo $i = output loop
done = end loop
Conditional Statements
Help execute a code only when a condition is met, otherwise run another code.
EXAMPLE
# Defining the Interpreter
#!/bin/bash
echo "Please enter your name first:"
read name
if [ "$name" = "Stewart" ]; then
echo "Welcome Stewart! Here is the secret: THM:Script"
else
echo "Sorry! You are not authorized to access the secret."
fi
variable = name
if = starts the conditional statement and compare the value of the variable with the string "Stewart"
- if a match = display secret
- if NOT a match = display "Sorry! You are not authorized to access the secret."
fi = end the 'for loop'
Comments
A comment = sentence explaining the following part of the code
Comments don't affect the running of the script
begin with # + space = '# '
Example: # Asking the user to enter a value
Best practice: place comments in major and complex areas of the script
The Locker Script
# Defining the Interpreter
#!/bin/bash
# Defining the variable
username=""
companymame=""
pin=""
# Defining the loop
for i in {1..3}; do
# Defining the conditional statements
if [ "$i" -eq 1 ]; then
echo "Enter your Username:"
read username
elif [ "$i -eq 2 ]; then
echo "Enter your Company name:"
read companyname
else
echo "Enter your PIN:"
read pin
fi
done
# Checking if the user entered the correct details
if [ "$username" = "John" ] && [ "$companyname" = "Tryhackme" ] && [ "$pin" = "7385" ]; then
echo "Authentication Successful. You can now access your locker, John."
else
echo "Authentication Denied!!"
fi
Practical Exercise
change user to root: sudo su
enter user's pw
__
whoami: see who I am
__
cat /var/log/authentication.log




