source upload

This commit is contained in:
Razor12911
2022-01-17 22:16:47 +02:00
parent 12936d065b
commit 098e8c48de
1778 changed files with 1206749 additions and 0 deletions

View File

@@ -0,0 +1,143 @@
program Project2;
{$APPTYPE CONSOLE}
uses
SysUtils, Classes, Windows,
MemoryModule in '..\MemoryModule.pas',
FuncHook in '..\FuncHook.pas',
MemoryModuleHook in '..\MemoryModuleHook.pas';
const
SUsage =
'Test project for loading DLL from memory'+sLineBreak+
'Params:'+sLineBreak+
' [DLL name] (required) - full path to DLL to load'+sLineBreak+
' [Function name] (optional) - function to execute (no parameters, result is DWORD/Handle/Pointer)'+sLineBreak+
'Good testing sample is <%WinDir%\System32\KernelBase.dll> and <GetCurrentThread>';
type
TNativeUIntFunc = function: NativeUInt;
var
ms: TMemoryStream;
lib : TMemoryModule;
func: TNativeUIntFunc;
res: array[0..2] of NativeUInt;
i: Integer;
function GetLibPtrProc(lpLibFileName: PWideChar): Pointer;
begin
// Catch only those paths that start with *, let others go
if lpLibFileName^ = '*' then
Result := ms.Memory;
end;
function CheckLoadLib(lib: Pointer): Boolean;
begin
if lib = nil then
begin
Writeln('Error loading lib '+ParamStr(1)+': '+SysErrorMessage(GetLastError));
Exit(False);
end;
Writeln(ParamStr(1)+' loaded');
Exit(True);
end;
function CheckLoadAndExecFunc(func: TNativeUIntFunc; out res: NativeUInt): Boolean;
begin
if @func = nil then
begin
Writeln('Error loading func '+ParamStr(2)+': '+SysErrorMessage(GetLastError));
Exit(False);
end;
res := func;
Writeln(Format('Function call result: %u (%x)', [res, res]));
Exit(True);
end;
begin
try
if ParamCount = 0 then
begin
Writeln(SUsage);
Exit;
end;
Writeln('===== Test #0, usual load =====');
try
lib := Pointer(LoadLibrary(PChar(ParamStr(1))));
if not CheckLoadLib(lib) then Exit;
if ParamStr(2) <> '' then
begin
func := TNativeUIntFunc(GetProcAddress(HMODULE(lib), PAnsiChar(AnsiString(ParamStr(2)))));
if not CheckLoadAndExecFunc(func, res[0]) then Exit;
end;
finally
FreeLibrary(HMODULE(lib));
end;
Writeln('===== Test #1, load from memory =====');
try
ms := TMemoryStream.Create;
ms.LoadFromFile(ParamStr(1));
ms.Position := 0;
lib := MemoryLoadLibary(ms.Memory);
ms.Free;
if not CheckLoadLib(lib) then Exit;
if ParamStr(2) <> '' then
begin
func := TNativeUIntFunc(MemoryGetProcAddress(lib, PAnsiChar(AnsiString(ParamStr(2)))));
if not CheckLoadAndExecFunc(func, res[1]) then Exit;
end;
finally
MemoryFreeLibrary(lib);
end;
Writeln('===== Test #2, load with hooking =====');
if not InstallHook(@GetLibPtrProc) then
begin
Writeln('Error installing hook');
Exit;
end;
try
ms := TMemoryStream.Create;
ms.LoadFromFile(ParamStr(1));
ms.Position := 0;
// Custom lib names example:
// Adding * char to the lib path for callback to distinguish whether it should act
lib := Pointer(LoadLibrary(PChar('*'+ParamStr(1))));
ms.Free;
if not CheckLoadLib(lib) then Exit;
if ParamStr(2) <> '' then
begin
func := TNativeUIntFunc(GetProcAddress(HMODULE(lib), PAnsiChar(AnsiString(ParamStr(2)))));
if not CheckLoadAndExecFunc(func, res[2]) then Exit;
end;
finally
FreeLibrary(HMODULE(lib));
UninstallHook;
end;
if ParamStr(2) <> '' then
begin
Writeln('===== Test #3, comparing results =====');
for i := Low(res) to High(res) do
if res[i] <> res[0] then
begin
Writeln('Failure! Results vary');
Exit;
end;
Writeln('Success! Results identical')
end;
except on E: Exception do
Writeln('Error: '+E.Message);
end;
Readln;
end.

