-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDeployWin.cpp
More file actions
471 lines (416 loc) · 11.7 KB
/
DeployWin.cpp
File metadata and controls
471 lines (416 loc) · 11.7 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
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
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
#include "wchar.h"
#include <string>
#include <thread>
#include <chrono>
#include <queue>
#include <mutex>
#include "DeployWin.h"
#include "ntdef.h"
#include "windows.h"
#include "psapi.h"
#ifdef HAVE_STRICMP
#define strcasecmp stricmp
#endif // strcasecmp
/*
待复制的文件的队列
*/
static std::queue<std::string> dllqueue;
static std::mutex dllqueuelock;
static void dllqueueadd(std::string dllpath)
{
std::lock_guard<std::mutex> lock(dllqueuelock);
dllqueue.push(dllpath);
}
static bool dllqueueisempty()
{
return dllqueue.empty();
}
static std::string dllqueueget()
{
std::lock_guard<std::mutex> lock(dllqueuelock);
std::string ret;
if(!dllqueueisempty())
{
ret=dllqueue.front();
dllqueue.pop();
}
return ret;
}
/*
是否运行标志
*/
static bool IsRunning=false;
/*
是否正在拷贝标志
*/
static bool IsCopying=true;
/*
获取环境变量函数
*/
static std::string GetEnv(std::string key)
{
if(key.empty())
{
return std::string();
}
const char* value=getenv(key.c_str());
if(value!=NULL)
{
return value;
}
else
{
return std::string();
}
}
/*
获取当前进程目录
*/
static std::string GetCurrentProcessDir()
{
char buff[MAX_PATH]= {0};
GetModuleFileNameA(NULL,buff,MAX_PATH);
std::string exepath(buff);
for(size_t i=0; i<exepath.length(); i++)
{
if(exepath.c_str()[exepath.length()-1-i]=='\\')
{
return exepath.substr(0,exepath.length()-i);
}
}
return std::string(".\\");
}
/*
获取系统目录
*/
static std::string GetSystemRoot()
{
return GetEnv("SYSTEMROOT");
}
/*
判断是否是数字
*/
static bool string_is_number(const std::string& s)
{
std::string::const_iterator it = s.begin();
while (it != s.end() && std::isdigit(*it)) ++it;
return !s.empty() && it == s.end();
}
/*
是否启动DeployWin
当检测到环境变量DEPLOYWIN=1时启动DeployWin线程
*/
bool StartDeployWin()
{
return GetEnv("DEPLOYWIN")=="1";
}
/*
自动退出时间
*/
static int GetAutoExit()
{
std::string autoexit=GetEnv("DEPLOYWIN_AUTOEXIT");
if(string_is_number(autoexit))
{
int ret=-1;
try
{
ret=std::stoi(autoexit);
}
catch(...)
{
}
return ret;
}
else
{
return -1;
}
}
/*
注册加载dll回调
*/
#ifndef STATUS_SUCCESS
#define STATUS_SUCCESS 0
#endif // STATUS_SUCCESS
#ifndef PCUNICODE_STRING
typedef const UNICODE_STRING* PCUNICODE_STRING;
#endif // PCUNICODE_STRING
#ifndef LDR_DLL_LOADED_NOTIFICATION_DATA
typedef struct _LDR_DLL_LOADED_NOTIFICATION_DATA
{
ULONG Flags; //Reserved.
PCUNICODE_STRING FullDllName; //The full path name of the DLL module.
PCUNICODE_STRING BaseDllName; //The base file name of the DLL module.
PVOID DllBase; //A pointer to the base address for the DLL in memory.
ULONG SizeOfImage; //The size of the DLL image, in bytes.
} LDR_DLL_LOADED_NOTIFICATION_DATA, *PLDR_DLL_LOADED_NOTIFICATION_DATA;
#endif // LDR_DLL_LOADED_NOTIFICATION_DATA
#ifndef LDR_DLL_UNLOADED_NOTIFICATION_DATA
typedef struct _LDR_DLL_UNLOADED_NOTIFICATION_DATA
{
ULONG Flags; //Reserved.
PCUNICODE_STRING FullDllName; //The full path name of the DLL module.
PCUNICODE_STRING BaseDllName; //The base file name of the DLL module.
PVOID DllBase; //A pointer to the base address for the DLL in memory.
ULONG SizeOfImage; //The size of the DLL image, in bytes.
} LDR_DLL_UNLOADED_NOTIFICATION_DATA, *PLDR_DLL_UNLOADED_NOTIFICATION_DATA;
#endif // LDR_DLL_UNLOADED_NOTIFICATION_DATA
#ifndef LDR_DLL_NOTIFICATION_DATA
typedef union _LDR_DLL_NOTIFICATION_DATA
{
LDR_DLL_LOADED_NOTIFICATION_DATA Loaded;
LDR_DLL_UNLOADED_NOTIFICATION_DATA Unloaded;
} LDR_DLL_NOTIFICATION_DATA, *PLDR_DLL_NOTIFICATION_DATA;
#endif // LDR_DLL_NOTIFICATION_DATA
#ifndef PCLDR_DLL_NOTIFICATION_DATA
typedef const LDR_DLL_NOTIFICATION_DATA * PCLDR_DLL_NOTIFICATION_DATA;
#endif // PCLDR_DLL_NOTIFICATION_DATA
#ifndef LDR_DLL_NOTIFICATION_REASON_LOADED
#define LDR_DLL_NOTIFICATION_REASON_LOADED 1
#endif // LDR_DLL_NOTIFICATION_REASON_LOADED
#ifndef LDR_DLL_NOTIFICATION_REASON_UNLOADED
#define LDR_DLL_NOTIFICATION_REASON_UNLOADED 2
#endif // LDR_DLL_NOTIFICATION_REASON_UNLOADED
typedef VOID CALLBACK LdrDllNotification(
_In_ ULONG NotificationReason,
_In_ PCLDR_DLL_NOTIFICATION_DATA NotificationData,
_In_opt_ PVOID Context
);
#ifndef PLDR_DLL_NOTIFICATION_FUNCTION
typedef LdrDllNotification * PLDR_DLL_NOTIFICATION_FUNCTION;
#endif // PLDR_DLL_NOTIFICATION_FUNCTION
typedef NTSTATUS NTAPI (*LdrRegisterDllNotification_t)(
_In_ ULONG Flags,
_In_ PLDR_DLL_NOTIFICATION_FUNCTION NotificationFunction,
_In_opt_ PVOID Context,
_Out_ PVOID *Cookie
);
typedef NTSTATUS NTAPI (*LdrUnregisterDllNotification_t)(
_In_ PVOID Cookie
);
static LdrRegisterDllNotification_t LdrRegisterDllNotification=NULL;
static LdrUnregisterDllNotification_t LdrUnregisterDllNotification=NULL;
static bool loadfunctions()
{
//加载函数
LdrRegisterDllNotification=NULL;
LdrUnregisterDllNotification=NULL;
HMODULE ntdll=LoadLibraryA("ntdll.dll");
if(ntdll!=NULL)
{
LdrRegisterDllNotification=( LdrRegisterDllNotification_t)GetProcAddress(ntdll,"LdrRegisterDllNotification");
LdrUnregisterDllNotification=(LdrUnregisterDllNotification_t)GetProcAddress(ntdll,"LdrUnregisterDllNotification");
}
if(LdrRegisterDllNotification==NULL || LdrUnregisterDllNotification==NULL)
{
return false;
}
else
{
return true;
}
}
static VOID CALLBACK DeployWinLdrDllNotification(
_In_ ULONG NotificationReason,
_In_ PCLDR_DLL_NOTIFICATION_DATA NotificationData,
_In_opt_ PVOID Context
)
{
//dll通知回调
switch(NotificationReason)
{
case LDR_DLL_NOTIFICATION_REASON_LOADED:
{
printf("DLL Loaded:%S\n",NotificationData->Loaded.FullDllName->Buffer);
char buff[MAX_PATH+1]= {0};
snprintf(buff,sizeof(buff)-1,"%S",NotificationData->Loaded.FullDllName->Buffer);
dllqueueadd(buff);
}
break;
case LDR_DLL_NOTIFICATION_REASON_UNLOADED:
{
printf("DLL Unloaded:%S\n",NotificationData->Loaded.FullDllName->Buffer);
}
break;
default:
break;
}
}
/*
枚举已加载的模块
*/
static void enumloadeddll()
{
HANDLE process=GetCurrentProcess();
{
//最大枚举4096个依赖
HMODULE module_list[4096]= {0};
DWORD count=0;
if(EnumProcessModules(process,module_list,sizeof(module_list),&count))
{
for(size_t i=0; i<count/sizeof(module_list[0]); i++)
{
char buff[MAX_PATH+1]= {0};
GetModuleFileNameA(module_list[i],buff,MAX_PATH);
printf("DLL Loaded(Enum):%s\n",buff);
dllqueueadd(buff);
}
}
}
}
/*
启动与线程相关
*/
class deploywin_startup;
static void deploywin_thread(deploywin_startup * obj);
static class deploywin_startup
{
public:
deploywin_startup()
{
Cookie=NULL;
IsRunning=false;
if(!loadfunctions())
{
return;
}
if(StartDeployWin())
{
start_time=std::chrono::steady_clock::now();
printf("DeployWin is starting.\nTo avoid this,DO NOT SET DEPLOYWIN=1.\n");
if(LdrRegisterDllNotification(0,DeployWinLdrDllNotification,this,&Cookie)!= STATUS_SUCCESS)
{
return;
}
IsRunning=true;
std::thread(deploywin_thread,(this)).detach();
printf("SystemDir:%s\n",GetSystemRoot().c_str());
printf("Dir:%s\n",GetCurrentProcessDir().c_str());
if(GetAutoExit()>0)
{
printf("AutoExit:%ds\n",GetAutoExit());
}
//枚举已加载的DLL
enumloadeddll();
{
//等待当前队列的dll拷贝完成(主要是枚举的dll)
//若main函数快速退出(常见于console程序),则不等待有可能拷贝不能完成。在main函数执行前拷贝完成防止拷贝中断。
while(!dllqueueisempty() || IsCopying)
{
std::this_thread::sleep_for(std::chrono::seconds(1));
}
}
}
}
~deploywin_startup()
{
if(Cookie!=NULL)
{
LdrUnregisterDllNotification(Cookie);
}
}
private:
PVOID Cookie;
std::chrono::steady_clock::time_point start_time;
public:
std::chrono::steady_clock::time_point GetStartTime()
{
return start_time;
}
} g_deploywin_startup;
static bool dllneedcopy(std::string dll)
{
std::string sysroot=GetSystemRoot();
std::string processdir=GetCurrentProcessDir();
if(dll.empty())
{
return false;
}
if(0==strcasecmp(processdir.c_str(),dll.substr(0, processdir.length()).c_str()))
{
//排除当前目录下的dll
return false;
}
if(0==strcasecmp(sysroot.c_str(),dll.substr(0,sysroot.length()).c_str()))
{
//排除系统目录下的dll
return false;
}
if(0==strcasecmp(".exe",dll.substr(dll.length()-strlen(".exe")).c_str()))
{
//排除exe文件
return false;
}
return true;
}
static bool dllcopy(std::string dll)
{
if(dll.empty())
{
return false;
}
std::string processdir=GetCurrentProcessDir();
if(processdir.at(processdir.length()-1)!='\\')
{
processdir+='\\';
}
std::string dllname;
{
//获取dll名称
for(size_t i=0; i<dll.length(); i++)
{
if(dll.at(dll.length()-i-1)=='\\')
{
dllname=dll.substr(dll.length()-i);
break;
}
}
if(dllname.empty())
{
dllname=dll;
}
}
std::string newdll=(processdir+dllname);
return 0!=CopyFileA(dll.c_str(),newdll.c_str(),0);
}
static void deploywin_thread(deploywin_startup * obj)
{
while(true)
{
while(!dllqueueisempty())
{
IsCopying=true;
std::string dll=dllqueueget();
if(dllneedcopy(dll))
{
printf("DLL(%s) need copy!\n",dll.c_str());
if(dllcopy(dll))
{
printf("DLL(%s) copy success!\n",dll.c_str());
}
}
IsCopying=false;
}
{
//自动退出
if(GetAutoExit()>0)
{
if(std::chrono::steady_clock::now()-obj->GetStartTime() > std::chrono::seconds(GetAutoExit()))
{
printf("DeployWin will exit!\n");
exit(0);
}
}
}
std::this_thread::sleep_for(std::chrono::seconds(1));
}
}
/*
外部接口
*/
bool DeployWinIsRunning()
{
return IsRunning;
}