# Porting Windows Dynamic Link Libraries to Linux
## Introduction
This repository contains a library that allows native Linux programs to load
and call functions from a Windows DLL.
As a demonstration, I've ported Windows Defender to Linux.
```
$ ./mpclient eicar.com
main(): Scanning eicar.com...
EngineScanCallback(): Scanning input
EngineScanCallback(): Threat Virus:DOS/EICAR_Test_File identified.
```
### How does it work?
The `peloader` directory contains a custom PE/COFF loader derived from
ndiswrapper. The library will process the relocations and imports, then provide
a `dlopen`-like API. The code supports debugging with gdb (including symbols),
basic block coverage collection, and runtime hooking and patching.
![Is such a thing even possible?](https://media.giphy.com/media/2pDSW8QQU6jRe/giphy.gif)
### What works?
The intention is to allow scalable and efficient fuzzing of self-contained
Windows libraries on Linux. Good candidates might be video codecs,
decompression libraries, virus scanners, image decoders, and so on.
* C++ exception dispatch and unwinding.
* Loading additional symbols from IDA.
* Debugging with gdb (including symbols), breakpoints, stack traces, etc.
* Runtime hooking and patching.
* Support for ASAN and Valgrind to detect subtle memory corruption bugs.
If you need to add support for any external imports, writing stubs is usually
quick and easy.
### Why?
Distributed, scalable fuzzing on Windows can be challenging and inefficient.
This is especially true for endpoint security products, which use complex
interconnected components that span across kernel and user space. This
often requires spinning up an entire virtualized Windows environment to fuzz
them or collect coverage data.
This is less of a problem on Linux, and I've found that porting components of
Windows Antivirus products to Linux is often possible. This allows me to run
the code I’m testing in minimal containers with very little overhead, and
easily scale up testing.
This is just personal opinion, but I also think Linux has better tools. `¯\_(ツ)_/¯`
## Windows Defender
MsMpEng is the Malware Protection service that is enabled by default on Windows
8, 8.1, 10, Windows Server 2016, and so on. Additionally, Microsoft Security
Essentials, System Centre Endpoint Protection and various other Microsoft
security products share the same core engine.
The core component of MsMpEng responsible for scanning and analysis is called
mpengine. Mpengine is a vast and complex attack surface, comprising of handlers
for dozens of esoteric archive formats, executable packers, full system
emulators for various architectures and interpreters for various languages. All
of this code is accessible to remote attackers.
### Building
To build the test client, simply type `make`.
```
$ make
```
### Dependencies
*Note that the `.i686` or `:i386` suffixes are important, we need the 32bit libraries to use the 32bit dll.*
| Fedora / RedHat | Ubuntu / Debian | Comment |
| --------------------- | ----------------------------------- |:---------------------------- |
| `glibc-devel.i686` | `libc6-dev:i386` / `libc6-dev-i386` | Name varies with version. |
| `libgcc.i686` | `gcc-multilib` | |
| `readline-devel.i686` | `libreadline-dev:i386` | Optional, used in mpscript. |
| `cabextract` | `cabextract` | Used to extract definitions. |
You will need to download the 32-bit antimalware update file from this page:
* https://www.microsoft.com/security/portal/definitions/adl.aspx#manual
This should be a direct link to the right file:
* https://go.microsoft.com/fwlink/?LinkID=121721&arch=x86
This will download a file called `mpam-fe.exe`, which is a cabinet file that
can be extracted with `cabextract`. Extract the files into the `engine`
directory:
```
$ cabextract mpam-fe.exe
Extracting cabinet: mpam-fe.exe
extracting MPSigStub.exe
extracting mpavdlta.vdm
extracting mpasdlta.vdm
extracting mpavbase.vdm
extracting mpasbase.vdm
extracting mpengine.dll
All done, no errors.
```
If you want to know which version you got, try this:
```
$ exiftool mpengine.dll | grep 'Product Version Number'
Product Version Number : 1.1.13701.0
```
### Running
The main mpengine loader is called `mpclient`, it accepts filenames to scan as
a parameter.
```
$ ./mpclient netsky.exe
main(): Scanning netsky.exe...
EngineScanCallback(): Scanning input
EngineScanCallback(): Threat Worm:Win32/Netsky.P@mm identified.
```
There are some other sample tools, `mpstreamfuzz` and `mpscript`.
### Debugging
If you want to debug a crash, single step through a routine or set breakpoints,
follow these examples. First, you need a map file from IDA.
Microsoft doesn't release public symbols for every build, and sometimes the
symbols lag behind for a few months after release. Make sure you're using an
mpengine version with public symbols available.
Use the following sample commandline to generate map and idb files.
```
> idaw -A -P+ -S"createmap.idc mpengine.map" mpengine.dll
```
If you generate the map files on Windows, you'll get CRLF line terminators, fix
them like this:
```
$ dos2unix mpengine.map
```
When you run mpclient under gdb, it will detect a debugger and print the
commands you need to enter to teach gdb about the symbols:
```
$ gdb -q ./mpclient
(gdb) r testfile.txt
Starting program: mpclient
main(): GDB: add-symbol-file engine/mpengine.dll 0xf6af4008+0x1000
main(): GDB: shell bash genmapsym.sh 0xf6af4008+0x1000 symbols_19009.o < mpengine.map
main(): GDB: add-symbol-file symbols_19009.o 0
Program received signal SIGTRAP, Trace/breakpoint trap.
0x0804d213 in main (argc=1, argv=0xffffcc64, envp=0xffffcc6c) at mpclient.c:156
156 __debugbreak();
(gdb)
```
If you enter the commands it shows into gdb, you will have symbols available.
> *Note that `genmapsym.sh` assumes you're using GNU awk.*
```
(gdb) add-symbol-file engine/mpengine.dll 0xf6af4008+0x1000
add symbol table from file "engine/mpengine.dll" at
.text_addr = 0xf6af5008
Reading symbols from engine/mpengine.dll...done.
(gdb) shell bash genmapsym.sh 0xf6af4008+0x1000 symbols_19009.o < mpengine.map
(gdb) add-symbol-file symbols_19009.o 0
add symbol table from file "symbols_19009.o" at
.text_addr = 0x0
Reading symbols from symbols_19009.o...done.
(gdb) p as3_parsemetadata_swf_vars_t
$1 = {void (void)} 0xf6feb842 <as3_parsemetadata_swf_vars_t>
```
Then you can continue, and it will run as normal.
```
(gdb) c
```
Breakpoints, watchpoints and backtraces all work as normal, although it may be
more reliable to use hardware breakpoints than software breakpoints.
To use hardware breakpoints in gdb, you just use `hb` or `hbreak` instead of
`break`. Note that you only get a limited number of hardware breakpoints.
```
(gdb) b as3_parsemethodinfo_swf_vars_t
Breakpoint 1 at 0xf6feb8da
(gdb) c
Continuing.
main(): Scanning test/input.swf...
EngineScanCallback(): Scanning input
Breakpoint 1, 0xf6feb8da in as3_parsemethodinfo_swf_vars_t ()
(gdb) bt
#0 0xf6feb8da in as3_parsemethodinfo_swf_vars_t ()
#1 0xf6dbad7f in SwfScanFunc ()
#2 0xf6d73ec3 in UfsScannerWrapper__ScanFile_scanresult_t ()
#3 0xf6d6c9e3 in UfsClientRequest__fscan_SCAN_REPLY ()
#4 0xf6d6a818 in UfsNode__ScanLoopHelper_wchar_t ()
#5 0xf6d6a626 in UfsNode__Analyze_UfsAnalyzeSetup ()
#6 0xf6d71f7f in UfsClientRequest__AnalyzeLeaf_wchar_t ()
#7 0xf6d71bb9 in UfsClientRequest__AnalyzePath_wchar_t ()
#8 0xf6dbbd88 in std___String_alloc_std___String_base_types_char_std__allocator_char______Myptr_void_ ()
#9 0xf6d75e72 in UfsCmdBase__ExecuteCmd__lambda_c80a88e180c1f4524a759d69aa15f87e____lambda_c80a88e180c1f4524a759d69aa15f87e__ ()
Backtrace stopped: previous frame inner to this frame (corrupt stack?)
(gdb) x/3i $pc
=> 0xf6feb8da <as3_parsemethodinfo_swf_vars_t+7>: lea ebx,[edx+0x1c]
0xf6feb8dd <as3
没有合适的资源?快使用搜索试试~ 我知道了~
将Windows动态链接库移植到Linux-C/C++开发
共117个文件
c:61个
h:33个
makefile:5个
需积分: 31 24 下载量 129 浏览量
2021-05-26
20:30:14
上传
评论 2
收藏 807KB ZIP 举报
温馨提示
将Windows Dynamic Link库移植到Linux简介此存储库包含一个库,该库允许本机Linux程序从Windows DLL加载和调用函数。 作为演示,我已将Windows移植Windows动态链接库移植到Linux简介此存储库包含一个库,该库允许本机Linux程序从Windows DLL加载和调用函数。 作为演示,我已将Windows Defender移植到Linux。 $ ./mpclient eicar.com main():正在扫描eicar.com ... EngineScanCallback():正在扫描输入EngineScanCallback():已识别威胁病毒:DOS / EICAR_Test_File。 它是如何工作的? peloader目录包含一个自ndiswrapper派生的自定义PE / COFF加载器。
资源推荐
资源详情
资源评论
收起资源包目录
将Windows动态链接库移植到Linux-C/C++开发 (117个子文件)
AUTHORS 195B
ia32_opcode_tables.c 331KB
x86_format.c 50KB
rtlbitmap.c 29KB
pe_linker.c 24KB
ia32_insn.c 19KB
ia32_implicit.c 14KB
ia32_operand.c 13KB
hook.c 13KB
Files.c 11KB
Crypt.c 10KB
crt.c 10KB
ia32_modrm.c 9KB
mpclient.c 8KB
ia32_invariant.c 8KB
ia32_reg.c 8KB
mpscript.c 7KB
Registry.c 7KB
x86_disasm.c 7KB
Strings.c 6KB
ProcessThreads.c 6KB
Exception.c 5KB
coverage_parse_min.c 5KB
x86_insn.c 5KB
Threads.c 4KB
x86_operand_list.c 4KB
Heap.c 4KB
instrument.c 4KB
Version.c 3KB
Environment.c 3KB
Internal.c 3KB
LoadLibrary.c 3KB
SystemTime.c 3KB
TlsAlloc.c 2KB
Locale.c 2KB
x86_misc.c 2KB
WinTrust.c 2KB
extra.c 2KB
Paths.c 2KB
Memory.c 2KB
tree.c 2KB
log.c 2KB
util.c 2KB
EventTracing.c 1KB
x86_imm.c 1KB
GetStartupInfoW.c 1KB
GetSystemDirectory.c 1KB
CriticalSection.c 1KB
Ole.c 1KB
winstrings.c 1KB
Event.c 1KB
IsProcessorFeaturePresent.c 1KB
GetStdHandle.c 1KB
Handle.c 971B
Process.c 699B
EncodePointer.c 646B
InitializeSListHead.c 606B
GetLastError.c 598B
Debugger.c 522B
Wer.c 493B
Security.c 486B
ia32_settings.c 304B
deepcover.cpp 3KB
.gdbinit 2KB
.gitignore 82B
.gitignore 76B
codealloc.h 216KB
winnt_types.h 58KB
pe_linker.h 42KB
libdis.h 35KB
ia32_insn.h 20KB
rootcert.h 9KB
ntoskernel.h 6KB
crt_exports.h 6KB
streambuffer.h 4KB
rsignal.h 4KB
engineboot.h 3KB
hook.h 2KB
openscan.h 2KB
config.h 2KB
ia32_reg.h 2KB
scanreply.h 1KB
log.h 1KB
ia32_opcode_tables.h 1KB
ia32_settings.h 900B
util.h 815B
winexports.h 638B
tree.h 598B
instrument.h 440B
x86_imm.h 427B
winstrings.h 362B
ia32_implicit.h 355B
ia32_modrm.h 304B
ia32_operand.h 299B
ia32_invariant.h 236B
qword.h 218B
x86_operand_list.h 132B
libdis.h 18B
blacklist.h 0B
createmap.idc 294B
共 117 条
- 1
- 2
资源评论
丰雅
- 粉丝: 641
- 资源: 4580
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- 在不同操作系统下编译Android源码需要更改一些Android源码的配置项,脚本用于自动化更改配置项.zip
- 基于vue3的春节烟花许愿代码.zip学习资料
- YoloV8.2.10的YOLOV8的Segmentation权重文件
- YoloV8.2.10的YOLOV8的Pose权重文件
- 2002 年 Python 周模板 - 4 月 25 日至 29 日 LINUXTips.zip
- 烟花爆炸效果学习代码.zip学习资料开发
- 微信抢红包助手.zip学习资料参考资料程序
- YoloV8.2.10的YOLOV8的Classification权重文件
- 探索Python科学计算:SciPy库的深入指南
- 深入解析栈溢出:原因、影响与解决方案
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功