You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Crash when throwing exception inside loaded dll. #4
When throwing and catching an exception inside the "memory-loaded" dll the application crashes with "Unhandled exception at ...". Even though the appropriate catch is in place.
This is when using Visual Studio 2010. I've created a repository that demonstrates this problem. Hopefully i am the one who has missed something and the memory loading is working as intended.
A temporary fix is to disable DEP[1] in the Visual Studio build options. Another possibility is to implement the missing parts of the pe loading process in MemoryModule. I'll provide some code, if I get an allowance from my boss.
Hi JesseKlugmann, any updates on this? Could you provide some code, or give some hints what parts are missing and maybe describe how they should be implemented?
My guess (and I dont know a lot about the subject) is that you have to load the .SAFESEH section of the dll, otherwise the SafeSeh mechanism will terminate the process when an exception occurs, as it cannot verify that the exception handlers are safe. See http://msdn.microsoft.com/en-us/library/9a89h429(v=vs.90).aspx
on x32 the problem in inside RtlIsValidHandler (ntdll.dll). It's check if handler address in module.
For solve this problem you can disable DEP (that's not so cool because some times you can't do this because you inside another app) OR try to find function RtlInsertInvertedFunctionTable and addr of LdrpInvertedFunctionTable because it's all not exported. And call RtlInsertInvertedFunctionTable(LdrpInvertedFunctionTable, 'imagebase', 'sizeofimage') for manually loaded module.
on x64 problem is in two places
first, almost the same as x32. Occurs inside RtlLookupFunctionEntry, because it's module not registered in LdrpInvertedFunctionTable. Solve simple, because on x64 we have exported function RtlAddFunctionTable('addr of exception directory', 'count of handlers', 'imagebase') from ntdll.dll
second, inside RtlPcToFileHeader, which search module imagebase of handler addr in list PEB->Ldr->InLoadOrderModuleList. You can forcibly add your module to peb. You already have some implementation of this in https://github.com/fancycode/MemoryModule/tree/jojo_peb_compat
The "DarkMMap" project found here (https://github.com/DarthTon/DarkMMap) loads libraries from memory and according to the project page, has "Exception handling support (SEH and C++), needs more testing though, but seems reliable". Perhaps someone could take a look at how they implement exception handling support and adopt it to work with MemoryModule.
Activity
JesseKlugmann commentedon Jul 25, 2012
Hi
A temporary fix is to disable DEP[1] in the Visual Studio build options. Another possibility is to implement the missing parts of the pe loading process in MemoryModule. I'll provide some code, if I get an allowance from my boss.
[1] DEP: http://en.wikipedia.org/wiki/Data_Execution_Prevention
fancycode commentedon Oct 29, 2012
Hi JesseKlugmann, any updates on this? Could you provide some code, or give some hints what parts are missing and maybe describe how they should be implemented?
kovidgoyal commentedon Dec 1, 2012
My guess (and I dont know a lot about the subject) is that you have to load the .SAFESEH section of the dll, otherwise the SafeSeh mechanism will terminate the process when an exception occurs, as it cannot verify that the exception handlers are safe. See http://msdn.microsoft.com/en-us/library/9a89h429(v=vs.90).aspx
fancycode commentedon Mar 12, 2013
@JesseKlugmann do you have any hints or links to documentation how this could be implemented?
Zorro1 commentedon Jun 19, 2013
@fancycode
on x32 the problem in inside RtlIsValidHandler (ntdll.dll). It's check if handler address in module.
For solve this problem you can disable DEP (that's not so cool because some times you can't do this because you inside another app) OR try to find function RtlInsertInvertedFunctionTable and addr of LdrpInvertedFunctionTable because it's all not exported. And call RtlInsertInvertedFunctionTable(LdrpInvertedFunctionTable, 'imagebase', 'sizeofimage') for manually loaded module.
on x64 problem is in two places
bigmacattack commentedon Dec 30, 2013
The "DarkMMap" project found here (https://github.com/DarthTon/DarkMMap) loads libraries from memory and according to the project page, has "Exception handling support (SEH and C++), needs more testing though, but seems reliable". Perhaps someone could take a look at how they implement exception handling support and adopt it to work with MemoryModule.
Tsury commentedon Jun 24, 2014
I checked DarkMMap (now deprecated and called Blackbone). It works just fine but doesn't support Windows XP.
I found the relevant code from their library, can someone help incorporate it into MemoryModule? I'm a not too good at Win32...
It's in MMap.cpp:550
///
/// Set custom exception handler to bypass SafeSEH under DEP
///
/// image data
/// true on success
bool MMap::EnableExceptions( ImageContext* pImage )
{
BLACBONE_TRACE( L"ManualMap: Enabling exception support for image '%ls'", pImage->FileName.c_str() );
#ifdef USE64
size_t size = pImage->PEImage.DirectorySize( IMAGE_DIRECTORY_ENTRY_EXCEPTION );
IMAGE_RUNTIME_FUNCTION_ENTRY *pExpTable =
reinterpret_cast<decltype(pExpTable)>(pImage->PEImage.DirectoryAddress( IMAGE_DIRECTORY_ENTRY_EXCEPTION ));
#else
bool safeseh = false;
_process.nativeLdr().InsertInvertedFunctionTable( pImage->imgMem.ptr<void*>(), pImage->PEImage.imageSize(), safeseh );
#endif
}
fancycode commentedon Jun 24, 2014
Could you please create a pull request for the changes or provide a proper diff?
Tsury commentedon Jun 24, 2014
This is just code I found on Blackbone's repository which I found relevant. I lack the knowledge to actually add it to your library.
Here's a direct link to the relevant code:
https://github.com/DarthTon/Blackbone/blob/master/src/BlackBone/MMap.cpp#L550-L595
I think that using your knowledge and Zorro1's advice, you can manage to merge it into your library.
GR-C commentedon Aug 23, 2016
Hello, I'm also interessted in a solution.