1- #include < iostream>
2- #include < Windows.h>
3- #include < ios>
1+ #include " Threat.hpp"
42
5- // Should hijack target thread right here :)
6-
7- CONTEXT GetAndPrintContext (HANDLE hThread) {
8- CONTEXT context = { };
3+ Threat::Threat (const wchar_t *processName) : Process(processName) {
94 context.ContextFlags = CONTEXT_FULL ;
105
11- if (GetThreadContext (hThread, &context)) {
6+ HANDLE hSnapshot;
7+ THREADENTRY32 threadEntry = {};
8+ BOOL hResult;
129
10+ hSnapshot = CreateToolhelp32Snapshot (TH32CS_SNAPTHREAD , 0 );
11+ if (hSnapshot == INVALID_HANDLE_VALUE ) {
12+ std::cerr << " Failed to snapshot threads: " << GetLastError () << std::endl;
13+ return ;
1314 }
1415
15- return context;
16- }
16+ threadEntry.dwSize = sizeof (THREADENTRY32 );
17+ hResult = Thread32First (hSnapshot, &threadEntry);
18+
19+ while (hResult) {
20+ if (processId == threadEntry.th32OwnerProcessID ) {
21+ threadId = threadEntry.th32ThreadID ;
22+ break ;
23+ }
24+ hResult = Thread32Next (hSnapshot, &threadEntry);
25+ }
1726
18- int main () {
19- HANDLE hThread;
20- CONTEXT context;
27+ CloseHandle (hSnapshot);
2128
22- hThread = OpenThread (THREAD_ALL_ACCESS , FALSE , 6248 );
29+ hThread = OpenThread (THREAD_ALL_ACCESS , 0 , threadId );
2330 if (!hThread) {
24- std::cout << " Failed to open thread handle : " << GetLastError () << std::endl;
25- return 1 ;
31+ std::cerr << " [ ERROR ] OpenThread : " << GetLastError () << std::endl;
32+ return ;
2633 }
34+ }
2735
28- std::cout << " Successfully opened thread 0x" << hThread << std::endl;
36+ Threat::~Threat () {
37+ CloseHandle (hThread);
38+ }
2939
30- context = GetAndPrintContext (hThread);
40+ void Threat::DebugPrint () const {
41+ std::cout << " -------------------------" << std::endl;
42+ std::cout << " THIS IS A DEBUG PRINT ! " << std::endl;
43+ std::cout << " -------------------------" << std::endl;
44+ std::cout << " [ DEBUG ] processId: " << processId << std::endl;
45+ std::cout << " [ DEBUG ] threadId: " << threadId << std::endl;
46+ std::cout << " [ DEBUG ] context: 0x" << std::hex << &context << std::endl;
47+ std::cout << " [ DEBUG ] hThread: 0x" << std::hex << hThread << std::endl;
48+ }
3149
32- std::cout << " 0x7ffad8f3cca4" << std::endl;
50+ bool Threat::Hijack () {
51+ HMODULE hModules[1024 ];
52+ DWORD modulesCount;
3353
34- #ifdef _WIN64
35- std::cout << " RIP (Instruction Pointer): 0x" << std::hex << context.Rip << std::endl;
36- std::cout << " RSP (Stack Pointer): 0x" << std::hex << context.Rsp << std::endl;
37- #endif
54+ if (!EnumProcessModules (hProcess, hModules, sizeof (hModules), &modulesCount)) {
55+ std::cerr << " [ ERROR ] EnumProcessModules: " << GetLastError () << std::endl;
56+ CloseHandle (hProcess);
57+ return false ;
58+ }
3859
60+ HMODULE ntdll = nullptr ;
3961
40- if (SuspendThread (hThread) == -1 ) {
41- std::cout << " Failed to suspend thread : " << GetLastError () << std::endl;
42- return 1 ;
43- }
62+ for (size_t i = 0 ; i < (modulesCount / sizeof (HMODULE )); i++) {
63+ TCHAR path[MAX_PATH ];
64+
65+ if (!GetModuleFileNameEx (hProcess, hModules[i], path, sizeof (path) / sizeof (TCHAR ))) {
66+ std::cerr << " [ ERROR ] GetModuleFileNameA: " << GetLastError () << std::endl;
67+ return false ;
68+ }
4469
45- std::cout << " Sucessfully suspended thread" << std::endl;
70+ if (_tcsstr (path, L" ntdll.dll" ) == 0 ) {
71+ std::cout << " [ DEBUG ] ntdll found!" << std::endl;
72+ break ;
73+ }
4674
47- // Sleep(3000);
75+ MODULEINFO moduleInfos;
76+
77+ if (!K32GetModuleInformation (hProcess, hModules[i], &moduleInfos, sizeof (moduleInfos))) {
78+ std::cerr << " [ ERROR ] K32GetModuleInformation: " << GetLastError () << std::endl;
79+ return false ;
80+ }
4881
49- if (ResumeThread (hThread) == -1 ) {
50- std::cout << " Failed to resume thread : " << GetLastError () << std::endl;
51- return 1 ;
82+ std::cout << moduleInfos.SizeOfImage << std::endl;
83+
84+
85+ std::wcout << path << std::endl;
5286 }
87+
88+ while (1 ) {
89+ if (SuspendThread (hThread) == (DWORD ) -1 ) {
90+ std::cerr << " [ ERROR ] SuspendThread: " << GetLastError () << std::endl;
91+ return false ;
92+ }
93+
94+ std::cout << " [ DEBUG ] SuspendThread" << std::endl;
95+
96+ if (!GetThreadContext (hThread, &context)) {
97+ std::cerr << " [ ERROR ] GetThreadContext: " << GetLastError () << std::endl;
98+ if (ResumeThread (hThread) == (DWORD ) - 1 ) {
99+ std::cerr << " [ ERROR ] ResumeThread: " << GetLastError () << std::endl;
100+ }
101+ }
53102
54- std::cout << " Sucessfully resumed thread" << std::endl;
103+ Sleep (1000 );
104+ std::cout << " Resume in 3..." << std::endl;
105+ Sleep (1000 );
106+ std::cout << " Resume in 2..." << std::endl;
107+ Sleep (1000 );
108+ std::cout << " Resume in 1..." << std::endl;
109+
110+ if (ResumeThread (hThread) == (DWORD ) -1 ) {
111+ std::cerr << " [ ERROR ] ResumeThread: " << GetLastError () << std::endl;
112+ return false ;
113+ }
114+ std::cout << " [ DEBUG ] ResumeThread" << std::endl;
115+
116+ Sleep (5000 );
117+ }
55118}
0 commit comments