-
Notifications
You must be signed in to change notification settings - Fork 4
/
NinkoInstrumentation.cpp
245 lines (212 loc) · 5.86 KB
/
NinkoInstrumentation.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
#include "NinkoInstrumentation.h"
VOID ImageLoad( IMG img, VOID *v )
{
ADDRINT base = IMG_LowAddress(img);
fprintf(g_outfile, "Loading %s\r\nStart 0x%08x-0x%08x\r\n", IMG_Name(img).c_str(),IMG_LowAddress(img), IMG_HighAddress(img));
// hook globally.
if ( g_vars.hook_functions == true )
{
HookFunctions( img );
}
if ( compareFiles( IMG_Name( img ), g_vars.image_name ) == false)
{
return;
}
// Update vars with base address.
g_vars.code_start += base;
g_vars.code_end += base;
if (g_vars.start_log_on_exec != NULL)
{
g_vars.start_log_on_exec += base;
}
if (g_vars.stop_log_on_exec != NULL)
{
g_vars.stop_log_on_exec += base;
}
UpdateIgnoredCode( base );
if (g_vars.code_add != NULL)
{
UpdateCodeAdd( base );
}
fprintf(g_outfile, "Monitoring calls from code 0x%lx-0x%lx\r\n", g_vars.code_start, g_vars.code_end);
// 0 means we want to log all data writes
if (g_vars.data_start != 0)
{
g_vars.data_start += base;
}
// 0xffffffff means we want to log all data writes
if (g_vars.data_end != 0xFFFFFFFF)
{
g_vars.data_end += base;
}
UpdateIgnoredData( base );
if (g_vars.data_add != NULL)
{
UpdateDataAdd( base );
}
fprintf(g_outfile, "Monitoring data writes from 0x%lx-0x%lx\r\n", g_vars.data_start, g_vars.data_end);
}
/*
* ObfuscationWriteAndCallLogger - Main function for setting up our analysis routines.
* First we check if the instructions are in range and that the code isn't ignored.
*
* Logging Writes:
* Next, we check if we care about writes and that the instruction is indeed a write.
* Setup a generic writer that validates that the target address to be written to is
* in range of our data_start - data_end and not ignored.
*
* Logging Calls:
* For calls we have two possibilities, one we want to filter out any jmp/calls that
* jump into the same code block we monitor. Or two, we don't care where the jmps occur
* and we just want to log all that occur in our monitored code range (code_start - code_end)
*/
VOID ObfuscationWriteAndCallLogger( INS ins, VOID *v, const char *disasm, ADDRINT loc )
{
if ( g_vars.start_log_on_exec != NULL && IsStartLoggingLoc( loc ) )
{
ShouldStartLogging( ins, loc );
}
if ( g_vars.stop_log_on_exec != NULL && IsStopLoggingLoc( loc ) )
{
ShouldStopLogging( ins, loc );
}
// Make sure the call is in the range we care about, and it's not ignored.
if ( IsInstructionInRange( loc, false ) == 0 || IsCodeIgnored( loc ) == 1 )
{
return;
}
// make sure we care about even logging writes
if ( g_vars.disable_log_writes == false && INS_IsMemoryWrite( ins ) )
{
SimpleWriteLogger( ins, v, disasm, loc );
}
// For reads
if ( g_vars.disable_log_reads == false )
{
if ( INS_IsMemoryRead(ins) && !INS_IsPrefetch(ins) )
{
SimpleReadLogger( ins, v, disasm, loc, false );
}
else if (INS_HasMemoryRead2(ins) )
{
SimpleReadLogger( ins, v, disasm, loc, true );
}
}
// make sure we care about logging calls
if ( g_vars.disable_log_calls == false && INS_IsBranchOrCall( ins ) )
{
if ( g_vars.ignore_internal_calls )
{
FilteredCallLogger( ins, v, disasm, loc );
}
else
{
SimpleCallLogger( ins, v, disasm, loc );
}
}
}
VOID ShouldStartLogging( INS ins, ADDRINT loc )
{
INS_InsertCall(ins,
IPOINT_BEFORE,
AFUNPTR(StartLogging),
IARG_INST_PTR,
IARG_END);
}
VOID ShouldStopLogging( INS ins, ADDRINT loc )
{
INS_InsertCall(ins,
IPOINT_BEFORE,
AFUNPTR(StopLogging),
IARG_INST_PTR,
IARG_END);
}
VOID SimpleWriteLogger( INS ins, VOID *v, const char *disasm, ADDRINT loc )
{
INS_InsertIfCall(ins,
IPOINT_BEFORE,
AFUNPTR(IsWriteInRange),
IARG_THREAD_ID,
IARG_MEMORYWRITE_EA,
IARG_END);
if (INS_HasFallThrough(ins))
{
INS_InsertThenCall(ins,
IPOINT_AFTER, // note we want AFTER so like, we can see what was written.
AFUNPTR(LogMemoryWrite),
IARG_THREAD_ID,
IARG_MEMORYWRITE_SIZE,
IARG_PTR, disasm, // disassembled string
IARG_INST_PTR, // address of instruction
IARG_END);
}
if (INS_IsBranchOrCall(ins))
{
INS_InsertThenCall(ins,
IPOINT_TAKEN_BRANCH, // note we want AFTER so like, we can see what was written.
AFUNPTR(LogMemoryWrite),
IARG_THREAD_ID,
IARG_MEMORYWRITE_SIZE,
IARG_PTR, disasm, // disassembled string
IARG_INST_PTR, // address of instruction
IARG_END);
}
}
VOID SimpleReadLogger( INS ins, VOID *v, const char *disasm, ADDRINT loc, bool read_2 )
{
int read_ea = IARG_MEMORYREAD_EA;
if ( read_2 )
{
read_ea = IARG_MEMORYREAD2_EA;
}
INS_InsertIfCall(ins,
IPOINT_BEFORE,
AFUNPTR(IsReadInRange),
IARG_THREAD_ID,
read_ea,
IARG_END);
INS_InsertThenCall(ins,
IPOINT_BEFORE,
AFUNPTR(LogMemoryRead),
IARG_THREAD_ID,
read_ea,
IARG_MEMORYREAD_SIZE,
IARG_PTR, disasm, // disassembled string
IARG_INST_PTR, // address of instruction
IARG_END);
}
VOID FilteredCallLogger( INS ins, VOID *v, const char *disasm, ADDRINT loc )
{
INS_InsertIfCall(ins,
IPOINT_BEFORE,
(AFUNPTR)IsCallInternal,
IARG_BRANCH_TARGET_ADDR,
IARG_END);
INS_InsertThenCall(ins,
IPOINT_BEFORE,
(AFUNPTR)LogCall,
IARG_THREAD_ID, // thread ID of the executing thread
IARG_INST_PTR, // address of instruction
IARG_PTR, disasm, // disassembled string
IARG_BRANCH_TARGET_ADDR, // target
IARG_BRANCH_TAKEN, // Non zero if branch is taken
IARG_END);
}
VOID SimpleCallLogger( INS ins, VOID *v, const char *disasm, ADDRINT loc )
{
INS_InsertCall(ins,
IPOINT_BEFORE,
(AFUNPTR)LogCall,
IARG_THREAD_ID, // thread ID of the executing thread
IARG_INST_PTR, // address of instruction
IARG_PTR, disasm, // disassembled string
IARG_BRANCH_TARGET_ADDR, // target
IARG_BRANCH_TAKEN, // Non zero if branch is taken
IARG_END);
}
VOID Instruction(INS ins, VOID *v)
{
const char * disasm = dumpInstruction(ins);
ADDRINT loc = INS_Address(ins);
ObfuscationWriteAndCallLogger( ins, v, disasm, loc );
}