forked from Fatmike-GH/PELoader
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
106 lines (84 loc) · 3.39 KB
/
Copy pathmain.cpp
File metadata and controls
106 lines (84 loc) · 3.39 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
#include <Windows.h>
#include <string>
#include <iostream>
void NTAPI TlsCallback(PVOID hModule, DWORD dwReason, PVOID pContext);
DWORD WINAPI ThreadFunction(LPVOID lpParam);
#ifdef _WIN64
#pragma comment (linker, "/INCLUDE:_tls_used")
#pragma comment (linker, "/INCLUDE:tls_callback_func")
#pragma const_seg(".CRT$XLB")
EXTERN_C const PIMAGE_TLS_CALLBACK tls_callback_func = (PIMAGE_TLS_CALLBACK)TlsCallback;
#pragma const_seg()
extern __declspec(thread) UINT64 _tlsVariable = 10;
#else
#pragma comment (linker, "/INCLUDE:__tls_used")
#pragma comment (linker, "/INCLUDE:_tls_callback_func")
#pragma data_seg(".CRT$XLB")
EXTERN_C PIMAGE_TLS_CALLBACK tls_callback_func = (PIMAGE_TLS_CALLBACK)TlsCallback;
#pragma data_seg()
extern __declspec(thread) UINT64 _tlsVariable = 10;
#endif
//#define DLL_PROCESS_ATTACH 1
//#define DLL_THREAD_ATTACH 2
//#define DLL_THREAD_DETACH 3
//#define DLL_PROCESS_DETACH 0
void NTAPI TlsCallback(PVOID hModule, DWORD dwReason, PVOID pContext)
{
if (dwReason == DLL_PROCESS_ATTACH)
{
std::cout << std::endl;
std::cout << "TlsCallback: DLL_PROCESS_ATTACH : Value of _tlsVariable is: " << std::to_string(_tlsVariable) << " (expected value: 10)" << std::endl;
std::cout << "Setting _tlsVariable to 111" << std::endl;
_tlsVariable = 111;
std::cout << std::endl;
}
else if (dwReason == DLL_PROCESS_DETACH)
{
return;
}
else if (dwReason == DLL_THREAD_ATTACH)
{
std::cout << std::endl;
std::cout << "TlsCallback: DLL_THREAD_ATTACH : Value of _tlsVariable is: " << std::to_string(_tlsVariable) << " (expected value: 10)" << std::endl;
std::cout << "Setting _tlsVariable to 333" << std::endl;
_tlsVariable = 333;
std::cout << std::endl;
}
else if (dwReason == DLL_THREAD_DETACH)
{
std::cout << std::endl;
std::cout << "TlsCallback: DLL_THREAD_DETACH : Value of _tlsVariable is : " << std::to_string(_tlsVariable) << " (expected value : 444)" << std::endl;
std::cout << std::endl;
}
}
DWORD WINAPI ThreadFunction(LPVOID lpParam)
{
std::cout << "New thread: Value of _tlsVariable is: " << std::to_string(_tlsVariable) << " (expected value: 333)" << std::endl;
std::cout << "New thread: Setting _tlsVariable to 444" << std::endl;
_tlsVariable = 444;
std::cout << "New thread: Value of _tlsVariable is: " << std::to_string(_tlsVariable) << " (expected value: 444)" << std::endl;
return 0;
}
int main()
{
std::cout << "Main thread: Value of _tlsVariable is: " << std::to_string(_tlsVariable) << " (expected value: 111)" << std::endl;
std::cout << "Main thread: Setting _tlsVariable to 222" << std::endl;
_tlsVariable = 222;
std::cout << "Main thread: Value of _tlsVariable is: " << std::to_string(_tlsVariable) << " (expected value: 222)" << std::endl;
std::cout << "Main thread: Starting thread..." << std::endl;
DWORD threadId = 0;
HANDLE threadHandle = CreateThread(NULL, 0, ThreadFunction, nullptr, 0, &threadId);
if (threadHandle == NULL)
{
std::cout << "Thread creation failed." << std::endl;
return 0;
}
WaitForSingleObject(threadHandle, INFINITE);
CloseHandle(threadHandle);
std::cout << "Main thread: Thread has finished execution." << std::endl;
std::cout << "Main thread: Value of _tlsVariable is: " << std::to_string(_tlsVariable) << " (expected value: 222)" << std::endl;
std::cout << std::endl;
std::cout << "Press a key to exit...";
std::cin.get();
return 0;
}