ApiCallTracer.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
/*
* ApiCallTracer, CC by: hasherezade@op.pl
* Runs with: Intel PIN (https://software.intel.com/en-us/articles/pin-a-dynamic-binary-instrumentation-tool)
*
* Prints to <output_file> :
* 1) calls to external modules
* 2) addresses of transitions from one sections to another
* (helpful in analysing obfuscated apps)
* args:
* -m <module_name> ; Analysed module name (by default same as app name)
* -o <output_path> Output file
*
* saves PID in <output_file>.pid
*/
#include "pin.H"
#include <iostream>
#include <fstream>
#include <set>
#include <map>
#define TOOL_NAME "ApiCallTracer"
#define PAGE_SIZE 0x1000
#define UNKNOWN_ADDR (-1)
/* ================================================================== */
// Global variables
/* ================================================================== */
std::string m_AnalysedApp;
std::string m_Param;
FILE *m_BlocksFile = NULL; // output
INT m_myPid = 0; //PID of application
std::string m_PidFileName;
std::set<ADDRINT> m_blocks; // set of unique blocks addresses
struct s_module {
std::string name;
ADDRINT start;
ADDRINT end;
};
std::map<ADDRINT, s_module> m_Modules;
std::map<ADDRINT, s_module> m_Sections;
const s_module* getModuleByAddr(ADDRINT Address, std::map<ADDRINT, s_module> &modules)
{
std::map<ADDRINT, s_module>::iterator bound = modules.upper_bound(Address);
std::map<ADDRINT, s_module>::iterator itr = modules.begin();
for ( ; itr != bound ; itr++) {
s_module &mod = itr->second;
if (Address >= mod.start && Address < mod.end) {
return &mod;
}
}
return NULL;
}
const bool isPageChanged(ADDRINT Address /* without imagebase */)
{
static ADDRINT prevPageAddr = UNKNOWN_ADDR;
ADDRINT currPageAddr = (Address / PAGE_SIZE);
if (prevPageAddr == UNKNOWN_ADDR || prevPageAddr != currPageAddr) { // execution in different memory page!
prevPageAddr = currPageAddr;
return true;
}
return false;
}
const bool isSectionChanged(ADDRINT Address /* without imagebase */)
{
static s_module* prevModule = NULL;
const s_module* currModule = getModuleByAddr(Address, m_Sections);
if (prevModule != currModule) {
prevModule = (s_module*) currModule;
return true;
}
return false;
}
bool isMyModule(const s_module* mod, std::string name)
{
if (!mod) return false;
std::size_t found = mod->name.find(name);
if (found != std::string::npos) {
return true;
}
return false;
}
/* ===================================================================== */
// Command line switches
/* ===================================================================== */
KNOB<string> KnobOutputFile(KNOB_MODE_WRITEONCE, "pintool",
"o", "", "specify file name for the output");
KNOB<string> KnobModuleName(KNOB_MODE_WRITEONCE, "pintool",
"m", "", "Analysed module name (by default same as app name)");
/* ===================================================================== */
// Utilities
/* ===================================================================== */
/*!
* Print out help message.
*/
INT32 Usage()
{
cerr << "This tool prints out : " << endl <<
"Addresses of redirections into to a new section." << endl << endl;
cerr << KNOB_BASE::StringKnobSummary() << endl;
return -1;
}
/* ===================================================================== */
// Analysis routines
/* ===================================================================== */
/*!
* This function is called for every basic block when it is about to be executed.
* @param[in] numInstInBbl number of instructions in the basic block
* @note use atomic operations for multi-threaded applications
*/
VOID SaveTranitions(ADDRINT Address, UINT32 numInstInBbl)
{
PIN_LockClient();
static s_module *prev_mod = NULL;
static bool is_prevMy = false;
static ADDRINT prevAddr = UNKNOWN_ADDR;
const s_module *mod_ptr = getModuleByAddr(Address, m_Modules);
bool is_currMy = isMyModule(mod_ptr, m_AnalysedApp);
if (is_currMy == false && is_prevMy == true && prevAddr != UNKNOWN_ADDR) {
if (mod_ptr) {
fprintf(m_BlocksFile, "%x;called module: %s:", prevAddr, mod_ptr->name.c_str());
IMG pImg = IMG_FindByAddress(Address);
RTN rtn = RTN_FindByAddress(Address);
if (IMG_Valid(pImg) && RTN_Valid(rtn)) {
const string func = RTN_Name(rtn);
fprintf(m_BlocksFile, "%s", func.c_str());
}
fprintf(m_BlocksFile, "\n");
} else {
fprintf(m_BlocksFile, "%x;called module: ?? [%x];\n", prevAddr, Address);
}
fflush(m_BlocksFile);
}
if (is_currMy) {
ADDRINT addr = Address - mod_ptr->start; // substract module's ImageBase
const s_module* sec = getModuleByAddr(addr, m_Sections);
if (isSectionChanged(addr)) {
std::string name = (sec != NULL) ? sec->name : "?";
fprintf(m_BlocksFile, "%x;sec: %s\n", addr, name.c_str());
fflush(m_BlocksFile);
}
prevAddr = addr; /* update saved */
}
/* update saved */
is_prevMy = is_currMy;
prev_mod = (s_module*) mod_ptr;
PIN_UnlockClient();
}
/* ===================================================================== */
// Instrumentation callbacks
/* ===================================================================== */
VOID Trace(TRACE trace, VOID *v)
{
// Visit every basic block in the trace
for (BBL bbl = TRACE_BblHead(trace); BBL_Valid(bbl); bbl = BBL_Next(bbl))
{
for (INS ins = BBL_InsHead(bbl); INS_Valid(ins); ins = INS_Next(ins)) {
INS_InsertCall(ins, IPOINT_BEFORE,
(AFUNPTR) SaveTranitions,
IARG_INST_PTR,
IARG_UINT32, BBL_NumIns(bbl), IARG_END);
}
}
}
VOID ImageLoad(IMG Image, VOID *v)
{
// Add module into a global map
s_module mod;
mod.name = std::string(IMG_Name(Image));
mod.start = IMG_LowAddress(Image);
mod.end = IMG_HighAddress(Image);
m_Modules[mod.start] = mod;
if (m_myPid == 0 && isMyModule(&mod, m_AnalysedApp)) {
FILE *pidFile = fopen(m_PidFileName.c_str(), "w");
if (pidFile) {
m_myPid = PIN_GetPid();
fprintf(pidFile, "%d\n", m_myPid);
fclose(pidFile);
}
// enumerate sections within the analysed module
for (SEC sec = IMG_SecHead(Image); SEC_Valid(sec); sec = SEC_Next(sec)) {
s_module section;
section.name = SEC_Name(sec);
section.start = SEC_Address(sec) - mod.start;
section.end = section.start + SEC_Size(sec);
m_Sections[section.start] = section;
}
}
}
/*!
* The main procedure of the tool.
* This function is called when the application image is loaded but not yet started.
* @param[in] argc total number of elements in the argv array
* @param[in] argv array of command line arguments,
* including pin -t <toolname> -- ...
*/
int main(int argc, char *argv[])
{
// Initialize PIN library. Print help message if -h(elp) is specified
// in the command line or the command line is invalid
PIN_InitSymbols();
if( PIN_Init(argc,argv) )
{
return Usage();
}
m_AnalysedApp = KnobModuleName.Value();
if (m_AnalysedApp.length() == 0) {
// init App Name (m_AnalysedApp):
for (int i = 1; i < (argc - 1); i++ ) {
if (strcmp(argv[i], "--") == 0) {
m_AnalysedApp = argv[i + 1];
if (i + 2 < argc) {
m_Param = argv[i + 2];
}
break;
}
}
}
// init output file:
string fileName = KnobOutputFile.Value();
if (fileName.empty()) fileName = "output.txt";
m_BlocksFile = fopen(fileName.c_str(), "a+");
m_PidFileName = fileName + ".pid";
// Register function to be called for every loaded module
IMG_AddInstrumentFunction(ImageLoad, 0);
// Register function to be called to instrument traces
TRACE_AddInstrumentFunction(Trace, 0);
cerr << "===============================================" << endl;
cerr << "This application is instrumented by "<< TOOL_NAME << endl;
cerr << "Tracing module: " << m_AnalysedApp << endl;
if (!KnobOutputFile.Value().empty())
{
cerr << "See file " << KnobOutputFile.Value() << " for analysis results" << endl;
}
cerr << "===============================================" << endl;
// Start the program, never returns
PIN_StartProgram();
return 0;
}
/* ===================================================================== */
/* eof */
/* ===================================================================== */