-
Notifications
You must be signed in to change notification settings - Fork 55
/
trashInstruction.c
68 lines (47 loc) · 1.62 KB
/
trashInstruction.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
#include "xor.h"
#include "trashList.h"
extern csh csHandle;
extern ks_engine *ksHandle;
extern uint32_t randNumber;
TrashInstruction *createTrashInstruction(){
TrashInstruction *tmpTI = NULL;
tmpTI = malloc(sizeof(TrashInstruction));
if(!tmpTI){
tmpTI = malloc(sizeof(TrashInstruction));
if(!tmpTI)
return NULL;
}
memset(tmpTI, 0, sizeof(TrashInstruction));
return tmpTI;
}
bool addToTrashInstructionList(TrashInstruction *list, uint32_t insnId){
TrashInstruction *tmpTI = list;
while(tmpTI->next)
tmpTI = tmpTI->next;
tmpTI->id = insnId;
if(!generateTrashInstruction(tmpTI)) return false;
tmpTI->next = createTrashInstruction();
if(!tmpTI->next)
return false;
return true;
}
bool generateTrashInstruction(TrashInstruction *curTI){
#define MAX_TRASH_INS 10
randNumber = rand()%TRASH_TYPES;
uint8_t *tmpTrashBuffer = NULL;
uint32_t tmpTrashSize = 0, totalTrashSize = 0;
static uint8_t trashBuffer[64];
uint32_t trashNum = rand()%MAX_TRASH_INS + 1;
for(int i = 0; i < trashNum; i++){
if(!generateAsm2(&tmpTrashBuffer, &tmpTrashSize, trashList[randNumber][rand()%trashSizes[randNumber]], cs_reg_name(csHandle, allRegs[randNumber][rand()%regSizes[randNumber]]))) return false;
memcpy(&trashBuffer[totalTrashSize], tmpTrashBuffer, tmpTrashSize);
ks_free(tmpTrashBuffer);
totalTrashSize += tmpTrashSize;
}
curTI->trashInsn = NULL;
curTI->trashInsn = malloc(totalTrashSize);
if(!curTI->trashInsn) return false;
memcpy(curTI->trashInsn, trashBuffer, totalTrashSize);
curTI->trashSize = totalTrashSize;
return true;
}