forked from WithSecureOpenSource/Sulo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
381 lines (293 loc) · 11.2 KB
/
main.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
/*
Copyright 2014 F-Secure
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
#include "ASMethodInfo.h"
#include "FlashPlayerConfigBuilder.h"
#include "Logger.h"
#include "SuloPluginManager.h"
#include "pin.h"
#include <iostream>
#include <map>
// Pin uses some base types that conflict with Windows types,
// see: https://software.intel.com/sites/landingpage/pintool/docs/49306/Pin/html/index.html#namespace
namespace WINDOWS
{
#include <windows.h>
}
// Flash Player config: all RVAs, data structure offfsets, etc.
static Config* config = NULL;
// Map of addresses of methods that have been verified
static map<ADDRINT,bool> verified;
// Whether the return address should be instrumented
static map<ADDRINT,ADDRINT> returnAddressToInstument;
// Addresses that verified methods are known to return to
static map<ADDRINT,bool> knownVerifiedMethodReturnAddress;
// Whether to log the return value or not. The last item in the vector is the latest call, the first the oldest
static map<ADDRINT,vector<bool>*> logReturnValue;
// Fast mode uses these to store the return address that should be instrumented
ADDRINT interestingReturnAddress = NULL;
ADDRINT stackPtrInterestingReturnAddress = NULL;
// These are used to help PIN inline some of the analysis call.
// If the value is true, the method is *potential* for analyzing (i.e. can have false positives)
#define PRIME (99991)
static bool potentialVerifiedMethod[PRIME];
static bool potentialVerifiedMethodReturnAddress[PRIME];
// General commandline options
KNOB<bool> KnobFast(KNOB_MODE_WRITEONCE, "pintool", "fast", "false", "Runs plugins but no call trace generation");
// Commandline options for call tracer plugin
KNOB<bool> KnobCallTracerEarlyTracing(KNOB_MODE_WRITEONCE, "pintool", "early_tracing", "false", "Start logging before actual Flash code runs?");
KNOB<string> KnobCallTracerTraceFile(KNOB_MODE_WRITEONCE, "pintool", "tracefile", "calltrace.txt", "Call trace filename");
// Commandline options for Flash dumper plugin
KNOB<string> KnobFlashDumperFilePrefix(KNOB_MODE_WRITEONCE, "pintool", "flash_dump_prefix", "dumped", "Filename prefix for dumped Flash objects");
// Commandline options for SecureSWF plugin
KNOB<string> KnobSecureSWFMethodName(KNOB_MODE_WRITEONCE, "pintool", "secureswf", "", "Name of the string decryption method");
// Print help message
INT32 Usage()
{
cerr << "This Pintool instruments Adobe Flash Player and logs calls to all ActionScript methods." << endl << endl;
cerr << KNOB_BASE::StringKnobSummary() << endl;
return -1;
}
// Mark the JITed the interpreted method as verified so we know to instrument it
VOID InterpretedMethodVerified(ADDRINT esi)
{
UINT32* methodInfo = (UINT32*)esi;
ADDRINT addr = methodInfo[config->invokerOffsetInMethodInfo/sizeof(UINT32)];
verified[addr] = true;
potentialVerifiedMethod[addr%PRIME] = true;
}
// Mark the JITed method as verified so we know to instrument it
VOID JITedMethodVerified(ADDRINT eax)
{
// EAX holds the address of the method
verified[eax] = true;
potentialVerifiedMethod[eax%PRIME] = true;
}
// This can have false positives, i.e. call is actually not worth analyzing.
// PIN can inline this code, resulting in better performance
ADDRINT ShouldCallBeAnalyzed(UINT32* esp, ADDRINT ret_addr, ADDRINT dest)
{
return (potentialVerifiedMethod[dest%PRIME] + potentialVerifiedMethodReturnAddress[ret_addr%PRIME]);
}
VOID MethodCallAnalysisFast(UINT32* esp, ADDRINT ret_addr, ADDRINT dest)
{
// Perform the necessary checks to filter out ShouldCallBeAnalyzed false positives
if (verified.find(dest) == verified.end() && knownVerifiedMethodReturnAddress.find(ret_addr) == knownVerifiedMethodReturnAddress.end())
{
return;
}
UINT32* methodEnv = (UINT32*)esp[0];
UINT32 argc = esp[1];
UINT32* argv = (UINT32*)esp[2];
UINT32* pMethodInfo = (UINT32*)methodEnv[config->methodInfoOffsetInMethodEnv/4];
ASMethodInfo* mi = ASMethodInfo::create(pMethodInfo);
string fullMethodName = mi->getMethodNameWithTraits();
bool shouldLogReturnValue = SuloPluginManager::beforeMethodCall(mi, fullMethodName, argc, argv);
if (shouldLogReturnValue)
{
interestingReturnAddress = ret_addr;
stackPtrInterestingReturnAddress = (ADDRINT)esp;
}
}
VOID MethodCallAnalysis(UINT32* esp, ADDRINT ret_addr, ADDRINT dest)
{
// Perform the necessary checks to filter out ShouldCallBeAnalyzed false positives
if (verified.find(dest) == verified.end() && knownVerifiedMethodReturnAddress.find(ret_addr) == knownVerifiedMethodReturnAddress.end())
{
return;
}
knownVerifiedMethodReturnAddress[ret_addr] = true;
potentialVerifiedMethodReturnAddress[ret_addr%PRIME] = true;
// This is not a call to verified method...
if (verified.find(dest) == verified.end())
{
// ...but we'e seen calls to verified methods from this same address earlier,
// so let's skip the return value
if (logReturnValue.find(ret_addr) != logReturnValue.end())
{
if (logReturnValue[ret_addr] == NULL)
{
logReturnValue[ret_addr] = new vector<bool>;
}
logReturnValue[ret_addr]->push_back(false);
}
return;
}
if (logReturnValue[ret_addr] == NULL)
{
logReturnValue[ret_addr] = new vector<bool>;
}
logReturnValue[ret_addr]->push_back(true);
UINT32* methodEnv = (UINT32*)esp[0];
UINT32 argc = esp[1];
UINT32* argv = (UINT32*)esp[2];
UINT32* pMethodInfo = (UINT32*)methodEnv[config->methodInfoOffsetInMethodEnv/4];
// Read/parse the full method name
ASMethodInfo* mi = ASMethodInfo::create(pMethodInfo);
string fullMethodName = mi->getMethodNameWithTraits();
SuloPluginManager::beforeMethodCall(mi, fullMethodName, argc, argv);
}
ADDRINT ShouldReturnAddressBeAnalyzedFast(ADDRINT ret_addr)
{
// This is NULL if we are not interested in logging the return value
return interestingReturnAddress;
}
ADDRINT ShouldReturnAddressBeAnalyzed(ADDRINT ret_addr)
{
if (logReturnValue.find(ret_addr) == logReturnValue.end())
{
return 0;
}
if (logReturnValue[ret_addr] == NULL)
{
return 0;
}
if (logReturnValue[ret_addr]->size() == 0)
{
return 0;
}
return 1;
}
VOID ReturnValueAnalysisFast(ADDRINT ret_addr, UINT32 eax, ADDRINT esp)
{
if (ret_addr != interestingReturnAddress || esp != stackPtrInterestingReturnAddress)
{
return;
}
interestingReturnAddress = NULL;
SuloPluginManager::afterMethodCall(eax);
}
VOID ReturnValueAnalysis(ADDRINT ret_addr, UINT32 eax, ADDRINT esp)
{
bool log = logReturnValue[ret_addr]->back();
if (log)
{
SuloPluginManager::afterMethodCall(eax);
}
logReturnValue[ret_addr]->pop_back();
}
// Pin calls this function before a code sequence is executed for the first time
VOID TraceCalls(INS ins, VOID *v)
{
// If we don't have a proper config, we cannot instrument anything
if (config == NULL)
{
return;
}
ADDRINT addr = INS_Address(ins);
IMG img = IMG_FindByAddress(addr);
// We are interested only calls from the JITted code. That code is not part of any valid image
if (IMG_Valid(img) && img != config->img)
{
return;
}
// We don't know the origins of the calls (only the targets) so we need to instrument every call
if (INS_IsCall(ins))
{
bool ok = false;
if (INS_RegRContain(ins, REG_EAX) || INS_RegRContain(ins, REG_EDX))
{
ok = true;
}
else if (INS_IsDirectCall(ins))
{
ADDRINT target_addr = INS_DirectBranchOrCallTargetAddress(ins);
IMG target_img = IMG_FindByAddress(target_addr);
if (!IMG_Valid(img) || img == config->img)
{
ok = true;
}
}
if (ok)
{
// Select which call analysis function to use depending on whether we are in fast mode
AFUNPTR analysisFunc = (AFUNPTR)MethodCallAnalysis;
if (KnobFast.Value())
{
analysisFunc = (AFUNPTR)MethodCallAnalysisFast;
}
ADDRINT ret_addr = INS_NextAddress(ins);
INS_InsertIfCall(ins, IPOINT_BEFORE, (AFUNPTR)ShouldCallBeAnalyzed, IARG_FUNCARG_CALLSITE_REFERENCE, 0, IARG_ADDRINT, ret_addr, IARG_BRANCH_TARGET_ADDR, IARG_END);
INS_InsertThenCall(ins, IPOINT_BEFORE, analysisFunc, IARG_FUNCARG_CALLSITE_REFERENCE, 0, IARG_ADDRINT, ret_addr, IARG_BRANCH_TARGET_ADDR, IARG_END);
returnAddressToInstument[ret_addr] = true;
return;
}
}
if (returnAddressToInstument.find(addr) != returnAddressToInstument.end())
{
// Select which call analysis function to use depending on whether we are in fast mode
AFUNPTR analysisFuncIf = (AFUNPTR)ShouldReturnAddressBeAnalyzed;
AFUNPTR analysisFuncThen = (AFUNPTR)ReturnValueAnalysis;
if (KnobFast.Value())
{
analysisFuncIf = (AFUNPTR)ShouldReturnAddressBeAnalyzedFast;
analysisFuncThen = (AFUNPTR)ReturnValueAnalysisFast;
}
INS_InsertIfCall(ins, IPOINT_BEFORE, analysisFuncIf, IARG_ADDRINT, addr, IARG_END);
INS_InsertThenCall(ins, IPOINT_BEFORE, analysisFuncThen, IARG_ADDRINT, addr, IARG_REG_VALUE, REG_EAX, IARG_FUNCARG_CALLSITE_REFERENCE, 0, IARG_END);
return;
}
}
// This routine is executed for each image
VOID ImageLoad(IMG img, VOID *v)
{
if (!FlashPlayerConfigBuilder::instance().isSupportedFlashPlayer(img))
{
return;
}
LOGF("Found supported Flash Player image: %s (0x%x - 0x%x)\n", IMG_Name(img).c_str(), IMG_LowAddress(img), IMG_HighAddress(img));
config = FlashPlayerConfigBuilder::instance().getConfig();
// config->setInterpRVA needs to be chosen in such way that
// the value of _invoker can be found from [ESI+config->invinvokerOffsetInMethodInfookerOffset]
RTN setInterp = RTN_CreateAt(config->loadOffset + config->setInterpRVA, "setInterp");
// config->setInterpRVA needs to be chosen in such way that
// the value of _implGPR can be found from EAX
RTN verifyOnCall = RTN_CreateAt(config->loadOffset + config->verifyOnCallRVA, "verifyOnCall");
if (setInterp == RTN_Invalid() || verifyOnCall == RTN_Invalid())
{
LOGF("Instrumenting Flash Player failed. Check setInterp and verifyOnCall RVAs in config.\n");
return;
}
RTN_Open(setInterp);
RTN_InsertCall(setInterp, IPOINT_BEFORE, (AFUNPTR)InterpretedMethodVerified, IARG_REG_VALUE, REG_ESI, IARG_END);
RTN_Close(setInterp);
RTN_Open(verifyOnCall);
RTN_InsertCall(verifyOnCall, IPOINT_AFTER, (AFUNPTR)JITedMethodVerified, IARG_REG_VALUE, REG_EAX, IARG_END);
RTN_Close(verifyOnCall);
// Register TraceCalls to be called to instrument instructions
INS_AddInstrumentFunction(TraceCalls, 0);
}
// This function is called when the instrumented application exits
VOID Fini(INT32 code, VOID *v)
{
SuloPluginManager::uninit();
LOGF("[END]\n");
}
int main(int argc, char* argv[])
{
// This is needed for the RTN_* functions
PIN_InitSymbols();
// Initialize Pin
if (PIN_Init(argc,argv))
{
return Usage();
}
// Now that we have access to commandline parameters, initialize the plugins
SuloPluginManager::init();
// Register ImageLoad to be called when an image is loaded
IMG_AddInstrumentFunction(ImageLoad, 0);
// Register Fini to be called when the application exists
PIN_AddFiniFunction(Fini, 0);
// Start the program, never returns
PIN_StartProgram();
return 0;
}