-
Notifications
You must be signed in to change notification settings - Fork 55
/
outsideInstruction.c
95 lines (72 loc) · 2.07 KB
/
outsideInstruction.c
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
#include "xor.h"
extern csh csHandle;
extern ks_engine *ksHandle;
uint32_t randNumber = 0;
#define CALL_ZERO_ASM "call 5; add dword ptr ss:[esp], 0xA; push 0x%08X; ret\0"
#define CALL_EAX_ASM "MOV EAX, 0x%08X; CALL EAX\0"
OutsideInstruction *createOustideInstruction(){
OutsideInstruction *tmpOI = NULL;
tmpOI = malloc(sizeof(OutsideInstruction));
if(!tmpOI){
tmpOI = malloc(sizeof(OutsideInstruction));//one last try
if(!tmpOI)
return NULL;
}
memset(tmpOI, 0, sizeof(OutsideInstruction));
return tmpOI;
}
bool addToOutisdeInstructionList(OutsideInstruction *list, cs_insn *insn, uint32_t insnId){
if(!list){
printf("Invalid instruction list.\n");
return false;
}
//only go forward if the instruction is a jump/call
if(insn->id != X86_INS_CALL && insn->id != X86_INS_JMP){
return false;
}
//search for empty spot
OutsideInstruction *tmpOI = list;
while(tmpOI->next){
tmpOI = tmpOI->next;
}
//add it
tmpOI->id = insnId;
tmpOI->type = insn->id;
tmpOI->destinationAddress = insn->detail->x86.operands[0].imm;
//create a new spot
tmpOI->next = createOustideInstruction();
if(!tmpOI){
printf("Failed to create a new spot for a new instruction..");
return false;
}
return true;
}
bool fixOutisdeInstructionList(OutsideInstruction *list){
OutsideInstruction *tmpOI = list;
while(tmpOI){
if(tmpOI->type == X86_INS_CALL){
randNumber = rand()%2;
if(!randNumber){
if(!generateAsm(&tmpOI->fixedOI, &tmpOI->fixedOISize, CALL_ZERO_ASM, tmpOI->destinationAddress)){
printf("Error generating Asm for OIList.\n");
return false;
}
}
else{
if(!generateAsm(&tmpOI->fixedOI, &tmpOI->fixedOISize, CALL_EAX_ASM, tmpOI->destinationAddress)){
printf("Error generating Asm for OIList.\n");
return false;
}
}
}
else if(tmpOI->type == X86_INS_JMP){
//TODO
}
else{
printf("Unknown instruction type in OI list. %d\n", tmpOI->type);
return false;
}
tmpOI = tmpOI->next;
}
return true;
}