-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathantinject.cpp
More file actions
256 lines (216 loc) · 5.88 KB
/
antinject.cpp
File metadata and controls
256 lines (216 loc) · 5.88 KB
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
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
#include <detours.h>
#include <typedef.h>
#include <avs_interface.h>
#include "antinject.h"
#define STATUS_UNSUCCESSFUL ((NTSTATUS)0xC0000001L)
typedef LONG NTSTATUS, *PNTSTATUS;
typedef struct _UNICODE_STRING {
USHORT Length;
USHORT MaximumLength;
PWSTR Buffer;
} UNICODE_STRING, *PUNICODE_STRING;
typedef const UNICODE_STRING* PCUNICODE_STRING;
typedef
NTSTATUS
(NTAPI *LdrLoadDll)(
IN PWCHAR pszSearchPath OPTIONAL,
IN PULONG puDllCharacteristics OPTIONAL,
IN PUNICODE_STRING pusDllName,
OUT PHANDLE phDllHandle);
LdrLoadDll OriLdrLoadDll = NULL;
antinject_notify g_notify = NULL;
//功能:测试一个样本函数
//参数:
// strFilePath 文件路径
//返回:无
void check_one_file(char *strFilePath,
char* virus_name,
size_t name_max_len)
{
FILE *fp;
uint32_t nFileSize;
char *pFileBuf;
void *nHandle;
StreamAvEngineIn saei;
StreamAvEngineOut saeo;
int nCheckResult;
fp = fopen(strFilePath, "rb");
if(NULL == fp) {
return;
}
fseek(fp, 0, SEEK_END);
nFileSize = ftell(fp);
fseek(fp, 0, SEEK_SET);
pFileBuf = (char *)malloc(nFileSize);
if(NULL != pFileBuf)
{
if(nFileSize == fread(pFileBuf, 1, nFileSize, fp))
{
int nI;
//打开一个检测句柄
nHandle = av_engine_create_handle(FILE_CHECK_HANDLE, NOT_CHECK_EMAIL, UNKNOW_STREAM_LEN);
if(NULL != nHandle)
{
saei.nEnd = STREAM_END;
saei.nDataLen = nFileSize;
saei.pData = pFileBuf;
saei.fin.nAllPacketBuf = FILE_ALL_PACKEt_BUF;
nCheckResult = file_av_engine_check_virus(&saei, nHandle, &saeo);
if(AV_ENGINE_FIND_VIRUS == nCheckResult)
{
strncpy(virus_name, saeo.virusInfo.strVirusName, name_max_len-1);
}
av_engine_close_handle(nHandle);
}
//打开一个检测句柄
nHandle = av_engine_create_handle(FILE_CHECK_HANDLE, NOT_CHECK_EMAIL, UNKNOW_STREAM_LEN);
if(NULL != nHandle)
{
saei.pData = pFileBuf;
saei.fin.nAllPacketBuf = FILE_ALL_PACKEt_BUF;
nI = nFileSize > 512 ? 512 : nFileSize;
for(; nI <= nFileSize; )
{
saei.nDataLen = nI;
if(nFileSize == nI)
{
saei.nEnd = STREAM_END;
}
else
{
saei.nEnd = STREAM_NOT_END;
}
nCheckResult = file_av_engine_check_virus(&saei, nHandle, &saeo);
if(AV_ENGINE_FIND_NOTHING != nCheckResult && AV_ENGINE_END_THIS_STREAM_CHECK != nCheckResult)
{
// printf("Found Virus(%d/%d %s %s)!\n",
// g_dwVirusCount,
// g_dwFileCount,
// saeo.virusInfo.strVirusName,
// saeo.virusInfo.strVirusType);
strncpy(virus_name, saeo.virusInfo.strVirusName, name_max_len-1);
break;
}
if(STREAM_END == saei.nEnd) break;
nI += 512;
if(nI > nFileSize) nI = nFileSize;
}
av_engine_close_handle(nHandle);
}
//打开一个检测句柄
nHandle = av_engine_create_handle(FILE_CHECK_HANDLE, NOT_CHECK_EMAIL, UNKNOW_STREAM_LEN);
if(NULL != nHandle)
{
saei.fin.nAllPacketBuf = FILE_SIGNLE_PACKET_BUF;
if(nFileSize >= 512)
{
int nOldnI = 0;
for(nI = 512; nI <= nFileSize; )
{
saei.pData = pFileBuf + nOldnI;
saei.nDataLen = nI - nOldnI;
if(nFileSize == nI)
{
saei.nEnd = STREAM_END;
}
else
{
saei.nEnd = STREAM_NOT_END;
}
nCheckResult = file_av_engine_check_virus(&saei, nHandle, &saeo);
if (AV_ENGINE_FIND_NOTHING != nCheckResult &&
AV_ENGINE_END_THIS_STREAM_CHECK != nCheckResult)
{
// g_dwVirusCount++;
// printf("Found Virus(%d/%d %s %s)!\n",
// g_dwVirusCount,
// g_dwFileCount,
// saeo.virusInfo.strVirusName,
// saeo.virusInfo.strVirusType);
strncpy(virus_name, saeo.virusInfo.strVirusName, name_max_len-1);
break;
}
if(STREAM_END == saei.nEnd) break;
nOldnI = nI;
nI += 512;
if(nI > nFileSize)
{
nI = nFileSize;
}
}
}
av_engine_close_handle(nHandle);
}
}
free(pFileBuf);
}
fclose(fp);
}
NTSTATUS NTAPI SecurityLdrLoadDll(
IN PWCHAR pszSearchPath OPTIONAL,
IN PULONG puDllCharacteristics OPTIONAL,
IN PUNICODE_STRING pusDllName,
OUT PHANDLE phDllHandle)
{
NTSTATUS Status = STATUS_UNSUCCESSFUL;
do {
char dllname[512] = {0};
wcstombs(dllname, pusDllName->Buffer, pusDllName->Length);
//进行病毒扫描
char virus_name[256] = {0};
check_one_file(dllname, virus_name, sizeof(virus_name));
// 通知上层逻辑, 让逻辑层来决定是否加载该DLL
bool is_malware = (strlen(virus_name) > 0 );
if (!g_notify(dllname, is_malware)) {
// 拒绝运行
break;
} else {
// 允许运行
Status=OriLdrLoadDll(pszSearchPath,
puDllCharacteristics,pusDllName,phDllHandle);
}
} while (false);
return Status;
}
bool enable_antinject(
antinject_notify notify_routine,
const char* virus_db_fullpath)
{
HMODULE ntdll = LoadLibrary("ntdll.dll");
if (ntdll == NULL) {
return false;
}
OriLdrLoadDll = (LdrLoadDll)GetProcAddress(ntdll, "LdrLoadDll");
if (OriLdrLoadDll == NULL) {
return false;
}
// 初始化病毒库
if (0 != init_stream_av_engine((char*)virus_db_fullpath)) {
// 初始化病毒库失败
return false;
}
// 配置病毒库
StreamAvEngineWorkType sewt;
sewt.nUnRar = 0; //解压包裹
sewt.nDecodeEmail = 0; //不解密邮件
sewt.nUnrarFloor = 1; //最大支持2层解压缩
sewt.nUnrarFileSize = 1024*1024; //对1M以上的文件不解压
config_stream_av_engine_work_type(&sewt);
DetourTransactionBegin();
DetourUpdateThread(GetCurrentThread());
DetourAttach(&(PVOID&)OriLdrLoadDll, SecurityLdrLoadDll);
DetourTransactionCommit();
g_notify = notify_routine;
return true;
}
void disable_antinject()
{
DetourTransactionBegin();
DetourUpdateThread(GetCurrentThread());
DetourDetach(&(PVOID&)OriLdrLoadDll, SecurityLdrLoadDll);
DetourTransactionCommit();
destroy_stream_av_engine();
}