ld.so exploit example
Prepare the environment
In the following section you can find the code of the files we are going to use to prepare the environment
Create those files in your machine in the same folder
Compile the library:
gcc -shared -o libcustom.so -fPIC libcustom.c
Copy **libcustom.so to /usr/lib:
sudo cp libcustom.so /usr/lib
(root privs)Compile the executable:
gcc sharedvuln.c -o sharedvuln -lcustom
Check the environment
Check that libcustom.so is being loaded from /usr/lib and that you can execute the binary.
Exploit
In this scenario we are going to suppose that someone has created a vulnerable entry inside a file in /etc/ld.so.conf/:
The vulnerable folder is /home/ubuntu/lib (where we have writable access). Downloadand compile the following code inside that path:
Now that we have created the malicious libcustom library inside the misconfigured path, we need to wait for a reboot or for the root user to execute ldconfig
(in case you can execute this binary as sudo or it has the suid bit you will be able to execute it yourself).
Once this has happened recheck where is the sharevuln
executable loading the libcustom.so
library from:
As you can see it's loading it from /home/ubuntu/lib
and if any user executes it, a shell will be executed:
Note that in this example we haven't escalated privileges, but modifying the commands executed and waiting for root or other privileged user to execute the vulnerable binary we will be able to escalate privileges.
Other misconfigurations - Same vuln
In the previous example we faked a misconfiguration where an administrator set a non-privileged folder inside a configuration file inside /etc/ld.so.conf.d/
.
But there are other misconfigurations that can cause the same vulnerability, if you have write permissions in some config file inside /etc/ld.so.conf.d
s, in the folder /etc/ld.so.conf.d
or in the file /etc/ld.so.conf
you can configure the same vulnerability and exploit it.
Exploit 2
Suppose you have sudo privileges over ldconfig
.
You can indicate ldconfig
where to load the conf files from, so we can take advantage of it to make ldconfig
load arbitrary folders.
So, lets create the files and folders needed to load "/tmp":
Now, as indicated in the previous exploit, create the malicious library inside /tmp. And finally, lets load the path and check where is the binary loading the library from:
As you can see, having sudo privileges over ldconfig
you can exploit the same vulnerability.
I didn't find a reliable way to exploit this vuln if ldconfig
is configured with the suid bit. The following error appear: /sbin/ldconfig.real: Can't create temporary cache file /etc/ld.so.cache~: Permission denied
References
Dab machine in HTB
Last updated