DLL Redirection — debugging techniques for Windows Applications

Posted at Jun 07, 11:31h in linker Macrium Software, Marketing Categories: linker, windows-internals, debugging, dll, devteam

Just how exactly should that API be called?

When you’re dealing with a simple Windows API call such as MessageBox, you can probably deduce what you should be doing via an experiment or two, but some API calls are non-trivial and involve a lot of set up, particularly the likes of Volume Shadow Copy, and some more obscure, undocumented APIs.

Luckily, there is a way to trivially work out how applications use these APIs, if you can identify one that does what you think it should. Firstly, dependency walker should be used to profile the calls an application is making and which DLLs are being made.

The next thing you need to know is that it is possible to redirect DLL function calls in Windows. This technique means you can effectively replace a system DLL with your own DLL, provided you redirect all of the symbols in it to the original.

So the next thing you need to do is to list the functions exported by a DLL. The SDK gives you a utility to do this — here’s an example:

C:> dumpbin /exports c:windowssystem32kernel32.dll
Microsoft (R) COFF/PE Dumper Version 11.00.50214.1
Copyright (C) Microsoft Corporation. All rights reserved.
Dump of file c:windowsSystem32kernel32.dll
File Type: DLL
Section contains the following exports for KERNEL32.dll
00000000 characteristics
4E20FCBC time date stamp Sat Jul 16 03:51:40 2011
0.00 version
1 ordinal base
1390 number of functions
1390 number of names
ordinal hint RVA      name
...
138 89 00023190 CreateFileA
139 8A 0000EAD0 CreateFileMappingA
140 8B 00064FB0 CreateFileMappingNumaA
141 8C 0004C550 CreateFileMappingNumaW
142 8D 0000F9F0 CreateFileMappingW
143 8E 000759C0 CreateFileTransactedA
144 8F 00075820 CreateFileTransactedW
145 90 00011870 CreateFileW
...
Since I detest the very sight of VBScript, I chose to write the next part in Python. This essentially processes the raw output from dumpbin, producing a header which includes linker pragmas to set up DLL redirection.
[code language="python"]
#!/usr/bin/env python
import re
import sys
def get_functions(file_path):
funcs = []
exports = re.compile('s+d+s+[A-Fa-f0-9]+s+'
'[A-Fa-f0-9]+s+(?P<functionname>S+)')
redirexps = re.compile('s+d+s+[A-Fa-f0-9]+s+(?P<functionname>S+)'
's+(forwarded to '
'(?P<forward_name>[A-Za-z0-9_.]+))')
with open(file_path, "rb") as f:
while True:
line = f.readline()
if not line:
break
matches = exports.match(line)
if matches:
funcs.append(matches.group('functionname'))
matches = redirexps.match(line)
if matches:
funcs.append(matches.group('functionname'))

return funcs
def convert_to_pragma(funcs, orig_dll):
return ["#pragma comment(linker, "/export:%s=%s.%s,@%d")" %
(func, orig_dll, func, i) for i, func in enumerate(funcs, 1)]
def write_defs(funcs, output_file):
deffile_text = "LIBRARY vssapi.dllnn"
deffile_text += "EXPORTSn"
for func in funcs:
"""
special processing is needed for C++ redirects
"""
deffile_text += " %s=Redirected%sn" % (func, func)

deffile_text += "nn"
with open(output_file, "wb") as f:
f.write(deffile_text)
def write_full_header(pragma_lines, output):
header_text = "/* DLL Redirection Header */ nn"
header_text += "#pragma oncenn"
for p in pragma_lines:
header_text += "%sn" % (p)
header_text += "nn"
with open(output, "wb") as f:
f.write(header_text)
if __name__ == "__main__":
input_file = sys.argv[1]
target_dll = sys.argv[2]
output_prefix = sys.argv[3]
output_header = "%s.h" % (output_prefix)
output_defs = "%s.def" % (output_prefix)

write_full_header(convert_to_pragma(get_functions(input_file), target_dll),
output_header)
write_defs(get_functions(input_file), output_defs)
[/code]
If you include this header in a project named after the DLL you want to intercept, in our case kernel32.dll, and rename the original dll kernel32_0.dll, paste these into the application directory, depends.exe will show you this:
Kernel32.dll Redirection in Dependency Walker
In other words, all the calls to kernel32.dll (ours) are being redirected to the original - note we've some interesting behaviour going on, because our kernel32.dll uses the real kernel32.dll...
Now, here's the magic. We do not have to redirect those functions - we could export symbols matching the names of those functions ourselves, and do whatever we felt like doing.
This is how you work out how an obscure API call is used - replace it with a stub which does two things:
  • Writes the arguments somewhere useful, e.g. a log file
  • Calls the original
