#include <stdio.h>
#include <unistd.h>
#include <dlfcn.h>
#include <execinfo.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
// 표준함수 close 와 동일한 함수 정의
int close(int fildes)
{
int (*new_close)(int fildes);
int result = 0;
// close 함수의 정상적인 동작을 위해 Hooking 함수에서 표준 close 함수 호출
new_close = dlsym(RTLD_NEXT, "close");
result = new_close(fildes);
// FD값이 100 이상인 경우에 한하여 close 호출시 fd 값과, Backtrace 값을 찍도록함
if(fildes > 100)
{
printf("\e[1;31m [test]close(%d) ret(%d)\e[0m\n", fildes, result);
int j, nptrs;
#define BTSIZE (3)
void *trace[100];
char **strings;
memset(trace, 0x00, 100);
nptrs = backtrace(trace, BTSIZE);
printf("backtrace() returned %d addresses\n", nptrs);
if(nptrs > 0)
{
strings = backtrace_symbols(trace, nptrs);
if (strings != NULL)
{
for (j = 1; j < nptrs; j++)
{
printf("%s\n", strings[j]);
}
free(strings);
}
}
}
return result;
}
int socket(int domain, int type, int protocol)
{
int (*new_socket)(int , int , int);
int result = 0;
new_socket= dlsym(RTLD_NEXT, "socket");
result = new_socket(domain, type, protocol);
{
printf("\e[1;31m[test] socket(%d, %d, %d) ret(%d)\e[0m\n", domain, type, protocol, result);
int j, nptrs;
#define BTSIZE (3)
void *trace[100];
char **strings;
memset(trace, 0x00, 100);
nptrs = backtrace(trace, BTSIZE);
printf("backtrace() returned %d addresses\n", nptrs);
if(nptrs > 0)
{
strings = backtrace_symbols(trace, nptrs);
if (strings != NULL)
{
for (j = 1; j < nptrs; j++)
{
printf("%s\n", strings[j]);
}
free(strings);
}
}
}
return result;
}
- 위의 코드에서 dlsym() 용도
ex)
static void* (*my_malloc)(size_t) = NULL;
my_malloc = dlsym(RTLD_NEXT, "malloc");
$ gcc hooking.c -o hooking.so -fPIC -shared -ldl -D_GNU_SOURCE
$ gcc test.c -o test -rdynamic
$ export LD_PRELOAD="hooking.so"
- 참고 페이지 : https://blog.netspi.com/function-hooking-part-i-hooking-shared-library-function-calls-in-linux/
error: stray '\357' in program 오류 해결방법 (0) | 2018.12.18 |
---|---|
tcpdump 크로스 컴파일 (cross compile arm) (2) | 2018.12.14 |
rsyslog 데몬을 이용한 네트워크 로깅기능 (0) | 2018.11.16 |
zeroMQ universal framework 빌드 방법 (0) | 2018.11.15 |
mobileVLCKit 빌드 방법 (1) | 2018.11.15 |