-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathwing_api.c
executable file
·356 lines (315 loc) · 8.36 KB
/
wing_api.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
/**
* 读取类的属性,这里为了兼容php7和其他版本所做的简单的条件编译封装
*/
zval *wing_zend_read_property(zend_class_entry *scope, zval *object, const char *name)
{
TSRMLS_FETCH();
#if PHP_MAJOR_VERSION >= 7
return zend_read_property(scope, object, name, strlen(name), 0, 0 TSRMLS_CC);
#else
return zend_read_property(scope, object, name, strlen(name), 0 TSRMLS_CC);
#endif
}
/**
* 判断文件是否为php文件
*
* @param const char* file
* @return BOOL
*/
int wing_file_is_php(const char *file)
{
char path[MAX_PATH] = {0};
if (strstr(file, "'") != NULL) {
char *st = (char*)file;
st++;
char *f = strstr(st, "'");
strncpy(path, st, f - st);
}
else if (strstr(file, "\"") != NULL) {
char *st = (char*)file;
st++;
char *f = strstr(st, "\"");
strncpy(path, st, f - st);
}
else if (strstr(file, "`") != NULL) {
char *st = (char*)file;
st++;
char *f = strstr(st, "`");
strncpy(path, st, f - st);
}
else if(strstr(file, " ") != NULL) {
char *st = (char*)file;
st++;
char *f = strstr(st, " ");
strncpy(path, st, f - st);
}
else {
strcpy(path, file);
}
char *ext = strrchr(path, '.');
if (ext) {
ext++;
if (strcmp(ext, "php") == 0 && wing_access(path, R_OK) == 0) {
return 1;
}
}
if (wing_access(path, R_OK) != 0) {
return 0;
}
FILE *handle = fopen(path, "r");
if (!handle) {
return 0;
}
char *find = NULL;
char line[8] = { 0 };
fgets(line, 7, handle);
find = strstr(line, "<?php");
if (find == line) {
fclose(handle);
return 1;
}
memset(line, 0, 8);
fgets(line, 7, handle);
find = strstr(line, "<?php");
if (find == line) {
fclose(handle);
return 1;
}
fclose(handle);
return 0;
}
//int wing_write_cmdline(unsigned long process_id, char *cmdline)
//{
// char buffer[MAX_PATH];
// sprintf(buffer, "/proc/%lu/cmdline", process_id);
// if (access(buffer, F_OK) == 0) {
// //linux处理
// return 1;
// }
/*char tmp[MAX_PATH] = {0};
wing_get_tmp_dir((char**)&tmp);
char path[MAX_PATH] = {0};
strcpy(path, tmp);
strcpy((char*)(path+strlen(tmp)), "/");
char _process_id[32] = {0};
sprintf(_process_id, "%lu", process_id);
strcpy((char*)(path+strlen(tmp)+1), _process_id);
if (access(path, F_OK) != 0) {
if (0 != mkdir(path, 0777)) {
return 0;
}
}
strcpy((char*)(path+strlen(tmp)+1+strlen(_process_id)), "/cmdline");
FILE *handle = fopen((const char*)path, "w");
if (handle) {
if (1 != fwrite(cmdline, strlen(cmdline), 1, handle)) {
fclose(handle);
//写入失败
return 0;
}
fclose(handle);
return 1;
}
return 0;*/
//}
#ifdef __APPLE__
/**
* mac下面获取进程的启动命令
*
* @param int pid 进程id
* @param char **buffer 输出结果到buffer
*/
void wing_get_cmdline(int pid, char **buffer)
{
int mib[3], argmax, nargs, c = 0;
size_t size;
char *procargs, *sp, *np, *cp;
int show_args = 1;
mib[0] = CTL_KERN;
mib[1] = KERN_ARGMAX;
size = sizeof(argmax);
if (sysctl(mib, 2, &argmax, &size, NULL, 0) == -1) {
goto ERROR_A;
}
/* Allocate space for the arguments. */
procargs = (char *)malloc(argmax);
if (procargs == NULL) {
goto ERROR_A;
}
/*
* Make a sysctl() call to get the raw argument space of the process.
* The layout is documented in start.s, which is part of the Csu
* project. In summary, it looks like:
*
* /---------------\ 0x00000000
* : :
* : :
* |---------------|
* | argc |
* |---------------|
* | arg[0] |
* |---------------|
* : :
* : :
* |---------------|
* | arg[argc - 1] |
* |---------------|
* | 0 |
* |---------------|
* | env[0] |
* |---------------|
* : :
* : :
* |---------------|
* | env[n] |
* |---------------|
* | 0 |
* |---------------| <-- Beginning of data returned by sysctl() is here.
* | argc |
* |---------------|
* | exec_path |
* |:::::::::::::::|
* | |
* | String area. |
* | |
* |---------------| <-- Top of stack.
* : :
* : :
* \---------------/ 0xffffffff
*/
mib[0] = CTL_KERN;
mib[1] = KERN_PROCARGS2;
mib[2] = pid;
size = (size_t)argmax;
if (sysctl(mib, 3, procargs, &size, NULL, 0) == -1) {
goto ERROR_B;
}
memcpy(&nargs, procargs, sizeof(nargs));
cp = procargs + sizeof(nargs);
/* Skip the saved exec_path. */
for (; cp < &procargs[size]; cp++) {
if (*cp == '\0') {
/* End of exec_path reached. */
break;
}
}
if (cp == &procargs[size]) {
goto ERROR_B;
}
/* Skip trailing '\0' characters. */
for (; cp < &procargs[size]; cp++) {
if (*cp != '\0') {
/* Beginning of first argument reached. */
break;
}
}
if (cp == &procargs[size]) {
goto ERROR_B;
}
/* Save where the argv[0] string starts. */
sp = cp;
/*
* Iterate through the '\0'-terminated strings and convert '\0' to ' '
* until a string is found that has a '=' character in it (or there are
* no more strings in procargs). There is no way to deterministically
* know where the command arguments end and the environment strings
* start, which is why the '=' character is searched for as a heuristic.
*/
for (np = NULL; c < nargs && cp < &procargs[size]; cp++) {
if (*cp == '\0') {
c++;
if (np != NULL) {
/* Convert previous '\0'. */
*np = ' ';
} else {
/* *argv0len = cp - sp; */
}
/* Note location of current '\0'. */
np = cp;
if (!show_args) {
/*
* Don't convert '\0' characters to ' '.
* However, we needed to know that the
* command name was terminated, which we
* now know.
*/
break;
}
}
}
/*
* sp points to the beginning of the arguments/environment string, and
* np should point to the '\0' terminator for the string.
*/
if (np == NULL || np == sp) {
/* Empty or unterminated string. */
goto ERROR_B;
}
size = strlen(sp)+1;
*buffer = (char*)malloc(size);
memset(*buffer, 0, size);
/* Make a copy of the string. */
strcpy(*buffer, sp);//, MAX_PATH-1);
/* Clean up. */
free(procargs);
return;
ERROR_B:
free(procargs);
*buffer = NULL;
procargs = NULL;
ERROR_A:
*buffer = NULL;
//fprintf(stderr, "Sorry, failed\n");
//exit(2);
}
#else
/**
* linux下获取进程的启动命令(进程名称),这里获取到的命令可能不准确
* 如果被set_process_title重命名过的进程无法正常获取,只会返回
*
* @param int pid 进程id
* @param char **buffer 输出结果到buffer
*/
void wing_get_cmdline(int process_id, char **buffer)
{
*buffer = NULL;
char file[MAX_PATH] = {0};
sprintf(file, "/proc/%d/cmdline", process_id);
if (wing_access(file, R_OK) != 0) {
return;
}
FILE *handle = fopen((const char*)file, "r");
if (!handle) {
*buffer = NULL;
return;
}
// fseek(handle, 0L, SEEK_END);
int filesize = 0;//ftell(handle);
while(!feof(handle)){
getc(handle);filesize++;
}
if (filesize <= 0) {
fclose(handle);
return;
}
rewind(handle);
*buffer = (char*)malloc(filesize+1);
if (*buffer == NULL) {
fclose(handle);
return;
}
memset(*buffer,0,filesize+1);
int c;
char *cs;
cs = *buffer;
while(!feof(handle)) {
c = getc(handle);
if (!c || c == NULL || c < 32) {
c = ' ';
}
*cs++ = c;
}
*cs='\0';
fclose(handle);
}
#endif