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
|
#define _CRT_SECURE_NO_WARNINGS
#include <inttypes.h>
#include <stdio.h>
#include <string.h>
#include "binaryninjaapi.h"
#include "asmx86/asmx86.h"
using namespace BinaryNinja;
using namespace std;
using namespace asmx86;
#define IL_FLAG_C 0
#define IL_FLAG_P 2
#define IL_FLAG_A 4
#define IL_FLAG_Z 6
#define IL_FLAG_S 7
#define IL_FLAG_D 10
#define IL_FLAG_O 11
#define IL_FLAGWRITE_ALL 1
#define IL_FLAGWRITE_NOCARRY 2
#define IL_FLAGWRITE_CO 3
#define REG_FSBASE 0x100
#define REG_GSBASE 0x101
#define TRAP_DIV 0
#define TRAP_ICEBP 1
#define TRAP_NMI 2
#define TRAP_BP 3
#define TRAP_OVERFLOW 4
#define TRAP_BOUND 5
#define TRAP_ILL 6
#define TRAP_NOT_AVAIL 7
#define TRAP_DOUBLE 8
#define TRAP_TSS 10
#define TRAP_NO_SEG 11
#define TRAP_STACK 12
#define TRAP_GPF 13
#define TRAP_PAGE 14
#define TRAP_FPU 16
#define TRAP_ALIGN 17
#define TRAP_MCE 18
#define TRAP_SIMD 19
static uint8_t GetShiftCountForScale(uint8_t scale)
{
switch (scale)
{
case 2:
return 1;
case 4:
return 2;
case 8:
return 3;
default:
return 0;
}
}
static uint32_t GetStackPointer(size_t addrSize)
{
switch (addrSize)
{
case 2:
return REG_SP;
case 4:
return REG_ESP;
default:
return REG_RSP;
}
}
static uint32_t GetFramePointer(size_t addrSize)
{
switch (addrSize)
{
case 2:
return REG_BP;
case 4:
return REG_EBP;
default:
return REG_RBP;
}
}
static uint32_t GetCountRegister(size_t addrSize)
{
switch (addrSize)
{
case 2:
return REG_CX;
case 4:
return REG_ECX;
default:
return REG_RCX;
}
}
static size_t GetILOperandMemoryAddress(LowLevelILFunction& il, InstructionOperand& operand, size_t i, size_t addrSize)
{
size_t offset;
if (operand.operand != MEM)
offset = il.Operand(i, il.Undefined());
else if ((operand.components[0] == NONE) && (operand.components[1] == NONE) && operand.relative)
offset = il.Operand(i, il.ConstPointer(addrSize, operand.immediate));
else if ((operand.components[0] == NONE) && (operand.components[1] == NONE))
offset = il.Operand(i, il.Const(addrSize, operand.immediate));
else if ((operand.components[1] == NONE) && (operand.immediate == 0))
offset = il.Operand(i, il.Register(addrSize, operand.components[0]));
else if (operand.components[1] == NONE)
{
offset = il.Operand(i, il.Add(addrSize, il.Register(addrSize, operand.components[0]),
il.Const(addrSize, operand.immediate)));
}
else if ((operand.components[0] == NONE) && (operand.scale == 1) && (operand.immediate == 0))
offset = il.Operand(i, il.Register(addrSize, operand.components[1]));
else if ((operand.components[0] == NONE) && (operand.scale == 1))
{
offset = il.Operand(i, il.Add(addrSize, il.Register(addrSize, operand.components[1]),
il.Const(addrSize, operand.immediate)));
}
else if ((operand.components[0] == NONE) && (operand.immediate == 0))
{
offset = il.Operand(i, il.ShiftLeft(addrSize, il.Register(addrSize, operand.components[1]),
il.Const(1, GetShiftCountForScale(operand.scale))));
}
else if (operand.components[0] == NONE)
{
offset = il.Operand(i, il.Add(addrSize, il.ShiftLeft(addrSize, il.Register(addrSize, operand.components[1]),
il.Const(1, GetShiftCountForScale(operand.scale))), il.Const(addrSize, operand.immediate)));
}
else if ((operand.scale == 1) && (operand.immediate == 0))
{
offset = il.Operand(i, il.Add(addrSize, il.Register(addrSize, operand.components[0]),
il.Register(addrSize, operand.components[1])));
}
else if (operand.scale == 1)
{
offset = il.Operand(i, il.Add(addrSize, il.Add(addrSize, il.Register(addrSize, operand.components[0]),
il.Register(addrSize, operand.components[1])), il.Const(addrSize, operand.immediate)));
}
else if (operand.immediate == 0)
{
offset = il.Operand(i, il.Add(addrSize, il.Register(addrSize, operand.components[0]),
il.ShiftLeft(addrSize, il.Register(addrSize, operand.components[1]),
il.Const(1, GetShiftCountForScale(operand.scale)))));
}
else
{
offset = il.Operand(i, il.Add(addrSize, il.Add(addrSize, il.Register(addrSize, operand.components[0]),
il.ShiftLeft(addrSize, il.Register(addrSize, operand.components[1]),
il.Const(1, GetShiftCountForScale(operand.scale)))), il.Const(addrSize, operand.immediate)));
}
if (operand.segment == SEG_FS)
return il.Operand(i, il.Add(addrSize, il.Register(addrSize, REG_FSBASE), offset));
if (operand.segment == SEG_GS)
return il.Operand(i, il.Add(addrSize, il.Register(addrSize, REG_GSBASE), offset));
return offset;
}
static size_t ReadILOperand(LowLevelILFunction& il, Instruction& instr, size_t i, size_t addrSize, bool isAddress = false)
{
InstructionOperand& operand = instr.operands[i];
switch (operand.operand)
{
case NONE:
return il.Undefined();
case IMM:
if (isAddress)
return il.Operand(i, il.ConstPointer(operand.size, operand.immediate));
else
return il.Operand(i, il.Const(operand.size, operand.immediate));
case MEM:
return il.Operand(i, il.Load(operand.size, GetILOperandMemoryAddress(il, operand, i, addrSize)));
default:
return il.Operand(i, il.Register(operand.size, operand.operand));
}
}
static size_t WriteILOperand(LowLevelILFunction& il, Instruction& instr, size_t i, size_t addrSize, size_t value)
{
InstructionOperand& operand = instr.operands[i];
switch (operand.operand)
{
case NONE:
case IMM:
return il.Undefined();
case MEM:
return il.Operand(i, il.Store(operand.size, GetILOperandMemoryAddress(il, operand, i, addrSize), value));
default:
return il.Operand(i, il.SetRegister(operand.size, operand.operand, value));
}
}
static size_t DirectJump(Architecture* arch, LowLevelILFunction& il, uint64_t target, size_t addrSize)
{
BNLowLevelILLabel* label = il.GetLabelForAddress(arch, target);
if (label)
return il.Goto(*label);
else
return il.Jump(il.ConstPointer(addrSize, target));
}
static void ConditionalJump(Architecture* arch, LowLevelILFunction& il, size_t cond, size_t addrSize, uint64_t t, uint64_t f)
{
BNLowLevelILLabel* trueLabel = il.GetLabelForAddress(arch, t);
BNLowLevelILLabel* falseLabel = il.GetLabelForAddress(arch, f);
if (trueLabel && falseLabel)
{
il.AddInstruction(il.If(cond, *trueLabel, *falseLabel));
return;
}
LowLevelILLabel trueCode, falseCode;
if (trueLabel)
{
il.AddInstruction(il.If(cond, *trueLabel, falseCode));
il.MarkLabel(falseCode);
il.AddInstruction(il.Jump(il.ConstPointer(addrSize, f)));
return;
}
if (falseLabel)
{
il.AddInstruction(il.If(cond, trueCode, *falseLabel));
il.MarkLabel(trueCode);
il.AddInstruction(il.Jump(il.ConstPointer(addrSize, t)));
return;
}
il.AddInstruction(il.If(cond, trueCode, falseCode));
il.MarkLabel(trueCode);
il.AddInstruction(il.Jump(il.ConstPointer(addrSize, t)));
il.MarkLabel(falseCode);
il.AddInstruction(il.Jump(il.ConstPointer(addrSize, f)));
}
static void DirFlagIf(size_t addrSize,
LowLevelILFunction& il,
std::function<void (size_t addrSize, LowLevelILFunction& il)> addPreTestIl,
std::function<void (size_t addrSize, LowLevelILFunction& il)> addDirFlagSetIl,
std::function<void (size_t addrSize, LowLevelILFunction& il)> addDirFlagClearIl)
{
LowLevelILLabel dirFlagSet, dirFlagClear, dirFlagDone;
addPreTestIl(addrSize, il);
il.AddInstruction(il.If(il.Flag(IL_FLAG_D), dirFlagSet, dirFlagClear));
il.MarkLabel(dirFlagSet);
addDirFlagSetIl(addrSize, il);
il.AddInstruction(il.Goto(dirFlagDone));
il.MarkLabel(dirFlagClear);
addDirFlagClearIl(addrSize, il);
il.AddInstruction(il.Goto(dirFlagDone));
il.MarkLabel(dirFlagDone);
}
static void Repeat(size_t addrSize,
Instruction& instr,
LowLevelILFunction& il,
std::function<void (size_t addrSize, LowLevelILFunction& il)> addil)
{
LowLevelILLabel trueLabel, falseLabel, doneLabel;
if (instr.flags & X86_FLAG_ANY_REP)
{
il.AddInstruction(il.Goto(trueLabel));
il.MarkLabel(trueLabel);
il.AddInstruction(il.If(il.CompareEqual(addrSize, il.Register(addrSize, GetCountRegister(addrSize)),
il.Const(addrSize, 0)), doneLabel, falseLabel));
il.MarkLabel(falseLabel);
}
addil(addrSize, il);
if (instr.flags & X86_FLAG_ANY_REP)
{
il.AddInstruction(il.SetRegister(addrSize, GetCountRegister(addrSize),
il.Sub(addrSize, il.Register(addrSize, GetCountRegister(addrSize)),
il.Const(addrSize, 1))));
if (instr.flags & X86_FLAG_REPE)
il.AddInstruction(il.If(il.FlagCondition(LLFC_E), trueLabel, doneLabel));
else if (instr.flags & X86_FLAG_REPNE)
il.AddInstruction(il.If(il.FlagCondition(LLFC_NE), trueLabel, doneLabel));
else
il.AddInstruction(il.Goto(trueLabel));
il.MarkLabel(doneLabel);
}
}
// This is a wrapper for the x86 architecture. Its useful for extending and improving
// the existing core x86 architecture.
class x86ArchitectureExtension: public Architecture
{
Architecture* m_arch;
public:
x86ArchitectureExtension() : Architecture("x86_extension")
{
m_arch = new CoreArchitecture(BNGetArchitectureByName("x86"));
}
virtual size_t GetAddressSize() const override
{
return 4;
}
virtual BNEndianness GetEndianness() const override
{
return LittleEndian;
}
virtual bool GetInstructionInfo(const uint8_t* data, uint64_t addr, size_t maxLen, InstructionInfo& result) override
{
return m_arch->GetInstructionInfo(data, addr, maxLen, result);
}
virtual bool GetInstructionText(const uint8_t* data, uint64_t addr, size_t& len, vector<InstructionTextToken>& result) override
{
return m_arch->GetInstructionText(data, addr, len, result);
}
virtual bool GetInstructionLowLevelIL(const uint8_t* data, uint64_t addr, size_t& len, LowLevelILFunction& il) override
{
Instruction instr;
if (!asmx86::Disassemble32(data, addr, len, &instr))
{
il.AddInstruction(il.Undefined());
return false;
}
if (instr.operation == CPUID)
{
// The default implementation of CPUID doesn't set registers to constant values
// Here we'll emulate a Intel(R) Core(TM) i5-6267U CPU @ 2.90GHz with _eax set to 1
il.AddInstruction(il.Register(4, REG_EAX)); // Reference the register so we know it is read
il.AddInstruction(il.SetRegister(4, REG_EAX, il.Const(4, 0x000406e3)));
il.AddInstruction(il.SetRegister(4, REG_EBX, il.Const(4, 0x03100800)));
il.AddInstruction(il.SetRegister(4, REG_ECX, il.Const(4, 0x7ffafbbf)));
il.AddInstruction(il.SetRegister(4, REG_EDX, il.Const(4, 0xbfebfbff)));
len = instr.length;
return true;
}
return m_arch->GetInstructionLowLevelIL(data, addr, len, il);
}
virtual size_t GetFlagWriteLowLevelIL(BNLowLevelILOperation op, size_t size, uint32_t flagWriteType,
uint32_t flag, BNRegisterOrConstant* operands, size_t operandCount, LowLevelILFunction& il) override
{
return m_arch->GetFlagWriteLowLevelIL(op,size, flagWriteType, flag, operands, operandCount, il);
}
virtual string GetRegisterName(uint32_t reg) override
{
return m_arch->GetRegisterName(reg);
}
virtual string GetFlagName(uint32_t flag) override
{
return m_arch->GetFlagName(flag);
}
virtual vector<uint32_t> GetAllFlags() override
{
return m_arch->GetAllFlags();
}
virtual string GetFlagWriteTypeName(uint32_t flags) override
{
return m_arch->GetFlagWriteTypeName(flags);
}
virtual vector<uint32_t> GetAllFlagWriteTypes() override
{
return m_arch->GetAllFlagWriteTypes();
}
virtual BNFlagRole GetFlagRole(uint32_t flag) override
{
return m_arch->GetFlagRole(flag);
}
virtual vector<uint32_t> GetFlagsRequiredForFlagCondition(BNLowLevelILFlagCondition cond) override
{
return m_arch->GetFlagsRequiredForFlagCondition(cond);
}
virtual vector<uint32_t> GetFlagsWrittenByFlagWriteType(uint32_t writeType) override
{
return m_arch->GetFlagsWrittenByFlagWriteType(writeType);
}
virtual bool IsNeverBranchPatchAvailable(const uint8_t* data, uint64_t addr, size_t len) override
{
return m_arch->IsNeverBranchPatchAvailable(data, addr, len);
}
virtual bool IsAlwaysBranchPatchAvailable(const uint8_t* data, uint64_t addr, size_t len) override
{
return m_arch->IsAlwaysBranchPatchAvailable(data, addr, len);
}
virtual bool IsInvertBranchPatchAvailable(const uint8_t* data, uint64_t addr, size_t len) override
{
return m_arch->IsInvertBranchPatchAvailable(data, addr, len);
}
virtual bool IsSkipAndReturnZeroPatchAvailable(const uint8_t* data, uint64_t addr, size_t len) override
{
return m_arch->IsSkipAndReturnZeroPatchAvailable(data, addr, len);
}
virtual bool IsSkipAndReturnValuePatchAvailable(const uint8_t* data, uint64_t addr, size_t len) override
{
return m_arch->IsSkipAndReturnValuePatchAvailable(data, addr, len);
}
virtual bool ConvertToNop(uint8_t* data, uint64_t addr, size_t len) override
{
return m_arch->ConvertToNop(data, addr, len);
}
virtual bool AlwaysBranch(uint8_t* data, uint64_t addr, size_t len) override
{
return m_arch->AlwaysBranch(data, addr, len);
}
virtual bool InvertBranch(uint8_t* data, uint64_t addr, size_t len) override
{
return m_arch->InvertBranch(data, addr, len);
}
virtual bool SkipAndReturnValue(uint8_t* data, uint64_t addr, size_t len, uint64_t value) override
{
return m_arch->SkipAndReturnValue(data, addr, len, value);
}
virtual vector<uint32_t> GetFullWidthRegisters() override
{
return m_arch->GetFullWidthRegisters();
}
virtual vector<uint32_t> GetGlobalRegisters() override
{
return m_arch->GetGlobalRegisters();
}
virtual vector<uint32_t> GetAllRegisters() override
{
return m_arch->GetAllRegisters();
}
virtual BNRegisterInfo GetRegisterInfo(uint32_t reg) override
{
return m_arch->GetRegisterInfo(reg);
}
virtual uint32_t GetStackPointerRegister() override
{
return m_arch->GetStackPointerRegister();
}
virtual bool Assemble(const string& code, uint64_t addr, DataBuffer& result, string& errors) override
{
return m_arch->Assemble(code, addr, result, errors);
}
};
extern "C"
{
BINARYNINJAPLUGIN bool CorePluginInit()
{
Architecture* x86ext = new x86ArchitectureExtension();
Architecture::Register(x86ext);
// Register the architectures with the binary format parsers so that they know when to use
// these architectures for disassembling an executable file
BinaryViewType::RegisterArchitecture("ELF", 3, LittleEndian, x86ext);
BinaryViewType::RegisterArchitecture("PE", 0x14c, LittleEndian, x86ext);
BinaryViewType::RegisterArchitecture("Mach-O", 0x00000007, LittleEndian, x86ext);
x86ext->SetBinaryViewTypeConstant("ELF", "R_COPY", 5);
x86ext->SetBinaryViewTypeConstant("ELF", "R_JUMP_SLOT", 7);
return true;
}
}
|