-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathemu_script.cpp
More file actions
221 lines (204 loc) · 6.97 KB
/
Copy pathemu_script.cpp
File metadata and controls
221 lines (204 loc) · 6.97 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
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
/*
Scripting support for the x86 emulator IdaPro plugin
Copyright (c) 2008-2022 Chris Eagle
This program is free software; you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by the Free
Software Foundation; either version 2 of the License, or (at your option)
any later version.
This program is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
more details.
You should have received a copy of the GNU General Public License along with
this program; if not, write to the Free Software Foundation, Inc., 59 Temple
Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include <ida.hpp>
#include <expr.hpp>
#include "cpu.h"
#include "emu_script.h"
#include "sdk_versions.h"
bool set_idc_func_ex(const char *name, idc_func_t *fp, const char *args, int extfunc_flags) {
ext_idcfunc_t func;
func.name = name;
func.fptr = fp;
func.args = args;
func.defvals = NULL;
func.ndefvals = 0;
func.flags = extfunc_flags;
return add_idc_func(func);
}
/*
* prototypes for functions in x86emu.cpp that we use
* to implement some of the scripted behavior
*/
void run();
void trace();
void stepOne();
void traceOne();
void emuSyncDisplay();
void setIdcRegister(unsigned int idc_reg_num, unsigned int newVal);
void addBreakpoint(unsigned int addr);
/*
* native implementation of EmuRun.
*/
static error_t idaapi idc_emu_run(idc_value_t * /*argv*/, idc_value_t * /*res*/) {
run();
return eOk;
}
/*
* native implementation of EmuStepOne.
*/
static error_t idaapi idc_emu_step(idc_value_t * /*argv*/, idc_value_t * /*res*/) {
stepOne();
return eOk;
}
/*
* native implementation of EmuTraceOne.
*/
static error_t idaapi idc_emu_trace_one(idc_value_t * /*argv*/, idc_value_t * /*res*/) {
traceOne();
return eOk;
}
/*
* native implementation of EmuTrace.
*/
static error_t idaapi idc_emu_trace(idc_value_t * /*argv*/, idc_value_t * /*res*/) {
trace();
return eOk;
}
/*
* native implementation of EmuSync.
*/
static error_t idaapi idc_emu_sync(idc_value_t * /*argv*/, idc_value_t * /*res*/) {
emuSyncDisplay();
return eOk;
}
/*
* native implementation of EmuGetReg. Converts a register constant
* into the appropriate offset into the cpu struct and returns the
* value of the indicated register. Returns -1 if an invalid register
* number is specified.
*/
static error_t idaapi idc_emu_getreg(idc_value_t *argv, idc_value_t *res) {
res->vtype = VT_LONG;
if (argv[0].vtype == VT_LONG) {
unsigned int regnum = (unsigned int)argv[0].num;
switch (regnum) {
case EAX_REG: case ECX_REG: case EDX_REG: case EBX_REG:
case ESP_REG: case EBP_REG: case ESI_REG: case EDI_REG:
res->num = cpu.general[regnum - EAX_REG];
break;
case EIP_REG:
res->num = cpu.eip;
break;
case EFLAGS_REG:
res->num = cpu.eflags;
break;
case CS_REG: case SS_REG: case DS_REG: case ES_REG: case FS_REG: case GS_REG:
res->num = cpu.segReg[regnum - CS_REG];
break;
case CS_BASE: case SS_BASE: case DS_BASE: case ES_BASE: case FS_BASE: case GS_BASE:
res->num = cpu.segBase[regnum - CS_BASE];
break;
case DR0_REG: case DR1_REG: case DR2_REG: case DR3_REG:
case DR4_REG: case DR5_REG: case DR6_REG: case DR7_REG:
res->num = cpu.debug_regs[regnum - DR0_REG];
break;
default:
res->num = -1;
break;
}
}
else {
res->num = -1;
}
return eOk;
}
/*
* native implementation of EmuSetReg. Converts a register constant
* into the appropriate offset into the cpu struct and sets the
* value of the indicated register. Returns 0 on success and -1 if an
* invalid register number is specified.
*/
static error_t idaapi idc_emu_setreg(idc_value_t *argv, idc_value_t *res) {
res->vtype = VT_LONG;
res->num = 0;
if (argv[0].vtype == VT_LONG && argv[1].vtype == VT_LONG) {
unsigned int regnum = (unsigned int)argv[0].num;
unsigned int regval = (unsigned int)argv[1].num;
switch (regnum) {
case EAX_REG: case ECX_REG: case EDX_REG: case EBX_REG:
case ESP_REG: case EBP_REG: case ESI_REG: case EDI_REG:
case EIP_REG:
case EFLAGS_REG:
//these registers are all displayed so we need to update the
//respective control as well as set the register
setIdcRegister(regnum, regval);
break;
case CS_REG: case SS_REG: case DS_REG: case ES_REG: case FS_REG: case GS_REG:
cpu.segReg[regnum - CS_REG] = regval;
break;
case CS_BASE: case SS_BASE: case DS_BASE: case ES_BASE: case FS_BASE: case GS_BASE:
cpu.segBase[regnum - CS_BASE] = regval;
break;
case DR0_REG: case DR1_REG: case DR2_REG: case DR3_REG:
case DR4_REG: case DR5_REG: case DR6_REG: case DR7_REG:
cpu.debug_regs[regnum - DR0_REG] = regval;
break;
default:
res->num = -1;
break;
}
}
else {
res->num = -1;
}
return eOk;
}
/*
* native implementation of EmuAddBpt. Adds an emulator breakpoint
* at the specified address.
*/
static error_t idaapi idc_emu_addbpt(idc_value_t *argv, idc_value_t *res) {
res->vtype = VT_LONG;
if (argv[0].vtype == VT_LONG) {
unsigned int addr = (unsigned int)argv[0].num;
addBreakpoint(addr);
res->num = 1;
}
else {
res->num = 0;
}
return eOk;
}
/*
* Register new IDC functions for use with the emulator
*/
void register_funcs() {
static const char idc_void[] = { 0 };
// static const char idc_str_args[] = { VT_STR, 0 };
static const char idc_long[] = { VT_LONG, 0 };
static const char idc_long_long[] = { VT_LONG, VT_LONG, 0 };
set_idc_func_ex("EmuRun", idc_emu_run, idc_void, EXTFUN_BASE);
set_idc_func_ex("EmuTrace", idc_emu_trace, idc_void, EXTFUN_BASE);
set_idc_func_ex("EmuStepOne", idc_emu_step, idc_void, EXTFUN_BASE);
set_idc_func_ex("EmuTraceOne", idc_emu_trace_one, idc_void, EXTFUN_BASE);
set_idc_func_ex("EmuSync", idc_emu_sync, idc_void, EXTFUN_BASE);
set_idc_func_ex("EmuGetReg", idc_emu_getreg, idc_long, EXTFUN_BASE);
set_idc_func_ex("EmuSetReg", idc_emu_setreg, idc_long_long, EXTFUN_BASE);
set_idc_func_ex("EmuAddBpt", idc_emu_addbpt, idc_long, EXTFUN_BASE);
}
/*
* Unregister IDC functions when the plugin is unloaded
*/
void unregister_funcs() {
set_idc_func_ex("EmuRun", NULL, NULL, 0);
set_idc_func_ex("EmuTrace", NULL, NULL, 0);
set_idc_func_ex("EmuStepOne", NULL, NULL, 0);
set_idc_func_ex("EmuTraceOne", NULL, NULL, 0);
set_idc_func_ex("EmuSync", NULL, NULL, 0);
set_idc_func_ex("EmuGetReg", NULL, NULL, 0);
set_idc_func_ex("EmuSetReg", NULL, NULL, 0);
set_idc_func_ex("EmuAddBpt", NULL, NULL, 0);
}