-
Notifications
You must be signed in to change notification settings - Fork 4
/
CmdDlg.cpp
482 lines (349 loc) · 8.84 KB
/
CmdDlg.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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
// CmdDlg.cpp : 实现文件
//
#include "stdafx.h"
#include "DataMonitor.h"
#include "CmdDlg.h"
#include ".\cmddlg.h"
#include "cppst.h"
#include "BinModifyDlg.h"
extern "C" {
#include "disasm/disasm.h"
}
//////////////////////////////////////////////////////////////////////////
#define CRLF _T("\r\n")
#define IsValidPtr(Ptr) ( ULONG_PTR(Ptr) < 0x80000000 && ULONG_PTR(Ptr) > 0x10000)
//////////////////////////////////////////////////////////////////////////
BEGIN_COMMAND_MAP(CCmdDlg, CCommand)
COMMAND_MAP_ENTRY(_T("CLS") , &CCmdDlg::Cmd_Cls)
COMMAND_MAP_ENTRY(_T("RTTI") , &CCmdDlg::Cmd_RTTI)
COMMAND_MAP_ENTRY(_T("DB") , &CCmdDlg::Cmd_DB)
COMMAND_MAP_ENTRY(_T("DD") , &CCmdDlg::Cmd_DD)
COMMAND_MAP_ENTRY(_T("DW") , &CCmdDlg::Cmd_DW)
COMMAND_MAP_ENTRY(_T("U") , &CCmdDlg::Cmd_U)
COMMAND_MAP_ENTRY(_T("E") , &CCmdDlg::Cmd_E)
END_COMMAND_MAP()
// CCmdDlg 对话框
IMPLEMENT_DYNAMIC(CCmdDlg, CDialog)
CCmdDlg::CCmdDlg(CWnd* pParent /*=NULL*/)
: CDialog(CCmdDlg::IDD, pParent)
, m_strInput(_T(""))
{
m_dumpAddr = m_disasmAddr = 0;
}
CCmdDlg::~CCmdDlg()
{
}
void CCmdDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
DDX_Control(pDX, IDC_INPUT, m_cbInput);
DDX_CBString(pDX, IDC_INPUT, m_strInput);
DDX_Control(pDX, IDC_OUTPUT, m_output);
}
BEGIN_MESSAGE_MAP(CCmdDlg, CDialog)
END_MESSAGE_MAP()
// CCmdDlg 消息处理程序
BOOL CCmdDlg::OnInitDialog()
{
CDialog::OnInitDialog();
m_editlog.SetEditCtrl(m_output.GetSafeHwnd());
// m_editlog.AddText("Try");
// m_output.SetWindowText("afsfsa");
return TRUE; // return TRUE unless you set the focus to a control
// 异常: OCX 属性页应返回 FALSE
}
BOOL CCmdDlg::PreTranslateMessage(MSG* pMsg)
{
if (pMsg->message == WM_KEYDOWN) {
if (pMsg->wParam == VK_RETURN) {
OnReturn();
return FALSE;
} else if (pMsg->wParam == 'D') {
if (GetKeyState(VK_CONTROL) & 0x8000) {
AfxGetMainWnd()->PostMessage(WM_COMMAND, ID_VIEW_CMDDLG, 0);
return TRUE;
}
}
}
return CDialog::PreTranslateMessage(pMsg);
}
void CCmdDlg::OnReturn()
{
if (GetFocus() == NULL || GetFocus()->GetParent() != &m_cbInput) {
m_cbInput.SetFocus();
return;
}
UpdateData();
if (m_strInput.GetLength() == 0)
m_strInput = m_strLastCmd;
if (m_strInput.GetLength() == 0)
return;
PrintLine(_T("> %s"), (LPCTSTR )m_strInput);
if (OnCmd(m_strInput)) {
m_cbInput.InsertString(0, m_strInput);
if (m_cbInput.GetCount() >= 100)
m_cbInput.DeleteString(m_cbInput.GetCount() - 1);
} else {
Print(_T("invalid command") CRLF );
}
m_strLastCmd = m_strInput;
m_strInput.Empty();
UpdateData(FALSE);
}
BOOL CCmdDlg::OnCmd(CString& cmd)
{
if (!Parse(cmd))
return FALSE;
return Run();
}
void CCmdDlg::Print(LPCTSTR fmt, ...)
{
TCHAR str[2048];
va_list varg;
va_start(varg, fmt);
_vsntprintf(str, _countof(str), fmt, varg);
va_end(varg);
m_editlog.AddText(str);
}
void CCmdDlg::PrintLine(LPCTSTR fmt, ...)
{
TCHAR str[2048];
va_list varg;
va_start(varg, fmt);
_vsntprintf(str, _countof(str), fmt, varg);
va_end(varg);
lstrcat(str, CRLF);
m_editlog.AddText(str);
}
BOOL CCmdDlg::Cmd_Cls()
{
m_output.SetWindowText(_T(""));
return TRUE;
}
BOOL CCmdDlg::Cmd_RTTI()
{
if (GetParamCount() != 2) {
PrintLine(_T("invalid command"));
return TRUE;
}
ULONG_PTR addr = xtol(GetParam(1));
if (!IsValidPtr(addr)) {
PrintLine(_T("invalid address: %p"), addr );
return TRUE;
}
ULONG_PTR vtablePtr = 0;
g_dbgEng->ReadMemory((PVOID )addr, (PBYTE )&vtablePtr, sizeof(vtablePtr));
ULONG_PTR rttiPtr = vtablePtr - sizeof(ULONG_PTR);
ULONG_PTR rtti;
g_dbgEng->ReadMemory((PVOID )rttiPtr, (PBYTE )&rtti, sizeof(rtti));
ULONG_PTR typeDescPtrPtr = rtti + offsetof(RTTICompleteObjectLocator, pTypeDescriptor);
ULONG_PTR typeDescPtr;
g_dbgEng->ReadMemory((PVOID )typeDescPtrPtr, (PBYTE )&typeDescPtr, sizeof(typeDescPtr));
TypeDescriptor typeDesc;
memset(&typeDesc, 0 , sizeof(typeDesc));
g_dbgEng->ReadMemory((PVOID )typeDescPtr, (PBYTE )&typeDesc, sizeof(typeDesc));
PrintLine("class name: %s", typeDesc.name);
ULONG_PTR vtable[32];
memset(vtable, 0 , sizeof(vtable));
g_dbgEng->ReadMemory((PVOID )vtablePtr, (PBYTE )vtable, sizeof(vtable));
PrintLine("vtable: %p", vtablePtr);
for (int i = 0; i < 32; i ++) {
if (!IsValidPtr(vtable[i]))
break;
PrintLine("\t%d\t%p", i, vtable[i]);
}
return TRUE;
}
BOOL CCmdDlg::Cmd_DB()
{
ULONG_PTR addr;
if (GetParamCount() >= 2) {
ULONG_PTR addr = xtol(GetParam(1));
if (!IsValidPtr(addr)) {
PrintLine(_T("invalid address: %p"), addr );
return TRUE;
}
} else
addr = m_dumpAddr;
BYTE buf[1024];
size_t len = 0x10;
if (GetParamCount() > 2) {
len = atol(GetParam(2));
if (len == 0)
len = 1;
if (len > sizeof(buf))
len = sizeof(buf);
}
size_t readlen = g_dbgEng->ReadMemory((PVOID )addr, buf, len);
len = readlen;
size_t n = 0;
while (n < len) {
Print("%p\t", addr + n);
for (int i = 0; i < 0x10; i ++) {
if (n + i < len)
Print("%02X ", buf[n + i]);
else
Print(" ");
}
Print("|");
for (int i = 0; i < 0x10 && n + i < len; i ++) {
BYTE c = buf[n + i];
#define IsVisableChar(C) ( (C) < 0x20 )
Print("%c", IsVisableChar(c) ? '.' : c);
}
Print(CRLF);
n += 0x10;
}
m_dumpAddr = addr + len;
return TRUE;
}
BOOL CCmdDlg::Cmd_DW()
{
ULONG_PTR addr;
if (GetParamCount() >= 2) {
ULONG_PTR addr = xtol(GetParam(1));
if (!IsValidPtr(addr)) {
PrintLine(_T("invalid address: %p"), addr );
return TRUE;
}
} else
addr = m_dumpAddr;
BYTE buf[1024];
size_t len = 0x10 / sizeof(WORD);
if (GetParamCount() > 2) {
len = atol(GetParam(2));
if (len == 0)
len = 1;
if (len > sizeof(buf) / sizeof(WORD))
len = sizeof(buf) / sizeof(WORD);
}
size_t readlen = g_dbgEng->ReadMemory((PVOID )addr, buf, len * sizeof(WORD));
len = readlen / sizeof(WORD);
size_t n = 0;
while (n < len) {
Print("%p\t", addr + n * sizeof(WORD));
for (int i = 0; i < 0x10 && n < len; i ++, n ++ ) {
Print("%04X ", *(WORD* )&buf[n * sizeof(WORD)]);
}
Print(CRLF);
}
m_dumpAddr = addr + len * sizeof(WORD);
return TRUE;
}
BOOL CCmdDlg::Cmd_DD()
{
ULONG_PTR addr;
if (GetParamCount() >= 2) {
ULONG_PTR addr = xtol(GetParam(1));
if (!IsValidPtr(addr)) {
PrintLine(_T("invalid address: %p"), addr );
return TRUE;
}
} else
addr = m_dumpAddr;
BYTE buf[1024];
size_t len = 0x10 / sizeof(DWORD);
if (GetParamCount() > 2) {
len = atol(GetParam(2));
if (len == 0)
len = 1;
if (len > sizeof(buf) / sizeof(DWORD))
len = sizeof(buf) / sizeof(DWORD);
}
size_t readlen = g_dbgEng->ReadMemory((PVOID )addr, buf, len * sizeof(DWORD));
len = readlen / sizeof(DWORD);
size_t n = 0;
while (n < len) {
Print("%p\t", addr + n * sizeof(DWORD));
for (int i = 0; i < 0x10 && n < len; i ++, n ++) {
Print("%08X ", *(DWORD* )&buf[n * sizeof(DWORD)]);
}
Print(CRLF);
}
m_dumpAddr = addr + len * sizeof(DWORD);
return TRUE;
}
BOOL CCmdDlg::Cmd_U()
{
ULONG_PTR addr;
if (GetParamCount() >= 2) {
addr = xtol(GetParam(1));
if (!IsValidPtr(addr)) {
PrintLine(_T("invalid address: %p"), addr );
return TRUE;
}
} else
addr = m_disasmAddr;
BYTE buf[1024];
size_t len = 0x10;
if (GetParamCount() > 2) {
len = atol(GetParam(2));
if (len == 0)
len = 1;
if (len > sizeof(buf))
len = sizeof(buf);
}
size_t readlen = g_dbgEng->ReadMemory((PVOID )addr, buf, len);
len = readlen;
t_disasm da;
memset(&da, 0, sizeof(da));
// ulong l = Disasm(code, len, (ulong )addr, &da, DISASM_SIZE);
ULONG pos = 0;
ulong l;
m_disasmAddr = addr;
while (pos <= len) {
l = Disasm((char* )&buf[pos], len - pos, (ulong )addr + pos, &da, DISASM_CODE);
if (l == 0)
break;
m_disasmAddr += l;
PrintLine(" %p | %3i %-24s %-24s\n", addr + pos, l, da.dump, da.result);
pos += l;
}
return TRUE;
}
bool CCmdDlg::ModifyMem(ULONG_PTR addr, BYTE data[], size_t len)
{
return g_dbgEng->WriteMemory(PVOID(addr), data, len) == len;
}
BOOL CCmdDlg::Cmd_E()
{
if (GetParamCount() != 2) {
PrintLine(_T("invalid command"));
return TRUE;
}
ULONG_PTR addr = xtol(GetParam(1));
if (!IsValidPtr(addr)) {
PrintLine(_T("invalid address: %p"), addr );
return TRUE;
}
CBinModifyDlg dlg;
if (dlg.DoModal() != IDOK)
return TRUE;
DWORD n;
WCHAR* wstr;
size_t len;
bool result = false;
USES_CONVERSION;
switch(dlg.m_type) {
case 0: // BYTE
case 1: // WORD
case 2: // DWORD
if (dlg.m_hex)
_stscanf((LPCTSTR )dlg.m_strWhat, "%x", &n);
else
_stscanf((LPCTSTR )dlg.m_strWhat, "%d", &n);
result = ModifyMem(addr, (BYTE* )&n, dlg.m_type == 0 ? 1 : dlg.m_type * 2);
break;
case 3: // ASCII
result = ModifyMem(addr, (BYTE* )(LPCSTR )dlg.m_strWhat, dlg.m_strWhat.GetLength());
break;
case 4: // UNICODE
wstr = T2W((LPCSTR )dlg.m_strWhat);
len = wcslen(wstr) * 2;
result = ModifyMem(addr, (BYTE* )wstr, len);
break;
}
if (!result)
MessageBox(_T("modification failed."), NULL);
return TRUE;
}