Matrix-3: Vulnhub Walkthrough


Today we are going to take another CTF challenge from the series of Matrix. The credit for making this VM machine goes to “Ajay Verma” and it is another boot2root challenge where we have to root the server and capture the flag to complete the challenge.
You can download this VM here.
Security Level: Intermediate
Penetrating Methodology:
1.     Scanning
·       Netdiscover
·       NMAP
2.     Enumeration
·       Web Directory search 
3.     Exploitation
·       Ghidra
·       SSH
4.     Privilege Escalation
·       Exploiting Sudo rights

Walkthrough:

Scanning:
Let’s start of by scanning the network and identifying host IP address. We can identify our host IP as 192.168.1.104 by using Netdiscover.


Then we used Nmap for port enumeration. We found that port 80 is open, SSH is running on port 6464 and port 7331 is open on the target machine.

nmap –p- –A 192.168.1.104



Enumeration:

As we can see port 80 is open, we tried to open the IP address in our browser but we didn’t find anything useful on the webpage.




So we used dirb for directory enumeration.


After brute forcing with dirb we found a directory named /assets




We opened the assets directory in the browser and found an image file named Matrix_can-show-you-the-door.png under /assets/img/ URL.





We first opened this image but didn’t find anything of our use. Then upon looking at the file name properly we found out that the name of the file is itself giving us the path forward.
So we used Matrix in the URL as show in the image below and it worked for us.
From the contents of the directory Matrix we understood that we have to make a right combination of the alpha numeric to go ahead.




So after trying multiple combinations we used our little brain more aggressively and made a combination of n/e/o/6/4,  neo is the name of the actor in the Matrix movie and 64 number is I guess favorite number of the creator of this VM because he is using it everywhere.



We downloaded the file secret.gz and found that it’s actually a txt file and is containing the username and password.
file secret.gz
cat secret.gz



Upon cracking the hashed password using online tool hashkiller, we found the password as passwd.




If you remember from the nmap scan we have a port 7331 open and it was protected with Basic Authentication.
So we tried to open the URL http://192.168.1.104:7331  and were prompted for authentication, so we used admin:passwd as username and password and were able to login successfully.




But we couldn’t find anything useful there, so we used dirb with already obtained username and password for directory bruteforcing.
After bruteforcing we found a directory named data.
dirb http://192.168.1.104:7331 / -u admin:passwd



In the data directory we found a file name data which came out to be a DOS file.


Exploitation:
We took help of our best friend in need Google to know how to open a DOS file. And after some research we found a tool named Ghidra for opening a DOS file.
After opening the data file with Ghidra tool we found a username and password guest:7R1n17yN30





As we already know from our nmap scan that there is SSH running on port 6464 on the target machine, so we tried to ssh the target machine with the above found username and password and were successfully able to login.
ssh guest@192.168.1.104 –p 6464

id

But we were providing with the restricted bash (rbash) shell, so we used –t option to run ssh with noprofile extension and we got a complete shell of guest user.
Checking the sudo permissions for guest user we came to know that this user can run /bin/cp with permissions of another user trinity.

ssh guest@192.168.1.104 –p6464 –t “bash –noprofile”

sudo -l



Privilege Escalation:
To elevate to a more privilege’s user, what we did is we created a new ssh key pair, gave read write execute permissions to id_rsa.pub file so that we would be able to copy it to our target location.
ssh-keygen
cd .ssh
chmod 777 id_rsa.pub




And then we took the advantage of sudo permission to copy the id_rsa.pub file in the /home/trinity/.ssh/authorized_keys folder. Now we can access ssh of the target machine with trinity user using the id_rsa key.
Checking the sudo permission for trinity it can execute oracle file with root permissions.

cp id_rsa.pub /home/guest
cd ..
sudo –u trinity /bin/cp/ ./id_rsa.pub /home/trinity/.ssh/authorized_keys
ssh trinity@127.0.0.1 –I /.ssh/id_rsa –p 6464
sudo -l



But there was no file with the name oracle in the /home/trinity directory, so we created an oracle file with /bin/sh in it using echo command. In the end we executed the oracle file with sudo command, we got the root shell.
 And once you have the root shell you can easily get the flag.
