-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathhook.c
More file actions
101 lines (84 loc) · 2.74 KB
/
hook.c
File metadata and controls
101 lines (84 loc) · 2.74 KB
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <dlfcn.h>
#include <unistd.h>
#include <stdint.h>
#include <ctype.h>
#include <nghttp2/nghttp2.h>
#define _CONSTRUCTOR __attribute__((constructor))
#define _DESTRUCTOR __attribute__ ((destructor))
#define PRINT_TARGET stderr
#define LOADORDIE(var, name) \
do {\
const char *err; \
(var) = dlsym(RTLD_NEXT, (name)); \
fprintf(PRINT_TARGET, "Hooked %s at 0x%X\n", (name), (var)); \
if ((err = dlerror()) != NULL) { \
fprintf(stderr, "dlsym %s: %s\n", (name), err); \
exit(EXIT_FAILURE); \
} \
} while(0)
struct hook_struct {
int (*SSL_read)(void *ssl, void *buf, int num);
int (*SSL_write)(void *ssl, const void *buf, int num);
int32_t (*nghttp2_submit_request)(nghttp2_session *session, const nghttp2_priority_spec *pri_spec, const nghttp2_nv *nva, size_t nvlen, const nghttp2_data_provider *data_prd, void *stream_user_data);
};
static struct hook_struct HOOK_TABLE;
void _CONSTRUCTOR hook_init(void) {
dlerror();
LOADORDIE(HOOK_TABLE.SSL_read, "SSL_read");
LOADORDIE(HOOK_TABLE.SSL_write, "SSL_write");
LOADORDIE(HOOK_TABLE.nghttp2_submit_request, "nghttp2_submit_request");
}
void _DESTRUCTOR hook_fini(void) {
fprintf(PRINT_TARGET, "Library unloaded...\n");
}
/* Prints n bytes from mem */
/* This is probably pretty slow but it looks nicer when printed */
static void memprint(unsigned char *mem, int n) {
for (int i = 0; i < n; i++) {
if (isprint(mem[i])) {
fprintf(PRINT_TARGET, "%c", mem[i]);
} else if (mem[i]) {
fprintf(PRINT_TARGET, "\\x%02X", mem[i]);
}
}
}
int SSL_read(void *ssl, void *buf, int num) {
int ret;
/* Hook post-call */
ret = HOOK_TABLE.SSL_read(ssl, buf, num);
if (ssl != NULL && buf != NULL && ret > 0) {
fprintf(PRINT_TARGET, "SSL_read hook:\n");
memprint(buf, num);
fprintf(PRINT_TARGET, "\n\n");
}
return ret;
}
int SSL_write(void *ssl, void *buf, int num) {
/* Hook pre-call */
if (ssl != NULL && buf != NULL) {
fprintf(PRINT_TARGET, "SSL_write hook:\n");
memprint(buf, num);
fprintf(PRINT_TARGET, "\n\n");
}
return HOOK_TABLE.SSL_write(ssl, buf, num);
}
int32_t nghttp2_submit_request(nghttp2_session *session,
const nghttp2_priority_spec *pri_spec,
const nghttp2_nv *nva, size_t nvlen,
const nghttp2_data_provider *data_prd,
void *stream_user_data) {
fprintf(PRINT_TARGET, "nghttp2_submit_request hook:\n");
for (int i = 0; i < nvlen; i++) {
const nghttp2_nv *field = &nva[i];
memprint(field->name, field->namelen);
memprint((unsigned char *)" ", 1);
memprint(field->value, field->valuelen);
fprintf(PRINT_TARGET, "\n");
}
fprintf(PRINT_TARGET, "\n");
return HOOK_TABLE.nghttp2_submit_request(session, pri_spec, nva, nvlen, data_prd, stream_user_data);
}