It's that simple. The possibilities extend way beyond this, of course. You can write any code you like in place of the existing function call. A brief function would look like this:
[code language="cpp"]
typedef HANDLE (WINAPI *pCreateFileW)(LPCTSTR,DWORD,DWORD,LPSECURITY_ATTRIBUTES,
DWORD, DWORD, HANDLE);
extern "C" HANDLE WINAPI RedirectedCreateFileW (
LPCTSTR lpFileName,
DWORD dwDesiredAccess,
DWORD dwShareMode,
LPSECURITY_ATTRIBUTES lpSA,
DWORD dwCreationDisposition,
DWORD dwFlagsAndAttributes,
HANDLE hTemplateFile)
{
HMODULE hOrigLib = LoadLibrary(L"kernel32_0.dll");
pCreateFileW orig_func = (pCreateFileW)
GetProcAddress(hOrigLib, "CreateFileW");
// do something here
HANDLE h = orig_func(lpFileName, dwDesiredAccess, dwShareMode, lpSA,
dwCreationDisposition, dwFlagsAndAttributes, hTemplateFile);
CloseHandle(hOrigLib);
return h;
}
[/code]
Of course, this method is quite intrusive, requiring copying DLLs into place. On the plus side, it works with delay-loaded DLLs via LoadLibrary, for which IAT Hooking fails. Since we're not needing to hide with this technique, for what we want it makes perfect sense.

Previous Post

How to discover SQL instances

Next Post

Supporting Windows XP SP2+ with vNext

Recent Posts

  • Introducing Macrium Site Manager 8.1
  • How to Create Strong Passwords and Remember Them
  • Macrium awarded UK’s Best Workplaces™ recognition!
  • World Backup Day 2023
  • Macrium Software is officially one of the UK's Best Workplaces™ for Wellbeing!

Tags

All Tags
backup
macrium-reflect
product
cybersecurity
company
macrium
microsoft
infosec
techie-tuesday

Archive

All >
2023
2022
2021
2020
2019
2018
2017
2016
2015
2014
2013
2012

哆哆女性网在年代文中不思进取六月二十七日望湖楼醉书不锈钢方钢起名诗词 男孩人名男孩起名字姓许属猴的人起名适合什么生猪宝宝起名喜用字甜蜜演员表双胎起的男孩名字大全gif动画素材电脑开机蓝屏书店起什么名好魔兽风云雄霸天下重生之苦尽甘来疯巫妖的实验日志甜食起名姓郑 起名字新白蛇问仙2018年历华中科技大学文华学院地址给通讯公司起名大全集起名姓名 周易八戒网站免费观看视频黄宗羲三才五格起名软件好武汉同志磁盘被写保护儿童玩具店起名文星高照注册金属公司起名淀粉肠小王子日销售额涨超10倍罗斯否认插足凯特王妃婚姻不负春光新的一天从800个哈欠开始有个姐真把千机伞做出来了国产伟哥去年销售近13亿充个话费竟沦为间接洗钱工具重庆警方辟谣“男子杀人焚尸”男子给前妻转账 现任妻子起诉要回春分繁花正当时呼北高速交通事故已致14人死亡杨洋拄拐现身医院月嫂回应掌掴婴儿是在赶虫子男孩疑遭霸凌 家长讨说法被踢出群因自嘲式简历走红的教授更新简介网友建议重庆地铁不准乘客携带菜筐清明节放假3天调休1天郑州一火锅店爆改成麻辣烫店19岁小伙救下5人后溺亡 多方发声两大学生合买彩票中奖一人不认账张家界的山上“长”满了韩国人?单亲妈妈陷入热恋 14岁儿子报警#春分立蛋大挑战#青海通报栏杆断裂小学生跌落住进ICU代拍被何赛飞拿着魔杖追着打315晚会后胖东来又人满为患了当地回应沈阳致3死车祸车主疑毒驾武汉大学樱花即将进入盛花期张立群任西安交通大学校长为江西彩礼“减负”的“试婚人”网友洛杉矶偶遇贾玲倪萍分享减重40斤方法男孩8年未见母亲被告知被遗忘小米汽车超级工厂正式揭幕周杰伦一审败诉网易特朗普谈“凯特王妃P图照”考生莫言也上北大硕士复试名单了妈妈回应孩子在校撞护栏坠楼恒大被罚41.75亿到底怎么缴男子持台球杆殴打2名女店员被抓校方回应护栏损坏小学生课间坠楼外国人感慨凌晨的中国很安全火箭最近9战8胜1负王树国3次鞠躬告别西交大师生房客欠租失踪 房东直发愁萧美琴窜访捷克 外交部回应山西省委原副书记商黎光被逮捕阿根廷将发行1万与2万面值的纸币英国王室又一合照被质疑P图男子被猫抓伤后确诊“猫抓病”

哆哆女性网 XML地图 TXT地图 虚拟主机 SEO 网站制作 网站优化