echo “/bin/sh” > oracle
chmod 777 oracle
sudo ./oracle
Id
ls
cat flag.txt



Linux for Pentester: sed Privilege Escalation


This article will take our readers through all about Stream Editor (Sed), which is one of the most prominent text-processing service on GNU/Linux. In this article we came with the brief introductory guide to sed which supports the main concern that how sed works and how we can accomplish its supplementary practice in the operation of Privilege Escalation.

NOTE: “The main objective of publishing the series of “Linux for pentester” is to introduce the circumstances and any kind of hurdles that can be faced by any pentester while solving CTF challenges or OSCP labs which are based on Linux privilege escalations. Here we do not criticizing any kind of misconfiguration that a network or system administrator does for providing higher permissions on any programs/binaries/files & etc.”

Table of Content
Overview to sed                                            
·        Summary to sed
·        Chief Action achieved using sed
o   Replacement with the sed command
o   Printing and viewing from sed command
o   Deleting lines with sed

Abusing sed
·        SUDO Lab setups for privilege Escalation
·        Exploiting SUDO

Summary to sed
SED command in LINUX/UNIX is stands for “stream editor” that can implement lots of purpose on file like, searching, find and replace, insertion or deletion. However most common use of SED command is for exchange or for discover and swap. By using SED you can edit files even without opening it, which is much faster technique to find and replace something in file. It is a powerful text stream editor which can do insertion, deletion, search etc. for any file as per user requirements. This command also supports regular expression that allows it to perform complex pattern matching too. Now to know further about “sed” command we will start from its help option.

Note:It's worth remarking that this article omits several commands, as our main concern is to reach about the “sed” influence over Privilege Escalation.
sed --help


Key actions achieved by “sed”
1       Replacement with the sed command: As we know the “sed” performs many tasks that includes insertion, deletion, modification and so on for any file as per user request so now we will start our journey to explore the entire utility of sed one by one.

1.1 Substituting or switching string: “sed” is used to replace or swap the string so whenever we need to exchange any string within a file then we will frame command as:

nano Ignite.txt
cat Ignite.txt
sed 's/Ignite/Egnyte/' Ignite.txt
In the above command “s” denotes the substitution action. The “Ignite” is the hunt pattern and the “Egnyte” is the replacement string. By default, the sed command replaces the first incidence of the pattern in each line and it won’t replace the second, third…occurrence in the line.


1.2 Substituting the nth existence in a line: When we want to replace nth occurrence i.e. first, second and so on existence of a pattern in a line then we will use the /1, /2 etc flags to mention the nth term.
sed 's/Ignite/Egnyte/2' Ignite.txt

Here I’m swapping for 2nd occurrence in each line.


1.3 Substituting all the existence at a time: As we know by default the sed command replaces the first incidence of the pattern in each line so if we wish to replace all occurrence simultaneously within a file then we can use flag “/g” for this purpose.

 sed 's/Ignite/Egnyte/g' Ignite.txt
1.4 Substituting from nth occurrence to all existences: When we use “/g” this will make change globally to the entire file so if we want to make this swapping from a specific place then we need to mention that value(nth) from where we want to make changes.

sed 's/Ignite/Egnyte/3g' Ignite.txt
On framing the above command it will replace all the patterns from the nth occurrence globally.
Note: In the below image you can’t see any changes for flag “3g” as my file doesn’t contain any 3rd occurrence of replaced word but whenever there is existence of substituted word at multiple times within a line then you can clearly see the changes that how its change globally from nth term.


1.5 Substituting the existence for a particular range:  We can limit the sed command to replace the string for a particular range. This can be achieve by framing command as shown below.
sed ‘1,3 s/Ignite/Egnyte/’ Ignite.txt

On framing this command the “sed” will replace “Ignite” starting from first line to third line.
Note:  One can use “$” in place of end index if we want substitute from nth term to last line in the file.


2       Printing and viewing from sed command: Apart from substituting the string sed can help in printing and viewing a file as per user’s instruction.

2.1 Replicating the replaced line with /p flag: If we want to make duplication for replaced line then we can use “/p” flag which prints the replaced line twice on the terminal. If a line does not have the search pattern and is not replaced, then it will print that line only once.
sed ‘s/Ignite/Egnyte/p’ Ignite.txt

