forked from alexander-pick/patchdiff2_ida6
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathx86.cpp
502 lines (424 loc) · 11.7 KB
/
x86.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
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
/*
Patchdiff2
Portions (C) 2011 Alexander Pick
Portions (C) 2010 Nicolas Pouvesle
Portions (C) 2007 - 2009 Tenable Network Security, Inc.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License version 2 as
published by the Free Software Foundation.
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, see <http://www.gnu.org/licenses/>.
*/
#include <ida.hpp>
#include <bytes.hpp>
#include "x86.hpp"
#include "patchdiff.hpp"
extern cpu_t patchdiff_cpu;
/*------------------------------------------------*/
/* function : x86_is_rex_prefix */
/* arguments: unsigned char val */
/* description: detects if instruction is a Rex */
/* prefix */
/*------------------------------------------------*/
bool x86_is_rex_prefix(unsigned char val)
{
if (val >= 0x40 && val <= 0x4F)
return true;
return false;
}
/*------------------------------------------------*/
/* function : x86_is_push_register */
/* arguments: unsigned char val */
/* description: detects if instruction is push reg*/
/*------------------------------------------------*/
bool x86_is_push_register (unsigned char val)
{
switch (val)
{
case 0x50: // push eax
case 0x51: // push ecx
case 0x52: // push edx
case 0x53: // push ebx
case 0x54: // push esp
case 0x55: // push ebp
case 0x56: // push esi
case 0x57: // push edi
return 1;
default:
return 0;
}
}
/*------------------------------------------------*/
/* function : x86_is_pop_register */
/* arguments: unsigned char val */
/* description: detects if instruction is pop reg */
/*------------------------------------------------*/
bool x86_is_pop_register (unsigned char val)
{
switch (val)
{
case 0x58: // pop eax
case 0x59: // pop ecx
case 0x5A: // pop edx
case 0x5B: // pop ebx
case 0x5C: // pop esp
case 0x5D: // pop ebp
case 0x5E: // pop esi
case 0x5F: // pop edi
return 1;
default:
return 0;
}
}
/*------------------------------------------------*/
/* function : x86_is_inc_register */
/* arguments: unsigned char val */
/* description: detects if instruction is inc reg */
/*------------------------------------------------*/
bool x86_is_inc_register (unsigned char val)
{
switch (val)
{
case 0x40: // inc eax
case 0x41: // inc ecx
case 0x42: // inc edx
case 0x43: // inc ebx
case 0x44: // inc esp
case 0x45: // inc ebp
case 0x46: // inc esi
case 0x47: // inc edi
return 1;
default:
return 0;
}
}
/*------------------------------------------------*/
/* function : x86_is_dec_register */
/* arguments: unsigned char val */
/* description: detects if instruction is dec reg */
/*------------------------------------------------*/
bool x86_is_dec_register (unsigned char val)
{
switch (val)
{
case 0x48: // dec eax
case 0x49: // dec ecx
case 0x4A: // dec edx
case 0x4B: // dec ebx
case 0x4C: // dec esp
case 0x4D: // dec ebp
case 0x4E: // dec esi
case 0x4F: // dec edi
return 1;
default:
return 0;
}
}
/*------------------------------------------------*/
/* function : is_nop */
/* arguments: unsigned char _byte */
/* description: detect if instruction is nop */
/* (nop, mov reg, reg, ...) */
/*------------------------------------------------*/
bool x86_is_nop (unsigned char _byte, ea_t ea)
{
unsigned char val;
unsigned short val2;
unsigned long val3;
if (patchdiff_cpu == CPU_X8664 && x86_is_rex_prefix(_byte))
{
ea++;
_byte = get_byte(ea+1);
}
// mov reg, reg - xchg reg, reg
if (_byte == 0x8A || _byte == 0x8B || _byte == 0x87 || _byte == 0x86)
{
val = get_byte(ea+1);
if ( (val == 0xC0) || // mov eax, eax (al, al)
(val == 0xC9) || // mov ecx, ecx
(val == 0xDB) || // mov ebx, ebx
(val == 0xD2) || // mov edx, edx
(val == 0xF6) || // mov esi, esi
(val == 0xFF) || // mov edi, edi
(val == 0xE4) || // mov esp, esp
(val == 0xED) ) // mov ebp, ebp
return true;
}
if (_byte == 0x8D)
{
val = get_byte(ea+1);
if ( (val == 0x00) || // lea eax, [eax]
(val == 0x09) || // lea ecx, [ecx]
(val == 0x42) || // lea edx, [edx]
(val == 0x4b) || // lea ebx, [ebx]
(val == 0x36) || // lea esi, [esi]
(val == 0x3f) ) // lea edi, [edi]
{
return true;
}
else if ( (val == 0x40) || // lea eax, [eax+0]
(val == 0x49) || // lea ecx, [ecx+0]
(val == 0x52) || // lea edx, [edx+0]
(val == 0x5b) || // lea ebx, [ebx+0]
(val == 0x6d) || // lea ebp, [ebp+0]
(val == 0x76) || // lea esi, [esi+0]
(val == 0x7f) ) // lea edi, [edi+0]
{
val = get_byte(ea+2);
if (val == 0x00)
return true;
}
else if ( (val == 0x80) || // lea eax, [eax+0x00000000]
(val == 0x89) || // lea ecx, [ecx+0x00000000]
(val == 0x92) || // lea edx, [edx+0x00000000]
(val == 0x9b) || // lea ebx, [ebx+0x00000000]
(val == 0xad) || // lea ebp, [ebp+0x00000000]
(val == 0xB6) || // lea esi, [esi+0x00000000]
(val == 0xBf) ) // lea edi, [edi+0x00000000]
{
val3 = get_long(ea+2);
if (val3 == 0x00)
return true;
}
else if (val == 0xb4)
{
val = get_byte(ea+2);
if (val == 0x26)
{
val3 = get_long(ea+3);
if (val3 == 0x00) // lea esi, [esi+0x00000000]
return true;
}
}
else if (val == 0x24)
{
val = get_byte(ea+2);
if (val == 0x24) // lea esp, [esp]
return true;
}
else if (val == 0x64)
{
val2 = get_word(ea+2);
if (val2 == 0x24) // lea esp, [esp+0]
return true;
}
else if (val == 0xa4)
{
val = get_byte(ea+2);
if (val == 0x24)
{
val3 = get_long(ea+3);
if (val3 == 0x00) // lea esp, [esp+0x00000000]
return true;
}
}
}
// nop
if (_byte == 0x90)
return true;
return false;
}
/*------------------------------------------------*/
/* function : x86_remove_instr */
/* arguments: unsigned char byte, ea_t ea */
/* description: Returns true is the instruction */
/* must be ignored */
/*------------------------------------------------*/
bool x86_remove_instr(unsigned char byte, ea_t ea)
{
// removes nop
if (x86_is_nop(byte, ea))
return true;
return false;
}
/*------------------------------------------------*/
/* function : x86_convert_16bit_rep */
/* arguments: unsigned char val, instr address */
/* description: converts byte if instruction is a */
/* 16 bit rep/repe/repz/repne/repnz */
/* notes: detects changes like 66 F3 -> F3 66 */
/*------------------------------------------------*/
bool x86_convert_16bit_rep(unsigned char * byte, ea_t ea)
{
unsigned char byte2;
if (*byte == 0x66)
{
byte2 = get_byte(ea+1);
if (byte2 == 0xF3 || byte2 == 0xF2)
{
*byte = byte2;
return true;
}
}
return false;
}
/*------------------------------------------------*/
/* function : x86_convert_cond_jump */
/* arguments: unsigned char val, instr address */
/* description: converts byte if instruction is a */
/* conditionnal jump. If jnz returns */
/* jz, if jne return je, ... */
/*------------------------------------------------*/
bool x86_convert_cond_jump(unsigned char * byte, ea_t ea)
{
unsigned char byte2 = *byte;
if (byte2 == 0x0F)
{
byte2 = get_byte(ea+1) - 0x10;
}
if (byte2 >= 0x70 && byte2 <= 0x7F)
{
switch (byte2)
{
case 0x77: // ja -> jb
*byte = 0x72;
break;
case 0x73: // jae -> jbe
*byte = 0x76;
break;
case 0x75: // jnz -> jz
*byte = 0x74;
break;
case 0x7F: // jg -> jl
*byte = 0x7C;
break;
case 0x7D: // jge -> jle
*byte = 0x7E;
break;
case 0x71: // jno -> jo
*byte = 0x70;
break;
case 0x7B: // jnp -> jp
*byte = 0x7A;
break;
case 0x79: // jns -> js
*byte = 0x78;
break;
default:
*byte = byte2;
}
return true;
}
return false;
}
/*------------------------------------------------*/
/* function : x86_convert_cond_jump */
/* arguments: unsigned char val, instr address */
/* description: converts byte if instruction is a */
/* conditionnal jump. If jnz returns */
/* jz, if jne return je, ... */
/*------------------------------------------------*/
int x86_is_cond_jump_pos(ea_t ea)
{
unsigned char byte2 = get_byte(ea);
if (byte2 == 0x0F)
{
byte2 = get_byte(ea+1) - 0x10;
}
if (byte2 >= 0x70 && byte2 <= 0x7F)
{
switch (byte2)
{
case 0x77: //ja
case 0x72: //jb
case 0x74: //jz
case 0x7F: //jg
case 0x7C: //jl
case 0x70: //jo
case 0x7A: //jp
case 0x78: //js
return 1;
default:
return 2;
}
}
return 0;
}
/*------------------------------------------------*/
/* function : x86_get_fake_jump */
/* arguments: ea_t ea */
/* description: Returns jump for jump $5/$2 */
/*------------------------------------------------*/
ea_t x86_get_fake_jump(ea_t ea)
{
unsigned char byte;
unsigned long l;
byte = get_byte(ea);
if (byte == 0xE9)
{
l = get_long(ea+1);
if (l == 0)
return get_item_end(ea);
}
else if (byte == 0xeb)
{
byte = get_byte(ea+1);
if (byte == 0)
return get_item_end(ea);
}
return BADADDR;
}
/*------------------------------------------------*/
/* function : x86_is_direct_jump */
/* arguments: ea_t ea */
/* description: Returns TRUE if a direct jump */
/*------------------------------------------------*/
bool x86_is_direct_jump(ea_t ea)
{
unsigned char byte;
byte = get_byte(ea);
switch (byte)
{
case 0xE9:
case 0xEA:
case 0xEB:
case 0xFF:
return true;
default:
return false;
}
return false;
}
/*------------------------------------------------*/
/* function : x86_is_end_block */
/* arguments: ea_t ea */
/* description: Returns true on int 3 */
/*------------------------------------------------*/
bool x86_is_end_block(ea_t ea)
{
if (get_byte(ea) == 0xCC)
return true;
return false;
}
/*------------------------------------------------*/
/* function : x86_get_byte */
/* arguments: unsigned char _byte */
/* description: Returns opcode */
/* note: convert push/pop registers and remove rex*/
/* prefix */
/*------------------------------------------------*/
unsigned char x86_get_byte(ea_t ea)
{
unsigned char byte;
byte = get_byte(ea);
if (patchdiff_cpu == CPU_X8664 && x86_is_rex_prefix(byte))
{
ea++;
byte = get_byte(ea);
}
if (x86_is_push_register (byte))
byte = 0x50; // push eax
else if (x86_is_pop_register (byte))
byte = 0x58; // pop eax
else if (x86_is_inc_register(byte))
byte = 0x40; // inc eax
else if (x86_is_dec_register(byte))
byte = 0x48; // dec eax
x86_convert_16bit_rep(&byte, ea);
x86_convert_cond_jump(&byte, ea);
return byte;
}