-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathavctrl.cpp
456 lines (396 loc) · 9.85 KB
/
avctrl.cpp
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
#pragma warning(disable:4996)
#include <stdio.h>
#include <errno.h>
#include <windows.h>
#include <shlwapi.h>
#include <direct.h>
#include "typedef.h"
#include <inject.h>
#include <pipe.h>
#include <fifo.h>
#include <md5.h>
#include <strhelp.h>
#include "debug.h"
#include "psmsg.h"
#include "avctrl.h"
#define MAX_QUEUE_SIZE 1024
//引擎描述信息
typedef struct _scanner_t {
char name[256];
char cmdline[1024];
char path[512];
uint32_t pid;
int force_flush; //是否强行刷新缓存
int pipe;
fifo_t queue;
HANDLE hthread;
char sample_dir[1024];
HANDLE ready_event;
}scanner_t;
/*
//等待slaver连接
bool CVirusScanner::WaitSlaveConnected(
uint32_t pid_scanner,
int timeout_seconds )
{
if( !g_center.bOpening ) {
SetLastError( ERROR_SHUTDOWN_IN_PROGRESS );
return false;
}
HANDLE hConnectNotify = GetConnectEventHandle( idEngine );
if( hConnectNotify == NULL ) {
return false;
}
if( WAIT_OBJECT_0 != WaitForSingleObject( hConnectNotify,
timeout_seconds * 1000 ) ) {
//连接超时
return false;
}
return true;
}
//等待slave准备就绪
bool CVirusScanner::WaitSlaveReady(
uint32_t pid_scanner,
int timeout_seconds )
{
if( !g_center.bOpening ) {
SetLastError( ERROR_SHUTDOWN_IN_PROGRESS );
return false;
}
HANDLE hReady = GetReadyEventHandle(pid_scanner);
if(hReady == NULL) {
return false;
}
if(WAIT_OBJECT_0 != WaitForSingleObject(hReady, 30 * 1000)) {
return false;
}
return true;
}
*/
bool start_scanner(scanner_t* scanner)
{
STARTUPINFO si = {0};
si.cb = sizeof(STARTUPINFO);
si.dwFlags |= STARTF_USESTDHANDLES;
PROCESS_INFORMATION pi = {0};
char cwd[520] = {0};
_getcwd(cwd, sizeof(cwd) - 1);
_chdir(scanner->path);
if (!CreateProcess(NULL, (LPSTR)scanner->cmdline, NULL, NULL, FALSE,
CREATE_SUSPENDED, NULL,
scanner->path,
&si,
&pi) ) {
_chdir(cwd);
return false;
}
_chdir(cwd);
scanner->pid = pi.dwProcessId;
scanner->hthread = pi.hThread;
return true;
}
bool run_scanner(scanner_t* scanner)
{
if (-1 == ResumeThread(scanner->hthread)) {
return false;
}
return true;
}
bool inject_scanner(uint32_t pid_scanner)
{
//将slave模块远程注入到扫描器进程中
char injectdll_path[MAX_PATH] = {0};
_getcwd(injectdll_path, MAX_PATH);
PathAppend(injectdll_path, "avslave.dll");
return inject_dll_to_process(pid_scanner, injectdll_path);
}
/*
bool connect_scanner(scanner_t* scanner)
{
char pscom_name[128] = {0};
memset(pscom_name, 0, sizeof(pscom_name));
_snprintf(pscom_name, sizeof(pscom_name) - 1, "_%d_avslave_", scanner->pid);
int count = 15;
while(count-->0) {
//每次尝试间隔1秒
Sleep(1000);
//连接服务器
scanner->pipe = open_pipe(pscom_name);
if (scanner->pipe == INVALID_PIPE) {
continue;
}
//连接成功
//命令slaver进行初始化工作
atach_t request = {0};
request.msgid = PMSG_ATACH;
//wcsncpy(request.scanfolder, scan_folder, sizeof(request.scanfolder) - 1 );
request.force_flush = scanner->force_flush;
int nb = write_pipe(scanner->pipe, (uint8_t*)&request, sizeof(request));
if ( sizeof(request) != nb) {
break;
}
uint32_t result = 0;
nb = read_pipe(scanner->pipe, (uint8_t*)&result, sizeof(result));
if( nb != sizeof(uint32_t ) ) {
return -1;
}
if (result != 0) {
return -1;
}
//等待scanner准备就绪
//wait_scanner_ready(scanner);
return true;
}
return false;
}*/
bool wait_scanner_ready(scanner_t* scanner)
{
uint32_t ret = WaitForSingleObject(scanner->ready_event, 5000);
return ret == WAIT_OBJECT_0;
}
int reset_scanner(scanner_t* scanner)
{
//启动进程
if (!start_scanner(scanner)) {
free(scanner);
scanner = NULL;
errno = EINVAL;
return false;
}
if(!inject_scanner(scanner->pid)) {
free(scanner);
scanner = NULL;
errno = EINVAL;
return false;
}
if(!run_scanner(scanner)) {
free(scanner);
scanner = NULL;
errno = EINVAL;
return false;
}
return true;
}
bool is_win7_laster()
{
/*
HMODULE ntdll = LoadLibrary( "kernel32.dll" );
if ( NULL == ntdll ) {
return false;
}
*/
OSVERSIONINFO osinfo = {0};
osinfo.dwOSVersionInfoSize = sizeof( osinfo );
GetVersionEx( &osinfo );
return osinfo.dwMajorVersion >= 6 && osinfo.dwMinorVersion >= 1;
}
DWORD WINAPI pipe_thread(LPVOID lpThreadParameter)
{
dprintf("pipe thread start\n");
uint32_t pid = GetCurrentProcessId();
scanner_t* scanner = (scanner_t*)lpThreadParameter;
if (!listen_pipe(scanner->pipe)) {
close_pipe(scanner->pipe);
return false;
}
while(true) {
char buffer[1024] = {0};
int nb = read_pipe(scanner->pipe, buffer, sizeof(buffer));
if (nb < 0 ) {
if (GetLastError() == 0x6D) {
//remote disconnected
dprintf("antivirus %s already disconnected\n", scanner->name);
} else {
dprintf("GetLastError:%08x, HANDLE=%08x", GetLastError(), scanner->pipe);
}
//disconnected
break;
}
uint32_t msgid = *(uint32_t*)buffer;
char* data = buffer + sizeof(uint32_t);
int data_len = nb - sizeof(uint32_t);
dprintf("msg:%d\n", msgid);
switch(msgid){
case PMSG_SCAN: {
//从队列中取一个样本, 将其返回
do {
sample_t* sample = (sample_t*)fifo_get(&scanner->queue);
if (sample == NULL) {
Sleep(100);
continue;
}
dprintf("fifo get success, %S", sample->cFileName);
write_pipe(scanner->pipe, sample, sizeof(sample_t));
free(sample);
sample = NULL;
break;
} while(true);
break;
} case PMSG_INIT: {
init_t init = {0};
init.is_win7_laster = is_win7_laster();
write_pipe(scanner->pipe, &init, sizeof(init_t));
break;
} case PMSG_READY: {
SetEvent(scanner->ready_event);
break;
} case PMSG_EXIT: {
ExitProcess( 0 );
break;
} default:
break;
}
}
close_pipe(scanner->pipe);
dprintf("pipe thread exit");
return 0;
}
bool start_comm(scanner_t* scanner)
{
char pipename[1024] = {0};
_snprintf(pipename, sizeof(pipename) - 1, "avctrl_%d", scanner->pid);
scanner->pipe = create_pipe(pipename);
if (scanner->pipe == INVALID_PIPE) {
return false;
}
CreateThread(0, 0, pipe_thread, (void*)scanner, 0, 0);
return true;
}
//加载启动引擎
int create_scanner(const char* name, const char* cmdline,
const char* path,
const char* sample_dir)
{
//启动进程间通信模块
//连接成功
scanner_t* scanner = (scanner_t*)malloc(sizeof(scanner_t));
if (scanner == NULL) {
errno = ENOMEM;
return INVALID_SCANNER;
}
memset(scanner, 0, sizeof(scanner_t));
strncpy(scanner->name, name, sizeof(scanner->name) - 1);
strncpy(scanner->cmdline, cmdline, sizeof(scanner->cmdline) - 1);
strncpy(scanner->path, path, sizeof(scanner->path) - 1);
strncpy(scanner->sample_dir, sample_dir, sizeof(scanner->sample_dir) -1);
fifo_init(&scanner->queue, MAX_QUEUE_SIZE);
scanner->ready_event = CreateEvent(NULL, TRUE, FALSE, NULL);
if (scanner->ready_event == NULL) {
free(scanner);
scanner = NULL;
errno = EINVAL;
return INVALID_SCANNER;
}
//启动进程
if (!start_scanner(scanner)) {
free(scanner);
scanner = NULL;
errno = EINVAL;
return INVALID_SCANNER;
}
if (!start_comm(scanner)) {
free(scanner);
scanner = NULL;
errno = EINVAL;
return INVALID_SCANNER;
}
if (!inject_scanner(scanner->pid)) {
free(scanner);
scanner = NULL;
errno = EINVAL;
return INVALID_SCANNER;
}
if (!run_scanner(scanner)) {
free(scanner);
scanner = NULL;
errno = EINVAL;
return INVALID_SCANNER;
}
if (!wait_scanner_ready(scanner)) {
free(scanner);
scanner = NULL;
errno = EINVAL;
return INVALID_SCANNER;
}
return (int)scanner;
}
int scan_file(int fd, const char* sample_file)
{
scanner_t* scanner = (scanner_t*)fd;
if (scanner == NULL) {
return EINVAL;
}
/*
if (!is_alive(scanner)) {
if(!reset_scanner(scanner)) {
return EINVAL;
}
}
*/
//计算样本MD5值, 将样本复制到内部目录下
CMD5 hash;
hash.HashFile(sample_file);
hash.Final();
MD5_32 md5 = {0};
hash.GetHash(&md5);
char str_md5[128] = {0};
MakeHexString(md5._digest, 16, str_md5, sizeof(str_md5) - 1);
char new_file[1024] = {0};
_snprintf(new_file, sizeof(new_file) - 1, "%s\\%s",
scanner->sample_dir, str_md5);
CopyFile(sample_file, new_file, TRUE);
sample_t* sample = (sample_t*)malloc(sizeof(sample_t));
if (sample == NULL) {
return EINVAL;
}
memset(sample, 0, sizeof(sample_t));
sample->msgid = PMSG_SCAN;
const char* pos = strrchr(sample_file, '\\');
if( pos == NULL ) {
return -1;
}
strncpy(sample->file, pos + 1, sizeof(sample->file) - 1);
//读取样本的文件属性
sample->dwFileAttributes = GetFileAttributes(new_file);
if (sample->dwFileAttributes == INVALID_FILE_ATTRIBUTES) {
return -1;
}
//读取样本的文件时间
HANDLE hFile = CreateFile(new_file, GENERIC_READ,
FILE_SHARE_READ, NULL, OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL, NULL);
if (hFile == INVALID_HANDLE_VALUE) {
return -1;
}
do {
if (!GetFileTime(hFile, &sample->ftCreationTime,
&sample->ftLastAccessTime,
&sample->ftLastWriteTime)) {
break;
}
//读取文件的大小
sample->nFileSizeLow = GetFileSize(hFile, &sample->nFileSizeHigh);
}while(false);
CloseHandle(hFile);
hFile = NULL;
mbstowcs(sample->cFileName, str_md5, strlen(str_md5));
//加入队列
while(!fifo_put(&scanner->queue, (void*)sample)) {
//可能队列已经满了, 继续
Sleep(100);
}
return 0;
}
uint32_t get_scanner_pid(int sd)
{
scanner_t* scanner = (scanner_t*)sd;
return scanner->pid;
}
void close_scanner(int sd)
{
}
bool is_scanner_idle(int sd)
{
scanner_t* scanner = (scanner_t*)sd;
return fifo_empty(&scanner->queue);
}