Entwickler-Ecke

Windows API - Dll Injection mit Erstellung eines Formulars


Wotan89 - Sa 10.11.07 13:34
Titel: Dll Injection mit Erstellung eines Formulars
Die Dll-Injection klappt sonst einwandfrei, aber wenn ich mit der dll ein Formular erzeugen möchte, klappt diese plötzlich sofort wieder zusammen :cry:
injection-script:

Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
unit Unit1;

interface

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

type
  TForm1 = class(TForm)
    Edit1: TEdit;
    Button1: TButton;
    Label1: TLabel;
    procedure Button1Click(Sender: TObject);
  private
    { Private-Deklarationen }
  public
    { Public-Deklarationen }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

function InjectIntoProcess(lpProcessID: Cardinal; lpDllname: String):Boolean;
var
  hProc: Cardinal;
  oAlloc: Pointer;
  cWPM: Cardinal;
  hRemThread: Cardinal;
begin
  result := false;
  SetLastError(ERROR_SUCCESS);
  hProc := OpenProcess(PROCESS_ALL_ACCESS, false, lpProcessID);
  oAlloc := VirtualAllocEx(hProc, 0, length(lpDllname), MEM_COMMIT, PAGE_EXECUTE_READWRITE);
  WriteProcessMemory(hProc, oAlloc, PChar(lpDllName), length(lpDllName), cWPM);
  CreateRemoteThread(hProc, nil0, GetProcAddress(GetModuleHandle('kernel32.dll'), 'LoadLibraryA'), oAlloc, 0, hRemThread);
  if GetLastError = ERROR_SUCCESS then
   result := true;
  CloseHandle(hProc);
end;

procedure TForm1.Button1Click(Sender: TObject);
var pid,h:cardinal;
begin
h:=findwindow(nil,pchar(edit1.Text));
getwindowthreadprocessid(h,@pid);
label1.Caption:=booltostr(InjectIntoProcess(pid,'E:\Dokumente und Einstellungen\Stefan_Admin\Eigene Dateien\Programmierung\code-injection3\project1.dll'),true);
end;

end.


Meine dll:

Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
library Project1;

uses
  Forms,
  Unit1 in 'Unit1.pas' {Form1};

{$R *.res}

begin
Form1.showform;
end.

und meine Unit für die dll:

Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
+
unit Unit1;

interface

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

type
  TForm1 = class(TForm)
    Timer1: TTimer;
    procedure Timer1Timer(Sender: TObject);
    procedure FormCreate(Sender: TObject);
  private
    { Private-Deklarationen }
  public
    { Public-Deklarationen } Function showform:boolean ;
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

Function TForm1.showform:boolean ;
begin
result:=false;
Form1:=Tform1.Create(Application);
Form1.Show;
result:=true;
end;


end.

Wenn ich application.Initialize usw. drin stehen lasse, fährt sich der Prozess, in welchen ich die dll injeziere fest und geht erst weiter, wenn mein Formular beendet wird...[/quote]


Wotan89 - Mo 12.11.07 18:13

Hat denn keiner eine Idee, wie das funktionieren köntte oder was ich noch beachten müsste? Wäre echt toll, wenn mir jemand weiterhelfen könnte :P


R4Y - So 02.12.07 14:16

ich weiß net ob du das so meinst aber so klappt es bei mir ...


Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
procedure Show;
var
 Form2:TForm;
begin
 Form2:=TForm.Create(nil);
 Form2.Name:='Form1';
 Form2.Show;
end;

export
 Show;


Erstellt eine Form die leer ist..

Moderiert von user profile iconNarses: Code- durch Delphi-Tags ersetzt


dummzeuch - So 02.12.07 14:55
Titel: Re: Dll Injection mit Erstellung eines Formulars
Hi,

user profile iconWotan89 hat folgendes geschrieben:
Die Dll-Injection klappt sonst einwandfrei, aber wenn ich mit der dll ein Formular erzeugen möchte, klappt diese plötzlich sofort wieder zusammen


Delphi-Quelltext
1:
Form1.showform;                    


Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
type
  TForm1 = class(TForm)
  public
    Function showform:boolean ;
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

Function TForm1.showform:boolean ;
begin
  result:=false;
  Form1:=Tform1.Create(Application);
  Form1.Show;
  result:=true;
end;


Auah, das tut ja schonmal ziemlich weh:

1. form1.ShowForm ruft eine Methode einer nicht initialisierten Variable form1 auf. Das funktioniert nur deshalb, weil Du in ShowForm nicht auf eine virtuelle Methode oder ein Feld des Formulars zugreifst. Mach daraus mal schlenigst eine Klassenmethode und rufe sie als solche auf:


Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
  TForm1.ShowForm

type
  TForm1 = class(TForm)
  public
    class Function showform:boolean ;
  end;


2. Innerhalb einer DLL auf Application zuzugreifen ist zumindest gefaehrlich, wenn man nicht im Hinterkopf behaelt, dass diese globale Application-Variable eine andere ist als die im Hauptprogramm. Ich wuerde das komplett rauslassen und als Owner des Formulars nil angeben.

3. Die globale Variabel Form1 sollte man sich am Besten komplett schenken (Deklaration loeschen), es sei denn, man laesst das Formular automatisch erzeugen (was ich in der Regel nur fuer das Haupt-Formular eines Programmes mache). Sonst ist die Gefahr zu gross, dass man ueber diese Variable von anderen Programmteilen aus direkt auf das Formular zugreift, obwohl nicht sichergestellt ist, dass es ueberhaupt schon erzeugt wurde.

Meine Vermutung ist, dass 2. Dein Problem ausloest.

twm