mirror of
https://github.com/nganhkhoa/CTF-All-In-One.git
synced 2025-03-13 00:17:33 +07:00
70 lines
2.3 KiB
C++
70 lines
2.3 KiB
C++
#include <iostream>
|
|
#include <fstream>
|
|
#include "pin.H"
|
|
|
|
ofstream OutFile;
|
|
|
|
// The running count of instructions is kept here
|
|
// make it static to help the compiler optimize docount
|
|
static UINT64 icount = 0;
|
|
|
|
// This function is called before every instruction is executed
|
|
VOID docount(void *ip) {
|
|
if ((long int)ip == 0x0047b96e) icount++; // 0x0047b960: compare mapanic(provided_flag[i]) with constant_binary_blob[i]
|
|
}
|
|
|
|
// Pin calls this function every time a new instruction is encountered
|
|
VOID Instruction(INS ins, VOID *v)
|
|
{
|
|
// Insert a call to docount before every instruction, no arguments are passed
|
|
INS_InsertCall(ins, IPOINT_BEFORE, (AFUNPTR)docount, IARG_INST_PTR, IARG_END); // IARG_INST_PTR: Type: ADDRINT. The address of the instrumented instruction.
|
|
}
|
|
|
|
KNOB<string> KnobOutputFile(KNOB_MODE_WRITEONCE, "pintool",
|
|
"o", "inscount.out", "specify output file name");
|
|
|
|
// This function is called when the application exits
|
|
VOID Fini(INT32 code, VOID *v)
|
|
{
|
|
// Write to a file since cout and cerr maybe closed by the application
|
|
OutFile.setf(ios::showbase);
|
|
OutFile << "Count " << icount << endl;
|
|
OutFile.close();
|
|
}
|
|
|
|
/* ===================================================================== */
|
|
/* Print Help Message */
|
|
/* ===================================================================== */
|
|
|
|
INT32 Usage()
|
|
{
|
|
cerr << "This tool counts the number of dynamic instructions executed" << endl;
|
|
cerr << endl << KNOB_BASE::StringKnobSummary() << endl;
|
|
return -1;
|
|
}
|
|
|
|
/* ===================================================================== */
|
|
/* Main */
|
|
/* ===================================================================== */
|
|
/* argc, argv are the entire command line: pin -t <toolname> -- ... */
|
|
/* ===================================================================== */
|
|
|
|
int main(int argc, char * argv[])
|
|
{
|
|
// Initialize pin
|
|
if (PIN_Init(argc, argv)) return Usage();
|
|
|
|
OutFile.open(KnobOutputFile.Value().c_str());
|
|
|
|
// Register Instruction to be called to instrument instructions
|
|
INS_AddInstrumentFunction(Instruction, 0);
|
|
|
|
// Register Fini to be called when the application exits
|
|
PIN_AddFiniFunction(Fini, 0);
|
|
|
|
// Start the program, never returns
|
|
PIN_StartProgram();
|
|
|
|
return 0;
|
|
}
|