8 #include <boost/format.hpp>
9 #include <boost/date_time/posix_time/posix_time.hpp>
10 #include <boost/filesystem.hpp>
11 #include <boost/log/trivial.hpp>
15 #include "instruction.h"
16 #include "analysis_functions.h"
17 #include "tainting_functions.h"
18 #include "resolving_functions.h"
19 #include "logging_functions.h"
21 /*====================================================================================================================*/
23 extern std::map
< ADDRINT
,
24 instruction
> addr_ins_static_map
;
26 extern std::vector
<ADDRINT
> explored_trace
;
28 extern bool in_tainting
;
30 extern map_ins_io dta_inss_io
;
32 extern UINT8 received_msg_num
;
34 extern boost::shared_ptr
<boost::posix_time::ptime
> start_ptr_time
;
35 extern boost::shared_ptr
<boost::posix_time::ptime
> stop_ptr_time
;
38 * @brief instrumentation function.
40 * @param ins current examined instruction.
41 * @param data not used.
44 VOID
ins_instrumenter(INS ins
, VOID
*data
)
46 // logging the parsed instructions statically
47 ADDRINT ins_addr
= INS_Address(ins
);
49 addr_ins_static_map
[ins_addr
] = instruction(ins
);
50 // addr_ins_static_map[ins_addr].contained_image = contained_image_name(ins_addr);
55 // || INS_IsSyscall(ins)
56 // || INS_IsSysret(ins)
60 // omit these instructions
64 if (received_msg_num
== 1)
68 start_ptr_time
.reset(new boost::posix_time::ptime(boost::posix_time::microsec_clock::local_time()));
74 if (INS_IsSyscall(ins
))
76 INS_InsertPredicatedCall(ins
, IPOINT_BEFORE
,
77 (AFUNPTR
)logging_syscall_instruction_analyzer
,
84 INS_InsertPredicatedCall(ins
, IPOINT_BEFORE
,
85 (AFUNPTR
)logging_general_instruction_analyzer
,
89 if (addr_ins_static_map
[ins_addr
].category
== XED_CATEGORY_COND_BR
)
91 // conditional branch logging
92 INS_InsertPredicatedCall(ins
, IPOINT_BEFORE
,
93 (AFUNPTR
)logging_cond_br_analyzer
,
100 if (INS_IsMemoryRead(ins
))
102 // memory read logging
103 INS_InsertPredicatedCall(ins
, IPOINT_BEFORE
,
104 (AFUNPTR
)logging_mem_read_instruction_analyzer
,
106 IARG_MEMORYREAD_EA
, IARG_MEMORYREAD_SIZE
,
111 if (INS_IsMemoryWrite(ins
))
113 // memory written logging
114 INS_InsertPredicatedCall(ins
, IPOINT_BEFORE
,
115 (AFUNPTR
)logging_mem_write_instruction_analyzer
,
117 IARG_MEMORYWRITE_EA
, IARG_MEMORYWRITE_SIZE
,
124 INS_InsertPredicatedCall(ins
, IPOINT_BEFORE
,
125 (AFUNPTR
)tainting_general_instruction_analyzer
,
129 else // in rollbacking
131 /* START RESOLVING */
132 INS_InsertPredicatedCall(ins
, IPOINT_BEFORE
,
133 (AFUNPTR
)resolving_ins_count_analyzer
,
137 if (INS_IsMemoryRead(ins
))
139 // INS_InsertPredicatedCall(ins, IPOINT_BEFORE, (AFUNPTR)resolving_mem_to_st_analyzer,
141 // IARG_MEMORYREAD_EA, IARG_MEMORYREAD_SIZE,
146 if (INS_IsMemoryWrite(ins
))
148 INS_InsertPredicatedCall(ins
, IPOINT_BEFORE
,
149 (AFUNPTR
)resolving_st_to_mem_analyzer
,
151 IARG_MEMORYWRITE_EA
, IARG_MEMORYWRITE_SIZE
,
156 // note that conditional branches are always direct
157 if (addr_ins_static_map
[ins_addr
].category
== XED_CATEGORY_COND_BR
)
159 INS_InsertPredicatedCall(ins
, IPOINT_BEFORE
,
160 (AFUNPTR
)resolving_cond_branch_analyzer
,
167 if (INS_IsIndirectBranchOrCall(ins
))
169 INS_InsertPredicatedCall(ins
, IPOINT_BEFORE
,
170 (AFUNPTR
)resolving_indirect_branch_call_analyzer
,
172 IARG_BRANCH_TARGET_ADDR
,
183 /*====================================================================================================================*/
185 VOID
image_load_instrumenter(IMG loaded_img
, VOID
*data
)
187 std::cout
<< "image_load_instrumenter activated\n";
189 const static std::string
winsock_dll_name("WS2_32.dll");
191 if (received_msg_num
< 1)
193 // verify whether the winsock2 module is loaded
194 boost::filesystem::path
loaded_image_path(IMG_Name(loaded_img
));
195 std::cout
<< "Loaded module: " << loaded_image_path
.filename() << "\n";
196 if (loaded_image_path
.filename() == winsock_dll_name
)
198 std::cout
<< "winsock found\n";
199 RTN recv_function
= RTN_FindByName(loaded_img
, "recv");
200 if (RTN_Valid(recv_function
))
202 std::cout
<< "recv instrumented\n";
203 RTN_Open(recv_function
);
205 RTN_InsertCall(recv_function
, IPOINT_BEFORE
, (AFUNPTR
)logging_before_recv_functions_analyzer
,
206 IARG_FUNCARG_ENTRYPOINT_VALUE
, 1,
208 RTN_InsertCall(recv_function
, IPOINT_AFTER
, (AFUNPTR
)logging_after_recv_functions_analyzer
,
209 IARG_FUNCRET_EXITPOINT_VALUE
,
212 RTN_Close(recv_function
);
215 RTN recvfrom_function
= RTN_FindByName(loaded_img
, "recvfrom");
216 if (RTN_Valid(recvfrom_function
))
218 std::cout
<< "recvfrom instrumented\n";
219 RTN_Open(recvfrom_function
);
221 RTN_InsertCall(recvfrom_function
, IPOINT_BEFORE
,
222 (AFUNPTR
)logging_before_recv_functions_analyzer
,
223 IARG_FUNCARG_ENTRYPOINT_VALUE
, 1,
225 RTN_InsertCall(recvfrom_function
, IPOINT_AFTER
,
226 (AFUNPTR
)logging_after_recv_functions_analyzer
,
227 IARG_FUNCRET_EXITPOINT_VALUE
,
230 RTN_Close(recvfrom_function
);
233 RTN wsarecv_function
= RTN_FindByName(loaded_img
, "WSARecv");
234 if (RTN_Valid(wsarecv_function
))
236 std::cout
<< "WSARecv instrumented\n";
237 RTN_Open(wsarecv_function
);
239 RTN_InsertCall(wsarecv_function
, IPOINT_BEFORE
,
240 (AFUNPTR
)logging_before_wsarecv_functions_analyzer
,
241 IARG_FUNCARG_ENTRYPOINT_VALUE
, 1,
243 RTN_InsertCall(wsarecv_function
, IPOINT_AFTER
,
244 (AFUNPTR
)logging_after_wsarecv_funtions_analyzer
,
247 RTN_Close(wsarecv_function
);
250 RTN wsarecvfrom_function
= RTN_FindByName(loaded_img
, "WSARecvFrom");
251 if (RTN_Valid(wsarecvfrom_function
))
253 std::cout
<< "WSARecvFrom instrumented\n";
254 RTN_Open(wsarecvfrom_function
);
256 RTN_InsertCall(wsarecvfrom_function
, IPOINT_BEFORE
,
257 (AFUNPTR
)logging_before_wsarecv_functions_analyzer
,
258 IARG_FUNCARG_ENTRYPOINT_VALUE
, 1,
260 RTN_InsertCall(wsarecvfrom_function
, IPOINT_AFTER
,
261 (AFUNPTR
)logging_after_wsarecv_funtions_analyzer
,
264 RTN_Close(wsarecvfrom_function
);
272 /*====================================================================================================================*/
274 BOOL
process_create_instrumenter(CHILD_PROCESS created_process
, VOID
* data
)
276 BOOST_LOG_TRIVIAL(warning
)
277 << boost::format("new process created with id %d") % CHILD_PROCESS_GetId(created_process
);