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