-
Notifications
You must be signed in to change notification settings - Fork 147
Expand file tree
/
Copy pathpayload.c
More file actions
327 lines (270 loc) · 8.65 KB
/
payload.c
File metadata and controls
327 lines (270 loc) · 8.65 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
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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
/*
* payload.c - FakeHTTP: https://github.com/MikeWang000000/FakeHTTP
*
* Copyright (C) 2025 MikeWang000000
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#define _GNU_SOURCE
#include "payload.h"
#include <errno.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include "logging.h"
#include "globvar.h"
#define BUFFLEN 1200
#define SET_BE16(a, u16) \
do { \
(a)[0] = (u16) >> (8); \
(a)[1] = (u16) & (0xff); \
} while (0)
struct payload_node {
uint8_t payload[BUFFLEN];
size_t payload_len;
struct payload_node *next;
};
static const char *http_fmt =
"GET / HTTP/1.1\r\n"
"Host: %s\r\n"
"Accept: */*\r\n"
"User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 "
"(KHTML, like Gecko) Chrome/123.0.0.0 Safari/537.36\r\n"
"\r\n";
struct tls_ext_server_name_head {
uint8_t type[2];
uint8_t length[2];
uint8_t server_name_list_length[2];
uint8_t server_name_type;
uint8_t server_name_length[2];
};
struct tls_ext_padding_head {
uint8_t type[2];
uint8_t length[2];
};
static const struct tls_client_hello {
uint8_t data_01[11];
uint8_t random[32];
uint8_t session_id_length;
uint8_t session_id[32];
uint8_t data_02[39];
uint8_t data_sni[275];
} cli_hello_tmpl = {
.data_01 =
{
0x16, /* handshake */
0x03, 0x03, /* tlsv1.2 */
0x01, 0x81, /* length */
0x01, /* client hello */
0x00, 0x01, 0x7d, /* client hello length */
0x03, 0x03 /* tlsv1.2 */
},
.session_id_length = 32,
.data_02 =
{
0x00, 0x02, /* cipher suites length */
0xc0, 0x2b, /* TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256 */
0x01, /* compression methods length */
0x00, /* null */
0x01, 0x32, /* extensions length */
0x00, 0x0a, /* ext. supported_groups */
0x00, 0x04, /* ext. length */
0x00, 0x02, /* list length */
0x00, 0x17, /* secp256r1 */
0x00, 0x0d, /* ext. signature_algorithms */
0x00, 0x04, /* ext. length */
0x00, 0x02, /* list length */
0x04, 0x03, /* ecdsa_secp256r1_sha256 */
0x00, 0x10, /* ext. alpn */
0x00, 0x0b, /* ext. length */
0x00, 0x09, /* alpn length */
0x08, /* alpn string length */
'h', 't', 't', 'p', '/', '1', '.', '1' /* alpn string */
},
};
static struct payload_node *current_node;
static int make_http_get(uint8_t *buffer, size_t *len, char *hostname)
{
int len_, buffsize;
buffsize = *len;
len_ = snprintf((char *) buffer, buffsize, http_fmt, hostname);
if (len_ < 0) {
E("ERROR: snprintf(): %s", "failure");
return -1;
} else if (len_ >= buffsize) {
E("ERROR: hostname is too long");
return -1;
}
*len = len_;
return 0;
}
static int make_tls_client_hello(uint8_t *buffer, size_t *len, char *hostname)
{
int padding_len;
size_t i, buffsize;
struct tls_client_hello *tls_data;
struct tls_ext_server_name_head *server_name_head;
struct tls_ext_padding_head *padding_head;
buffsize = *len;
if (buffsize < (int) sizeof(*tls_data)) {
E("ERROR: buffer is too small");
return -1;
}
tls_data = (struct tls_client_hello *) buffer;
memcpy(tls_data, &cli_hello_tmpl, sizeof(cli_hello_tmpl));
for (i = 0; i < sizeof(tls_data->random); i++) {
tls_data->random[i] = rand();
}
for (i = 0; i < sizeof(tls_data->session_id); i++) {
tls_data->session_id[i] = rand();
}
size_t hostname_len = strlen(hostname);
padding_len = sizeof(tls_data->data_sni) -
sizeof(struct tls_ext_server_name_head) - strlen(hostname) -
sizeof(struct tls_ext_padding_head);
if (padding_len < 0) {
E("ERROR: hostname is too long");
return -1;
}
server_name_head = (struct tls_ext_server_name_head *) tls_data->data_sni;
SET_BE16(server_name_head->type, 0);
SET_BE16(server_name_head->length, hostname_len + 5);
SET_BE16(server_name_head->server_name_list_length, hostname_len + 3);
SET_BE16(server_name_head->server_name_length, hostname_len);
memcpy((uint8_t *) server_name_head + sizeof(*server_name_head), hostname,
hostname_len);
padding_head = (struct tls_ext_padding_head *) (tls_data->data_sni +
sizeof(*server_name_head) +
hostname_len);
SET_BE16(padding_head->type, 21);
SET_BE16(padding_head->length, padding_len);
memset((uint8_t *) padding_head + sizeof(*padding_head), 0, padding_len);
*len = sizeof(*tls_data);
return 0;
}
static int make_custom(uint8_t *buffer, size_t *len, char *filepath)
{
int res, len_, buffsize;
FILE *fp;
len_ = 0;
buffsize = *len;
fp = fopen(filepath, "rb");
if (!fp) {
E("ERROR: fopen(): %s: %s", filepath, strerror(errno));
return -1;
}
while (!feof(fp) && !ferror(fp) && len_ < buffsize) {
len_ += fread(buffer + len_, 1, buffsize - len_, fp);
}
if (ferror(fp)) {
E("ERROR: fread(): %s: %s", filepath, "failure");
fclose(fp);
return -1;
}
if (!feof(fp)) {
E("ERROR: %s: Data too long. Maximum length is %d", filepath,
buffsize);
fclose(fp);
return -1;
}
res = fclose(fp);
if (res < 0) {
E("ERROR: fclose(): %s", strerror(errno));
return -1;
}
*len = len_;
return 0;
}
int fh_payload_setup(void)
{
int res;
size_t len;
struct payload_info *pinfo;
struct payload_node *node, *next;
for (pinfo = g_ctx.plinfo; pinfo->type; pinfo++) {
node = malloc(sizeof(*node));
if (!node) {
E("ERROR: malloc(): %s", strerror(errno));
goto cleanup;
}
if (current_node) {
next = current_node->next;
current_node->next = node;
node->next = next;
} else {
current_node = node;
node->next = node;
}
switch (pinfo->type) {
case FH_PAYLOAD_CUSTOM:
len = sizeof(node->payload);
res = make_custom(node->payload, &len, pinfo->info);
if (res < 0) {
E(T(make_custom));
goto cleanup;
}
node->payload_len = len;
break;
case FH_PAYLOAD_HTTP:
len = sizeof(node->payload);
res = make_http_get(node->payload, &len, pinfo->info);
if (res < 0) {
E(T(make_http_get));
goto cleanup;
}
node->payload_len = len;
break;
case FH_PAYLOAD_HTTPS:
len = sizeof(node->payload);
res = make_tls_client_hello(node->payload, &len, pinfo->info);
if (res < 0) {
E(T(make_tls_client_hello));
goto cleanup;
}
node->payload_len = len;
break;
default:
E("ERROR: Unknown payload type");
goto cleanup;
}
}
if (!current_node) {
E("ERROR: No payload is available");
goto cleanup;
}
current_node = current_node->next;
return 0;
cleanup:
fh_payload_cleanup();
return -1;
}
void fh_payload_cleanup(void)
{
struct payload_node *node, *next_node;
node = current_node;
while (node) {
next_node = node->next;
free(node);
if (next_node == current_node) {
break;
}
node = next_node;
}
}
void th_payload_get(uint8_t **payload_ptr, size_t *payload_len)
{
*payload_ptr = current_node->payload;
*payload_len = current_node->payload_len;
current_node = current_node->next;
}