forked from easysoft/phpmicro
-
Notifications
You must be signed in to change notification settings - Fork 0
/
php_micro_fileinfo.c
359 lines (328 loc) · 10.8 KB
/
php_micro_fileinfo.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
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
/*
micro SAPI for PHP - php_micro_filesize.c
filesize reproduction utilities for micro
Copyright 2020 Longyan
Copyright 2022 Yun Dou <[email protected]>
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
#include "php.h"
#include "php_micro.h"
#include "php_micro_helper.h"
#include <stdint.h>
#if defined(PHP_WIN32)
# include "win32/codepage.h"
# include <windows.h>
# define SFX_FILESIZE 0L
#elif defined(__linux)
# include <sys/auxv.h>
#elif defined(__APPLE__)
# include <mach-o/dyld.h>
#else
# error because we donot support that platform yet
#endif
#ifndef PHP_WIN32
# include <errno.h>
# include <fcntl.h>
# include <sys/stat.h>
# include <sys/types.h>
# include <unistd.h>
#endif // ndef PHP_WIN32
const char *micro_get_filename(void);
// do we need uint64_t for sfx size?
static uint32_t _final_sfx_filesize = 0;
uint32_t _micro_get_sfx_filesize(void);
uint32_t micro_get_sfx_filesize(void) {
return _final_sfx_filesize;
}
typedef struct _ext_ini_header_t {
uint8_t magic[4];
uint8_t len[4];
} ext_ini_header_t;
struct _ext_ini {
size_t size;
char *data;
} micro_ext_ini = {.size = 0, .data = NULL};
// shabby endian-independent check
#define checkmagic(var) \
(var[0] != PHP_MICRO_INIMARK[0] || var[1] != PHP_MICRO_INIMARK[1] || var[2] != PHP_MICRO_INIMARK[2] || \
var[3] != PHP_MICRO_INIMARK[3])
#ifdef PHP_WIN32
const wchar_t *micro_get_filename_w();
#endif // PHP_WIN32
int micro_fileinfo_init(void) {
int ret = 0;
uint32_t len = 0;
uint32_t sfx_filesize = _micro_get_sfx_filesize();
#ifdef PHP_WIN32
LPCWSTR self_path = micro_get_filename_w();
HANDLE handle = CreateFileW(self_path,
FILE_ATTRIBUTE_READONLY,
FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
NULL,
OPEN_EXISTING,
0,
NULL);
if (INVALID_HANDLE_VALUE == handle) {
ret = FAILURE;
goto end;
}
DWORD filesize = GetFileSize(handle, NULL);
dbgprintf("%d, %d\n", sfx_filesize, filesize);
if (filesize <= sfx_filesize) {
fwprintf(stderr, L"no payload found.\n" PHP_MICRO_HINT, self_path);
ret = FAILURE;
goto end;
}
# define seekfile(x) \
do { SetFilePointer(handle, x, 0, FILE_BEGIN); } while (0)
# define readfile(dest, size, red) \
do { ReadFile(handle, dest, size, &red, NULL); } while (0)
# define closefile() \
do { \
if (INVALID_HANDLE_VALUE != handle) { \
CloseHandle(handle); \
} \
} while (0)
#else
const char *self_path = micro_get_filename();
int fd = open(self_path, O_RDONLY);
if (-1 == fd) {
// TODO: tell failed here
ret = errno;
goto end;
}
struct stat stats;
ret = stat(self_path, &stats);
if (-1 == ret) {
// TODO: tell failed here
ret = errno;
goto end;
}
size_t filesize = stats.st_size;
if (filesize <= sfx_filesize) {
fprintf(stderr, "no payload found.\n" PHP_MICRO_HINT, self_path);
ret = FAILURE;
goto end;
}
# define seekfile(x) \
do { lseek(fd, x, SEEK_SET); } while (0)
# define readfile(dest, size, red) \
do { red = read(fd, dest, size); } while (0)
# define closefile() \
do { \
if (-1 != fd) { \
close(fd); \
} \
} while (0)
#endif // PHP_WIN32
ext_ini_header_t ext_ini_header = {0};
if (filesize <= sfx_filesize + sizeof(ext_ini_header)) {
ret = FAILURE;
goto end;
}
// we may have extra ini configs.
seekfile(sfx_filesize);
uint32_t red = 0;
readfile(&ext_ini_header, sizeof(ext_ini_header), red);
if (sizeof(ext_ini_header) != red) {
// cannot read file
ret = errno;
goto end;
}
if (checkmagic(ext_ini_header.magic)) {
// bad magic, not an extra ini
ret = SUCCESS;
goto end;
}
// shabby ntohl
len = (ext_ini_header.len[0] << 24) + (ext_ini_header.len[1] << 16) + (ext_ini_header.len[2] << 8) +
ext_ini_header.len[3];
dbgprintf("len is %d\n", len);
if (filesize <= sfx_filesize + sizeof(ext_ini_header) + len) {
// bad len, not an extra ini
ret = SUCCESS;
len = 0;
goto end;
}
micro_ext_ini.data = malloc(len + 2);
readfile(micro_ext_ini.data, len, red);
if (len != red) {
// cannot read file
ret = errno;
len = 0;
free(micro_ext_ini.data);
micro_ext_ini.data = NULL;
goto end;
}
// two '\0's like hardcoden inis
micro_ext_ini.data[len] = '\0';
micro_ext_ini.data[len + 1] = '\0';
dbgprintf("using ext ini %s\n", micro_ext_ini.data);
micro_ext_ini.size = len + 1;
len += sizeof(ext_ini_header_t);
end:
_final_sfx_filesize = sfx_filesize + len;
closefile();
return ret;
#undef seekfile
#undef readfile
#undef closefile
}
/*
* _micro_get_sfx_filesize - get (real) sfx size using resource(win) / 2 stage build constant (others)
*/
uint32_t _micro_get_sfx_filesize(void) {
static uint32_t _sfx_filesize = SFX_FILESIZE;
#ifdef PHP_WIN32
dbgprintf("_sfx_filesize: %d, %p\n", _sfx_filesize, &_sfx_filesize);
dbgprintf("resource: %p\n", FindResourceA(NULL, MAKEINTRESOURCEA(PHP_MICRO_SFX_FILESIZE_ID), RT_RCDATA));
dbgprintf("err: %8x\n", GetLastError());
if (SFX_FILESIZE == _sfx_filesize) {
memcpy((void *)&_sfx_filesize,
LockResource(
LoadResource(NULL, FindResourceA(NULL, MAKEINTRESOURCEA(PHP_MICRO_SFX_FILESIZE_ID), RT_RCDATA))),
sizeof(uint32_t));
}
return _sfx_filesize;
#else
return _sfx_filesize;
#endif
}
#ifdef PHP_WIN32
const wchar_t *micro_get_filename_w() {
static LPWSTR self_filename = NULL;
// dbgprintf("fuck %S\n", self_filename);
if (self_filename) {
return self_filename;
}
DWORD self_filename_chars = MAX_PATH;
self_filename = malloc(self_filename_chars * sizeof(WCHAR));
DWORD wapiret = 0;
DWORD (*myGetModuleFileNameExW)(HANDLE, HMODULE, LPWSTR, DWORD);
HMODULE hKernel32 = GetModuleHandleW(L"kernel32.dll");
myGetModuleFileNameExW = (void *)GetProcAddress(hKernel32, "K32GetModuleFileNameExW");
if (NULL == myGetModuleFileNameExW) {
HMODULE hPsapi = GetModuleHandleW(L"psapi.dll");
myGetModuleFileNameExW = (void *)GetProcAddress(hPsapi, "GetModuleFileNameExW");
}
if (NULL == myGetModuleFileNameExW) {
dbgprintf("cannot get self path via win32api\n");
return NULL;
}
while (self_filename_chars ==
(wapiret = myGetModuleFileNameExW(GetCurrentProcess(), NULL, self_filename, self_filename_chars))) {
dbgprintf("wapiret is %d\n", wapiret);
dbgprintf("lensize is %d\n", self_filename_chars);
if (
# if WINVER < _WIN32_WINNT_VISTA
ERROR_SUCCESS
# else
ERROR_INSUFFICIENT_BUFFER
# endif
== GetLastError()) {
self_filename_chars += MAX_PATH;
self_filename = realloc(self_filename, self_filename_chars * sizeof(WCHAR));
} else {
dbgprintf("cannot get self path\n");
return NULL;
}
};
dbgprintf("wapiret is %d\n", wapiret);
dbgprintf("lensize is %d\n", self_filename_chars);
if (wapiret > MAX_PATH && memcmp(L"\\\\?\\", self_filename, 4 * sizeof(WCHAR))) {
dbgprintf("\\\\?\\-ize self_filename\n");
LPWSTR buf = malloc((wapiret + 5) * sizeof(WCHAR));
memcpy(buf, L"\\\\?\\", 4 * sizeof(WCHAR));
memcpy(buf + 4, self_filename, wapiret * sizeof(WCHAR));
buf[wapiret + 4] = L'\0';
free(self_filename);
self_filename = buf;
}
dbgprintf("self is %S\n", self_filename);
return self_filename;
}
const char *micro_get_filename(void) {
return php_win32_cp_w_to_utf8(micro_get_filename_w());
}
#elif defined(__linux)
const char *micro_get_filename(void) {
static char *self_filename = NULL;
if (NULL == self_filename) {
self_filename = malloc(PATH_MAX);
(void)realpath((const char *)getauxval(AT_EXECFN), self_filename);
}
return self_filename;
}
#elif defined(__APPLE__)
const char *micro_get_filename(void) {
static char *self_path = NULL;
if (NULL == self_path) {
uint32_t len = 0;
if (-1 != _NSGetExecutablePath(NULL, &len)) {
goto error;
}
self_path = malloc(len);
if (NULL == self_path) {
goto error;
}
if (0 != _NSGetExecutablePath(self_path, &len)) {
goto error;
}
}
return self_path;
error:
if (NULL != self_path) {
self_path[0] = '\0';
}
return NULL;
}
#else
# error "not support this system yet"
#endif
size_t micro_get_filename_len(void) {
static size_t _micro_filename_l = -1;
if (-1 == _micro_filename_l) {
_micro_filename_l = strlen(micro_get_filename());
}
return _micro_filename_l;
}
// deprecated
#if MICRO_USE_OLD_PHAR_HOOK
int is_stream_self(php_stream *stream) {
dbgprintf("checking %s\n", stream->orig_path);
# ifdef PHP_WIN32
LPCWSTR stream_path_w = php_win32_ioutil_any_to_w(stream->orig_path);
size_t stream_path_w_len = wcslen(stream_path_w);
LPCWSTR my_path_w = micro_get_filename_w();
size_t my_path_w_len = wcslen(my_path_w);
dbgprintf("with self: %S\n", my_path_w);
if (my_path_w_len == stream_path_w_len && 0 == wcscmp(stream_path_w, my_path_w)) {
# else
const char *stream_path = stream->orig_path;
size_t stream_path_len = strlen(stream_path);
const char *my_path = micro_get_filename();
size_t my_path_len = strlen(my_path);
dbgprintf("with self: %s\n", my_path);
if (my_path_len == stream_path_len && 0 == strcmp(stream_path, my_path)) {
# endif
dbgprintf("is self\n");
return 1;
}
dbgprintf("not self\n");
return 0;
}
#endif
PHP_FUNCTION(micro_get_self_filename) {
RETURN_STRING(micro_get_filename());
}
PHP_FUNCTION(micro_get_sfx_filesize) {
RETURN_LONG(micro_get_sfx_filesize());
}