| 1 |
#include <stdio.h> |
| 2 |
#include <stdlib.h> |
| 3 |
#include <sys/stat.h> |
| 4 |
#include <sys/types.h> |
| 5 |
#include <fcntl.h> |
| 6 |
#include <unistd.h> |
| 7 |
#ifdef _WIN32 |
| 8 |
#include <windows.h> |
| 9 |
#else |
| 10 |
#define O_BINARY 0 |
| 11 |
#include <sys/wait.h> |
| 12 |
#endif |
| 13 |
|
| 14 |
#define DO1(X) X(1) |
| 15 |
#define DO2(X) DO1(X) X(2) |
| 16 |
#define DO3(X) DO2(X) X(3) |
| 17 |
#define DO4(X) DO3(X) X(4) |
| 18 |
#define DO5(X) DO4(X) X(5) |
| 19 |
#define DO6(X) DO5(X) X(6) |
| 20 |
#define DO7(X) DO6(X) X(7) |
| 21 |
#define DO8(X) DO7(X) X(8) |
| 22 |
#define DO9(X) DO8(X) X(9) |
| 23 |
#define DO10(X) DO9(X) X(10) |
| 24 |
#define DO11(X) DO10(X) X(11) |
| 25 |
#define DO12(X) DO11(X) X(12) |
| 26 |
#define DO13(X) DO12(X) X(13) |
| 27 |
#define DO14(X) DO13(X) X(14) |
| 28 |
#define DO15(X) DO14(X) X(15) |
| 29 |
#define DO16(X) DO15(X) X(16) |
| 30 |
#define DO17(X) DO16(X) X(17) |
| 31 |
#define DO18(X) DO17(X) X(18) |
| 32 |
#define DO19(X) DO18(X) X(19) |
| 33 |
#define DO20(X) DO19(X) X(20) |
| 34 |
#define DO21(X) DO20(X) X(21) |
| 35 |
#define DO22(X) DO21(X) X(22) |
| 36 |
#define DO23(X) DO22(X) X(23) |
| 37 |
#define DO24(X) DO23(X) X(24) |
| 38 |
#define DO25(X) DO24(X) X(25) |
| 39 |
#define DO26(X) DO25(X) X(26) |
| 40 |
#define DO27(X) DO26(X) X(27) |
| 41 |
#define DO28(X) DO27(X) X(28) |
| 42 |
#define DO29(X) DO28(X) X(29) |
| 43 |
#define DO30(X) DO29(X) X(30) |
| 44 |
#define DO31(X) DO30(X) X(31) |
| 45 |
#define DO32(X) DO31(X) X(32) |
| 46 |
|
| 47 |
#define DO(NUMBER, X) \ |
| 48 |
DO##NUMBER(X) |
| 49 |
|
| 50 |
// The following works for GCC producing ELFs, but not with mingw-w64 |
| 51 |
// producing Windows PEs: |
| 52 |
// extern const char __attribute__((weak)) _binary_exe ## index ## _start[0]; |
| 53 |
// Luckily using weakref instead works for both, albeit with the difference in |
| 54 |
// whether an underscore is prepended. |
| 55 |
// |
| 56 |
// These symbols will correspond with embedded EXE object files that we create |
| 57 |
// with `ld -r -b binary` and then link with. |
| 58 |
|
| 59 |
#ifdef __MINGW32__ |
| 60 |
#define DEFINE(index) \ |
| 61 |
static const char __attribute__((weakref, alias("binary_exe" #index "_start"))) exe##index##_start[0]; \ |
| 62 |
static const char __attribute__((weakref, alias("binary_exe" #index "_end"))) exe##index##_end[0]; |
| 63 |
#else |
| 64 |
#define DEFINE(index) \ |
| 65 |
static const char __attribute__((weakref, alias("_binary_exe" #index "_start"))) exe##index##_start[0]; \ |
| 66 |
static const char __attribute__((weakref, alias("_binary_exe" #index "_end"))) exe##index##_end[0]; |
| 67 |
#endif |
| 68 |
|
| 69 |
DO(32, DEFINE) |
| 70 |
|
| 71 |
void exec_in_new_process(const char *filename) |
| 72 |
{ |
| 73 |
|
| 74 |
#ifdef _WIN32 |
| 75 |
PROCESS_INFORMATION pi = {0}; |
| 76 |
STARTUPINFO si = {0}; |
| 77 |
si.cb = sizeof(si); |
| 78 |
BOOL result = CreateProcess(NULL, (char *)filename, NULL, NULL, TRUE, |
| 79 |
NORMAL_PRIORITY_CLASS, NULL, NULL, |
| 80 |
&si, &pi); |
| 81 |
if (result) { |
| 82 |
WaitForSingleObject(pi.hProcess, INFINITE); |
| 83 |
CloseHandle(pi.hProcess); |
| 84 |
CloseHandle(pi.hThread); |
| 85 |
} |
| 86 |
#else |
| 87 |
pid_t pid = fork(); |
| 88 |
if (0 == pid) { |
| 89 |
char *const args[2] = {(char *)filename, NULL}; |
| 90 |
execv(filename, args); |
| 91 |
exit(1); |
| 92 |
} else { |
| 93 |
waitpid(pid, NULL, 0); |
| 94 |
} |
| 95 |
#endif |
| 96 |
} |
| 97 |
|
| 98 |
#define EXTRACT_EXECUTE_DELETE(index) \ |
| 99 |
do { \ |
| 100 |
size_t size = exe##index##_end - exe##index##_start; \ |
| 101 |
if (!size) break; \ |
| 102 |
printf("Extracting file with size %zd\n", size); \ |
| 103 |
/* Linux doesn't care if there is a file extension, but Windows does */ \ |
| 104 |
const char *filename = "exe" #index ".exe"; \ |
| 105 |
int fd = open(filename, O_WRONLY | O_CREAT | O_BINARY, S_IRWXU | S_IRWXG | S_IRWXO); \ |
| 106 |
if (-1 == fd) { \ |
| 107 |
perror("open"); \ |
| 108 |
break; \ |
| 109 |
} \ |
| 110 |
int pos = 0; \ |
| 111 |
while (pos < size) { \ |
| 112 |
int written = write(fd, exe##index##_start + pos, size - pos); \ |
| 113 |
if (-1 == written) { \ |
| 114 |
perror("write"); \ |
| 115 |
break; \ |
| 116 |
} \ |
| 117 |
pos += written; \ |
| 118 |
} \ |
| 119 |
close(fd); \ |
| 120 |
if (pos == size) { \ |
| 121 |
exec_in_new_process(filename); \ |
| 122 |
} \ |
| 123 |
unlink(filename); \ |
| 124 |
printf("Finished extracting and executing\n"); \ |
| 125 |
} while (0); |
| 126 |
|
| 127 |
#define DROP_AND_EXECUTE() \ |
| 128 |
DO(32, EXTRACT_EXECUTE_DELETE) |