-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathAutoUninstall.cpp
1561 lines (1463 loc) · 41.5 KB
/
AutoUninstall.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
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#define DEVCPP
#define DELETE_NO_DELAY
//#define NO_DEBUGLOG
//Linker Options 额外的链接参数
//-lgdi32 -lpsapi
#include <Windows.h>
#include <winternl.h>
#include <psapi.h>
#include <thread>
#include <iostream>
#include <sstream>
#include <string>
#include <vector>
#include <io.h>
#include <tlhelp32.h>
#include <sys/stat.h>
#include <cmath>
using namespace std;
#define CJZAPI __stdcall
#define KEY_DOWN(sth) (GetAsyncKeyState(sth) & 0x8000 ? 1 : 0)
#define fequ(f1, f2) (abs((f1)-(f2)) < 0.001)
#define ADDRESS intptr_t
#define TEXT_LEFT 80
#define TEXT_TOP 100
#define TEXT_FS 24
#define TEXT_V_ADD 27
#define TEXT_FONTNAME "Minecraft AE Pixel"
#define MSG_SHOWN_TIME 4000.0 //ms
#define MSG_FS 22
#define MSG_V_ADD 26
#define MSG_FONTNAME "Minecraft AE Pixel"
void AddMessage(const string& s);
void ErrMessage(const string& s);
#define LOG_NAME "Uninstall.LOG"
#ifndef NO_DEBUGLOG
void DebugLog(const string& s)
{
FILE*fp = fopen(LOG_NAME, "a+");
fprintf(fp, (s+'\n').c_str());
fclose(fp);
}
#else
#define DebugLog(...)
#endif
HINSTANCE _hInstance{nullptr};
HINSTANCE _hPrevInstance{nullptr};
LPSTR _lpCmdLine{nullptr};
int _nShowCmd;
bool admin{false};
int scr_w;
int scr_h;
int taskbar_h;
HWND hwnd{nullptr};
HWND hwnd_target{nullptr};
template <typename T>
string CJZAPI str(const T& value)
{
stringstream ss;
ss << value;
string s;
ss >> s;
return s;
}
inline bool CJZAPI strequ(char *str, const char *obj)
{ //比较是否一样
return (strcmp((const char *)str, obj) == 0 ? true : false);
}
inline bool CJZAPI sequ(const string& s1, const string& s2)
{
return _stricmp(s1.c_str(), s2.c_str()) == 0;
}
string CJZAPI strtail(const string& s, int cnt = 1) {
//012 len=3
//abc s.substr(2,1)
if (cnt > s.size())
return s;
return s.substr(s.size() - cnt, cnt);
}
string CJZAPI strhead(const string& s, int cnt = 1) {
if (cnt > s.size())
return s;
return s.substr(0, cnt);
}
string CJZAPI strxtail(const string& s, int cnt = 1) {
if (cnt > s.size())
return "";
return s.substr(0, s.size() - cnt);
}
string CJZAPI strxhead(const string& s, int cnt = 1) {
if (cnt > s.size())
return "";
return s.substr(cnt, s.size() - cnt);
}
string CJZAPI strip(const string& s)
{
string res = s;
while(!res.empty() && (res.at(0) == '\r' || res.at(0) == '\n' || res.at(0) == '\0'))
res = res.substr(1, res.size() - 1);
while(!res.empty() && (res.at(res.size()-1) == '\r' || res.at(res.size()-1) == '\n' || res.at(0) == '\0'))
res = res.substr(0, res.size() - 1);
return res;
}
string ConvertErrorCodeToString(DWORD ErrorCode)
{
HLOCAL LocalAddress {nullptr};
FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_IGNORE_INSERTS | FORMAT_MESSAGE_FROM_SYSTEM,
NULL, ErrorCode, 0, (PTSTR)&LocalAddress, 0, NULL);
return strxtail(LPSTR(LocalAddress), 2); //去除换行符
}
BOOL CJZAPI IsFile(const string& lpPath)
{ //是否是文件
int res;
#ifndef DEVCPP
struct _stat buf;
res = _stat(lpPath.c_str(), &buf);
#else
struct _stat64i32 buf;
res = _stat64i32(lpPath.c_str(), &buf);
#endif
return (buf.st_mode & _S_IFREG);
}
BOOL CJZAPI IsDir(const string& lpPath)
{ //是否是文件夹
int res;
#ifndef DEVCPP
struct _stat buf;
res = _stat(lpPath.c_str(), &buf);
#else
struct _stat64i32 buf;
res = _stat64i32(lpPath.c_str(), &buf);
#endif
return (buf.st_mode & _S_IFDIR);
}
inline BOOL CJZAPI ExistFile(const string& strFile) {
//文件或文件夹都可以
return !_access(strFile.c_str(), S_OK);//S_OK表示只检查是否存在
}
string CJZAPI GetFileDirectory(string path)
{ //返回的最后会有反斜线
if (IsDir(path))
{
if (strtail(path) != "\\")
path += "\\";
return path;
}
if (strtail(path) == "\\")
path = strxtail(path);
string ret = "";
bool suc = false;
for (int i = path.size() - 1; i >= 0; i--)
{
if (path.at(i) == '\\')
{
ret = path.substr(0, i + 1);
suc = true;
break;
}
}
if (!suc)
SetLastError(3L); //还是要return的
if (strtail(ret) != "\\")
ret += "\\";
return ret;
}
vector<string> CJZAPI GetDirFiles(const string& dir, const string& filter = "*.*")
{
if (dir.empty() || (filter != "" && !ExistFile(dir)))
{
return vector<string>();
}
_finddata_t fileDir;
ADDRESS lfDir = 0;
vector<string> files{};
string dirp = dir + filter; //它是查找式的
if ((lfDir = _findfirst(dirp.c_str(), &fileDir)) == -1)
{
return vector<string>();
}
else {
do { //遍历目录
if (!strequ(fileDir.name, ".") && !strequ(fileDir.name, "..")) //排除这两个狗东西
files.push_back(string(fileDir.name));
} while (_findnext(lfDir, &fileDir) == 0);
}
_findclose(lfDir);
return files;
}
void GetDirFilesR_Proc(vector<string>* result, const string& odir /*backslashed*/, const string& childDir, const string& filter)
{
vector<string> matchedFiles = GetDirFiles(odir + childDir, filter);
for (auto& f : matchedFiles)
if (IsFile(odir + childDir + f))
{
result->push_back(childDir + f);
}
matchedFiles.clear();
vector<string> all = GetDirFiles(odir + childDir, "*");
for (auto& ele : all)
if (IsDir(odir + childDir + ele))
{
GetDirFilesR_Proc(result, odir, childDir + ele + "\\", filter);
}
}
vector<string> CJZAPI GetDirFilesR(const string& dir /*backslashed*/, const string& filter = "*.*")
{
vector<string> result;
GetDirFilesR_Proc(&result, dir, "", filter);
return result;
}
inline double CJZAPI EaseOutCubic(double _x)
{
return 1 - pow(1 - _x, 3);
}
inline double CJZAPI EaseInExpo(double _x)
{
return fequ(_x, 0.0) ? 0 : pow(2, 10 * _x - 10);
}
template <typename _T>
inline _T CJZAPI ClampA(_T& val, _T min=0, _T max=2147483647) { //限定范围
if(val < min) val = min;
else if(val > max) val = max;
return val;
}
template <typename _T>
inline _T CJZAPI Clamp(_T val, _T min=0, _T max=2147483647) { //限定范围
if(val < min) val = min;
else if(val > max) val = max;
return val;
}
template <typename _T>
inline double CJZAPI Lerp(_T startValue, _T endValue, double ratio)
{
return startValue + (endValue - startValue) * ratio;
}
template <typename _T>
inline double CJZAPI LerpClamp(_T startValue, _T endValue, double ratio)
{
return Clamp<double>(Lerp(startValue,endValue,ratio), min(startValue,endValue), max(endValue,startValue));
}
inline HFONT CJZAPI CreateFont(int height, int width, LPCSTR lpFamilyName)
{
return CreateFont(height,width,0,0,FW_NORMAL,0,0,0,0,0,0,0,0,lpFamilyName);
}
inline int GetScreenHeight(void) //获取屏幕高度
{
return GetSystemMetrics(SM_CYSCREEN);
}
inline int GetScreenWidth(void) //获取屏幕宽度
{
return GetSystemMetrics(SM_CXSCREEN);
}
RECT CJZAPI GetSystemWorkAreaRect(void) //获取工作区矩形
{
RECT rt;
SystemParametersInfo(SPI_GETWORKAREA,0,&rt,0); // 获得工作区大小
return rt;
}
LONG CJZAPI GetTaskbarHeight(void) //获取任务栏高度
{
INT cyScreen = GetScreenHeight();
RECT rt = GetSystemWorkAreaRect();
return (cyScreen - (rt.bottom - rt.top));
}
BOOL CJZAPI IsRunAsAdmin(HANDLE hProcess=GetCurrentProcess())
{ //是否有管理员权限
BOOL bElevated = FALSE;
HANDLE hToken = NULL; // Get current process token
if ( !OpenProcessToken(hProcess, TOKEN_QUERY, &hToken ) )
return FALSE;
TOKEN_ELEVATION tokenEle;
DWORD dwRetLen = 0; // Retrieve token elevation information
if ( GetTokenInformation( hToken, TokenElevation, &tokenEle, sizeof(tokenEle), &dwRetLen ) )
{
if ( dwRetLen == sizeof(tokenEle) )
{
bElevated = tokenEle.TokenIsElevated;
}
}
CloseHandle( hToken );
return bElevated;
}
VOID CJZAPI AdminRun(LPCSTR csExe, LPCSTR csParam=NULL, DWORD nShow=SW_SHOW)
{ //以管理员身份运行
SHELLEXECUTEINFO ShExecInfo;
ShExecInfo.cbSize = sizeof(SHELLEXECUTEINFO);
ShExecInfo.fMask = SEE_MASK_NOCLOSEPROCESS ;
ShExecInfo.hwnd = NULL;
ShExecInfo.lpVerb = "runas";
ShExecInfo.lpFile = csExe/*"cmd"*/;
ShExecInfo.lpParameters = csParam;
ShExecInfo.lpDirectory = NULL;
ShExecInfo.nShow = nShow;
ShExecInfo.hInstApp = NULL;
BOOL ret = ShellExecuteEx(&ShExecInfo);
/*WaitForSingleObject(ShExecInfo.hProcess, INFINITE);
GetExitCodeProcess(ShExecInfo.hProcess, &dwCode); */
CloseHandle(ShExecInfo.hProcess);
return;
} //from https://blog.csdn.net/mpp_king/article/details/80194563
HANDLE CJZAPI GetProcessHandle(DWORD pid) //通过进程ID获取进程句柄
{
return OpenProcess(PROCESS_ALL_ACCESS, FALSE, pid);
}HANDLE CJZAPI GetProcessHandle(LPCSTR lpName) //通过进程名获取进程句柄
{ //******警告!区分大小写!!!!******//
//*****警告!必须加扩展名!!!!*****//
HANDLE hSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
if (INVALID_HANDLE_VALUE == hSnapshot)
{
return NULL;
}
PROCESSENTRY32 pe = { sizeof(pe) };
BOOL fOk;
for (fOk = Process32First(hSnapshot, &pe); fOk; fOk = Process32Next(hSnapshot, &pe))
{
//cout << "\n进程:" << pe.szExeFile;
if (!_stricmp(pe.szExeFile, lpName))
{
CloseHandle(hSnapshot);
return GetProcessHandle(pe.th32ProcessID);
}
}
CloseHandle(hSnapshot);
return NULL;
}
//from https://blog.csdn.net/fzuim/article/details/60954959
string GetProcessName(DWORD pid)
{
HANDLE hSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
if (INVALID_HANDLE_VALUE == hSnapshot)
{
return string("");
}
PROCESSENTRY32 pe = { sizeof(pe) };
BOOL fOk;
for (fOk = Process32First(hSnapshot, &pe); fOk; fOk = Process32Next(hSnapshot, &pe))
{
if (pe.th32ProcessID == pid)
{
CloseHandle(hSnapshot);
// cout<<"text="<<pe.szExeFile;
// pname=pe.szExeFile;
// return (LPSTR)pe.szExeFile;
return (string)pe.szExeFile;
}
}
CloseHandle(hSnapshot);
return string("");
}
bool CJZAPI HaveProcess(DWORD pid)
{
HANDLE hSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
if (INVALID_HANDLE_VALUE == hSnapshot)
return false;
PROCESSENTRY32 pe = { sizeof(pe) };
BOOL fOk;
for (fOk = Process32First(hSnapshot, &pe); fOk; fOk = Process32Next(hSnapshot, &pe))
if (pe.th32ProcessID == pid)
{
CloseHandle(hSnapshot);
return true;
}
CloseHandle(hSnapshot);
return false;
}
BOOL CJZAPI HaveProcess(LPCSTR lpName)
{ //******警告!区分大小写!!!!******//
//*****警告!必须加扩展名!!!!*****//
HANDLE hSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
if (INVALID_HANDLE_VALUE == hSnapshot)
{
return NULL;
}
PROCESSENTRY32 pe = { sizeof(pe) };
BOOL fOk;
for (fOk = Process32First(hSnapshot, &pe); fOk; fOk = Process32Next(hSnapshot, &pe))
if (!_stricmp(pe.szExeFile, lpName)) //相同名称 忽略大小写
{
CloseHandle(hSnapshot);
return true;
}
CloseHandle(hSnapshot);
return false;
}
bool FreezeProcess(HANDLE hProc) //冻结进程
{
int pid = GetProcessId(hProc);
THREADENTRY32 th32;
th32.dwSize = sizeof(th32);
HANDLE hThreadSnap = CreateToolhelp32Snapshot(TH32CS_SNAPTHREAD, 0);
if (hThreadSnap == INVALID_HANDLE_VALUE)
{
ErrMessage("CreateToolhelp32Snapshot 调用失败");
return false;
}
bool b;
b = Thread32First(hThreadSnap, &th32);
while (b)
{
if (th32.th32OwnerProcessID == pid)
{
HANDLE oth = OpenThread(THREAD_ALL_ACCESS, FALSE, th32.th32ThreadID);
if (!(SuspendThread(oth)))
{
// SetColor(11, 0);
// printf("\n已冻结线程ID: %d", th32.th32ThreadID);
// printf("\n已冻结进程ID: %d", th32.th32OwnerProcessID);
}
CloseHandle(oth);
break;
}
Thread32Next(hThreadSnap, &th32);
}
CloseHandle(hThreadSnap);
return true;
}
bool UnfreezeProcess(HANDLE hProc) //解冻进程
{
int pid = GetProcessId(hProc);
THREADENTRY32 th32;
th32.dwSize = sizeof(th32);
HANDLE hThreadSnap = CreateToolhelp32Snapshot(TH32CS_SNAPTHREAD, 0);
if (hThreadSnap == INVALID_HANDLE_VALUE)
{
ErrMessage("CreateToolhelp32Snapshot 调用失败");
return false;
}
bool b;
b = Thread32First(hThreadSnap, &th32);
while (b)
{
if (th32.th32OwnerProcessID == pid)
{
HANDLE oth = OpenThread(THREAD_ALL_ACCESS, FALSE, th32.th32ThreadID);
if (!(ResumeThread(oth)))
{
// printf("\n已解冻线程ID: %d", th32.th32ThreadID);
// printf("\n已解冻进程ID: %d", th32.th32OwnerProcessID);
}
CloseHandle(oth);
break;
}
Thread32Next(hThreadSnap, &th32);
}
CloseHandle(hThreadSnap);
return true;
}
typedef LONG (__stdcall *PROCNTQSIP)(HANDLE,UINT,PVOID,ULONG,PULONG);
DWORD GetParentProcessId(DWORD dwProcessId)
{
LONG status;
DWORD dwParentPID = (DWORD)-1;
HANDLE hProcess;
PROCESS_BASIC_INFORMATION pbi;
PROCNTQSIP NtQueryInformationProcess = (PROCNTQSIP)GetProcAddress(
GetModuleHandle("ntdll"), "NtQueryInformationProcess");
if(NULL == NtQueryInformationProcess)
{
return (DWORD)-1;
}
// Get process handle
hProcess = OpenProcess(PROCESS_QUERY_INFORMATION,FALSE, dwProcessId);
if (!hProcess)
{
return (DWORD)-1;
}
// Retrieve information
status = NtQueryInformationProcess( hProcess,
ProcessBasicInformation,
(PVOID)&pbi,
sizeof(PROCESS_BASIC_INFORMATION),
NULL
);
// Copy parent Id on success
if (!status)
{
dwParentPID = pbi.InheritedFromUniqueProcessId;
}
CloseHandle (hProcess);
return dwParentPID;
} //from https://www.cnblogs.com/jkcx/p/7457339.html
HMODULE hKernel = nullptr;
typedef WINBOOL (WINAPI *T_EnumProcessModules) (HANDLE, HMODULE*, DWORD, LPDWORD);
typedef WINBOOL (WINAPI *T_GetModuleFileNameExA) (HANDLE, HMODULE, LPSTR, DWORD);
string CJZAPI GetProcessPath(HANDLE hProc) //获取进程EXE绝对路径
{
//注意:动态调用!
hKernel = LoadLibrary("KERNEL32.dll"); //核心32 DLL模块句柄
if(!hKernel || hKernel == INVALID_HANDLE_VALUE)
{
ErrMessage("无法获取Kernel32.dll的模块句柄");
return string("");
}
T_EnumProcessModules EPM = nullptr; //函数变量
T_GetModuleFileNameExA GMFNE = nullptr; //函数变量
EPM = (T_EnumProcessModules)GetProcAddress(hKernel, "K32EnumProcessModules");
GMFNE = (T_GetModuleFileNameExA)GetProcAddress(hKernel, "K32GetModuleFileNameExA");
if (!EPM || !GMFNE)
{ //没找到
FreeLibrary(hKernel);
ErrMessage("加载Kernel32.dll时出错: 找不到指定的函数: -> K32EnumProcessModules() -> K32GetModuleFileNameExA()");
return string("");
}
//关键操作
HMODULE hMod;
DWORD need;
CHAR thePath[MAX_PATH]{ 0 };
EPM(hProc, &hMod, sizeof(hMod), &need);
GMFNE(hProc, hMod, thePath, sizeof(thePath));
FreeLibrary(hKernel);
return string(thePath);
}
string GetProcessPath2(DWORD pid)
{
HANDLE hProcess = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE, pid);
if (hProcess != NULL)
{
char buffer[MAX_PATH];
DWORD dwSize = GetModuleFileNameExA(hProcess, NULL, buffer, MAX_PATH);
CloseHandle(hProcess);
if (dwSize != 0)
{
return string(buffer);
}
}else{
ErrMessage("无法获取进程句柄");
}
return "";
}
//-----------------------------------------------------------------------------------------------------------
// 函数: UninjectDll
// 功能: 从目标进程中卸载一个指定 Dll 模块文件.
// 参数: [in] const TCHAR* ptszDllFile - Dll 文件名及路径
// [in] HANDLE hProc - 目标进程句柄
// 返回: bool - 卸载成功返回 true, 卸载失败则返回 false.
// 说明: 采用远程线程注入技术实现
//-----------------------------------------------------------------------------------------------------------
bool CJZAPI UninjectDll(const TCHAR* ptszDllFile, HANDLE hProc, bool mute = false)
{
// 参数无效
if (NULL == ptszDllFile || 0 == lstrlen(ptszDllFile))
return false;
HANDLE hModuleSnap = INVALID_HANDLE_VALUE;
HANDLE hProcess = hProc;
HANDLE hThread = NULL;
DWORD pid;
pid = GetProcessId(hProc);
// 获取模块快照
hModuleSnap = CreateToolhelp32Snapshot(TH32CS_SNAPMODULE, pid);
if (INVALID_HANDLE_VALUE == hModuleSnap)
{
if (!mute)
ErrMessage("获取模块快照时遭到失败");
return false;
}
MODULEENTRY32 me32;
memset(&me32, 0, sizeof(MODULEENTRY32));
me32.dwSize = sizeof(MODULEENTRY32);
// 开始遍历
if (FALSE == Module32First(hModuleSnap, &me32))
{
if (!mute)
ErrMessage("没有模块或是模块遍历遭到错误");
CloseHandle(hModuleSnap);
return false;
}
// 遍历查找指定模块
bool isFound = false;
do
{
isFound = (0 == _stricmp(me32.szModule, ptszDllFile) || 0 == _stricmp(me32.szExePath, ptszDllFile));
if (isFound) // 找到指定模块
{
break;
}
} while (TRUE == Module32Next(hModuleSnap, &me32));
CloseHandle(hModuleSnap);
if (false == isFound)
{
if (!mute)
ErrMessage("模块未找到");
return false;
}
// 从 Kernel32.dll 中获取 FreeLibrary 函数地址
LPTHREAD_START_ROUTINE lpThreadFun = (PTHREAD_START_ROUTINE)GetProcAddress(GetModuleHandle("Kernel32"), "FreeLibrary");
if (NULL == lpThreadFun)
{
if (!mute)
ErrMessage("无法从 Kernel32.dll 取函数 FreeLibrary");
CloseHandle(hProcess);
return false;
}
// 创建远程线程调用 FreeLibrary
hThread = CreateRemoteThread(hProcess, NULL, 0, lpThreadFun, me32.modBaseAddr /* 模块地址 */, 0, NULL);
if (NULL == hThread)
{
if (!mute)
ErrMessage("远程线程句柄为空");
CloseHandle(hProcess);
return false;
}
// 等待远程线程结束
WaitForSingleObject(hThread, INFINITE);
// 清理
CloseHandle(hThread);
CloseHandle(hProcess);
return true;
}
void UninjectDllFromAllProcesses(const char* dllpath)
{
if(!dllpath || !ExistFile(dllpath))
return;
HANDLE hSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
if (INVALID_HANDLE_VALUE == hSnapshot)
{
ErrMessage("无法创建进程快照");
return;
}
PROCESSENTRY32 pe = { sizeof(pe) };
BOOL fOk;
for (fOk = Process32First(hSnapshot, &pe); fOk; fOk = Process32Next(hSnapshot, &pe))
{
HANDLE hProc = GetProcessHandle(pe.th32ProcessID);
if (hProc && hProc != INVALID_HANDLE_VALUE)
{
if (UninjectDll(dllpath, hProc, true))
AddMessage("成功从 " + str(pe.szExeFile) + "中卸载 " + dllpath);
}
}
CloseHandle(hSnapshot);
}
#define WVC_AMP 12
#define WVC_OMEGA 13.0
#define WVC_PHASE0 0
clock_t lastWvBeg=0;
inline COLORREF WaveColor(COLORREF originClr, float phi=0.0f) { //originClr将成为最大值
//闪烁的颜色 赋予游戏文字灵性
short val = WVC_AMP * sin(WVC_OMEGA*((clock()-lastWvBeg)/1000.0)+WVC_PHASE0+phi) - WVC_AMP*2;
short r=GetRValue(originClr)+val,g=GetGValue(originClr)+val,b=GetBValue(originClr)+val;
ClampA<short>(r,2,255);
ClampA<short>(g,2,255);
ClampA<short>(b,2,255);
return RGB(r,g,b);
}
// 辅助函数:RGB到HSV颜色转换
void RGBtoHSV(COLORREF rgb, double& h, double& s, double& v) {
double r = GetRValue(rgb) / 255.0;
double g = GetGValue(rgb) / 255.0;
double b = GetBValue(rgb) / 255.0;
double minVal = std::min(std::min(r, g), b);
double maxVal = std::max(std::max(r, g), b);
double delta = maxVal - minVal;
// 计算色相
if (delta > 0) {
if (maxVal == r)
h = 60.0 * fmod((g - b) / delta, 6.0);
else if (maxVal == g)
h = 60.0 * ((b - r) / delta + 2.0);
else if (maxVal == b)
h = 60.0 * ((r - g) / delta + 4.0);
} else {
h = 0.0;
}
// 计算饱和度和亮度
s = (maxVal > 0) ? (delta / maxVal) : 0.0;
v = maxVal;
}
// 辅助函数:HSV到RGB颜色转换
COLORREF HSVtoRGB(double h, double s, double v) {
int hi = static_cast<int>(floor(h / 60.0)) % 6;
double f = h / 60.0 - floor(h / 60.0);
double p = v * (1.0 - s);
double q = v * (1.0 - f * s);
double t = v * (1.0 - (1.0 - f) * s);
switch (hi) {
case 0: return RGB(static_cast<int>(v * 255), static_cast<int>(t * 255), static_cast<int>(p * 255));
case 1: return RGB(static_cast<int>(q * 255), static_cast<int>(v * 255), static_cast<int>(p * 255));
case 2: return RGB(static_cast<int>(p * 255), static_cast<int>(v * 255), static_cast<int>(t * 255));
case 3: return RGB(static_cast<int>(p * 255), static_cast<int>(q * 255), static_cast<int>(v * 255));
case 4: return RGB(static_cast<int>(t * 255), static_cast<int>(p * 255), static_cast<int>(v * 255));
case 5: return RGB(static_cast<int>(v * 255), static_cast<int>(p * 255), static_cast<int>(q * 255));
default: return RGB(0, 0, 0); // Shouldn't reach here
}
}
// 主函数:返回随时间变化的彩虹色
COLORREF RainbowColor(float phi = 0.0f) {
//phi: 0.0~1.0
// 假设时间按秒计算,这里使用系统时间或其他适当的时间源
double timeInSeconds = (GetTickCount()) / 1000.0 + 12.0 * phi;
// 色相按时间变化
double hue = fmod(timeInSeconds * 30.0, 360.0); // 假设每秒变化30度
// 饱和度和亮度保持不变
double saturation = 1.0;
double value = 1.0;
// 将HSV颜色转换为RGB并返回
return HSVtoRGB(hue, saturation, value);
}
COLORREF RainbowColorQuick(float phi = 0.0f) {
// 假设时间按秒计算,这里使用系统时间或其他适当的时间源
double timeInSeconds = GetTickCount() / 1000.0 + 6.0 * phi;
// 色相按时间变化
double hue = fmod(timeInSeconds * 60.0, 360.0); // 假设每秒变化30度
// 饱和度和亮度保持不变
double saturation = 1.0;
double value = 1.0;
// 将HSV颜色转换为RGB并返回
return HSVtoRGB(hue, saturation, value);
}
COLORREF CJZAPI StepColor(COLORREF startColor, COLORREF endColor, double rate)
{
if(fequ(rate,0.0)) return startColor;
if(fequ(rate,1.0)) return endColor;
//颜色的渐变
int r = (GetRValue(endColor) - GetRValue(startColor));
int g = (GetGValue(endColor) - GetGValue(startColor));
int b = (GetBValue(endColor) - GetBValue(startColor));
int nSteps = max(abs(r), max(abs(g), abs(b)));
if (nSteps < 1) nSteps = 1;
// Calculate the step size for each color
float rStep = r / (float)nSteps;
float gStep = g / (float)nSteps;
float bStep = b / (float)nSteps;
// Reset the colors to the starting position
float fr = GetRValue(startColor);
float fg = GetGValue(startColor);
float fb = GetBValue(startColor);
COLORREF color;
for (int i = 0; i < int(nSteps * rate); i++) {
fr += rStep;
fg += gStep;
fb += bStep;
color = RGB((int)(fr + 0.5), (int)(fg + 0.5), (int)(fb + 0.5));
//color 即为重建颜色
}
return color;
}//from https://bbs.csdn.net/topics/240006897 , owner: zgl7903
inline COLORREF CJZAPI InvertedColor(COLORREF color)
{
return RGB(GetBValue(color), GetGValue(color), GetRValue(color));
}
//////////////////////////////////////////////////////////////////////////////
HDC hdcOrigin{nullptr};
HDC hdcBuffer{nullptr};
PAINTSTRUCT ps;
HFONT hFontText{nullptr};
HFONT hFontMsg{nullptr};
void ClearDevice(HDC _hdc = hdcBuffer, HWND _hwnd = hwnd)
{
// 清屏:使用透明色填充整个客户区域
RECT rcClient;
GetClientRect(_hwnd, &rcClient);
HBRUSH hBrush = CreateSolidBrush(RGB(0, 0, 0));
FillRect(_hdc, &rcClient, hBrush);
DeleteObject(hBrush);
}
void BeginDraw()
{
hdcOrigin = BeginPaint(hwnd, &ps);
hFontText = CreateFontA(TEXT_FS, 0, TEXT_FONTNAME);
hFontMsg = CreateFontA(MSG_FS, 0, MSG_FONTNAME);
SelectObject(hdcBuffer, hFontText);
SetBkMode(hdcBuffer, TRANSPARENT);
}
void EndDraw()
{
DeleteObject(hFontText);
DeleteObject(hFontMsg);
EndPaint(hwnd, &ps);
}
vector<pair<string, clock_t>> s_msgs{};
#define MSG_MAX_CNT ((scr_h - taskbar_h) / MSG_V_ADD)
void AddMessage(const string& s)
{
DebugLog(s);
s_msgs.push_back(make_pair(s, clock()));
if(s_msgs.size() > MSG_MAX_CNT)
s_msgs.erase(s_msgs.begin());
}
void ErrMessage(const string& s)
{
AddMessage("错误:" + s + " ( " + ConvertErrorCodeToString(GetLastError()) + " )");
}
const string s_cmdguides[] {
"[Alt+1] Uninstall 卸载",
"[Alt+2] Kill and Visit 诛并访其家",
"[Alt+3] Delete EXE 删除程序",
"[Alt+4] Force Exterminate 强制灭门",
"[Alt+5] Freeze 冻结",
"[Alt+6] Soul Detach 灵魂脱壳",
"[Alt+7] Visit 登门拜访",
};
void DrawUI()
{
DWORD pid{0L};
DWORD ppid{0L};
GetWindowThreadProcessId(hwnd_target, &pid);
ppid = GetParentProcessId(pid);
string procName {GetProcessName(pid)};
string pprocName {GetProcessName(ppid)};
SelectObject(hdcBuffer, hFontText);
SetBkMode(hdcBuffer, admin?OPAQUE:TRANSPARENT);
vector<string> strs
{
"Target Application 目标应用:",
procName + " ( " + str(pid) + " ) ",
"",
};
for(const auto& S : s_cmdguides)
strs.emplace_back(S);
strs.emplace_back("");
strs.emplace_back("Parent: " + pprocName + " ( " + str(ppid) + " ) ");
if (!admin)
strs.emplace_back("[Alt+A] Admin 提权");
int i{0};
for(const auto& s : strs)
{
if(s.empty())
{
++i;
continue;
}
if (admin && i != strs.size() - 1)
{
SetTextColor(hdcBuffer, RainbowColor(i * 0.1f));
SetBkColor(hdcBuffer, StepColor(InvertedColor(RainbowColor(i * 0.1f)), RGB(1,1,1), 0.8));
}else{
SetTextColor(hdcBuffer, StepColor(RainbowColor(i * 0.1f), RGB(255, 200, 200), 0.8));
}
TextOutA(hdcBuffer, TEXT_LEFT, TEXT_TOP + i * TEXT_V_ADD, s.c_str(),s.length());
++i;
}
if(!s_msgs.empty())
{
SetBkColor(hdcBuffer, RGB(1, 1, 1));
SelectObject(hdcBuffer, hFontMsg);
int j{0};
for(auto const& _msg : s_msgs)
{
if(clock() - _msg.second >= MSG_SHOWN_TIME)
{
++j;
continue;
}
SetTextColor(hdcBuffer, StepColor(RGB(0, 255, 0), RGB(255, 0, 0), (clock() - _msg.second) / MSG_SHOWN_TIME));
RECT rt { .left = 0,
.top = scr_h - taskbar_h - MSG_V_ADD * s_msgs.size() + MSG_V_ADD * j,
.right = scr_w,
.bottom = scr_h - taskbar_h + MSG_FS - MSG_V_ADD * s_msgs.size() + MSG_V_ADD * j,};
DrawTextA(hdcBuffer, _msg.first.c_str(), _msg.first.length(), &rt, DT_CENTER);
++j;
}
}
// else{
// const string _msg{"-/-"};
// SetTextColor(hdcBuffer, RGB(255, 0, 0));
// RECT rt { .left = 0,.top = scr_h - taskbar_h - 100, .right = scr_w, .bottom = scr_h - taskbar_h - 60,};
// DrawTextA(hdcBuffer, _msg.c_str(), _msg.length(), &rt, DT_CENTER);
// }
}
void DoUninstall()
{
DWORD pid{0L};
BOOL bRet = GetWindowThreadProcessId(hwnd_target, &pid);
if(!bRet)
{
ErrMessage("无法获取 PID");
return;
}
// HANDLE hProc = GetProcessHandle(pid);
// if(!hProc || hProc == INVALID_HANDLE_VALUE)
// {
// ErrMessage("无法获取进程句柄");
// return;
// }
string path = GetProcessPath2(pid);
if(path.empty())
{
ErrMessage("无法获取进程家的路径");
}
string dirpath {GetFileDirectory(path)};
AddMessage("目录路径:" + dirpath);
vector<string> uninsts = GetDirFilesR(dirpath, "uninst*.exe");
if(uninsts.empty())
{
AddMessage("错误:没有发现任何卸载程序!可使用灭门应对之");
return;
}
if(uninsts.size() > 1)
AddMessage("有多项程序符合条件,将选择第一个" + uninsts.at(0));
else
AddMessage("唯一符合程序:" + uninsts.at(0));
AdminRun((dirpath + uninsts.at(0)).c_str());
Sleep(500);
if(HaveProcess(uninsts.at(0).c_str()))
AddMessage("卸载程序已经开始运行");
else
ErrMessage("卸载程序运行失败");
}
void DoKillAndVisit()
{
DWORD pid{0L};
BOOL bRet = GetWindowThreadProcessId(hwnd_target, &pid);
if(!bRet)
{
ErrMessage("无法获取 PID");
return;
}
HANDLE hProc = GetProcessHandle(pid);
if(!hProc || hProc == INVALID_HANDLE_VALUE)
{
ErrMessage("无法获取进程句柄");
return;
}
string path = GetProcessPath2(pid);
if(path.empty())
{
ErrMessage("无法获取进程家的路径");
}
bRet = TerminateProcess(hProc, 0);
if(!bRet)
{
ErrMessage("终止进程失败");
return;
}
AddMessage("终止进程 Pid=" + str(pid) + " hProc=" + str(reinterpret_cast<ADDRESS>(hProc)));
if(path.empty())
return;
if(!ExistFile(path))
{
ErrMessage("路径无效:" + path);
return;
}
// AddMessage(path);
string dirpath {strxtail(GetFileDirectory(path))};
WinExec(("explorer.exe \"" + dirpath + "\"").c_str(), SW_SHOWMAXIMIZED);
AddMessage("拜访:" + dirpath);
}
void DoDeleteExe()
{
DWORD pid{0L};
BOOL bRet = GetWindowThreadProcessId(hwnd_target, &pid);
if(!bRet)
{