-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.c
More file actions
590 lines (509 loc) · 19.8 KB
/
main.c
File metadata and controls
590 lines (509 loc) · 19.8 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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
/**
* binject - Pure C alternative to postject
* Main CLI entry point
*/
#define _POSIX_C_SOURCE 200809L // For strdup
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <limits.h>
#include <sys/stat.h>
#ifdef _WIN32
#include <process.h>
// Windows doesn't define S_ISREG macro, so we define it ourselves
#ifndef S_ISREG
#define S_ISREG(m) (((m) & _S_IFMT) == _S_IFREG)
#endif
#else
#include <sys/wait.h>
#include <unistd.h>
#endif
#include "binject.h"
#include "buffer_constants.h"
#include "binary_format.h"
#include "debug_common.h"
/**
* Check if a file has a .json extension
*/
static int is_json_file(const char *path) {
if (!path) return 0;
const char *ext = strrchr(path, '.');
return ext && strcmp(ext, ".json") == 0;
}
/**
* Validate that a path is a legitimate Node.js binary
* Basic validation: must be an existing executable file
*/
static int validate_node_binary(const char *path) {
if (!path || strlen(path) == 0) {
return 0;
}
// Resolve to canonical absolute path to prevent path traversal
char resolved_path[PATH_MAX];
#ifdef _WIN32
if (_fullpath(resolved_path, path, PATH_MAX) == NULL) {
fprintf(stderr, "Error: Invalid path: %s\n", path);
return 0;
}
#else
if (realpath(path, resolved_path) == NULL) {
fprintf(stderr, "Error: Invalid path: %s\n", path);
return 0;
}
#endif
// Check if file exists and is executable
struct stat st;
if (stat(resolved_path, &st) != 0) {
fprintf(stderr, "Error: Node binary not found: %s\n", resolved_path);
return 0;
}
if (!S_ISREG(st.st_mode)) {
fprintf(stderr, "Error: Node binary path is not a regular file: %s\n", resolved_path);
return 0;
}
#ifndef _WIN32
// On Unix, check if file is executable
if (!(st.st_mode & S_IXUSR) && !(st.st_mode & S_IXGRP) && !(st.st_mode & S_IXOTH)) {
fprintf(stderr, "Error: Node binary is not executable: %s\n", resolved_path);
return 0;
}
#endif
// Verify it's actually a valid binary format (prevent arbitrary command execution)
FILE *fp = fopen(resolved_path, "rb");
if (!fp) {
fprintf(stderr, "Error: Cannot open node binary for validation: %s\n", resolved_path);
return 0;
}
uint8_t magic[4];
size_t bytes_read = fread(magic, 1, 4, fp);
fclose(fp);
if (bytes_read != 4) {
fprintf(stderr, "Error: Node binary too small to be valid: %s\n", path);
return 0;
}
// Check for valid executable format using shared detection
binary_format_t format = detect_binary_format(magic);
if (format == BINARY_FORMAT_UNKNOWN) {
fprintf(stderr, "Error: Node binary is not a valid executable format: %s\n", path);
return 0;
}
return 1;
}
/**
* Find system Node.js binary for running --experimental-sea-config
* Returns path to node binary, or NULL if not found
* Caller is responsible for freeing the returned string
*/
static char* find_system_node_binary(void) {
const char *candidates[] = {
"/usr/local/bin/node",
"/usr/bin/node",
"/opt/homebrew/bin/node",
NULL
};
// Try known paths first
for (int i = 0; candidates[i] != NULL; i++) {
// Try to validate if it's a real node binary
if (validate_node_binary(candidates[i])) {
return strdup(candidates[i]);
}
}
// Fall back to "node" in PATH - don't validate since it's not a file path
// Let execvp search PATH for us
return strdup("node");
}
/**
* Generate SEA blob from JSON config using node --experimental-sea-config
* Returns path to generated blob (caller must free), or NULL on error
*/
static char* generate_sea_blob_from_config(const char *config_path) {
// Find system Node.js binary
char *node_binary = find_system_node_binary();
if (!node_binary) {
return NULL;
}
// Validate config_path doesn't contain dangerous patterns
if (!config_path || strlen(config_path) == 0) {
fprintf(stderr, "Error: Config path is empty\n");
free(node_binary);
return NULL;
}
// Check for path traversal attempts
if (strstr(config_path, "..") != NULL) {
fprintf(stderr, "Error: Path traversal detected in config path\n");
free(node_binary);
return NULL;
}
// Verify file exists and is readable
struct stat st;
if (stat(config_path, &st) != 0) {
fprintf(stderr, "Error: Config file not found: %s\n", config_path);
free(node_binary);
return NULL;
}
// Verify it's a regular file (not symlink, device, etc)
if (!S_ISREG(st.st_mode)) {
fprintf(stderr, "Error: Config path is not a regular file: %s\n", config_path);
free(node_binary);
return NULL;
}
printf("Detected SEA config file: %s\n", config_path);
printf("Generating SEA blob using: %s --experimental-sea-config %s\n",
node_binary, config_path);
#ifdef _WIN32
// Windows: use _spawnvp and _cwait
char *argv[] = {
(char*)node_binary,
(char*)"--experimental-sea-config",
(char*)config_path,
NULL
};
intptr_t pid = _spawnvp(_P_NOWAIT, node_binary, (const char* const*)argv);
if (pid == -1) {
fprintf(stderr, "Error: Failed to spawn process\n");
free(node_binary);
return NULL;
}
int status;
if (_cwait(&status, pid, 0) == -1) {
fprintf(stderr, "Error: Failed to wait for process\n");
free(node_binary);
return NULL;
}
if (status != 0) {
fprintf(stderr, "Error: node --experimental-sea-config failed with exit code %d\n", status);
free(node_binary);
return NULL;
}
#else
// Unix: use fork and exec
pid_t pid = fork();
if (pid == -1) {
fprintf(stderr, "Error: Failed to fork process\n");
free(node_binary);
return NULL;
}
if (pid == 0) {
// Child process: run node --experimental-sea-config
char *argv[] = {
(char*)node_binary,
(char*)"--experimental-sea-config",
(char*)config_path,
NULL
};
execvp(node_binary, argv);
// If execvp returns, it failed - use _exit to avoid flushing buffers
_exit(1);
}
// Parent: wait for child
int status;
waitpid(pid, &status, 0);
if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) {
fprintf(stderr, "Error: node --experimental-sea-config failed\n");
free(node_binary);
return NULL;
}
#endif
// Done with node_binary - free it
free(node_binary);
node_binary = NULL;
// Read the JSON config to find the output blob path
FILE *config_file = fopen(config_path, "r");
if (!config_file) {
fprintf(stderr, "Error: Failed to open config file: %s\n", config_path);
return NULL;
}
// Read file contents
if (fseek(config_file, 0, SEEK_END) != 0) {
fprintf(stderr, "Error: Cannot seek config file\n");
fclose(config_file);
return NULL;
}
long file_size = ftell(config_file);
if (file_size < 0) {
fprintf(stderr, "Error: Cannot determine config file size\n");
fclose(config_file);
return NULL;
}
if (file_size > MAX_JSON_CONFIG_SIZE) {
fprintf(stderr, "Error: Config file too large (max 1MB)\n");
fclose(config_file);
return NULL;
}
if (fseek(config_file, 0, SEEK_SET) != 0) {
fprintf(stderr, "Error: Cannot seek config file\n");
fclose(config_file);
return NULL;
}
char *json_content = malloc(file_size + 1);
if (!json_content) {
fprintf(stderr, "Error: Failed to allocate memory for config\n");
fclose(config_file);
return NULL;
}
size_t bytes_read = fread(json_content, 1, file_size, config_file);
fclose(config_file);
if (bytes_read != (size_t)file_size) {
fprintf(stderr, "Error: Failed to read config file completely\n");
free(json_content);
return NULL;
}
json_content[file_size] = '\0';
// JSON parsing to extract "output" field
// Look for "output": "filename.blob" pattern
// Use more robust parsing to avoid false matches in strings/comments
char *blob_filename = NULL;
char *search_pos = json_content;
char *json_end = json_content + file_size;
// Validate JSON structure basics - check nesting depth
int nesting_depth = 0;
int max_nesting = 50; // Reasonable limit for config files
for (char *p = json_content; p < json_end; p++) {
if (*p == '{' || *p == '[') nesting_depth++;
if (*p == '}' || *p == ']') nesting_depth--;
if (nesting_depth > max_nesting || nesting_depth < 0) {
fprintf(stderr, "Error: JSON nesting too deep or unbalanced\n");
free(json_content);
return NULL;
}
}
// Search for "output" key - must be followed by : and "value"
while ((search_pos = strstr(search_pos, "\"output\"")) != NULL) {
// Skip past the "output" key
search_pos += 8; // length of "output"
// Skip whitespace
while (*search_pos && (*search_pos == ' ' || *search_pos == '\t' ||
*search_pos == '\r' || *search_pos == '\n')) {
search_pos++;
}
// Must be followed by colon
if (*search_pos != ':') {
continue;
}
search_pos++;
// Skip whitespace after colon
while (*search_pos && (*search_pos == ' ' || *search_pos == '\t' ||
*search_pos == '\r' || *search_pos == '\n')) {
search_pos++;
}
// Must be followed by opening quote
if (*search_pos != '"') {
continue;
}
search_pos++; // Move past opening quote
// Find closing quote (handling proper escape sequences)
char *value_start = search_pos;
char *value_end = NULL;
// Add bounds check to prevent reading beyond buffer
while (*search_pos && search_pos < json_end) {
if (*search_pos == '"') {
// Count consecutive backslashes before this quote
int backslash_count = 0;
const char *check_pos = search_pos - 1;
// Ensure we don't go before the buffer start - check BEFORE dereferencing
while (check_pos >= value_start && check_pos >= json_content) {
if (*check_pos != '\\') {
break;
}
backslash_count++;
check_pos--;
}
// Even number of backslashes (including 0) means quote is NOT escaped
if (backslash_count % 2 == 0) {
value_end = search_pos;
break;
}
}
search_pos++;
}
if (value_end) {
size_t len = value_end - value_start;
if (len > 0 && len < TEMP_PATH_BUFFER_SIZE) { // Sanity check on path length
blob_filename = malloc(len + 1);
if (blob_filename) {
memcpy(blob_filename, value_start, len);
blob_filename[len] = '\0';
break;
}
}
}
}
free(json_content);
if (!blob_filename) {
fprintf(stderr, "Error: Could not parse 'output' field from config\n");
return NULL;
}
// Validate blob path to prevent path traversal attacks
// Check for suspicious patterns
if (strstr(blob_filename, "..") != NULL ||
strstr(blob_filename, "/./") != NULL ||
strstr(blob_filename, "\\.\\") != NULL) {
fprintf(stderr, "Error: Path traversal detected in output path: %s\n", blob_filename);
free(blob_filename);
return NULL;
}
// Reject absolute paths that go outside current directory
if (blob_filename[0] == '/' || (strlen(blob_filename) > 2 && blob_filename[1] == ':')) {
fprintf(stderr, "Error: Absolute paths not allowed in output: %s\n", blob_filename);
free(blob_filename);
return NULL;
}
// Use the blob_filename as-is - Node.js doesn't resolve relative paths,
// it uses them relative to the current working directory.
// We mimic this behavior, but only allow paths within CWD or absolute paths.
char *blob_path = blob_filename;
// Verify the blob file was created - open directly instead of stat to avoid TOCTOU
FILE *verify_fp = fopen(blob_path, "rb");
if (!verify_fp) {
fprintf(stderr, "Error: Generated blob file not found: %s\n", blob_path);
free(blob_path);
return NULL;
}
fclose(verify_fp);
printf("✓ Generated SEA blob: %s\n", blob_path);
return blob_path;
}
static void print_usage(const char *program) {
printf("binject - Pure C alternative to postject\n\n");
printf("Usage:\n");
printf(" %s inject -e <executable> -o <output> [--sea <path>] [--vfs <path>|--vfs-on-disk <path>|--vfs-in-memory <path>|--vfs-compat] [--skip-repack]\n", program);
printf(" %s list <executable>\n", program);
printf(" %s extract -e <executable> [--vfs|--sea] -o <output>\n", program);
printf(" %s verify -e <executable> [--vfs|--sea]\n", program);
printf(" %s --help\n", program);
printf(" %s --version\n\n", program);
printf("Commands:\n");
printf(" inject Inject a resource into an executable\n");
printf(" list List all embedded resources\n");
printf(" extract Extract a resource from an executable\n");
printf(" verify Verify the integrity of a resource\n\n");
printf("Options:\n");
printf(" -o, --output <path> Output file path\n");
printf(" -e, --executable <path> Input executable path\n");
printf(" --vfs <path> Inject VFS blob to NODE_SEA/__SMOL_VFS_BLOB (extracts to disk at runtime)\n");
printf(" --vfs-on-disk <path> Alias for --vfs\n");
printf(" --vfs-in-memory <path> Inject VFS blob and keep in memory at runtime (no extraction)\n");
printf(" --vfs-compat Enable VFS support without bundling files (compatibility mode)\n");
printf(" --sea <path> Inject SEA blob to NODE_SEA/__NODE_SEA_BLOB\n");
printf(" --skip-repack Skip repacking compressed stub (for testing extracted binary)\n");
printf(" If path ends in .json, runs: node --experimental-sea-config <path>\n");
printf(" -h, --help Show this help message\n");
printf(" -v, --version Show version information\n");
}
int main(int argc, char *argv[]) {
DEBUG_INIT();
if (argc < 2) {
print_usage(argv[0]);
return BINJECT_ERROR_INVALID_ARGS;
}
const char *command = argv[1];
if (strcmp(command, "--version") == 0 || strcmp(command, "-v") == 0) {
printf("binject %s\n", VERSION);
return BINJECT_OK;
}
if (strcmp(command, "--help") == 0 || strcmp(command, "-h") == 0) {
print_usage(argv[0]);
return BINJECT_OK;
}
if (strcmp(command, "inject") == 0) {
const char *executable = NULL;
const char *output = NULL;
const char *sea_resource = NULL;
const char *vfs_resource = NULL;
int vfs_in_memory = 0; // Default: extract VFS to disk at runtime
int skip_repack = 0; // Default: repack compressed stubs
for (int i = 2; i < argc; i++) {
if (strcmp(argv[i], "-e") == 0 || strcmp(argv[i], "--executable") == 0) {
if (i + 1 < argc) executable = argv[++i];
} else if (strcmp(argv[i], "-o") == 0 || strcmp(argv[i], "--output") == 0) {
if (i + 1 < argc) output = argv[++i];
} else if (strcmp(argv[i], "--vfs") == 0 || strcmp(argv[i], "--vfs-on-disk") == 0) {
if (i + 1 < argc) vfs_resource = argv[++i];
} else if (strcmp(argv[i], "--vfs-in-memory") == 0) {
if (i + 1 < argc) vfs_resource = argv[++i];
vfs_in_memory = 1;
} else if (strcmp(argv[i], "--vfs-compat") == 0) {
vfs_resource = ""; // Empty string marker for VFS compatibility mode
} else if (strcmp(argv[i], "--sea") == 0) {
if (i + 1 < argc) sea_resource = argv[++i];
} else if (strcmp(argv[i], "--skip-repack") == 0) {
skip_repack = 1;
}
}
if (!executable || !output || (!sea_resource && !vfs_resource)) {
fprintf(stderr, "Error: inject requires --executable, --output, and at least one of --sea <path> or --vfs <path>\n");
return BINJECT_ERROR_INVALID_ARGS;
}
if (vfs_resource && !sea_resource) {
fprintf(stderr, "Error: --vfs requires --sea to be specified\n");
fprintf(stderr, "VFS (Virtual File System) must be injected alongside a SEA (Single Executable Application) blob\n");
return BINJECT_ERROR_INVALID_ARGS;
}
// Check if SEA resource is a JSON config file
// If so, generate the blob using node --experimental-sea-config
char *generated_blob = NULL;
if (sea_resource && is_json_file(sea_resource)) {
generated_blob = generate_sea_blob_from_config(sea_resource);
if (!generated_blob) {
fprintf(stderr, "Error: Failed to generate SEA blob from config\n");
return BINJECT_ERROR;
}
sea_resource = generated_blob; // Use generated blob instead
}
int result = binject_batch(executable, output, sea_resource, vfs_resource, vfs_in_memory, skip_repack);
// Clean up generated blob if we created one
if (generated_blob) {
free(generated_blob);
}
return result;
}
if (strcmp(command, "list") == 0) {
if (argc < 3) {
fprintf(stderr, "Error: list requires an executable path\n");
return BINJECT_ERROR_INVALID_ARGS;
}
return binject_list(argv[2]);
}
if (strcmp(command, "extract") == 0) {
const char *executable = NULL;
const char *section = NULL;
const char *output = NULL;
for (int i = 2; i < argc; i++) {
if (strcmp(argv[i], "-e") == 0 || strcmp(argv[i], "--executable") == 0) {
if (i + 1 < argc) executable = argv[++i];
} else if (strcmp(argv[i], "--vfs") == 0) {
section = "vfs";
} else if (strcmp(argv[i], "--sea") == 0) {
section = "sea";
} else if (strcmp(argv[i], "-o") == 0 || strcmp(argv[i], "--output") == 0) {
if (i + 1 < argc) output = argv[++i];
}
}
if (!executable || !section || !output) {
fprintf(stderr, "Error: extract requires --executable, either --vfs or --sea, and --output\n");
return BINJECT_ERROR_INVALID_ARGS;
}
return binject_extract(executable, section, output);
}
if (strcmp(command, "verify") == 0) {
const char *executable = NULL;
const char *section = NULL;
for (int i = 2; i < argc; i++) {
if (strcmp(argv[i], "-e") == 0 || strcmp(argv[i], "--executable") == 0) {
if (i + 1 < argc) executable = argv[++i];
} else if (strcmp(argv[i], "--vfs") == 0) {
section = "vfs";
} else if (strcmp(argv[i], "--sea") == 0) {
section = "sea";
}
}
if (!executable || !section) {
fprintf(stderr, "Error: verify requires --executable and either --vfs or --sea\n");
return BINJECT_ERROR_INVALID_ARGS;
}
return binject_verify(executable, section);
}
fprintf(stderr, "Error: unknown command '%s'\n", command);
print_usage(argv[0]);
return BINJECT_ERROR_INVALID_ARGS;
}