1
votes

I have the following situation:

I have a delphi application {$APPTYPE GUI}. (APP1)

If APP1 gets started, it runs the code between begin and end., just as it should be.

Later, APP1 will be transformed to a DLL (another application will do that - APP2).

APP2 adds the IMAGE_FILE_DLL flag to the Characteristics in the NTFileHeader of APP1. Then APP2 tries to load the DLL (APP1) with LoadLibrary (or some other COM Command to load a dll) but it returns the error:

Windows encountered an internal error while initializing COM libraries.

I've done all this with a C project and used the WinMain function. However it seems not to work in Delphi (APP1 gets not started as a DLL). How is it possible to convert APP1 to a working DLL?

EDIT:

I'm trying to port this code from C to Delphi : http://level-23.info/forum/showthread.php?14721-UAC-Bypass-for-Windows-7-RTM-SP1-Windows-8-DP&p=31749

I've ported it correctly and everything works but the CRYPTBASE.dll (APP1) doesn't start . (See Error above)

In a nutshell: Create a delphi application, add the IMAGE_FILE_DLL characteristics in the file header. Rename it to CRYPTBASE.dll and copy it to C:\Windows\System32\sysprep. Then start sysprep.exe

INFOS HERE: http://www.pretentiousname.com/misc/W7E_Source/win7_uac_poc_details.html

2
I think you'll need to elaborate here. I have absolutely no idea what you mean.David Heffernan
Is there any reason you couldn't export a different function name? The imported WinMain might be conflicting with the host application's WinMain...Joe
I don't understand this at all. And I don't really understand what that code was trying to do.David Heffernan
What's the reason for telling us that your application is $Apptype GUI? That's by default any delphi VCL form application and you don't have to declare it anywhere. There's two types, console and gui, which one did you create?Peter
I think he wants to have an executable that injects itself into another executable "as a dll"Remko

2 Answers

5
votes

WinMain is a just a name, by convention, to use as the entry point of an executable. The convention for DLL's is to use the name DllMain. The Windows loader does not search for WinMain and LoadLibrary does not search for DllMain, it just calls the entrypoint in the pe header.

Delphi doesn't use either, the exported name of the entry point is start.

WinMain signature differs from DllMain (WinMain takes four parameters), my suggestion is to declare a function DllMain and export it in your exe:

function DllMain(hinstDLL: THandle; fdwReason: DWORD; lpvReserverd: Pointer): BOOL; stdcall;
begin
  // do something
end;

  exports
    DllMain;

The code that modifies your exe (in mem I presume) to be a dll should set the entry point to DllMain (get it's address by walking the EAT).

Also: make sure that the relocation table it not stripped (in release mode) as DLL's require it when they are rebased.

1
votes

I don't think you should do that at all. Code is compiled with different assumptions when building EXE and DLLs, it will not work if you simply flip the flag and change the extension.

Trying something like that is a good way to experiment and learn stuff but it's a bad idea for production.

Tell us what are you trying to achieve, in more concrete terms than "To run my DLL as EXE". Why do you need to run your DLL as EXE?

For instance, you can build DLL and then load and call it with rundll32. If you need this for COM, you can build an COM host exe and rely on COM's automatic marshalling to achieve the effect of "as if my code was in the same process". It's all already present in COM.