Giter VIP home page Giter VIP logo

ld-preload's Introduction

LD_PRELOAD

We can use LD_PRELOAD environment variable to set a location to fetch shared libraries. By setting this variable, we can overwrite the existing functions and make the standard command work differently, the way we want it to.

The linker links the libraries in the path provided by LD_PRELOAD for compiling the main file. Once a function is linked, when other instance of same function shows up, original location is ignored and the newer location is used.

For example, let us try to overwrite the puts function present in stdio.h file.

./img/img4.png

Consider a file main.c, with following content:

#include <stdio.h>

int theFunction(const char *s)
{
    return puts(s);
}

int main (int argc, char** argv) {
    theFunction("Hello, this is traditional work flow.");
    printf("%s: puts location: %p\n", __FILE__, puts);
}

Compiling and running the main.c file gives following output with location of puts as 0x7f877af52ef0.

./img/img1.png

Create another file, unmain.c as follows:

#include <stdio.h>

int puts(const char *__s)
{
    return printf("New puts, hackerman alert!\n");
}

Now, create a shared library of this unmain.c file with command:

gcc -fPIC unmain.c -shared -o unmain.so

Update the LD_PRELOAD with the location of the shared library, unmain.so:

export LD_PRELOAD="$PWD/unmain.so"

And then again run the first compiled main.c file’s executable main.o to see that the location of the puts has been updated. For my case, new location has been set to 0x7f6003f5d119.

./main.o

./img/img2.png

To remove the shared library, use unset to unset value of LD_PRELOAD.

unset LD_PRELOAD

./img/img3.png

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.