DLL Hijacking: The Code

DZone 's Guide to

DLL Hijacking: The Code

Continuing a look at DLL hijacking, we'll turn to the code, with a simple example written with GCC on Kali Linux.

· Integration Zone ·
Free Resource

In my previous article on DLL Hijacking, I abstractly discussed how exactly DLL hijacking works, on different operating systems and development frameworks, and ways you can protect your code from this exploit. Today I’m going to show you some code. This is heavily based on the dlopen man page, feel free to reference.

This is written on Kali Linux, using GCC and make. I have a simple makefile, a driving program that side-loads a library, and two different libraries. We’ll build all of these with a manual copy step where you actually hijack the DLL. So first. let’s look at the makefile:

all: main libs

        gcc -rdynamic -ldl -o main main.c

libs: goodlib badlib

        gcc -Wall -fPIC -c printer.c
        gcc -shared -Wl,-soname,libprtr.so.1 -o libprtr.so.1.0 printer.o

        gcc -Wall -fPIC -c bad_printer.c
        gcc -shared -Wl,-soname,libbadprtr.so.1 -o libbadprtr.so.1.0 bad_printer.o

        rm *.o
        rm lib*

This is a pretty straightforward makefile. We define a couple of targets, the all target, which has dependencies on main and libs. The main target builds the main driver, and the libs target has a dependency on goodlib and badlib, which build the good library and the hijacking library respectively. We wrap up this makefile fun with a clean target, ‘cause we’re OCD like that.

So let’s look at the main program first:

#include <dlfcn.h>
#include <stdio.h>

#define OK      0
#define FAIL    1
#define LIBNAME "./libprtr.so.1.0"
#define FUNAME  "print"

int main(int argc, char *argv[]) {
    void *handle            = NULL;
    void (*printer)(void)   = NULL;
    int result              = 0;

    handle = dlopen(LIBNAME, RTLD_LAZY);
    if (handle == NULL) {
        printf("Error opening library: %s\n", dlerror());
        return FAIL;

    *(void **) (&printer) = dlsym(handle, FUNAME);
    if (printer == NULL) {
        printf("Error opening function: %s\n", dlerror());        
        return FAIL;


    if (dlclose(handle)) {
        printf("Error closing library: %s\n", dlerror());
        return FAIL;

    return OK;

Here, we’re loading a specific SO file, one we’re building and distributing side-by-side with this nifty application. Which prints stuff. But the printing function is in the library we distribute because we’re likely to change the printed message based on the nearest holiday (we are such great planners!). Basically, we load the library via dlopen(.), then grab the function pointer from the library via dlsym(.), print the message from the library function, then close the library with dlclose(.). Then we exit the program.

The libraries are as complex as you’d think, filled with functionality for printing a single message:

#include <stdio.h>

void print(void) {
    printf("do some stuff.\n");

...and our evil, hijacking library:

#include <stdio.h>

void do_other_stuff(void) {
    printf("do lots of other stuff.\n");

void print(void) {
    printf("do some stuff.\n");

Note the hook into do_other_stuff(.) in the hijacking library, where we execute our nefarious evilness.

Now, we have looked through all the code. Let’s build and run.

samhain@durga:~/Work/loading# make
gcc -Wall -fPIC -c printer.c
gcc -shared -Wl,-soname,libprtr.so.1 -o libprtr.so.1.0 printer.o
gcc -Wall -fPIC -c bad_printer.c
gcc -shared -Wl,-soname,libbadprtr.so.1 -o libbadprtr.so.1.0 bad_printer.o
samhain@durga:~/Work/loading# ./main
do some stuff.

We’ve run our application, which has done some stuff on our behalf. Excellent! That’s why this app was so highly rated. Now, say, we navigate our browser to some of the more shady sites on the internet, and at one of them, we inadvertently download a new library (feel free to copy libbadprtr.so.1.0 over libprtr.so.1.0 instead of going to those dark corners of the internet yourself).

The next time we run our printing app, we see this:

samhain@durga:~/Work/loading# ./main
do lots of other stuff.
do some stuff.

Oh noes. We have been hacked!

So you see, as long as the new DLL has the appropriate entry points defined, applications are happy to load the library and execute the named function. So how to get around this? Well, first, look at the size of the library:

samhain@durga:~/Work/loading# ls -al lib*
-rwxr-xr-x 1 root root 4792 Jan 1 09:56 libbadprtr.so.1.0
-rwxr-xr-x 1 root root 4580 Jan 1 09:56 libprtr.so.1.0

You’ll notice libbadprtr.so is noticeably larger than libprtr.so. Let’s take a look at the hashes of the files. We’ll whip up a quick python script to extract the sha256 hashes:

import hashlib as h

GOOD_FILENAME = 'libprtr.so.1.0'
BAD_FILENAME = 'libbadprtr.so.1.0'

def extract_signature(file):
  file_r = file.read()
  hash = h.sha256(file_r)
  print hash.hexdigest()

with open(GOOD_FILENAME, 'rb') as file:
  print 'Processing good file (%s):' % GOOD_FILENAME

with open(BAD_FILENAME, 'rb') as file:
  print 'Processing bad file (%s):' % BAD_FILENAME

Which gives us, when run:

samhain@durga:~/Work/loading# python hasher.py
Processing good file (libprtr.so.1.0):
Processing bad file (libbadprtr.so.1.0):

The hashes are remarkably different as well, as you’d expect. But why the file size? Isn’t the hash enough? Well, yes, for sha2 series hashes, you’re probably okay using just the hash. In the past, though, malicious actors have been able to generate hash collisions in MD5, for example, but measuring the expected file size can give you an additional data point to use to determine the authenticity of a library. Quick and easy, using a combination of hashes and file sizes can give you a remarkable amount of protection against DLL hijacking.

security, software development

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}