Skip to content
This repository was archived by the owner on Dec 30, 2022. It is now read-only.

Commit 914f70a

Browse files
committed
Updated DLLirant to 0.3 (fixed some issues and added DLL Proxying)
1 parent 8965026 commit 914f70a

File tree

2 files changed

+46
-11
lines changed

2 files changed

+46
-11
lines changed

DLLirant.py

Lines changed: 45 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,12 @@
88
import argparse
99
import subprocess
1010
import pefile
11+
import string
12+
import random
1113

1214
PARSER = argparse.ArgumentParser()
13-
PARSER.add_argument('-f', '--file', type=str, help='define the targeted binary', required=True)
15+
PARSER.add_argument('-f', '--file', type=str, help='define the targeted binary (use it alone without -p or -n)', required=False)
16+
PARSER.add_argument('-p', '--proxydll', type=str, help='create a proxy dll (redirecting all the functions to the original DLL)', required=False)
1417
ARGS = PARSER.parse_args()
1518

1619
# The DLL filenames who starts with one element of this list will not be checked.
@@ -29,15 +32,19 @@
2932
'ole32',
3033
'ninput',
3134
'setupapi',
32-
'mscoree'
35+
'mscoree',
36+
'msvcp_win',
37+
'oleaut32',
38+
'advapi32',
39+
'crypt32'
3340
}
3441

3542
def ascii():
3643
print('·▄▄▄▄ ▄▄▌ ▄▄▌ ▪ ▄▄▄ ▄▄▄· ▐ ▄ ▄▄▄▄▄')
3744
print('██▪ ██ ██• ██• ██ ▀▄ █·▐█ ▀█ •█▌▐█•██ ')
3845
print('▐█· ▐█▌██▪ ██▪ ▐█·▐▀▀▄ ▄█▀▀█ ▐█▐▐▌ ▐█.▪')
3946
print('██. ██ ▐█▌▐▌▐█▌▐▌▐█▌▐█•█▌▐█ ▪▐▌██▐█▌ ▐█▌·')
40-
print('▀▀▀▀▀• .▀▀▀ .▀▀▀ ▀▀▀.▀ ▀ ▀ ▀ ▀▀ █▪ ▀▀▀ v0.2')
47+
print('▀▀▀▀▀• .▀▀▀ .▀▀▀ ▀▀▀.▀ ▀ ▀ ▀ ▀▀ █▪ ▀▀▀ v0.3')
4148

4249
def rreplace(s, old, new):
4350
return (s[::-1].replace(old[::-1],new[::-1], 1))[::-1]
@@ -112,7 +119,8 @@ def check_if_excluded(dll_name):
112119
def get_imports_functions(dll_name, imports):
113120
functions = []
114121
for imp in imports:
115-
functions.append(imp.name.decode('utf-8'))
122+
if imp.name is not None:
123+
functions.append(imp.name.decode('utf-8'))
116124
return functions
117125

118126
def generate_test_dll(functions = None):
@@ -122,13 +130,19 @@ def generate_test_dll(functions = None):
122130
if functions is not None:
123131
for line in fin:
124132
if '##DLL_MAIN##' in line:
125-
fout.write(line.replace('##DLL_MAIN##', ''))
133+
if ARGS.proxydll:
134+
fout.write(line.replace('##DLL_MAIN##', 'CreateThread(NULL, NULL, (LPTHREAD_START_ROUTINE)Main, NULL, NULL, NULL);\nbreak;'))
135+
else:
136+
fout.write(line.replace('##DLL_MAIN##', ''))
126137
elif '##EXPORTED_FUNCTIONS##' in line:
127-
for func in functions:
128-
if len(func) > 0:
129-
exported_functions.append(f'__declspec(dllexport) void {func}()' + '{ Main(); }')
130-
exported_functions = '\n'.join(exported_functions)
131-
fout.write(line.replace('##EXPORTED_FUNCTIONS##', exported_functions))
138+
if ARGS.proxydll:
139+
fout.write(line.replace('##EXPORTED_FUNCTIONS##', functions))
140+
else:
141+
for func in functions:
142+
if len(func) > 0:
143+
exported_functions.append(f'__declspec(dllexport) void {func}()' + '{ Main(); }')
144+
exported_functions = '\n'.join(exported_functions)
145+
fout.write(line.replace('##EXPORTED_FUNCTIONS##', exported_functions))
132146
else:
133147
fout.write(line)
134148
else:
@@ -176,7 +190,28 @@ def check_dll_hijacking(binary_name, binary_original_directory, dll_name, export
176190
input(f'\n\n[+] [!] Admin privs required on {binary_name} start it manually to test the dll hijack and press enter to continue.')
177191
return False
178192

193+
def generate_proxy_dll():
194+
exported_functions = []
195+
196+
letters = string.ascii_letters
197+
name_dll = ''.join(random.choice(letters) for i in range(5))
198+
original_name = os.path.basename(ARGS.proxydll)
199+
200+
pe = pefile.PE(ARGS.proxydll)
201+
202+
for entry in pe.DIRECTORY_ENTRY_EXPORT.symbols:
203+
func = entry.name.decode('utf-8')
204+
exported_functions.append(f'#pragma comment(linker,"/export:{func}={name_dll}.{func},@{entry.ordinal}")')
205+
exported_functions = '\n'.join(exported_functions)
206+
207+
generate_test_dll(exported_functions)
208+
print(f'\n\n[+] Rename the original dll file {name_dll}.dll and copy the compiled dll to the original directory as {original_name}')
209+
179210
def main():
211+
if ARGS.proxydll:
212+
generate_proxy_dll()
213+
sys.exit()
214+
180215
# Create output dir if not exists.
181216
create_dir('output')
182217

DLLirantDLL/dllmain-preset.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@
33
#include <stdlib.h>
44

55
int Main() {
6-
MessageBoxW(0, L"DLL Hijack found!", L"DLL Hijack", 0);
76
FILE* fptr;
87
fptr = fopen("C:\\DLLirant\\output.txt", "w");
98
fprintf(fptr, "%s", "It works !\n");
109
fclose(fptr);
10+
MessageBoxW(0, L"DLL Hijack found!", L"DLL Hijack", 0);
1111
return 1;
1212
}
1313

0 commit comments

Comments
 (0)