-
Notifications
You must be signed in to change notification settings - Fork 29
Expand file tree
/
Copy pathinit.cpp
More file actions
150 lines (120 loc) · 2.53 KB
/
init.cpp
File metadata and controls
150 lines (120 loc) · 2.53 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
#include "repl.h"
static BOOL winrepl_create_debuggee(shell_t *sh)
{
STARTUPINFO si = { 0 };
TCHAR fileName[MAX_PATH] = { 0 };
GetModuleFileName(NULL, fileName, MAX_PATH);
si.dwFlags = STARTF_USESHOWWINDOW;
si.wShowWindow = SW_HIDE; // already 0
si.cb = sizeof(si);
if (!CreateProcess(
fileName,
NULL,
NULL,
NULL,
FALSE,
DEBUG_ONLY_THIS_PROCESS,
NULL,
NULL,
&si,
&sh->procInfo
))
{
return FALSE;
}
// workaround for a bug on startup (Windows 8.1 x64), SetThreadContext would fail for some reason
CloseHandle(sh->procInfo.hThread);
if (!(sh->procInfo.hThread = OpenThread(
THREAD_SET_CONTEXT | THREAD_GET_CONTEXT | THREAD_QUERY_INFORMATION,
FALSE,
sh->procInfo.dwThreadId
)))
{
return FALSE;
}
// swallow initial debug events
while (TRUE)
{
DEBUG_EVENT dbg = { 0 };
if (!WaitForDebugEvent(&dbg, 1000))
break;
if (dbg.dwDebugEventCode == CREATE_PROCESS_DEBUG_EVENT)
CloseHandle(dbg.u.CreateProcessInfo.hFile);
if (dbg.dwDebugEventCode == LOAD_DLL_DEBUG_EVENT)
{
if (dbg.u.LoadDll.hFile)
CloseHandle(dbg.u.LoadDll.hFile);
}
if (dbg.dwDebugEventCode == EXCEPTION_DEBUG_EVENT &&
dbg.dwThreadId == sh->procInfo.dwThreadId)
break;
ContinueDebugEvent(dbg.dwProcessId, dbg.dwThreadId, DBG_CONTINUE);
}
return TRUE;
}
static BOOL winrepl_alloc_mem(shell_t *sh)
{
if (sh->nMemSize == 0)
sh->nMemSize = WINREPL_INIT_MEM_SIZE;
sh->lpStartAddress = VirtualAllocEx(
sh->procInfo.hProcess,
NULL,
sh->nMemSize,
MEM_COMMIT,
PAGE_EXECUTE_READ);
return sh->lpStartAddress != NULL;
}
static BOOL winrepl_reset_context(shell_t *sh)
{
CONTEXT ctx = { 0 };
ctx.ContextFlags = CONTEXT_ALL;
if (!GetThreadContext(sh->procInfo.hThread, &ctx))
return FALSE;
#ifdef _M_X64
ctx.Rip = (DWORD64)sh->lpStartAddress;
ctx.Rax = 0;
ctx.Rbx = 0;
ctx.Rcx = 0;
ctx.Rdx = 0;
ctx.Rsi = 0;
ctx.Rdi = 0;
ctx.Rbp = 0;
ctx.R8 = 0;
ctx.R9 = 0;
ctx.R10 = 0;
ctx.R11 = 0;
ctx.R12 = 0;
ctx.R13 = 0;
ctx.R14 = 0;
ctx.R15 = 0;
ctx.EFlags = 0;
#elif defined(_M_IX86)
ctx.Eip = (DWORD)sh->lpStartAddress;
ctx.Eax = 0;
ctx.Ebx = 0;
ctx.Ecx = 0;
ctx.Edx = 0;
ctx.Esi = 0;
ctx.Edi = 0;
ctx.Ebp = 0;
ctx.EFlags = 0;
#elif defined(_M_ARM)
// todo: ARM?
return FALSE;
#else
return FALSE;
#endif
sh->prev = ctx;
sh->curr = ctx;
return SetThreadContext(sh->procInfo.hThread, &ctx);
}
BOOL shelldev_init(shell_t *sh)
{
if (!winrepl_create_debuggee(sh))
return FALSE;
if (!winrepl_alloc_mem(sh))
return FALSE;
if (!winrepl_reset_context(sh))
return FALSE;
return TRUE;
}