Comments (17)
Hello, thank you for your explanation. When the export function is called for the first time, the initialization function of mscoreei.dll will be called first, and the assembly file version will be checked in it and the cli environment will be loaded. Therefore, to execute the export method in .net correctly, the assembly file must be stored on the hard disk, and its location must be specified when calling LdrLoadDllMemoryExW.
from memorymodulepp.
Hello, I just updated the code. I think it can work correctly for you. If it still throws an exception, please tell me your .net framework version and clr.dll file version.
from memorymodulepp.
I don't think I understand why you use MMPP to load .NET assemblies from memory. On the one hand, C++/CLI supports loading assemblies from memory (as shown in Figure 1); on the other hand, if you don’t use C++/CLI, I don’t understand how to call managed code from native code.
I have adjusted the loader so that it can load .NET assemblies (as shown in Figure 2), but it can only be loaded in 32-bit programs, because as far as I know, almost all .NET assemblies have 32-bit PE headers . But Win32 API LoadLibrary can load the .NET assembly correctly even if it is a 64-bit program.
from memorymodulepp.
Hello, No, there are 64bit assemblies.
You need to set the project completely to x64, then use DllExport package to export functions from managed dll.
Here's a sample x64 managed dll which works fine with LoadLibraryW
It has one exported function :
typedef void(*ManagedExportFuncType)(const char* str);
It shows the text parameter in a message box.
ManagedLib_x64.zip
from memorymodulepp.
Nice!, since we are already using detours, can't it be virtualized by hooking some functions?
from memorymodulepp.
Thank you for your reply. During mscoreei.dll initialization, clr.dll will be loaded and CorDllMainForThunk will be called. This function will remap the assembly we want to execute. Therefore, it is not feasible for us to deal with it by hooking.
from memorymodulepp.
Anyway, I found the necessary functions through disassembly and dynamic debugging and hooked them:
CreateFileW
GetFileInformationByHandle
GetFileAttributesExW
CreateFileMappingW
MapViewOfFileEx
UnmapViewOfFile
CloseHandle
GetFileVersion
Then a simple .NET assembly was successfully loaded and called. Due to the complexity of .NET, our hooks cannot guarantee the correct execution of complex assemblies and cross-version compatibility of the .NET framework.
from memorymodulepp.
Good job!
I cloned HandleTlsData and tried a very basic .net dll (the one I sent above) and it didn't work throw a error :
Microsoft C++ exception: HRException at memory location 0x000000852EEFF838.
Is there any extra step I should do?
from memorymodulepp.
Hi. You need to specify the LOAD_FLAGS_HOOK_DOT_NET
flag when calling LdrLoadDllMemoryExW
. When I was debugging this issue, I found that MmpTls made false assertions when processing some threads created by the kernel that the user could not perceive.
from memorymodulepp.
If you don't mind, can you upload a sample solution?
from memorymodulepp.
This bug has been fixed in the 41c1175 commit.
from memorymodulepp.
I still can't make it working on a simple hello world dll, It throw HResult error.
Can you push a example for .net to the branch?
Thanks!
from memorymodulepp.
What is your version of Windows? I tested it on a Windows 7 virtual machine and it also threw an exception, but it worked correctly on another Windows 10 version. Because different versions of clr.dll have different implementations of remapping, it is difficult for us to be compatible with all versions by being compatible with one version.
from memorymodulepp.
Hi there, I have Win 10 21H1 (1904)
from memorymodulepp.
Hello @bb107 and thanks! The sample you uploaded works with the dll nice! But when I use the same in my LdrLoadDll dll gets loaded but just after that it throws Unhandled Exception: OutOfMemoryException.
any idea?
UPDATE 1 : After some digging I found out it's caused by GetProcAddressForCaller
because native .net loads another .net native dll and it calls the function using GetProcAddressForCaller
, Should we hook it too?
from memorymodulepp.
Hi. I cannot reproduce this exception. Please upload a sample that can trigger this exception.
from memorymodulepp.
Sure, I'll upload a sample repo in the first chance i've got.
Thanks for your time and work!
from memorymodulepp.
Related Issues (20)
- Crash!!! HOT 3
- 帧不在模块中 HOT 11
- 链接器选项 /DYNAMICBASE:NO 引发的崩溃 HOT 1
- Go编译的DLL无法加载 HOT 1
- 加载_运行_释放_再加载_再运行,有很大几率报错:帧不在模块中 HOT 4
- Crash after working some time. HOT 13
- Crash Crash Crash Crash Crash Crash HOT 4
- 被加载 dll 报错:系统找不到指定的文件 HOT 13
- 帧不在模块中
- [Feature Request] DLL releases? HOT 9
- 在调用枚举摄像头设备的dll时出现奇怪的崩溃 HOT 17
- 一些崩溃
- 最新版编译的 Dll 中 ReflectiveLoader 导出函数没有了 HOT 1
- Loading a protected DLL HOT 5
- 不能识别通过loadlibrary加载的dll,存在重复加载 HOT 1
- 无论加载任何dll,一旦执行这个VirtualAccessCheck就会报错 STATUS_ACCESS_VIOLATION,注释掉之后工作正常 HOT 4
- 加载最新版sciter.dll崩溃STATUS_ACCESS_VIOLATION错 HOT 4
- 是否可以添加反初始化
- 内存加载的dll可以进行debug么,让调试器识别对应的pdb文件 HOT 3
- Missing ``MemoryGetProcAddress`` && ``MemoryLoadLibraryEx(const void *, size_t, CustomAllocFunc, CustomFreeFunc, CustomLoadLibraryFunc, CustomGetProcAddressFunc, CustomFreeLibraryFunc, void *)`` HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from memorymodulepp.