View File

@@ -0,0 +1,162 @@
 <Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<ProjectGuid>{6C9E4C93-535D-4563-AD0C-384ED9405089}</ProjectGuid>
<ProjectVersion>13.4</ProjectVersion>
<FrameworkType>None</FrameworkType>
<MainSource>Project2.dpr</MainSource>
<Base>True</Base>
<Config Condition="'$(Config)'==''">Debug</Config>
<Platform Condition="'$(Platform)'==''">Win64</Platform>
<TargetedPlatforms>3</TargetedPlatforms>
<AppType>Console</AppType>
</PropertyGroup>
<PropertyGroup Condition="'$(Config)'=='Base' or '$(Base)'!=''">
<Base>true</Base>
</PropertyGroup>
<PropertyGroup Condition="('$(Platform)'=='Win64' and '$(Base)'=='true') or '$(Base_Win64)'!=''">
<Base_Win64>true</Base_Win64>
<CfgParent>Base</CfgParent>
<Base>true</Base>
</PropertyGroup>
<PropertyGroup Condition="('$(Platform)'=='Win32' and '$(Base)'=='true') or '$(Base_Win32)'!=''">
<Base_Win32>true</Base_Win32>
<CfgParent>Base</CfgParent>
<Base>true</Base>
</PropertyGroup>
<PropertyGroup Condition="'$(Config)'=='Debug' or '$(Cfg_1)'!=''">
<Cfg_1>true</Cfg_1>
<CfgParent>Base</CfgParent>
<Base>true</Base>
</PropertyGroup>
<PropertyGroup Condition="('$(Platform)'=='Win32' and '$(Cfg_1)'=='true') or '$(Cfg_1_Win32)'!=''">
<Cfg_1_Win32>true</Cfg_1_Win32>
<CfgParent>Cfg_1</CfgParent>
<Cfg_1>true</Cfg_1>
<Base>true</Base>
</PropertyGroup>
<PropertyGroup Condition="'$(Config)'=='Release' or '$(Cfg_2)'!=''">
<Cfg_2>true</Cfg_2>
<CfgParent>Base</CfgParent>
<Base>true</Base>
</PropertyGroup>
<PropertyGroup Condition="'$(Base)'!=''">
<DCC_Namespace>System;Xml;Data;Datasnap;Web;Soap;Vcl;Vcl.Imaging;Vcl.Touch;Vcl.Samples;Vcl.Shell;$(DCC_Namespace)</DCC_Namespace>
<DCC_CodePage>65001</DCC_CodePage>
<DCC_LocalDebugSymbols>false</DCC_LocalDebugSymbols>
<Icon_MainIcon>$(BDS)\bin\delphi_PROJECTICON.ico</Icon_MainIcon>
<BRCC_CompilerToUse>rc</BRCC_CompilerToUse>
<DCC_DebugInformation>false</DCC_DebugInformation>
<VerInfo_Keys>CompanyName=;FileDescription=;FileVersion=1.0.0.0;InternalName=;LegalCopyright=;LegalTrademarks=;OriginalFilename=;ProductName=;ProductVersion=1.0.0.0;Comments=</VerInfo_Keys>
<BRCC_EnableMultiByte>true</BRCC_EnableMultiByte>
<VerInfo_Locale>1049</VerInfo_Locale>
<DCC_SymbolReferenceInfo>0</DCC_SymbolReferenceInfo>
<DCC_UsePackage>soaprtl;bindcompfmx;fmx;dsnap;rtl;dbrtl;fmxase;bindcomp;fmxobj;xmlrtl;ibxpress;DbxCommonDriver;fmxdae;dbxcds;bindengine;dbexpress;$(DCC_UsePackage)</DCC_UsePackage>
<Manifest_File>None</Manifest_File>
</PropertyGroup>
<PropertyGroup Condition="'$(Base_Win64)'!=''">
<VerInfo_Locale>1033</VerInfo_Locale>
<DCC_Namespace>Winapi;System.Win;Data.Win;Datasnap.Win;Web.Win;Soap.Win;Xml.Win;$(DCC_Namespace)</DCC_Namespace>
<DCC_UsePackage>ZCore;vcltouch;ZComponent;vclribbon;VclSmp;vcl;OverbyteIcsDXe2Run;TeeDB;vclib;ZDbc;Tee;ZPlain;ZParseSql;vclx;vclimg;VirtualTreesR;vclactnband;TeeUI;adortl;vcldb;$(DCC_UsePackage)</DCC_UsePackage>
</PropertyGroup>
<PropertyGroup Condition="'$(Base_Win32)'!=''">
<Debugger_RunParams>&quot;E:\tmp\New Folder2\Targets\TLS_Example_1.exe&quot;</Debugger_RunParams>
<VerInfo_Locale>1033</VerInfo_Locale>
<DCC_Namespace>Winapi;System.Win;Data.Win;Datasnap.Win;Web.Win;Soap.Win;Xml.Win;Bde;$(DCC_Namespace)</DCC_Namespace>
<DCC_UsePackage>sdlbasepack_rt_101X2;ZCore;vcltouch;ZComponent;vclribbon;VclSmp;vcl;frx16;OverbyteIcsDXe2Run;TeeDB;CodeSiteExpressPkg;WizFavoritesP;vclib;ZDbc;Tee;ZPlain;sdlgeopack_101X2;ZParseSql;vclx;sdlmathpack_rt_101X2;WizMenuActionsP;vclimg;fmi;sdlgeopack_rt_101X2;bdertl;VirtualTreesR;vclactnband;TeeUI;adortl;vcldb;WizTabToolsP;$(DCC_UsePackage)</DCC_UsePackage>
<VerInfo_Keys>CompanyName=;FileDescription=;FileVersion=1.0.0.0;InternalName=;LegalCopyright=;LegalTrademarks=;OriginalFilename=;ProductName=;ProductVersion=1.0.0.0;Comments=</VerInfo_Keys>
</PropertyGroup>
<PropertyGroup Condition="'$(Cfg_1)'!=''">
<DCC_CodePage>65001</DCC_CodePage>
<DCC_IntegerOverflowCheck>true</DCC_IntegerOverflowCheck>
<DCC_LocalDebugSymbols>true</DCC_LocalDebugSymbols>
<DCC_MapFile>0</DCC_MapFile>
<DCC_SymbolReferenceInfo>2</DCC_SymbolReferenceInfo>
<DCC_DebugInformation>true</DCC_DebugInformation>
<DCC_RangeChecking>true</DCC_RangeChecking>
<DCC_Define>DEBUG;$(DCC_Define)</DCC_Define>
<DCC_Optimize>false</DCC_Optimize>
<DCC_GenerateStackFrames>true</DCC_GenerateStackFrames>
<DCC_DebugInfoInExe>true</DCC_DebugInfoInExe>
</PropertyGroup>
<PropertyGroup Condition="'$(Cfg_1_Win32)'!=''">
<DCC_DebugDCUs>true</DCC_DebugDCUs>
<Debugger_RunParams>C:\Windows\SysWOW64\KernelBase.dll GetCurrentThread</Debugger_RunParams>
<VerInfo_Locale>1033</VerInfo_Locale>
<DCC_RemoteDebug>false</DCC_RemoteDebug>
</PropertyGroup>
<PropertyGroup Condition="'$(Cfg_2)'!=''">
<DCC_CodePage>65001</DCC_CodePage>
<DCC_LocalDebugSymbols>false</DCC_LocalDebugSymbols>
<DCC_Define>RELEASE;$(DCC_Define)</DCC_Define>
<DCC_SymbolReferenceInfo>0</DCC_SymbolReferenceInfo>
<DCC_DebugInformation>false</DCC_DebugInformation>
</PropertyGroup>
<ItemGroup>
<DelphiCompile Include="$(MainSource)">
<MainSource>MainSource</MainSource>
</DelphiCompile>
<DCCReference Include="..\MemoryModule.pas"/>
<DCCReference Include="..\FuncHook.pas"/>
<DCCReference Include="..\MemoryModuleHook.pas"/>
<BuildConfiguration Include="Release">
<Key>Cfg_2</Key>
<CfgParent>Base</CfgParent>
</BuildConfiguration>
<BuildConfiguration Include="Base">
<Key>Base</Key>
</BuildConfiguration>
<BuildConfiguration Include="Debug">
<Key>Cfg_1</Key>
<CfgParent>Base</CfgParent>
</BuildConfiguration>
</ItemGroup>
<ProjectExtensions>
<Borland.Personality>Delphi.Personality.12</Borland.Personality>
<Borland.ProjectType/>
<BorlandProject>
<Delphi.Personality>
<VersionInfo>
<VersionInfo Name="IncludeVerInfo">False</VersionInfo>
<VersionInfo Name="AutoIncBuild">False</VersionInfo>
<VersionInfo Name="MajorVer">1</VersionInfo>
<VersionInfo Name="MinorVer">0</VersionInfo>
<VersionInfo Name="Release">0</VersionInfo>
<VersionInfo Name="Build">0</VersionInfo>
<VersionInfo Name="Debug">False</VersionInfo>
<VersionInfo Name="PreRelease">False</VersionInfo>
<VersionInfo Name="Special">False</VersionInfo>
<VersionInfo Name="Private">False</VersionInfo>
<VersionInfo Name="DLL">False</VersionInfo>
<VersionInfo Name="Locale">1049</VersionInfo>
<VersionInfo Name="CodePage">1251</VersionInfo>
</VersionInfo>
<VersionInfoKeys>
<VersionInfoKeys Name="CompanyName"/>
<VersionInfoKeys Name="FileDescription"/>
<VersionInfoKeys Name="FileVersion">1.0.0.0</VersionInfoKeys>
<VersionInfoKeys Name="InternalName"/>
<VersionInfoKeys Name="LegalCopyright"/>
<VersionInfoKeys Name="LegalTrademarks"/>
<VersionInfoKeys Name="OriginalFilename"/>
<VersionInfoKeys Name="ProductName"/>
<VersionInfoKeys Name="ProductVersion">1.0.0.0</VersionInfoKeys>
<VersionInfoKeys Name="Comments"/>
</VersionInfoKeys>
<Excluded_Packages>
<Excluded_Packages Name="$(BDSBIN)\dclmid160.bpl">Embarcadero MyBase DataAccess Components</Excluded_Packages>
<Excluded_Packages Name="$(BDS)\UserDir\Bpl\test.bpl">(untitled)</Excluded_Packages>
</Excluded_Packages>
<Source>
<Source Name="MainSource">Project2.dpr</Source>
</Source>
</Delphi.Personality>
<Platforms>
<Platform value="Win64">True</Platform>
<Platform value="Win32">True</Platform>
</Platforms>
</BorlandProject>
<ProjectFileVersion>12</ProjectFileVersion>
</ProjectExtensions>
<Import Condition="Exists('$(BDS)\Bin\CodeGear.Delphi.Targets')" Project="$(BDS)\Bin\CodeGear.Delphi.Targets"/>
<Import Condition="Exists('$(APPDATA)\Embarcadero\$(BDSAPPDATABASEDIR)\$(PRODUCTVERSION)\UserTools.proj')" Project="$(APPDATA)\Embarcadero\$(BDSAPPDATABASEDIR)\$(PRODUCTVERSION)\UserTools.proj"/>
</Project>