DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
View Events Video Library
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Integrating PostgreSQL Databases with ANF: Join this workshop to learn how to create a PostgreSQL server using Instaclustr’s managed service

Mobile Database Essentials: Assess data needs, storage requirements, and more when leveraging databases for cloud and edge applications.

Monitoring and Observability for LLMs: Datadog and Google Cloud discuss how to achieve optimal AI model performance.

Automated Testing: The latest on architecture, TDD, and the benefits of AI and low-code tools.

Related

  • Advantages of Python as an AI and ML Development Language
  • Three Best React Form Libraries
  • Best Python Libraries for Web Scraping
  • 4 Email Validation and Verification Techniques for App Developers

Trending

  • Implementing Stronger RBAC and Multitenancy in Kubernetes Using Istio
  • Understanding Europe's Cyber Resilience Act and What It Means for You
  • Lost in Communication and Collaboration
  • Best Practices for Developing Cloud Applications

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.

Christopher Lamb user avatar by
Christopher Lamb
·
Jan. 07, 16 · Analysis
Like (3)
Save
Tweet
Share
3.97K Views

Join the DZone community and get the full member experience.

Join For Free

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

main:
        gcc -rdynamic -ldl -o main main.c

libs: goodlib badlib

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

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

clean:
        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;
    }

    (*printer)();

    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) {
    do_other_stuff();
    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
  extract_signature(file)

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

Which gives us, when run:

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

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.

Library

Opinions expressed by DZone contributors are their own.

Related

  • Advantages of Python as an AI and ML Development Language
  • Three Best React Form Libraries
  • Best Python Libraries for Web Scraping
  • 4 Email Validation and Verification Techniques for App Developers

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends: