-
Notifications
You must be signed in to change notification settings - Fork 4
/
NinkoHooks.cpp
119 lines (109 loc) · 2.89 KB
/
NinkoHooks.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
#include "NinkoHooks.h"
// Add your function hooks here, if you so please.
VOID HookFunctions( IMG img )
{
RTN rtn;
if ( (rtn = RTN_FindByName( img, "GetProcAddress" )) != RTN_Invalid() )
{
fprintf( g_outfile, "RTN: %s\r\n", RTN_Name( rtn ).c_str());
HookGetProcAddress( rtn );
}
/*
if ( (rtn = RTN_FindByName( img, "LoadLibrary" )) != RTN_Invalid() )
{
fprintf( outFile, "RTN: %s\r\n", RTN_Name( rtn ).c_str());
HookLoadLibrary( rtn );
}
*/
}
// GetProcAddress Hook
static WINDOWS::FARPROC replacementGetProcAddress(
AFUNPTR functionGetProcAddress,
WINDOWS::HMODULE hModule,
WINDOWS::LPCSTR lpProcName,
CONTEXT *ctx,
ADDRINT caller)
{
WINDOWS::FARPROC retFunc = 0;
char *filename = (char *)calloc(256, sizeof(char));
PIN_CallApplicationFunction(
ctx,
PIN_ThreadId(),
CALLINGSTD_STDCALL, functionGetProcAddress,
PIN_PARG(WINDOWS::FARPROC), &retFunc,
PIN_PARG(WINDOWS::HMODULE), hModule,
PIN_PARG(WINDOWS::LPCSTR), lpProcName,
PIN_PARG_END()
);
WINDOWS::GetModuleFileNameA(hModule, filename, 256);
if (((WINDOWS::DWORD)lpProcName >> 16) == 0)
{
fprintf( g_outfile, "%08x = GetProcAddress(%s,ord:%08x) [caller:%08x]\r\n", (unsigned int)retFunc, filename, lpProcName, caller);
}
else
{
fprintf( g_outfile, "%08x = GetProcAddress(%s,%s) [caller:%08x]\r\n", (unsigned int)retFunc, filename, lpProcName, caller);
}
return retFunc;
}
VOID HookGetProcAddress( RTN rtn )
{
PROTO proto =
PROTO_Allocate( PIN_PARG(WINDOWS::FARPROC),
CALLINGSTD_STDCALL,
"GetProcAddress",
PIN_PARG(WINDOWS::HMODULE), // hModule
PIN_PARG(WINDOWS::LPCTSTR), // lpProcName,
PIN_PARG_END()
);
RTN_ReplaceSignature( rtn, (AFUNPTR)replacementGetProcAddress,
IARG_PROTOTYPE, proto,
IARG_ORIG_FUNCPTR,
IARG_FUNCARG_ENTRYPOINT_VALUE, 0,
IARG_FUNCARG_ENTRYPOINT_VALUE, 1,
IARG_CONTEXT,
IARG_RETURN_IP,
IARG_END
);
PROTO_Free( proto );
}
/* LoadLibrary Hook
* Actually not used, because pin already kinda hooks/lets us know when a module is loaded.
*/
static WINDOWS::HMODULE replacementLoadLibrary(
AFUNPTR functionLoadLibrary,
WINDOWS::LPCTSTR lpFileName,
CONTEXT *ctx,
ADDRINT eip)
{
WINDOWS::HMODULE retModule = 0;
PIN_CallApplicationFunction(
ctx,
PIN_ThreadId(),
CALLINGSTD_STDCALL, functionLoadLibrary,
PIN_PARG(WINDOWS::HMODULE), &retModule,
PIN_PARG(WINDOWS::LPCTSTR), lpFileName,
PIN_PARG_END()
);
fprintf( g_outfile, "%08x = LoadLibrary(%s)\r\n", (unsigned int)retModule, lpFileName);
return retModule;
}
VOID HookLoadLibrary( RTN rtn )
{
PROTO proto =
PROTO_Allocate( PIN_PARG(WINDOWS::HMODULE),
CALLINGSTD_STDCALL,
"LoadLibrary",
PIN_PARG(WINDOWS::LPCTSTR), // lpFileName,
PIN_PARG_END()
);
RTN_ReplaceSignature( rtn, (AFUNPTR)replacementLoadLibrary,
IARG_PROTOTYPE, proto,
IARG_ORIG_FUNCPTR,
IARG_FUNCARG_ENTRYPOINT_VALUE, 0,
IARG_CONTEXT,
IARG_INST_PTR,
IARG_END
);
PROTO_Free( proto );
}