forked from ValdikSS/huawei_oled_hijack
-
Notifications
You must be signed in to change notification settings - Fork 1
/
web_hook.c
263 lines (224 loc) · 7.71 KB
/
web_hook.c
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
/*
* Web functions hook for Huawei portable LTE routers.
* This hook allows to call web interface API functions from "the inside", without
* authentication.
* Both GET and POST requests are supported.
*
* You can call every /api/ handlers, e.g. for
* GET http://192.168.8.1/api/wlan/station-information
* call
* ./client wlan station-information 1 1
*
* Used to get radio mode configuration (./client net net-mode 1 1),
* toggle Wi-Fi Extender mode (./client wlan handover-setting 1 1) etc.
*
* Compile:
* arm-linux-androideabi-gcc -shared -ldl -fPIC -pthread -DHOOK -DSOCK_NAME='"/var/webhook"' -O2 -D__ANDROID_API__=19 -s -o web_hook.so web_hook.c
* arm-linux-androideabi-gcc -fPIC -DCLIENT -DSOCK_NAME='"/var/webhook"' -O2 -D__ANDROID_API__=19 -s -o web_hook_client web_hook.c
*/
#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <dlfcn.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <pthread.h>
// UNIX socket name to listen/connect to
#if !defined(SOCK_NAME)
#error "You should define SOCK_NAME"
#endif
#if !defined(HOOK) && !defined(CLIENT)
#error "You should define either -DHOOK or -DCLIENT"
#endif
#define BUFSIZE 8192
// Building web_hook.so
#ifdef HOOK
#define HOOK_NUMBER 50
struct webhook_function_s {
const char* subsystemname;
void* hookfunction;
};
static struct webhook_function_s webhook_functions[HOOK_NUMBER];
static int webhook_functions_count = 0;
// Pointer to real "webserver_register_hookfunction" function
static void* (*webserver_r_h_real)(
int subsystemnum, const char *subsystemname,
void* hookfunction, void* global_release_msg) = NULL;
// Pointer to real "global_release_msg" function
static void (*global_release_msg_real)(void *ptr) = NULL;
// Search for registered web handlers and return pointer to hook function if found
static void* int_get_webhook(const char* name) {
int i;
if (!webhook_functions_count)
return NULL;
if (!name || strlen(name) == 0)
return NULL;
for (i = 0; i < webhook_functions_count; i++) {
if (strcmp(name, webhook_functions[i].subsystemname) == 0) {
fprintf(stderr, "[int_get_webhook] found func %d %s\n", i,
webhook_functions[i].subsystemname);
return webhook_functions[i].hookfunction;
}
}
fprintf(stderr, "[int_get_webhook] %s not found\n", name);
return NULL;
}
// Register hookfunction for given subsystemname
static int int_register_webhook(const char *subsystemname,
void* hookfunction)
{
if (webhook_functions_count >= HOOK_NUMBER)
return 0;
webhook_functions[webhook_functions_count].subsystemname = strdup(subsystemname);
webhook_functions[webhook_functions_count].hookfunction = hookfunction;
webhook_functions_count++;
fprintf(stderr, "[int_register_webhook] "
"Webhook for %s registered successfully!\n", subsystemname);
return 1;
}
static int create_socket(char* path) {
struct sockaddr_un addr;
int fd;
unlink(path);
if ((fd = socket(AF_UNIX, SOCK_STREAM, 0)) == -1) {
perror("Can't open socket");
exit(EXIT_FAILURE);
}
memset(&addr, 0, sizeof(addr));
addr.sun_family = AF_UNIX;
strcpy(addr.sun_path, path);
if (bind(fd, (struct sockaddr*)&addr, sizeof(addr)) == -1) {
perror("Can't bind socket");
exit(EXIT_FAILURE);
}
if (listen(fd, 5) == -1) {
perror("Can't listen socket");
exit(EXIT_FAILURE);
}
return fd;
}
// Socket server handler
static void* web_hookserver(void* nothing) {
int fd, client;
ssize_t rsize;
char buf[BUFSIZE];
char subsystemname[32];
char libfunction[64];
char *c_token = NULL;
int reqtype = 1;
void* ret;
void* (*webfunc)(const char *function_name,
int req_type_get_post,
char *req_body,
size_t req_size) = NULL;
fd = create_socket(SOCK_NAME);
fprintf(stderr, "Created socket\n");
for (;;) {
if ((client = accept(fd, NULL, NULL)) == -1) {
continue;
}
while ((rsize=read(client, buf, sizeof(buf) - 2)) > 0) {
buf[rsize] = '\0';
buf[strcspn(buf, "\r\n")] = '\0';
if ((c_token = strtok(buf, "|")) != NULL) {
strncpy(subsystemname, c_token, sizeof(subsystemname) - 1);
subsystemname[sizeof(subsystemname) - 1] = '\0';
if (!int_get_webhook(subsystemname)) {
close(client);
continue;
}
webfunc = int_get_webhook(subsystemname);
if ((c_token = strtok(NULL, "|")) == NULL) {
close(client);
continue;
}
strncpy(libfunction, c_token, sizeof(libfunction) - 1);
libfunction[sizeof(libfunction) - 1] = '\0';
if ((c_token = strtok(NULL, "|")) == NULL) {
close(client);
continue;
}
reqtype = 1;
reqtype = atoi(c_token);
if ((c_token = strtok(NULL, "|")) == NULL) {
close(client);
continue;
}
ret = webfunc(libfunction, reqtype, c_token, strlen(c_token));
if (ret) {
dprintf(client, "%s\n", ret);
global_release_msg_real(ret);
}
close(client);
}
}
close(client);
}
return 0;
}
void* webserver_register_hookfunction(int subsystemnum, const char *subsystemname,
void* hookfunction, void *global_release_msg)
{
static pthread_t webhookthread = {0};
unsetenv("LD_PRELOAD");
if (!webserver_r_h_real) {
webserver_r_h_real = dlsym(RTLD_NEXT, "webserver_register_hookfunction");
}
fprintf(stderr, "Trying to search for webhook %s\n", subsystemname);
if (!int_get_webhook(subsystemname)) {
fprintf(stderr, "Webhook not found, registering\n");
int_register_webhook(subsystemname, hookfunction);
fprintf(stderr, "Registered\n");
}
fprintf(stderr, "Checking global release\n");
if (!global_release_msg_real) {
fprintf(stderr, "No global release, saving\n");
global_release_msg_real = global_release_msg;
}
fprintf(stderr, "Checking for webhook thread\n");
if (!webhookthread) {
fprintf(stderr, "No webhook thread, creating one\n");
if (pthread_create(&webhookthread, NULL, web_hookserver, NULL)) {
perror("Error creating thread.");
}
}
return webserver_r_h_real(subsystemnum, subsystemname, hookfunction, global_release_msg);
}
#endif
// Building web_hook_client
#ifdef CLIENT
static int open_socket(char* path) {
struct sockaddr_un addr;
int fd;
if ((fd = socket(AF_UNIX, SOCK_STREAM, 0)) == -1) {
perror("Can't open socket");
exit(EXIT_FAILURE);
}
memset(&addr, 0, sizeof(addr));
addr.sun_family = AF_UNIX;
strcpy(addr.sun_path, path);
if (connect(fd, (struct sockaddr*)&addr, sizeof(addr)) == -1) {
perror("Can't connect");
exit(EXIT_FAILURE);
}
return fd;
}
int main(int argc, char* argv[]) {
int fd;
ssize_t rsize;
char buf[BUFSIZE];
if (argc != 5) {
puts("Need 4 arguments: <subsystemname> <funcname> <1 for get, 2 for post> <data>");
exit(EXIT_FAILURE);
}
fd = open_socket(SOCK_NAME);
dprintf(fd, "%s|%s|%s|%s\n", argv[1], argv[2], argv[3], argv[4]);
rsize = read(fd, buf, sizeof(buf) - 2);
buf[rsize] = '\0';
puts(buf);
close(fd);
return 0;
}
#endif