-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.cpp
More file actions
126 lines (105 loc) · 3.14 KB
/
main.cpp
File metadata and controls
126 lines (105 loc) · 3.14 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
#include "shared.h"
#include "FlashpointProxy.h"
#include <windows.h>
#include <winnt.h>
#include <process.h>
CONTEXT originalMainThreadContext = {};
// just to be safe, this is in it's own function
// I want this exception handler to be cleaned up
// before the call to setCurrentThreadContext
bool __declspec(noinline) enableFlashpointProxyNoExcept() noexcept {
try {
if (!FlashpointProxy::enable()) {
return false;
}
} catch (...) {
return false;
}
return true;
}
void entryPoint() {
// the thread hasn't set up vectored exception handling at this stage
if (!enableFlashpointProxyNoExcept()) {
showLastError("Failed to Enable Flashpoint Proxy");
terminateCurrentProcess();
return;
}
setCurrentThreadContext(originalMainThreadContext);
}
unsigned int __stdcall dynamicThread(void* argList) {
// do not create any C++ objects here
// (unwinding interferes with _endthreadex)
if (!FlashpointProxy::enable()) {
showLastError("Failed to Enable Flashpoint Proxy");
terminateCurrentProcess();
_endthreadex(1);
return 1;
}
_endthreadex(0);
return 0;
}
bool dynamicLink() {
if (!FlashpointProxy::enable()) {
if (GetLastError() != ERROR_POSSIBLE_DEADLOCK) {
showLastError("Failed to Enable Flashpoint Proxy");
return false;
}
bool result = true;
// deadlock fallback
// nothing we can do except begin a thread
// and hope it finishes before WinInet is used
HANDLE thread = (HANDLE)_beginthreadex(NULL, 0, dynamicThread, NULL, 0, NULL);
SCOPE_EXIT {
if (!closeThread(thread)) {
showLastError("Failed to Close Thread");
result = false;
}
};
if (!thread || thread == INVALID_HANDLE_VALUE) {
showLastError("Failed to Begin Thread");
return false;
}
return result;
}
return true;
}
extern "C" BOOL WINAPI DllMain(HINSTANCE instance, DWORD reason, PCONTEXT contextPointer) {
if (reason == DLL_PROCESS_ATTACH) {
MAKE_SCOPE_EXIT(terminateCurrentProcessScopeExit) {
terminateCurrentProcess();
};
if (!DisableThreadLibraryCalls(instance)) {
showLastError("Failed to Disable Thread Library Calls");
return FALSE;
}
// check whether the context pointer actually pointing to anything
if (IS_INTRESOURCE(contextPointer)) {
// dynamic link
if (!dynamicLink()) {
showLastError("Failed to Dynamic Link");
return FALSE;
}
terminateCurrentProcessScopeExit.dismiss();
return TRUE;
}
// copy...
originalMainThreadContext = *contextPointer;
// set the new origin
DWORD_PTR origin = (DWORD_PTR)entryPoint;
// modify original...
#if defined _AMD64_ || defined _IA64_
contextPointer->Rip = origin;
#else
contextPointer->Eip = origin;
#endif
terminateCurrentProcessScopeExit.dismiss();
}
return TRUE;
}
/*
"The Lord said, 'Go out and stand on the mountain in the presence of the Lord, for the Lord is about to pass by.'
Then a great and powerful wind tore the mountains apart and shattered the rocks before the Lord, but the Lord was not in the wind.
After the wind there was an earthquake, but the Lord was not in the earthquake.
After the earthquake came a fire, but the Lord was not in the fire.
And after the fire came a gentle whisper." - 1 Kings 19:11-12
*/