LD_PRELOAD example

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
Substituting a function from dynamically linked library, under Linux:
Example:
/***
* LD_PRELOAD
*****************/
The main program:
main_test.c
gcc -o main_test main_test.c
//-----------
#include <stdio.h>
int main(int argc, char* argv[])
{
if (argc < 2) {
printf("Usage: %s <arg>\n", argv[0]);
return -1;
}
int res = strcmp("this_is_the_pass", argv[1]);
return res;
}
//-----------
We need to substitute strcmp, in order to display the password:
my_strcmp.c
gcc my_strcmp.c -shared -o my_strcmp.so -fPIC
//-----------
#include <stdio.h>
int strcmp(char *s1, char *s2)
{
printf("s1: %s\n", s1);
printf("s2: %s\n", s2);
return 0;
}
//-----------
run:
LD_PRELOAD="./my_strcmp.so" ./main_test gimme_pass
result:
//-----------
s1: this_is_the_pass
s2: gimme_pass