PDA

Ver la Versión Completa : Injectar recurso DLL en memoria en un en proceso externo


Milworm99
01-01-2018, 04:22:09
Hola con todos, Tengo un problema, mi DLL que eta guardado como recurso dentro de memoria, pero lo que quiero es que esa DLL guardada en Memoria llamarla para poder injectar a un proceso.

Para gurdar la DLL en memoria estoy que utilizo un modulo llamado 'BTMemoryModule.pas'

Este es mi código que uso

unit Unit1;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, BTMemoryModule, ExtCtrls,TlHelp32;

type
TForm1 = class(TForm)
lbl1: TLabel;
btn1: TButton;
Injectar: TTimer;
procedure InjectarTimer(Sender: TObject);
procedure btn1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;
HookLib: PBTMemoryModule = nil;
pDisableItem: procedure(Key: integer; disable: integer) stdcall = nil;
pRestoreAll : procedure stdcall = nil;


implementation

{$R *.dfm}
function LoadLibraryFromResource(const aResourceName: String): PBTMemoryModule;
var
ms: TMemoryStream;
rs: TResourceStream;
begin
ms := TMemoryStream.Create;
try
rs := TResourceStream.Create(HInstance, aResourceName, RT_RCDATA);
try
ms.CopyFrom(rs, 0);
ms.Position := 0;
finally
rs.Free;
end;

Result := BTMemoryLoadLibary(ms.Memory, ms.Size);
finally
ms.Free;
end;
end;

function GetPID(ProcessName: string): DWORD;
var MyHandle: THandle;
Struct: TProcessEntry32;
begin
Result:=0;
try
MyHandle:=CreateToolHelp32SnapShot(TH32CS_SNAPPROCESS, 0);
Struct.dwSize:=Sizeof(TProcessEntry32);
if Process32First(MyHandle, Struct) then
if Struct.szExeFile=ProcessName then
begin
Result:=Struct.th32ProcessID;
Exit;
end;
while Process32Next(MyHandle, Struct) do
if Struct.szExeFile=ProcessName then
begin
Result:=Struct.th32ProcessID;
Exit;
end;
except on exception do
Exit;
end;
end;

function Hook(PID:DWORD; sDll:string):Boolean;
var
hLib: Pointer;
hThread: THandle;
pMod: Pointer;
hOpen: THandle;
dWritten: Cardinal;
ThreadID: Cardinal;
begin
Result := FALSE;
hOpen := OpenProcess(PROCESS_ALL_ACCESS, FALSE, PID);
if hOpen <> INVALID_HANDLE_VALUE then
begin
hLib := GetProcAddress(GetModuleHandle(PChar('kernel32.dll')), PChar('LoadLibraryA'));
pMod := VirtualAllocEx(hOpen, nil, Length(sDll) + 1, MEM_COMMIT or MEM_RESERVE, PAGE_EXECUTE_READWRITE);
if WriteProcessMemory(hOpen, pMod, @sDll[1], Length(sDll), dWritten) then
Result := TRUE;
hThread := CreateRemoteThread(hOpen, nil, 0, hLib, pMod, 0, ThreadID);
WaitForSingleObject(hThread, INFINITE);
CloseHandle(hOpen);
CloseHandle(hThread);
end;
end;

procedure TForm1.btn1Click(Sender: TObject);
begin
// Abrir DLL //
HookLib := LoadLibraryFromResource('WallName');

if Hooklib <> nil then
begin
@pDisableItem := BTMemoryGetProcAddress(HookLib, 'wlDisableItem');
@pRestoreAll := BTMemoryGetProcAddress(HookLib, 'wlRestoreAll');
end;
end;

procedure TForm1.InjectarTimer(Sender: TObject);
var PID: DWORD;
begin
Injectar.Enabled:=False;
PID:=GetPID('Update.exe');
if PID=0 then
begin
Injectar.Enabled:=True;
Exit;
end;
Injectar.Enabled:=False;
if Hook(PID, 'WallName') then //// DLL al injectar ///
lbl1.Caption:= 'DLL Injectado...!!!';
end;
end.

Lo que me gustaria hacer es que la DLL guarda como recurso en memoria llamada "WallName" se injecte al proceso externo llamada "Update.exe"

Espero su ayuda, muchas gracias !!

Ñuño Martínez
09-01-2018, 11:06:15
"Injectar". Madre del amor hermosa... :rolleyes:

¿Qué es lo que pretendes hacer? ¿Un virus, un troyano, un entrenador...? Aunque no incumple la Guía de estilo, me da a mi que lo que pides no es muy ético.