Skip to content

Commit 7a238a7

Browse files
committed
Handle dlopen'ed library as Python would do for OpenSSL
1 parent 1d73b1a commit 7a238a7

File tree

2 files changed

+33
-3
lines changed

2 files changed

+33
-3
lines changed

CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
cmake_minimum_required(VERSION 3.6)
22
project(netintercept
3-
VERSION 0.2.0
3+
VERSION 0.3.0
44
)
55

66
include(FeatureSummary)

src/netintercept.c

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616

1717
#include <arpa/inet.h>
1818

19+
#include <link.h>
20+
1921
/* libpcap includes */
2022
#include <pcap/pcap.h>
2123

@@ -65,6 +67,25 @@ static const char *type_to_string(int domain) {
6567
}
6668
*/
6769

70+
struct dl_iterate_phdr_data {
71+
const char *func_name;
72+
void *func_cur;
73+
void *func_ptr;
74+
};
75+
76+
static int
77+
dl_iterate_phdr_callback(struct dl_phdr_info *info, size_t __attribute__((unused)) size, void *__data)
78+
{
79+
struct dl_iterate_phdr_data *data = (struct dl_iterate_phdr_data *)__data;
80+
void *dlh = dlopen(info->dlpi_name, RTLD_LAZY);
81+
void *func_ptr = dlsym(dlh, data->func_name);
82+
if(func_ptr && func_ptr != data->func_cur) {
83+
data->func_ptr = func_ptr;
84+
return 1;
85+
}
86+
return 0;
87+
}
88+
6889
#define NETINTERCEPT_STACK_PUSH() lock_counter++
6990
#define NETINTERCEPT_STACK_POP() lock_counter--
7091
#define NETINTERCEPT_STACK_SIZE() lock_counter
@@ -74,8 +95,17 @@ static const char *type_to_string(int domain) {
7495
if(!ctx.hook_name) {\
7596
ctx.hook_name = dlsym(RTLD_NEXT, func_name);\
7697
if(!ctx.hook_name) {\
77-
printf("[%d] dlsym error: %s\n", getpid(), dlerror());\
78-
abort();\
98+
struct dl_iterate_phdr_data data = {\
99+
func_name,\
100+
dlsym(RTLD_DEFAULT, func_name),\
101+
NULL\
102+
};\
103+
dl_iterate_phdr(dl_iterate_phdr_callback, &data);\
104+
if(!data.func_ptr) {\
105+
fprintf(stderr, "[%d] Could not hook %s\n", getpid(), func_name);\
106+
abort();\
107+
}\
108+
ctx.hook_name = data.func_ptr;\
79109
}\
80110
}\
81111
netintercept_unlock();

0 commit comments

Comments
 (0)