-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathSyringeDebugger.h
196 lines (156 loc) · 4.44 KB
/
SyringeDebugger.h
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
#pragma once
#define WIN32_LEAN_AND_MEAN
// WIN32_FAT_AND_STUPID
#include "CRC32.h"
#include "PortableExecutable.h"
#include <cstring>
#include <iostream>
#include <map>
#include <optional>
#include <string_view>
#include <windows.h>
class SyringeDebugger
{
static constexpr size_t MaxNameLength = 0x100u;
static constexpr BYTE INIT = 0x00;
static constexpr BYTE INT3 = 0xCC; // trap to debugger interrupt opcode.
static constexpr BYTE NOP = 0x90;
public:
SyringeDebugger(std::string_view filename)
: exe(filename)
{
RetrieveInfo();
}
// debugger
void Run(std::string_view arguments);
DWORD HandleException(DEBUG_EVENT const& dbgEvent);
// breakpoints
bool SetBP(void* address);
void RemoveBP(LPVOID address, bool restoreOpcode);
// memory
VirtualMemoryHandle AllocMem(void* address, size_t size);
bool PatchMem(void* address, void const* buffer, DWORD size);
bool ReadMem(void const* address, void* buffer, DWORD size);
// syringe
void FindDLLs();
private:
void RetrieveInfo();
void DebugProcess(std::string_view arguments);
// helper Functions
static DWORD __fastcall RelativeOffset(void const* from, void const* to);
template<typename T>
static void ApplyPatch(void* ptr, T&& data) noexcept
{
std::memcpy(ptr, &data, sizeof(data));
}
// thread info
struct ThreadInfo
{
ThreadInfo() = default;
ThreadInfo(HANDLE hThread) noexcept
: Thread{hThread}
{ }
ThreadHandle Thread;
LPVOID lastBP{ nullptr };
};
std::map<DWORD, ThreadInfo> Threads;
// process info
PROCESS_INFORMATION pInfo;
// flags
bool bEntryBP{ true };
// breakpoints
struct Hook
{
char lib[MaxNameLength];
char proc[MaxNameLength];
void* proc_address;
size_t num_overridden;
};
struct BreakpointInfo
{
BYTE original_opcode{ 0x0u };
std::vector<Hook> hooks;
VirtualMemoryHandle p_caller_code;
};
std::map<void*, BreakpointInfo> Breakpoints;
std::vector<Hook*> v_AllHooks;
std::vector<Hook*>::iterator loop_LoadLibrary;
// syringe
std::string exe;
void* pcEntryPoint{ nullptr };
void* pImLoadLibrary{ nullptr };
void* pImGetProcAddress{ nullptr };
VirtualMemoryHandle pAlloc;
DWORD dwTimeStamp{ 0u };
DWORD dwExeSize{ 0u };
DWORD dwExeCRC{ 0u };
bool bDLLsLoaded{ false };
bool bHooksCreated{ false };
bool bAVLogged{ false };
// data addresses
struct AllocData {
static constexpr auto CodeSize = 0x40u;
std::byte LoadLibraryFunc[CodeSize];
void* ProcAddress;
void* ReturnEIP;
char LibName[MaxNameLength];
char ProcName[MaxNameLength];
};
AllocData* GetData() const noexcept {
return reinterpret_cast<AllocData*>(pAlloc.get());
};
struct HookBuffer {
std::map<void*, std::vector<Hook>> hooks;
CRC32 checksum;
size_t count{ 0 };
void add(void* const eip, Hook const& hook) {
auto& h = hooks[eip];
h.push_back(hook);
checksum.compute(&eip, sizeof(eip));
checksum.compute(&hook.num_overridden, sizeof(hook.num_overridden));
count++;
}
void add(
void* const eip, std::string_view const filename,
std::string_view const proc, size_t const num_overridden)
{
Hook hook;
hook.lib[filename.copy(hook.lib, std::size(hook.lib) - 1)] = '\0';
hook.proc[proc.copy(hook.proc, std::size(hook.proc) - 1)] = '\0';
hook.proc_address = nullptr;
hook.num_overridden = num_overridden;
add(eip, hook);
}
};
bool ParseInjFileHooks(std::string_view lib, HookBuffer& hooks);
bool CanHostDLL(PortableExecutable const& DLL, IMAGE_SECTION_HEADER const& hosts) const;
bool ParseHooksSection(PortableExecutable const& DLL, IMAGE_SECTION_HEADER const& hooks, HookBuffer& buffer);
std::optional<bool> Handshake(char const* lib, int hooks, unsigned int crc);
};
// disable "structures padded due to alignment specifier"
#pragma warning(push)
#pragma warning(disable : 4324)
struct alignas(16) hookdecl {
unsigned int hookAddr;
unsigned int hookSize;
DWORD hookNamePtr;
};
struct alignas(16) hostdecl {
unsigned int hostChecksum;
DWORD hostNamePtr;
};
static_assert(sizeof(hookdecl) == 16);
static_assert(sizeof(hostdecl) == 16);
#pragma warning(pop)
struct SyringeHandshakeInfo
{
int cbSize;
int num_hooks;
unsigned int checksum;
DWORD exeFilesize;
DWORD exeTimestamp;
unsigned int exeCRC;
int cchMessage;
char* Message;
};
using SYRINGEHANDSHAKEFUNC = HRESULT(__cdecl *)(SyringeHandshakeInfo*);