MehdiMirzaie2 / 42-born2beroot

Step by step guide for Born2beroot

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Born2BeRoot Guide

This guide has 8 Parts:

  • Part 1 - Downloading Debian
  • Part 2 - Starting Your Virtual Machine
  • Part 3 - Configurating Your Virtual Machine
  • Part 4 - Connecting to Virtual Machine using SSH
  • Part 5 - Continue Configurating Your Virtual Machine
  • Part 6 - Creating sudo.log
  • Part 7 - Signature.txt
  • Part 8 - Your Born2BeRoot Defence Evaluation with Answers

This guide assumes Virtual Box is downloaded on your system

Part 1 - Downloading Debian

  1. Click on this link https://www.debian.org

  2. click Download

  3. click on 'debian-11.6.0-amd64-netinst.iso' to download it.

Part 1.1 - Creating folder for the VM, (this part is important for projects completed on the 42 computers)

  1. Open iterm2
  1. From the home directory type cd sgoinfre/students
  1. Then make a directory mkdir <your intra username>
  1. Then type chmod 700 <your intra username> && cd <your intra username>

  2. Then make directory called born2beroot (don't change the permisions)

  1. Add your Debian Download (from earlier) to the "born2beroot" folder you just created.

Part 2 - Starting your Virtual Machine

  1. Open Virtual Box

  2. Click on New

1*tkjZEbnHKqPGN24HQw_kRA

  1. Change Machine Folder to sgoinfre/students/your_intra_login/Virtual Machine Name and then click continue to move to the next step.

1*WyFDl98AZfft999XCKD6kA

  1. Set Memory Size as 1024 MB and click continue.

1*SoVNIKT340ARlLvQ7RuwDA

  1. Click Create a Virtual Hard Disk Now and then click Create to create the Hard Disk.

1*PzVboJLyLTs7qJmgbdoBYA

  1. Click VDI (VirtualBox Disk Image) and then click Continue to select VDI.

1*6_D9jIyOW0jE3a6vF_UzXg

  1. Click Dyamically Allocated and then click Continue to only use space on your Hard Disk.

1*HagpR-UD0HWCb7zRTeSQXQ

  1. Set Size as 8.00 GB and then click Continue.

1*rYdYJbPswCVCUa5pwKcRZA

  1. Click Settings and then click Storage to view your Virtual Machine Storage.

Screen Shot 2022-06-24 at 12 51 09 PM

  1. Click on Optical Drive (Optical Drive - far right blue small box).

1*je75kGWjXl0M6PlqEzHgoA

  1. Click on Choose a disk file... (2nd option in the drop down).

1*VDy31g0tePnUOuJ1cZQsxQ

  1. Then click on the Virtual Machine file (.iso).

1*FOldzHFaQ2JS_phe6z6T4g

  1. Click on your Virtual Machine and then click 'ok to confirm you Virtual Machine Storage.

1*Evj7Z2EOq102A1zUVgUnQg

  1. Click Start (The Green Arrow ➡️) to start your Virtual Machine.

1*Yg53c1-01g4VzTqhcVEEcA

Part 3 - Configurating Your Virtual Machine

In the Virtual Machine, you will not have access to your mouse and will only use your Keyboard to operate your Virtual Machine.

  1. To increase your Virtual Machine size, press command + c on your Apple Keyboard at the same time and then use your mouse to drag the screen to the size you wish or do the following:

178788620-61064b58-0c0c-4f48-815e-60b4a8eaecae

  1. Use the arrow keys on your keyboard 🔼 🔽 and press Enter on Install (This will start the installation proccess).

1*-tV-M-4g6MH8h6pWJ27bCg

  1. Press enter on English - English or your language of preference.

1*xeb8quQ-ccd5X51d8ToZRw

  1. Press enter on Australia or the country your installing this Virtual Machine.

1*WixFq3GJF9OjeH-zTBTN7Q

  1. Press enter on American English or your keyboard of preference.

1*zKUk6R9tls_jiyY81ue8kA

⚠️ NOTE: Whenever you are told to create a password, use the same password as everything.

  1. Create a Host Name as your login, with 42 at the end (eg. mmirzaie42) - write down your Host Name, as you will need this later on.

1*r0fzgkzXOjK2DfsBCh7wEQ

  1. Leave this blank, press enter on Continue.

1*81XjZBZg2bbNXunuxgnFPQ

  1. Create a Password for the Host Name - write this down as well, as you will need this later on.

1*ft498oj7syh4zVjI48U_tw

  1. Create a User Name without 42 at the end (eg. mmirzaie) - write down your Host Name, as you will need this later on.

1*rhJWnMKN0TPBZwqRey9OeQ

  1. Create a Password for the User Name (you might as well use the same password as your Host Password) write this down as well, as you will need this later on.

  2. Press enter on your Timezone (The timezone your currently doing this project in).

1*2i7svoURih_UIlRJ87rj5w

  1. Press enter on Guided - use entire disk and set up encrypted LVM (Second to last option from the list).

1*CsSx-ALmn8mMxvWicsNVAQ

  1. Press enter on Select Disk to Partition.

1*BTLz5sT6noL_SVQ7eq3u-A

  1. Press enter on Select Separate /home partition (Last option from the list).

1*r5zFPA7R_9BtIqwyOpCCVw

  1. Select Yes and press Enter to write the changes to disks and configure LVM.

1*NHdo3JbApICz0Co2epPLFA

  1. Press Enter to cancel Erasing data as you won't need this for your Virtual Machine.

1*KHmnCUJUWhf1minIdHNS4g

  1. Create a Encryption passphrase - write this down as well, as you will need this later on.

1*B0QL-gX7rZW5-RJyTD1uWw

  1. Retype the Encryption passphrase you just created.

1*xE1owXa0ttpvcioaEwnutA

  1. Type in max and press enter on Continue to assign the amount of volume group to use for guided partitioning.

1*SUFMu-qy3rBwIe9B0Bq3kg

  1. Press enter on Finish partitioning and write changes to disk.

Screen_Shot_2022-09-05_at_5 33 43_PM

  1. Press enter on Yes for Partition Disks.

1*yfXpHyGD37OGAOX7qs1Avw

  1. Press enter on No for Configure the package manager.

1*Mfb1YHt4K3pZJ12TF2dXAw

  1. Press enter in the country that your in.

1*vqV-bN3zDMqTBAKz_u548w

  1. Press enter on deb.debian.org.

1*bLnFC6MebhW1-YZlI2n9_A

  1. Leave this blank and press enter on continue.

1*e08pS8shLNmhZuFUrmuBwA

  1. Press enter on no for Configuring popularity-contest.

1*1I6fHG3MHuovrarqj9PNnA

  1. Deselect SSH server and standard system utilities by pressing the Space key and then press enter on Continue.

1*lGsuAQEwT0WBhb4kdUMp9g

  1. Press enter on Yes to Install the GRUB boot loader on a hard disk.

1*b2qXPye_kX8EudSvbO4yww

  1. Press enter on /dev/sda

174731491-7215eca8-f5e0-4862-a6ef-c49077c94cbe

  1. Press enter on continue to finish the installation.

1*riuXLYYgESxdq-lpkivFXQ

  1. Before we move onto starting your Virtual Machine, make sure you have your Host, Username and Password/s saved or written down somewhere.

Part 3.1 - Starting Your Virtual Machine

  1. Press enter on Debian GNU/Linux

  2. Enter your encryption password you had created before

  3. Login in as the your_username you had created before

  4. Type lsblk in your Virtual Machine to see the partition

Part 3.2 - Installing Sudo (Super User Do)

  1. First type su - to login in as the root user.
  2. Then type apt-get update -y
  3. Then type apt-get upgrade -y
  4. Then type apt install sudo
  5. Then type usermod -aG sudo your_username to add user in the sudo group (To check if user is in sudo group, type getent group sudo)
  6. Type sudo visudo to open sudoers file
  7. Lastly find - # User privilege specification, type your_username ALL=(ALL) ALL

Part 3.3 - Installing Git and Vim

  1. Then type apt-get install git -y to install Git
  2. Then type git --version to check the Git Version
  3. Then type apt-get install vim -y to install Vim

Part 3.4 - Installing and Configuring SSH (Secure Shell Host)

  1. Type sudo apt install openssh-server
  2. Type sudo systemctl status ssh to check SSH Server Status
  3. Type sudo vim /etc/ssh/sshd_config
  4. Find this line #Port22
  5. Change the line to Port 4242 without the # (Hash) in front of it
  6. Find this line #PermitRootLogin
  7. Change to PermitRootLogin no

1*riuXLYYgESxdq-lpkivFXQ

  1. Save and Exit Vim (press esc then shift ZZ)
  2. Then type sudo grep Port /etc/ssh/sshd_config to check if the port settings are right

Part 3.4.1

  1. Type sudo nano /etc/ssh/ssh_config
  2. Find line containing #Port 22
  3. Change it to Port 4242

1*riuXLYYgESxdq-lpkivFXQ

  1. Lastly type sudo service ssh restart to restart the SSH Service

Part 3.5 - Installing and Configuring UFW (Uncomplicated Firewall)

These steps are important as they will allow you to connect to your virtual machine through your host machine with iterm.

  1. First type apt-get install ufw to install UFW
  2. Type sudo ufw enable to inable UFW
  3. Type sudo ufw status numbered to check the status of UFW
  4. Type sudo ufw allow ssh to configure the Rules
  5. Type sudo ufw allow 4242 to configure the Port Rules
  6. Lastly Type sudo ufw status numbered to check the status of UFW 4242 Port

Part 4 Connecting to Virtual Machine using SSH

  1. To exit your Virtual Machine and use your mouse, press command on your Apple Keyboard and your mouse should appear
  2. Go to your Virtual Box Program
  3. Click on your Virtual Machine and select Settings
  4. Click Network then Adapter 1 then Advanced and then click on Port Forwarding 1*rCj_FeuZ5Rm2abz48qhulg
  5. Change the Host Port and Guest Port to 4242 1*61-KSUCFcerO1wPqBcYISg
  6. Then head back to your Virtual Machine
  7. Type sudo systemctl restart ssh to restart your SSH Server
  8. Type sudo service sshd status to check your SSH Status

part 4.1 connect your host computer to the VM

  1. Open an iTerm and type the following ssh your_username@127.0.0.1 -p 4242
  2. In case an error occurs, then type rm ~/.ssh/known_hosts in your iTerm and then retype ssh your_username@127.0.0.1 -p 4242
    • 2.2 now you are connect.
  3. Lastly type exit to quit your SSH iTerm Connection

Part 5 - Continue Configurating Your Virtual Machine

Part 5.1 - Setting Password Policy

  1. First type sudo apt-get install libpam-pwquality to install Password Quality Checking Library

  2. Then type sudo vim /etc/pam.d/common-password

  3. Find this line. password requisite pam_deny.so or

Screen Shot 2022-07-29 at 6 40 34 PM

4. Add this to the end of that line `minlen=10 ucredit=-1 dcredit=-1 maxrepeat=3 reject_username difok=7 enforce_for_root`
  • 4.1 The line should now look like this - password requisite pam_pwquality.so retry=3 minlen=10 ucredit=-1 dcredit=-1 maxrepeat=3 reject_username difok=7 enforce_for_root

179329787-1b718843-9272-43e4-8d92-8d83933cc938

  1. Save and Exit Vim
  2. Next type in your Virtual Machine sudo vim /etc/login.defs
  3. Find this part PASS_MAX_DAYS 9999 PASS_MIN_DAYS 0 PASS_WARN_AGE 7
  4. Change that part to PASS_MAX_DAYS 30 and PASS_MIN_DAYS 2 keep PASS_WARN_AGE 7 as the same
  5. Lastly type sudo reboot to reboot the change affects

Part 6 - Creating sudo.log

  1. First type su -, then type your password.
  2. Then type cd ..
  3. Then type cd var/log
  4. Then type mkdir sudo (if it already exists, then continue to the next step).
  5. Then type cd sudo && touch sudo.log
  6. Then type cd ~/../

Part 6.1 - Configuring Sudoers Group

  1. First type sudo nano /etc/sudoers to go the sudoers file
  2. Now edit your sudoers file to look like the following by adding in all of the defaults in the image below - 1*N4Ad-9k0vfvnWKNC5q6MjQ
Defaults	env_reset
Defaults	mail_badpass
Defaults	secure_path="/usr/local/sbin:/usr/local/bin:/usr/bin:/sbin:/bin"
Defaults	badpass_message="Password is wrong, please try again!"
Defaults	passwd_tries=3
Defaults	logfile="/var/log/sudo.log"
Defaults	log_input, log_output
Defaults	requiretty

Part 6.2 - Crontab Configuation

  1. Then type apt-get install -y net-tools to install the netstat tools
  2. Then type cd /usr/local/bin/
  3. Then type touch monitoring.sh
  4. Lastly type chmod 777 monitoring.sh

Part 6.2.1 - Copy Text Below onto Virtual Machine

The clip board of the host machine and VM are not connected so we will have to connect to the VM with ssh.

  1. Copy this text (To copy the text below, hover with your mouse to the right corner of the text below and a copy icon will appear).
#!/bin/bash
arc=$(uname -a)
pcpu=$(grep "physical id" /proc/cpuinfo | sort | uniq | wc -l) 
vcpu=$(grep "^processor" /proc/cpuinfo | wc -l)
fram=$(free -m | awk '$1 == "Mem:" {print $2}')
uram=$(free -m | awk '$1 == "Mem:" {print $3}')
pram=$(free | awk '$1 == "Mem:" {printf("%.2f"), $3/$2*100}')
fdisk=$(df -BG | grep '^/dev/' | grep -v '/boot$' | awk '{ft += $2} END {print ft}')
udisk=$(df -BM | grep '^/dev/' | grep -v '/boot$' | awk '{ut += $3} END {print ut}')
pdisk=$(df -BM | grep '^/dev/' | grep -v '/boot$' | awk '{ut += $3} {ft+= $2} END {printf("%d"), ut/ft*100}')
cpul=$(top -bn1 | grep '^%Cpu' | cut -c 9- | xargs | awk '{printf("%.1f%%"), $1 + $3}')
lb=$(who -b | awk '$1 == "system" {print $3 " " $4}')
lvmu=$(if [ $(lsblk | grep "lvm" | wc -l) -eq 0 ]; then echo no; else echo yes; fi)
ctcp=$(ss -neopt state established | wc -l)
ulog=$(users | wc -w)
ip=$(hostname -I)
mac=$(ip link show | grep "ether" | awk '{print $2}')
cmds=$(journalctl _COMM=sudo | grep COMMAND | wc -l)
wall "	#Architecture: $arc
	#CPU physical: $pcpu
	#vCPU: $vcpu
	#Memory Usage: $uram/${fram}MB ($pram%)
	#Disk Usage: $udisk/${fdisk}Gb ($pdisk%)
	#CPU load: $cpul
	#Last boot: $lb
	#LVM use: $lvmu
	#Connections TCP: $ctcp ESTABLISHED
	#User log: $ulog
	#Network: IP $ip ($mac)
	#Sudo: $cmds cmd"
  1. Then open up a iTerm2 seperate from your Virtual Machine and type in iTerm ssh your_host_name42@127.0.0.1 -p 4242 and then type your password, when it asks for it.
  2. Then type cd /usr/local/bin.
  3. Then type nano monitoring.sh and paste the text above into the vim monitoring.sh you just created, by doing command + v on your Apple keyboard.
  4. Save and Exit your monitoring.sh
  • 5.1 - Then type exit to exit the iTerm SSH Login.
  • 5.2 - Then go back to your Virtual Machine (not iTerm) and continue on with the steps below.
  1. Then type sudo visudo to open your sudoers file
  2. Add in this line your_username ALL=(ALL) NOPASSWD: /usr/local/bin/monitoring.sh under where its written %sudo ALL=(ALL:ALL) ALL
  3. It should look like this 1*l-7LtAqCon1gRkV3dY3qiQ
  4. Then exit and save your sudoers file
  5. Now type sudo reboot in your Virtual Machine to reboot sudo
  6. Type sudo /usr/local/bin/monitoring.sh to execute your script as su (super user)
  7. Type sudo crontab -u root -e to open the crontab and add the rule
  8. Lastly at the end of the crontab, type the following */10 * * * * /usr/local/bin/monitoring.sh this means that every 10 mins, this script will show

Part 7 - Signature.txt (Last Part Before Defence)

⚠️ Warning: before you generate a signature number, turn off your Virtual Machine. ⚠️

  1. Open iTerm and type cd
  2. Then type cd sgoinfre/students/<your_intra_username>/VirtualBox VMs
  3. Type shasum VirtualBox.vdi or whatever your Virtual Machine is called (This can take from a few seconds to 5 mins).
  4. Copy the output number and create a signature.txt file and paste that number in the file.
  5. Now you submit the signature.txt file with the output number in it.

⚠️ Warning: if you configure your VM after getting the signature, you have to regenerate the signiture. ⚠️

🥳 CONGRATULATIONS! YOU HAVE NOW FINISHED! NEXT IS THE EVALUATION 🔽


Part 8 - Born2BeRoot Defence Evaluation with Answers

Link to the Born2BeRoot Evaluation Checklist created by Adrian Musso-Gonzalez.

The Evaluation Questions

Why did I choose Debian?

Easier to install and configure so better for personal servers.

Difference between Debian and Rocky

The main difference between Debian and Rocky is that Debian is an independent, community-driven Linux distribution with a focus on stability, while Rocky Linux is a community enterprise Operating System (OS) based on CentOS, a previously popular Linux distribution that was discontinued by Red Hat. Additionally, Debian has a larger user community, a broader range of packages, and a more diverse set of supported architectures, while Rocky Linux aims to provide a stable, secure, and reliable enterprise-grade platform for organizations.

What is a Virtual Machine?

Is a resource that uses software instead of a physical computer to run programs or apps. Each VM has its own operating system and functions separately, so you can have more than one VM per machine. Can be used to test applications in a safe, separate environment. Works by using software to simulate virtual hardware and run on a host machine.

What is the difference between aptitude and APT (Advanced Packaging Tool)?

  • Aptitude is a high-level package manager while APT is lower level which can be used by other higher level package managers
  • Aptitude is smarter and will automatically remove unused packages or suggest installation of dependent packages
  • Apt will only do explicitly what it is told to do in the command line

What is AppArmor?

Linux security system that provides Mandatory Access Control (MAC) security. Allows the system admin to restrict the actions that processes can perform. It is included by default with Debian. Run aa-status to check if it is running.

Password Rules

For the password rules, we use the password quality checking library and there are two files the common-password file which sets the rules like upper and lower case characters, duplicate characters etc and the login.defs file which stores the password expiration rules (30 days etc). Sudo nano /etc/login.defs Sudo nano /etc/pam.d/common-password

What is LVM

Logical Volume Manager – allows us to easily manipulate the partitions or logical volume on a storage device.

UFW (Uncomplicated Firewall)

UFW is a interface to modify the firewall of the device without compromising security. You use it to configure which ports to allow connections to and which ports to close. This is useful in conjunction with SSH, can set a specific port for it to work with.

What is SSH?

SSH or Secure Shell is an authentication mechanism between a client and a host. It uses encryption techniques so that all communication between clients and hosts is done in encrypted form. User on Mac or Linux can use SSH the terminal to work on their server via SSH.

What is Cron?

Cron or cron job is a command line utility to schedule commands or scripts to happen at specific intervals or a specific time each day. Useful if you want to set your server to restart at a specific time each day.

  • cd /usr/local/bin – to show monitoring.sh
  • sudo crontab -u root -e – to edit the cron job
  • change script to */1 * * * * sleep 30s && script path – to run it every 30 seconds, delete the line to stop the job from running.

Creating a Group

  1. First type sudo groupadd user42 to create a group
  2. Then type sudo groupadd evaluating to create an evaluating group
  3. Lastly type getent group to check if the group has been created

Creating a User and Assigning Them Into The Group

  1. First type cut -d: -f1 /etc/passwd to check all local users
  2. Type sudo adduser new_username to create a username - write down your new_username, as you will need this later on.
  • 2.1 Type sudo usermod -aG user42 your_username
  • 2.2 Type sudo usermod -aG evaluating your_new_username
  1. Type getent group user42 to check if the user is the group
  2. Type getent group evaluating to check the group
  3. Type groups to see which groups the user account belongs to
  4. Lastly type chage -l your_new_username to check if the password rules are working in users

Evaluation Commands for UFW, Group, Host, lsblk and SSH

  • sudo ufw status
  • sudo systemctl status ssh
  • getent group sudo
  • getent group user42
  • sudo adduser new username
  • sudo groupadd groupname
  • sudo usermod -aG groupname username
  • sudo chage -l username - check password expire rules
  • hostnamectl
  • hostnamectl set-hostname new_hostname - to change the current hostname
  • Restart your Virtual Machine.
  • sudo nano /etc/hosts - change current hostname to new hostname
  • lsblk to display the partitions
  • dpkg -l | grep sudo – to show that sudo is installed
  • sudo ufw status numbered
  • sudo ufw allow port-id
  • sudo ufw delete rule number
  • ssh your_user_id@127.0.0.1 -p 4242 - do this in terminal to show that SSH to port 4242 is working

About

Step by step guide for Born2beroot