1- // dllmain.cpp : Defines the entry point for the DLL application.
21#include < cstdio>
3- #include < exception>
42#include < Windows.h>
5- #include < string>
6- #include < stdexcept>
73
84#pragma comment(lib,"ws2_32.lib")
95#pragma comment(lib,"wintrust.lib")
106#pragma comment(lib,"ntdll.lib")
117
12- typedef NTSTATUS (NTAPI* PUSER_THREAD_START_ROUTINE)(_In_ PVOID ThreadParameter);
13-
14- #define NtCurrentProcess () (HANDLE)-1
15-
16- #ifdef _WIN64
17- #define NtCurrentThreadLocalStoragePointer () *(LPVOID*)(LPBYTE(NtCurrentTeb()) + 0x58 )
18- #else
19- #define NtCurrentThreadLocalStoragePointer () *(LPVOID*)(LPBYTE(NtCurrentTeb()) + 0x2C )
20- #endif
21-
22- typedef struct _CLIENT_ID {
23- VOID* UniqueProcess;
24- VOID* UniqueThread;
25- }CLIENT_ID, * PCLIENT_ID;
26-
27- extern " C"
28- NTSYSAPI
29- NTSTATUS
30- NTAPI
31- RtlCreateUserThread (
32- _In_ HANDLE Process,
33- _In_opt_ PSECURITY_DESCRIPTOR ThreadSecurityDescriptor,
34- _In_ BOOLEAN CreateSuspended,
35- _In_opt_ ULONG ZeroBits,
36- _In_opt_ SIZE_T MaximumStackSize,
37- _In_opt_ SIZE_T CommittedStackSize,
38- _In_ PUSER_THREAD_START_ROUTINE StartAddress,
39- _In_opt_ PVOID Parameter,
40- _Out_opt_ PHANDLE Thread,
41- _Out_opt_ PCLIENT_ID ClientId
42- );
43-
448BOOL APIENTRY DllMain ( HMODULE hModule,
459 DWORD ul_reason_for_call,
4610 LPVOID lpReserved
@@ -60,131 +24,7 @@ BOOL APIENTRY DllMain( HMODULE hModule,
6024 return TRUE ;
6125}
6226
63- /*
64- exception type
65- 0 int
66- 1 char
67- 2 std::exception
68- ... DWORD64
69- */
70-
71- int exception (int exception_type) {
72- // int a = 0;
73- // __try {
74- // *(PDWORD)(nullptr) = -1;
75- // a = 2;
76- // }
77- // __except (EXCEPTION_EXECUTE_HANDLER) {
78- // printf("-----------\n");
79- // getchar();
80- // a = 1;
81- // }
82- try {
83- switch (exception_type) {
84- case 0 :
85- throw 0 ;
86- case 1 :
87- throw ' 1' ;
88- case 2 :
89- throw std::exception (" 2" );
90- case 3 :
91- {
92- std::string s = " foo" ;
93- s.at (10 );
94- }
95- default :
96- throw (DWORD64)-1 ;
97- }
98- return 0 ;
99- }
100- catch (int val) {
101- printf (" exception code = %d\n " , val);
102- return val;
103- }
104- catch (char val) {
105- printf (" exception code = %c\n " , val);
106- return val - ' 0' ;
107- }
108- catch (const std::out_of_range& e) {
109- printf (" %s\n " , e.what ());
110- return 3 ;
111- }
112- catch (std::exception val) {
113- printf (" exception code = %s\n " , val.what ());
114- return 2 ;
115- }
116- catch (...) {
117- printf (" exception catched!!\n " );
118- return 0 ;
119- }
120- // return a;
121- }
122-
12327int __test__ () {
12428 printf (" HelloWorld!\n " );
12529 return 0 ;
12630}
127-
128- static thread_local int x = 0xffccffdd ;
129- NTSTATUS WINAPI Thread (PVOID) {
130- printf (" [1] ThreadLocalStoragePointer = %p\n " , NtCurrentThreadLocalStoragePointer ());
131- return x == 0xffccffdd ? 0 : 1 ;
132- }
133-
134- int thread () {
135- x = 2 ;
136- printf (" [0] ThreadLocalStoragePointer = %p\n " , NtCurrentThreadLocalStoragePointer ());
137- HANDLE hThread;// = CreateThread(nullptr, 0, Thread, nullptr, 0, nullptr);
138- RtlCreateUserThread (NtCurrentProcess (), nullptr , FALSE , 0 , 0 , 0 , Thread, nullptr , &hThread, nullptr );
139- DWORD ret = -1 ;
140- if (hThread) {
141- WaitForSingleObject (hThread, 0xffffffff );
142- GetExitCodeThread (hThread, &ret);
143- CloseHandle (hThread);
144- return ret;
145- }
146- return -1 ;
147- }
148-
149- DWORD Value;
150- volatile LPDWORD lpAddr;
151-
152- LONG WINAPI Filter (_In_ struct _EXCEPTION_POINTERS * ExceptionInfo) {
153-
154- if (ExceptionInfo->ExceptionRecord ->ExceptionCode == STATUS_ACCESS_VIOLATION) {
155-
156- lpAddr = &Value;
157-
158- // +++++++
159- // begin compiler specific
160- // +++++++
161-
162- // ExceptionInfo->ContextRecord->Rip -= 7;
163- ExceptionInfo->ContextRecord ->Rax = (ULONG_PTR)lpAddr;
164-
165- // +++++++
166- // end compiler specific
167- // +++++++
168-
169- return EXCEPTION_CONTINUE_EXECUTION;
170- }
171-
172- return EXCEPTION_CONTINUE_SEARCH;
173- }
174-
175- int unhandled_exception () {
176- auto filter = SetUnhandledExceptionFilter (Filter);
177- auto ff = SetUnhandledExceptionFilter (filter);
178-
179- if (ff != Filter) {
180- printf (" %p\t %p\t %p\n failed\n " , filter, ff, Filter);
181- return 0 ;
182- }
183-
184- filter = SetUnhandledExceptionFilter (Filter);
185- lpAddr = nullptr ;
186- *lpAddr = 1 ;
187- SetUnhandledExceptionFilter (filter);
188-
189- return 1234 ;
190- }
0 commit comments