-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathexceptions.cpp
More file actions
153 lines (122 loc) · 4.83 KB
/
Copy pathexceptions.cpp
File metadata and controls
153 lines (122 loc) · 4.83 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
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
#include <asmjit/core/operand.h>
#include <asmjit/x86/x86operand.h>
#define PHNT_VERSION PHNT_WIN10_22H2
#include <phnt_windows.h>
#include <phnt.h>
#include <ntexapi.h>
#include <ntpsapi.h>
#include <minidumpapiset.h>
#include <TlHelp32.h>
#include <mmeapi.h>
#include <stdio.h>
#include <intrin.h>
#include <asmjit/core/jitruntime.h>
#include <asmjit/x86/x86assembler.h>
#include "utils.h"
#include "systemhooks.h"
#include "arxan.h"
#include "exceptions.h"
#include "syscalls.h"
HANDLE exceptionHandle = nullptr;
std::vector<int> syscalls;
#pragma optimize("", off)
LONG WINAPI exceptionHandler(const LPEXCEPTION_POINTERS info)
{
if (info->ExceptionRecord->ExceptionCode == EXCEPTION_BREAKPOINT)
{
return EXCEPTION_CONTINUE_EXECUTION;
}
if (info->ExceptionRecord->ExceptionCode == STATUS_INVALID_HANDLE)
{
return EXCEPTION_CONTINUE_EXECUTION;
}
if (info->ExceptionRecord->ExceptionCode == EXCEPTION_SINGLE_STEP)
{
uint64_t baseAddr = reinterpret_cast<uint64_t>(GetModuleHandle(nullptr));
uint64_t exceptionAddr = (uint64_t)info->ExceptionRecord->ExceptionAddress;
uint64_t idaExceptionAddr = exceptionAddr - baseAddr + StartOfTextSection - 0x1000;
uint64_t returnAddr = *(uint64_t*)info->ContextRecord->Rsp;
returnAddr = returnAddr - baseAddr + StartOfTextSection - 0x1000;
if (info->ContextRecord->Dr6 & 0x1)
{
// get size of image from codcw
uint64_t baseAddressStart = (uint64_t)GetModuleHandle(nullptr);
IMAGE_DOS_HEADER* pDOSHeader = (IMAGE_DOS_HEADER*)GetModuleHandle(nullptr);
IMAGE_NT_HEADERS* pNTHeaders =(IMAGE_NT_HEADERS*)((BYTE*)pDOSHeader + pDOSHeader->e_lfanew);
auto sizeOfImage = pNTHeaders->OptionalHeader.SizeOfImage;
uint64_t baseAddressEnd = baseAddressStart + sizeOfImage;
static int counter = 0;
counter++;
printf("bp1: %llx %llx %d\n", exceptionAddr, idaExceptionAddr, counter);
fprintf(logFile, "bp1: %llx %llx %d\n", exceptionAddr, idaExceptionAddr, counter);
fflush(logFile);
printf("loc %llx\n", info->ContextRecord->Rax);
}
if (info->ContextRecord->Dr6 & 0x2)
{
// get size of image from codcw
uint64_t baseAddressStart = (uint64_t)GetModuleHandle(nullptr);
IMAGE_DOS_HEADER* pDOSHeader = (IMAGE_DOS_HEADER*)GetModuleHandle(nullptr);
IMAGE_NT_HEADERS* pNTHeaders =(IMAGE_NT_HEADERS*)((BYTE*)pDOSHeader + pDOSHeader->e_lfanew);
auto sizeOfImage = pNTHeaders->OptionalHeader.SizeOfImage;
uint64_t baseAddressEnd = baseAddressStart + sizeOfImage;
static int counter = 0;
counter++;
printf("bp2: %llx %llx %d\n", exceptionAddr, idaExceptionAddr, counter);
fprintf(logFile, "bp2: %llx %llx %d\n", exceptionAddr, idaExceptionAddr, counter);
fflush(logFile);
printf("loc %llx\n", info->ContextRecord->Rax);
}
if (info->ContextRecord->Dr6 & 0x4)
{
// get size of image from codcw
uint64_t baseAddressStart = (uint64_t)GetModuleHandle(nullptr);
IMAGE_DOS_HEADER* pDOSHeader = (IMAGE_DOS_HEADER*)GetModuleHandle(nullptr);
IMAGE_NT_HEADERS* pNTHeaders =(IMAGE_NT_HEADERS*)((BYTE*)pDOSHeader + pDOSHeader->e_lfanew);
auto sizeOfImage = pNTHeaders->OptionalHeader.SizeOfImage;
uint64_t baseAddressEnd = baseAddressStart + sizeOfImage;
static int counter = 0;
counter++;
printf("bp3: %llx %llx %d\n", exceptionAddr, idaExceptionAddr, counter);
fprintf(logFile, "bp3: %llx %llx %d\n", exceptionAddr, idaExceptionAddr, counter);
fflush(logFile);
printf("loc %llx\n", info->ContextRecord->Rax);
}
if (info->ContextRecord->Dr6 & 0x8)
{
if (info->ContextRecord->Rax != AllocateVirtualMemorySysCall)
{
//printf("syscall %llx\n", info->ContextRecord->Rax);
info->ContextRecord->Rip = (uint64_t)ntdllAsmStubLocation;
}
}
info->ContextRecord->EFlags |= ResumeFlag;
return EXCEPTION_CONTINUE_EXECUTION;
}
if (info->ExceptionRecord->ExceptionCode == 0xc0000005)
{
static int access_violation_counter = 0;
access_violation_counter++;
if (access_violation_counter == 2)
{
printf("something exploded: %llx\n", info->ExceptionRecord->ExceptionCode);
fprintf(logFile, "something exploded: %llx\n", info->ExceptionRecord->ExceptionCode);
fflush(logFile);
HANDLE hFile = CreateFile("dump.dmp", GENERIC_READ | GENERIC_WRITE, 0, nullptr, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, nullptr);
MINIDUMP_EXCEPTION_INFORMATION minidump_exception_info = {GetCurrentThreadId(), info, FALSE};
constexpr auto type = MiniDumpIgnoreInaccessibleMemory //
| MiniDumpWithHandleData //
| MiniDumpScanMemory //
| MiniDumpWithProcessThreadData //
| MiniDumpWithFullMemoryInfo //
| MiniDumpWithThreadInfo //
| MiniDumpWithUnloadedModules;
MiniDumpWriteDump(GetCurrentProcess(), GetCurrentProcessId(), hFile, (MINIDUMP_TYPE)type, &minidump_exception_info, nullptr, nullptr);
CloseHandle(hFile);
SuspendAllThreads();
__debugbreak();
}
}
return EXCEPTION_CONTINUE_SEARCH;
}
#pragma optimize("", on)