Dewera / Lunar

A lightweight native DLL mapping library that supports mapping directly from memory
MIT License
584 stars 102 forks source link

How to call exported function? #22

Closed MasterSoft24 closed 3 years ago

MasterSoft24 commented 3 years ago

Hi! How I could call exported function from program who inject dll's?

Dewera commented 3 years ago

Map the DLL with it's headers, parse the export directory to get the address of said function, then execute it using some shellcode in the context of the process.

Whilst the library has the functionality to do this internally (and does do this during the mapping process,) I didn't make it public to avoid bloating the public interface.

MasterSoft24 commented 3 years ago

I does

mapper.MapLibrary();

var baseAddr = mapper.DllBaseAddress;

ExportedFunction ef = mapper.GetPEImage().ExportDirectory.GetExportedFunction("MyFunction");

mapper._processContext.CallRoutine(baseAddr + ef.RelativeAddress);

but seems that function does not worked and target process crashed. Maybe I loose somethings?

Dewera commented 3 years ago

Thanks for bringing this to my attention - I was actually just trying to figure out the same thing as I'm going to push through an update for static tls data in the next few days and was having issues with shellcode execution.

Basically, it seems I introduced a bug in a commit somewhere down that makes the shellcode execution method I'm using unreliable (however for some odd reason still works fine in some cases.)

I will try and get a patch for this out before I do the release (once I figure out what the issue actually is.)

Dewera commented 3 years ago

I think I found the issue. You can try the following in your version for the time being

Change the following

var status = Ntdll.NtCreateThreadEx(out var threadHandle, AccessMask.SpecificRightsAll | AccessMask.StandardRightsAll, IntPtr.Zero, process.SafeHandle, address, IntPtr.Zero, ThreadCreationFlags.HideFromDebugger | ThreadCreationFlags.SkipThreadAttach, 0, 0, 0, IntPtr.Zero);

To

var status = Ntdll.NtCreateThreadEx(out var threadHandle, AccessMask.SpecificRightsAll | AccessMask.StandardRightsAll, IntPtr.Zero, process.SafeHandle, address, IntPtr.Zero, 0, 0, 0, 0, IntPtr.Zero);

I'm hoping to swap the execution method to something else soon, but that should work on your copy for the time being

MasterSoft24 commented 3 years ago

You are right. This change has fix bug with target process crash. But my function still not working. During library mapping I've catch exception in LoadDependencies procedure. I've avoid it

            System.Xml.Linq.XDocument manifest = null; // _peImage.ResourceDirectory.GetManifest();

            try
            {
                manifest = _peImage.ResourceDirectory.GetManifest();
            }
            catch { }

            var activationContext = new ActivationContext(manifest, _processContext.Process);

But I dont know how correct it is.

Dewera commented 3 years ago

Can you send me the DLL so I can check that out and do a fix? Discord is Quin#4576

MasterSoft24 commented 3 years ago

Thank you again. Your fixes for https://github.com/Dewera/Lunar/issues/22#issuecomment-805391269 were really helpful. Problem solved