2.2 Printing only the replaced lines: If user wants to print only those lines which are substituted then he can use “-n” option following by print command as shown below.
sed -n ‘s/Ignite/Egnyte/p’ Ignite.txt

As from below image it can be cleared that on using “-n” the print flag has printed all the replaced line as output.


2.3 Printing lines by numbering it: This command is similar to “cat” in which we use “-n” for numbering the line for any file, same we can achieve from sed command too by framing the command as below.

sed = a.txt | sed 'N; s/^/     /; s/ *\(.\{4,\}\)\n/\1  /'
On drawing the above command sed will print the output by numbering each line as per user request.



2.4 Display a file from x to y range: If we want to view a file from an instance i.e. for a range of starting index to end index then we write command as:
sed -n ‘2,4p’ Ignite.txt

If we use “d” instead of “p” then sed will View the entire file except the given range.

2.5 Print nth line of the file: Inplace of fixing end index you can also leave it blank if you wish to print only a specefic line.
sed -n ‘4’p Ignite.txt
As in below screenshot you can see when i have used above mentioned command then sed has reflectd the output only to print for 4th line.



2.6 Print from nth line to end of file: To print any file from its nth line to the last (end of file) line then frame command as below:

sed -n ‘4,$’p Ignite.txt
Here “$” is indication for reflecting last line of file.


2.7 Print the line only for pattern matching: If we want to print only those lines which matches the given pattern then in this case we will draw command as:
sed -n /training/p Ignite.txt

From below image it is clear that how this command works. Here in below image I have print those lines which includes the word “training”.
2.8 Print lines which matches the pattern nth line: We can use numeric value along “p” to print for pattern matching till nth line.

sed -n ‘/cyber/,3p’ Ignite.txt


3 Deleting lines with sed: Now we check how we can delete the lines from file by the help of sed.

 3.1 Remove a specific line: To delete any particular line within a file us “d” option followed by sed command. Here I’m deleting 3rd line from “Ignite.txt”.

sed ‘3d’ Ignite.txt

3.2 Remove line for a range: If we wish to delete content till a particular range then we will set its “intial index value” and “end value” of file. In below image I have deleted content of “Ignite.txt” from its 3rd line to 5th line and will attain output for remaining file content.

sed ‘3,5d’ Ignite.txt



3.3 Remove from nth to last line: Instead of fixing end index one can also use “$” to delete lines till the end of file.

sed ‘2,$d’ Ignite.txt

Here “2” indicating for the initial index from where deletion must done and “$” is indicating to delete lines till the end of file.

3.4 Remove the last line: If we won’t set any index value then “$d” will simply delete only the last line of the file.

sed ‘$d’ Ignite.txt




3.5 Remove the pattern matching line: Sometimes we not only want to print or view those lines that matches particular pattern but also desire to delete them so in such case we will frame below command to attain output as per user request.
sed ‘/training/d’ Ignite.txt
Here in below image sed has deleted all those lines which matches the word “training”.



Abusing sed
Sudo Rights Lab setups for Privilege Escalation
Now we will start our mission of privilege escalation. To grab this first, we have to set up our lab of sed command with administrative rights. After that we will check for the sed command that what impact it has after getting sudo rights and how we can use it more for privilege escalation.
It can be clearly understood by the below image in which I have created a local user (test) who own all sudo rights as root and can achieve all task as admin.
To add sudo right open etc/sudoers file and type following as user Privilege specification.
test All=(root) NOPASSWD: /usr/bin/sed


Exploiting Sudo rights

Now we will start exploiting sed facility by taking the privilege of sudoer’s permission. For this very first we must have sessions of victim’s machine then only we can execute this task. Suppose we got the sessions of victim’s machine that will assist us to have local user access of the targeted system through which we can escalate the root user rights.
So now we will connect to the target machine with ssh, therefore, type following command to get access through local user login.
ssh test@192.168.1.108
Then we look for sudo right of “test” user (if given) and found that user “test” can execute the pip command as “root” without a password.
sudo -l
Now we will access our /etc/passwd file by the help sed command to escalate or maintain access with elevated privileges.
Conclusion: Hence we have successfully exploited “sed” by achieving its functionality after granting higher privilege. 



Reference link: https://gtfobins.github.io