I tried to apply the LD_PRELOAD trick to some native binary. I have done similar things before, but this time no luck. The call I was trying to intercept was timer_settime ().
Strace clearly shows that timer_settime () is called binary:
[pid 30500] timer_settime(0x2, 0, {it_interval={30, 0}, it_value={30, 0}}, {it_interval={0, 0}, it_value={0, 0}}) = 0
It has been called many times at different time intervals. I want to catch exactly the above with an interval of 30 seconds.
Here is my code, timerwrap.c:
#define _GNU_SOURCE
#include <dlfcn.h>
#include <time.h>
#include <stdio.h>
int timer_settime(timer_t timerid, int flags, const struct itimerspec *new_value, struct itimerspec *old_value)
{
printf("Enter timer.\n");
if((new_value->it_interval).tv_sec == 30) {
printf("Catched!\n");
return 0;
}
int (*real_timer_settime)(timer_t, int, const struct itimerspec *, struct itimerspec *);
real_timer_settime = dlsym(RTLD_NEXT, "timer_settime");
return real_timer_settime(timerid, flags, new_value, old_value);
}
Gcc command line:
gcc -Wall -g -shared -fPIC -o timerwrap.so timerwrap.c -ldl -lrt
run the program:
export LD_PRELOAD=/home/Work/C/timerwrap.so
./the_program
But he could not intercept the challenge.
I ran it again with LD_DEBUG = all for further study. It turns out that for many other characters, the timerwrap.so function was looked at, for example, the dlsym lookup path looks like this:
2006: symbol=dlsym; lookup in file=/.../the_program [0]
2006: symbol=dlsym; lookup in file=/home/Work/C/timerwrap.so [0]
2006: symbol=dlsym; lookup in file=./lib/libssl.so.6 [0]
2006: symbol=dlsym; lookup in file=/lib/i686/cmov/libdl.so.2 [0]
2006: binding file /.../the_program [0] to /lib/i686/cmov/libdl.so.2 [0]: normal symbol `dlsym' [GLIBC_2.0]
timer_settime /usr/lib/librt.so, timerwrap.so:
2006: symbol=timer_settime; lookup in file=/usr/lib/librt.so [0]
2006: binding file /usr/lib/librt.so [0] to /usr/lib/librt.so [0]: normal symbol `timer_settime'
? librt.so ? ?
. !