網頁

2014年5月10日 星期六

use LD_PRELOAD to patch your program

Env ?

  • Linux/Unix platform.
  • C/C++

What is it ?
  • You can set env variable LD_PRELOAD to "replace" the binary program's functions/subroutines with the functions/subroutines which are defined in the .so shared libraries specified in LD_PRELOAD.

How to use it ?

  • For example, if we want to replace "malloc" function of "ls" program with our own defined function.
    • Step1: write our self-defined malloc and compile it as .so shared library.
      • vim mylib.c:
      • #include <stdio.h>
        void* malloc(size_t size) {
            printf ("hello\n");
            return NULL;
        }
        
        
      • compile mylib.c as shared library
      • gcc -Wall -fpic -shared -o libmylib.so mylib.c
        
    • Step2: Run it !
      • Replace ls "malloc" with our own defined "malloc" in shared library
      • LD_PRELOAD=./libmylib.so /bin/ls
    • Result
      • hello
        hello
        hello
        hello
        hello
        hello
        hello
        /bin/ls: memory exhausted

Usage ?

  1. I guess for hacker/cracker it may be an important skill.
  2. We can fast replace the function with our defined function although we do not have source code.(patching/wrap function)



沒有留言:

張貼留言