This repository has been archived by the owner on Mar 21, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcommon.cpp
executable file
·291 lines (238 loc) · 8.29 KB
/
common.cpp
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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
/*
Copyright (c) 2016 Piotr Stolarz
asprext: ASProtect reverse engineering & analysis WinDbg extension
Distributed under the GNU General Public License (the License)
see accompanying file LICENSE for details.
This software is distributed WITHOUT ANY WARRANTY; without even the
implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the License for more information.
*/
#include "common.h"
#include <stdarg.h>
/*
TLS and dbg client related funcs
*/
static DWORD tls_i=TLS_OUT_OF_INDEXES;
void set_tls_i(DWORD tls_i) {
::tls_i=tls_i;
}
DWORD get_tls_i(void) {
return tls_i;
}
void set_client(PDEBUG_CLIENT4 Client) {
if (tls_i!=TLS_OUT_OF_INDEXES) TlsSetValue(tls_i, Client);
}
PDEBUG_CLIENT4 get_client(void) {
return (PDEBUG_CLIENT4)
(tls_i!=TLS_OUT_OF_INDEXES ? TlsGetValue(tls_i) : NULL);
}
static void vdbgprintf(
ULONG ctrl, ULONG mask, const char *pc_pref, const char *format, va_list args)
{
IDebugControl *DebugControl=NULL;
PDEBUG_CLIENT4 Client;
if (!(Client=get_client())) goto finish;
if (Client->QueryInterface(
__uuidof(IDebugControl), (void **)&DebugControl)!=S_OK) goto finish;
if (pc_pref) DebugControl->ControlledOutput(ctrl, mask, "%s: ", pc_pref);
DebugControl->ControlledOutputVaList(ctrl, mask, format, args);
finish:
if (DebugControl) DebugControl->Release();
return;
}
#define DBGPRNT_METHOD(name, pref) \
void name(const char *format, ...) { \
va_list args; \
va_start(args, format); \
vdbgprintf(DBGPRNT_OUTCTL_FLAGS, \
DBGPRNT_OUTPUT_FLAGS, pref, format, args);\
va_end(args); \
}
DBGPRNT_METHOD(dbgprintf, NULL)
DBGPRNT_METHOD(dbg_dbgprintf, "DBG")
DBGPRNT_METHOD(info_dbgprintf, "INFO")
DBGPRNT_METHOD(warn_dbgprintf, "WARN")
DBGPRNT_METHOD(err_dbgprintf, "ERR")
/*
memory access funcs
*/
#define MEMACCESS_METHOD(name, func) \
ULONG name(ULONG64 addr, PVOID p_buf, ULONG buf_sz, PULONG p_cb) \
{ \
ULONG ret=FALSE; \
IDebugDataSpaces *DebugDataSpaces=NULL; \
\
PDEBUG_CLIENT4 Client; \
if (Client=get_client()) { \
if (Client->QueryInterface(__uuidof(IDebugDataSpaces), \
(void **)&DebugDataSpaces)==S_OK) { \
ret=(DebugDataSpaces->func(addr, p_buf, buf_sz, p_cb)==S_OK);\
} \
} \
\
if (DebugDataSpaces) DebugDataSpaces->Release(); \
return ret; \
}
/* WdbgExts ReadMemory(), WriteMemory() analogous */
MEMACCESS_METHOD(read_memory, ReadVirtual)
MEMACCESS_METHOD(write_memory, WriteVirtual)
/* WdbgExts GetExpressionEx() analogous */
BOOL get_expression(PCSTR pc_expr, ULONG64 *p_val, PCSTR *ppc_rem)
{
BOOL ret=FALSE;
IDebugControl *DebugControl=NULL;
PDEBUG_CLIENT4 Client;
if (!(Client=get_client())) goto finish;
if ((Client->QueryInterface(
__uuidof(IDebugControl), (void **)&DebugControl))!=S_OK) goto finish;
ULONG rem_i;
DEBUG_VALUE val;
if (DebugControl->Evaluate(
pc_expr, DEBUG_VALUE_INT64, &val, &rem_i)!=S_OK) goto finish;
if (ppc_rem) {
for (pc_expr+=rem_i; isspace(*pc_expr); pc_expr++);
*ppc_rem = pc_expr;
}
*p_val = (ULONG64)val.I64;
ret=TRUE;
finish:
if (DebugControl) DebugControl->Release();
return ret;
}
/* Copy target's string 'targ_in_addr' into local buf 'pc_out_buf' ('out_buf_len'
long, which should be a divisible by 4). The func checks if the copied string
fits into the buffer and returns FALSE if the buffer is too small for the
string. 'end_chr' specifies end-of-string char (by default 0). Copied string
is NULL terminated w/o the end-of-string char.
*/
BOOL string_cpy_lt(
char *pc_out_buf, ULONG64 targ_in_addr, size_t out_buf_len, int end_chr)
{
BOOL ret=FALSE;
UINT32 dw;
char *pc_dw = (char*)&dw;
for (size_t i=0; i<out_buf_len; targ_in_addr+=sizeof(dw), i+=sizeof(dw))
{
ULONG cb;
if (!(read_memory(targ_in_addr, &dw, sizeof(dw), &cb) &&
cb==sizeof(dw))) goto err;
for (size_t j=0; j<sizeof(dw); j++)
if ((pc_out_buf[i+j]=pc_dw[j])==(char)end_chr) {
pc_out_buf[i+j]=0;
goto no_err;
}
}
/* string to long (end-of-string char absent) */
goto err;
no_err:
ret=TRUE;
err:
return ret;
}
/* Compare local string 'pc_str1' with target's string 'str2_addr' */
int string_cmp_lt(const char *pc_str1, ULONG64 str2_addr)
{
int ret=-1;
UINT32 dw;
char *pc_dw = (char*)&dw;
for (size_t i=0;; str2_addr+=sizeof(dw), i+=sizeof(dw))
{
ULONG cb;
if (!(read_memory(str2_addr, &dw, sizeof(dw), &cb) &&
cb==sizeof(dw))) break;
for (size_t j=0; j<sizeof(dw); j++) {
if (ret=pc_str1[i+j]-pc_dw[j]) goto finish;
if (!pc_dw[j]) goto finish;
}
}
finish:
return ret;
}
/* Reverse memchr(): look for a byte with a value other than 'val' */
void *rmemchr(const void *ptr, int val, size_t num)
{
void *ret=NULL;
for (size_t i=0; i<num; i++)
if (((char*)ptr)[i]!=val) { ret=(void*)&(((char*)ptr)[i]); break; }
return ret;
}
/* Get module name from module's full file name pointed by 'pc_name'. 'p_len'
will get module's length.
*/
char *get_mod_name(const char *pc_name, size_t *p_len)
{
const char *pc_mod = pc_name;
const char *sl = strrchr(pc_mod, '/');
const char *bsl = strrchr(pc_mod, '\\');
const char *col = strrchr(pc_mod, ':');
if (bsl) pc_mod=bsl+1;
else
if (sl) pc_mod=sl+1;
else
if (col) pc_mod=col+1;
const char *dot = strrchr(pc_mod, '.');
*p_len = (dot ? dot-pc_mod : strlen(pc_mod));
return (char*)pc_mod;
}
/* bytes to hex conversion */
void bts2hex(const unsigned char *in, size_t in_len, char *out)
{
static char HEX_DIGS[] = "0123456789abcdef";
size_t i,j;
for (i=j=0; i<in_len; i++) {
out[j++] = HEX_DIGS[in[i]>>4];
out[j++] = HEX_DIGS[in[i]&0x0f];
}
out[j]=0;
}
/* Copy memory starting from address 'mem_addr' with 'sz' size into file 'f'
(from its current position).
*/
cpy_ret_t mem2file(FILE *f, ULONG64 mem_addr, ULONG sz)
{
char read_buf[0x400];
cpy_ret_t ret = cpy_ok;
for (ULONG off=0; sz;)
{
ULONG cb;
ULONG read_sz = (sz<=sizeof(read_buf) ? sz : sizeof(read_buf));
if (!(read_memory(mem_addr+off, read_buf, read_sz, &cb) && cb==read_sz))
{ ret=cpy_src_err; goto finish; }
if (fwrite(read_buf, 1, read_sz, f)!=read_sz)
{ ret=cpy_dst_err; goto finish; }
sz -= read_sz;
off += read_sz;
}
finish:
return ret;
}
/* Copy a file (from its current position) into memory (at 'mem_addr' address)
with size 'sz'. If 'sz' is zero copy up to the end of the file.
*/
cpy_ret_t file2mem(ULONG64 mem_addr, FILE *f, size_t sz)
{
size_t off=0;
char read_buf[0x400];
BOOL b_loopcnt=TRUE;
cpy_ret_t ret = cpy_ok;
for (size_t to_proc=sz; b_loopcnt && (!sz || to_proc);)
{
ULONG cb;
size_t read_sz = (!sz ||
(to_proc>sizeof(read_buf)) ? sizeof(read_buf) : to_proc);
if ((cb=fread(read_buf, 1, read_sz, f))!=read_sz) {
if (sz) { ret=cpy_src_err; break; }
else { read_sz=cb; b_loopcnt=FALSE; }
}
if (!(write_memory(mem_addr+off, read_buf, read_sz, &cb) &&
cb==read_sz))
{
ret=cpy_dst_err;
goto finish;
}
to_proc-=read_sz;
off+=read_sz;
}
finish:
return ret;
}