tryhackme - Linux Agency

Table of Contents
Room: Linux Agency
Difficulty: Medium
This Room will help you to sharpen your Linux Skills and help you to learn basic privilege escalation in a HITMAN theme. So, pack your briefcase and grab your SilverBallers as its gonna be a tough ride.
Task 1 - Deploy the Machine
Welcome to Linux Agency. Agent 47, this is where you will need to go through several tests concerning linux fundamentals and privilege escalation techniques.
This room is proudly made by 0z09e and Xyan1d3
If you enjoy this room, please let us know by tagging us on Twitter. You may also contact us in case of some unintended routes or bugs, and we will be happy to resolve them.
Please do not ask for hints in the TryHackMe Discord server until 7PM on the 30th of January, 2021
Task 1.1 - Deploy This Machine
No Answer Needed
Task 2 - Let’s just jump in
Please wait about 1 minute before SSH’ing into the box.
SSH Username : agent47 SSH Password : 640509040147
Each flag found will serve as the password for the next user. The flag includes the username of the next user that is part of this challenge. The Flag format is : username{md5sum}
The order of users: agent47 –> mission1 –> mission30 will be part of Task 3: Linux Fundamentals.
After those missions, the next levels will be in Task 4: Privilege Escalation.
Task 2.1 - SSH into the box as agent47
No Answer Needed
Task 3 - Linux Fundamentals
Agent 47, we are ICA, the Linux Agency. We will test your Linux Fundamentals. Let’s see if you can pass all these challenges of basic Linux. The password of the next mission will be the flag of that mission. Example: mission1{1234567890} will be the password for the mission1 user.
What is the mission1 flag?
mission1{REDACTED}
I will try my best to get flags with oneliners or with less commands, let’s see how it goes :)
After SSH’ing as ‘agent47’, let’s run grep
for our first flag:
What is the mission2 flag?
mission2{REDACTED}
This was easy:
What is the mission3 flag?
mission3{REDACTED}
I think its gonna be a smooth ride:
What is the mission4 flag?
mission4{REDACTED}
Flag does not appears to be here, so, I ran grep
to find the flag but it was pointing me to this exact file:
It is weird, I tried nano
on it and found the flag.
- ^M is making everything on the left non-printable character, which is why they are not displayed while printing the flag.txt with cat.
You can run cat
with -v
to print non-printable characters:
What is the mission5 flag?
mission5{REDACTED}
What is the mission6 flag?
mission6{REDACTED}
What is the mission7 flag?
mission7{REDACTED}
What is the mission8 flag?
mission8{REDACTED}
When you switch to mission7 user, it tries to load .bashrc of user mission6, which is why it throws permission denied, and you can’t cd
into /mission7 directly, because here cd
will get you to /mission6 (you don’t have any permissions there), so, do cd ../mission7
to go to your home directory, and cat
out the flag.
What is the mission9 flag?
mission9{REDACTED}
Ride is still going smooth:
What is the mission10 flag?
mission10{REDACTED}
What is the mission11 flag?
mission11{REDACTED}
What is the mission12 flag?
mission12{REDACTED}
I looked for the flag using find / -iname flag.txt 2>/dev/null
and grep -r 'mission' ./
but couldn’t find it, so I tried to explore the .dotfiles and found this in .bashrc:
What is the mission13 flag?
mission13{REDACTED}
flag.txt file doesn’t have any permissions which is why we can’t read it, but we are the owner of the file, so we can simply change the permissions and read the flag.txt.
What is the mission14 flag?
mission14{REDACTED}
What is the mission15 flag?
mission15{REDACTED}
It is binary data:
NOTE: I used ‘CyberChef’ for converting.
After converting from binary to ascii, we get our flag:
mission15{REDACTED}
What is the mission16 flag?
mission16{REDACTED}
It looks like hex:
NOTE: I used ‘CyberChef’ for converting.
after converting from hexadecimal to ascii, we get our flag:
mission16{REDACTED}
What is the mission17 flag?
mission17{REDACTED}
- It is a binary file, let’s make it executable and run it.
What is the mission18 flag?
mission18{REDACTED}
- It is a
java
file, let’s compile it withjavac
and run it withjava
.
What is the mission19 flag?
mission19{REDACTED}
I like this part, because I get to learn how to compile and run with different compilers/interpreters.
- It is a ruby file, let’s run
ruby
on it.
What is the mission20 flag?
mission20{REDACTED}
- It is a c file, let’s compile it with
gcc
and run it.
What is the mission21 flag?
mission21{REDACTED}
What is the mission22 flag?
mission22{REDACTED}
If you check /etc/passwd, you see that user ‘mission21’ logged-in into /bin/sh
(not /bin/bash
):
And if we run /bin/bash
we get our flag:
You get the flag after spawning /bin/bash
(login shell will execute .bashrc which will echo out the flag on execution), as you can see this line in .bashrc:
What is the mission23 flag?
mission23{REDACTED}
If you check /etc/passwd, you see that we logged-in into /bin/python3
(not /bin/bash
):
To get into bash
from python interpreter, we need to spawn it using python pty
:
What is the mission24 flag?
mission24{REDACTED}
Ride is getting wavy :)
Let’s check the content of /etc/hosts file:
The first line of the content of the file look interesting, there is a webserver running at localhost.
Let’s interact with the local site (I used wget
here, you can use curl
if you want):
What is the mission25 flag?
mission25{REDACTED}
We got a binary that will give the flag if we give it money (LOL).
Let’s inspect it with ltrace
:
We can see here that the program is taking two environment variables (pocket and init) and printing out some sentences. Let’s assign some value to these variables and see how the program behaves.
Here we see a function call to strncmp()
function, let’s google it:
- The C library function int strncmp(const char *str1, const char *str2, size_t n) compares at most the first n bytes of str1 and str2.
- Parameters
str1 − This is the first string to be compared.
str2 − This is the second string to be compared.
n − The maximum number of characters to be compared.
Now we know that it is comparing the pocket variables value with the string money
and maximum number of characters that are compared are 5 of both, this made me think that maybe I should try giving string value money
(it can be any word, but should be 5 characters) to the pocket variable.
What is the mission26 flag?
mission26{REDACTED}
Unfortunately, it looks like that we don’t have normal means to look for stuff.
We can look what is in current directory by typing anything and doing a double tab:
We can see that we have a flag file here, so now we need something to get its content, luckyly we do have printf
:
You can do same thing with echo
(we have echo too):
And when I was trying to go to next level:
Well, this is not good.
There is nothing in PATH variable (that explains why we were not able to use various commands).
We just need to export the path: PATH=/bin:/usr/bin/su
, and switch user with su
.
What is the mission27 flag?
mission27{REDACTED}
I don’t know what to say here, LOL. Don’t forget to run file
command on files :)
What is the mission28 flag?
mission28{REDACTED}
This one was weird.
What is the mission29 flag?
mission29{REDACTED}
This time we are in /usr/bin/irb
, it is an interactive ruby shell:
Let’s spawn a bash
shell from here and find our flag:
This flag file and its content looks like that they are in mirror position, there is a tool called rev
that can be used here:
What is the mission30 flag?
mission30{REDACTED}
These looks like content files of a website:
What is viktor’s Flag?
viktor{REDACTED}
Well, I must say that things are super easy when you know grep
and find
(they are sherlock holmes of Linux world) and every Linux lad should know these.
Task 4 Privilege Escalation
Welcome to Privilege Escalation, 47. Glad you made it this far!!! Now, here are some special targets. Your Target is to teach these bad guys a lesson.
Good luck 47!!!!
su into viktor user using viktor’s flag as password
No Answer Needed
What is dalia’s flag?
dalia{REDACTED}
Found an interesting script:
/opt/scripts/47.sh
:
Let’s see if it is under crontab
:
- So this crontab is running forever (making sure that 47.sh file is new and ready to execute by user dalia who is executing it after 30 seconds)
- At first I got a little confused here because I didn’t knew in what sequence these are running, according this this forum, ‘The order for Ubuntu is top-down but in parallel’, which means that first task starts (and sleep for 30 seconds) and second task starts right after the first task starts and renew the contents of
47.sh
file (root users task) and after 30 seconds first task also completes i.e running47.sh
(user dalias task), and the cycle continues.
Let’s go with this payload and catch a reverse shell:
Start your listener to recieve the connection (I used nc
):
What is silvio’s flag?
silvio{REDACTED}
gtfobins is the first thing that came into my mind:
What is reza’s flag?
reza{REDACTED}
Again, GTFObins at rescue:
sudo -u reza PAGER='sh -c "exec sh 0<&1"' git -p help
What is jordan’s flag?
jordan{REDACTED}
Let’s try to run this script:
- It seems like it is trying to import a module named ‘shop’ which it can not find, well, we can give it one :)
- We can not write into /opt/scripts but we can on /tmp and give PATH to the Python in /tmp:
Content of shop.py:
It looks like it has been reversed, let’s run rev on the file:
What is ken’s flag?
ken{REDACTED}
gtfobins:
What is sean’s flag?
sean{REDACTED}
gtfobins:
Appearently we are not able to find the flag as ‘sean’ user, but we found that ‘sean’ is an adm
user:
adm
: Group adm is used for system monitoring tasks. Members of this group can read many log files in /var/log, and can use xconsole. Historically, /var/log was /usr/adm (and later /var/adm), thus the name of the group.
So, let’s take a look inside /var/log:
We got our flag, and we also got a base64 string, let’s decrypt it.
What is penelope’s flag?
penelope{REDACTED}
What is maya’s flag?
maya{REDACTED}
We got a suid base64
binary which is owned by the next user, ‘maya’.
gtfobins:
LFILE=/home/maya/flag.txt
./base64 "$LFILE" | base64 --decode
Hint: flag==password
So, we have an id_rsa file that belongs to robert user, and it can be used to infiltrate an entrypoint at localhost according to the text, let’s try it.
What is robert’s Passphrase?
<REDACTED>
I have copied id_rsa content into id_rsa file on my machine and changed permissions, after that I used ssh2john to get the hash of the file and put it to id_rsa.hash to used john
with rockyou.txt
wordlist to crack the password:
<REDACTED>
is the passphrase.
What is user.txt?
user{REDACTED}
Oh boy, this one is pretty cool (a ssh entrypoint at localhost).
It is a docker container (you can tell it from the hostname i.e ‘ec96850005d6’) that we just ssh’ed in (also /etc/passwd doesn’t contain all those users that we escalated from):
We have a docker
binary in /tmp as well, let’s get root first:
It is an old version of sudo
, we can use sudo Security Bypass to get root:
That was easy!
What is root.txt?
root{REDACTED}
I am not familier with docker, but thankfully this one was just simply listing and mounting the docker image (let’s use the binary we found in /tmp to investigate running docker instances):
gtfobins:
Let’s modify and run it:
Wooohooooo!
That was it for this room, I hope you liked reading this writeup, happy hacking :)