Delphi-PRAXiS

Delphi-PRAXiS (https://www.delphipraxis.net/forum.php)
-   Win32/Win64 API (native code) (https://www.delphipraxis.net/17-win32-win64-api-native-code/)
-   -   Delphi WM_COPYDATA funktioniert nicht von injezierte DLL aus (https://www.delphipraxis.net/102326-wm_copydata-funktioniert-nicht-von-injezierte-dll-aus.html)

hitzi 26. Okt 2007 18:58


WM_COPYDATA funktioniert nicht von injezierte DLL aus
 
Hallo,

ausgehend von dem Thread, wo ich aufgrund des nicht funktionierenden WM_COPYDATA auf die Named Pipes ausgewichen bin, welche dann auch nicht funktionierten. Nach einen Test von zwei normalen selbstgeschriebenen Windowsprogrammen, steht fest dass die verwendeten Funktionen für das Übertragen und Empfangen von WM_COPYDATA funktionieren.

Hier der Source von den beiden funktionierenden Programmen:
Delphi-Quellcode:
unit Unit1;

interface

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

type
  TForm1 = class(TForm)
    btn1: TButton;
    procedure btn1Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);
  private
    { Private-Deklarationen }
  public
    { Public-Deklarationen }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

const
  WM_COPYDATA        = $004A;

type Tmydata = packed record
      datacount: integer;
      ind: boolean;
     end;

var  mydata: TMyData;
      CDS: TCopyDataStruct;
      winh: integer;

procedure sendapp(len: integer; indata: boolean);
begin
  mydata.datacount := len;
  mydata.ind := indata;
  SendMessageA(winh,WM_COPYDATA,0,cardinal(@CDS));
end;

procedure TForm1.btn1Click(Sender: TObject);
begin
  winh := FindWindowA(nil,'ShowTraffic');
  if winh <> 0 then sendapp(10,true);
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  CDS.dwData := 0;
  CDS.cbData := sizeof(TMyData);
  CDS.lpData := @mydata;
end;

end.
Delphi-Quellcode:
unit Unit2;

interface

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

type
  TForm2 = class(TForm)
    mmo1: TMemo;
  private
    { Private-Deklarationen }
    procedure WMNOTIFYCD(var Msg: TWMCopyData); message WM_COPYDATA;
  public
    { Public-Deklarationen }
  end;

var
  Form2: TForm2;

implementation

{$R *.dfm}

type Tmydata = packed record
      datacount: integer;
      ind: boolean;
     end;

var mydata: TMydata;

procedure TForm2.WMNOTIFYCD(var Msg: TWMCopyData);
var exeanz: string;
begin
  if Msg.CopyDataStruct^.cbData = sizeof(TMydata) then
  begin
    CopyMemory(@myData,Msg.CopyDataStruct^.lpData,sizeof(TMyData));
    mmo1.Lines.Add(IntToStr(mydata.datacount))
  end;
end;

end.
Genau die gleichen Funktionen werden in einer injezierten DLL verwendet um WM_COPYDATA an ein Programm zu schicken. Die Funktion in der auch SendMessageA ausgelöst wird, wird ausgeführt (getestet mit einer Messagebox), aber es kommen im Hauptprogramm keine Daten an - die procedure WMNOTIFYCD(var Msg: TWMCopyData); message WM_COPYDATA; wird nie ausgelöst, wenn WM_COPYDATA von der injezierten DLL aus geschickt wird. Das ganze Messaging basiert zu großen Teilen auf dem TrafficCounter Beispiel der uallCollection. Dieses Beispiel funktioniert bei mir auch nicht, also es kommen keine Daten an (Vista 32bit).

Woran kann das liegen?

Gruss

SirThornberry 26. Okt 2007 19:06

Re: WM_COPYDATA funktioniert nicht von injezierte DLL aus
 
ich würde einfach sagen das vielleicht an das falsche handle sendest.

Apollonius 26. Okt 2007 19:26

Re: WM_COPYDATA funktioniert nicht von injezierte DLL aus
 
Wozu wird im ersten Programm WM_COPYDATA redeklariert?

hitzi 26. Okt 2007 20:39

Re: WM_COPYDATA funktioniert nicht von injezierte DLL aus
 
Die Idee mit dem Handle hatte ich auch schon. Aber das in der DLL ermittelte Handle zum Hauptprogramm stimmt mit dem angezeigten Handle in WinSpy überein.

Die Deklaration von WM_COPYDATA ist noch ein Überbleibsel von der DLL und kann in dem Beispielprogramm auch weggelassen werden. In der DLL muss das aber deklariert werden, da ich dort die Unit messages nicht eingebunden habe.

Bernhard Geyer 26. Okt 2007 20:48

Re: WM_COPYDATA funktioniert nicht von injezierte DLL aus
 
WM_COPYDATA-Messages werden von diversen Tools wie z.B. NVidia Desktopmanager "verschluckt" wenn sie im eigenen Prozess versendet werden.

hitzi 26. Okt 2007 21:04

Re: WM_COPYDATA funktioniert nicht von injezierte DLL aus
 
Liste der Anhänge anzeigen (Anzahl: 1)
Wie meinst du das? Ich hooke nichts von NVidia.

Hier mal der Code wo es nicht funktioniert.

Hauptptogramm (umgeschrieben, so dass es mit dem Editor funktioniert):
Delphi-Quellcode:
unit uMain;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, ExtCtrls, StdCtrls, uallHook, uallProcess, uallUtil, uallKernel;
 
type
  TfrmMain = class(TForm)
    lbl1: TLabel;
    tmrSearchCondor: TTimer;
    mmo1: TMemo;
    procedure FormCreate(Sender: TObject);
    procedure tmrSearchCondorTimer(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
  private
    { Private-Deklarationen }
    fCondorPID : DWord;
    fInjected : Boolean;
    fDontWork : Boolean;
    procedure SearchCondor;
    procedure InjectMyFunctions;
    procedure UnloadMyFunctions;
    function GetDebugPrivileges : Boolean;
    procedure WriteText(s : string);
    procedure WMNOTIFYCD(var Msg: TWMCopyData); message WM_COPYDATA;
  public
    { Public-Deklarationen }
  end;

var
  frmMain: TfrmMain;

implementation

{$R *.dfm}

type Tmydata = packed record
       datacount: integer;
       ind: boolean;
     end;

const cCondorApplication = 'notepad.exe';
      cinjComFuntionsDLL = 'injComFunctions.dll';

var myData : TMydata;

procedure TfrmMain.WMNOTIFYCD(var Msg: TWMCopyData);
begin
  if Msg.CopyDataStruct^.cbData = sizeof(TMydata) then
  begin
    CopyMemory(@myData,Msg.CopyDataStruct^.lpData,sizeof(TMyData));
    WriteText(IntToStr(mydata.datacount))
  end;
end;

procedure TfrmMain.WriteText(s : string);
begin
  mmo1.Lines.Add(DateTimeToStr(now) + ':> ' + s);
end;

procedure TfrmMain.InjectMyFunctions;
begin
  if not fInjected then begin
    if InjectLibrary(fCondorPID, PChar(GetExeDirectory + cinjComFuntionsDLL)) then fInjected := True;
  end;
end;

procedure TfrmMain.UnloadMyFunctions;
begin
  if fInjected then begin
    UnloadLibrary(fCondorPID, PChar(GetExeDirectory + cinjComFuntionsDLL));
    fInjected := False;
  end;
end;

procedure TfrmMain.SearchCondor;
begin
  fCondorPID := FindProcess(cCondorApplication);
  if fCondorPID <> 0 then begin
    lbl1.Caption := 'Notepad is running!';
    InjectMyFunctions;
  end else begin
    lbl1.Caption := 'Notepad isn''t running!';
  end;
end;

procedure TfrmMain.FormDestroy(Sender: TObject);
begin
  UnloadMyFunctions;
end;

function TfrmMain.GetDebugPrivileges : Boolean;
begin
  Result := False;
  if not SetDebugPrivilege(SE_PRIVILEGE_ENABLED) then begin
    Application.MessageBox('No Debug rights!', 'Error', MB_OK);
  end else begin
    Result := True;
  end;
end;

procedure TfrmMain.FormCreate(Sender: TObject);
begin
  fInjected := False;
  fDontWork := not GetDebugPrivileges;
  tmrSearchCondor.Enabled := not fDontWork;
end;

procedure TfrmMain.tmrSearchCondorTimer(Sender: TObject);
begin
  tmrSearchCondor.Enabled := False;
  SearchCondor;
  tmrSearchCondor.Enabled := True;
end;

end.
Und nun die DLL:
Delphi-Quellcode:
library injComFunctions;

uses
  windows, uallHook, SysUtils;

const WM_COPYDATA = $004A;

type Tmydata = packed record
      datacount: integer;
      ind: boolean;
     end;

var
  nextCreateFile, oldCreateFile : function(lpFileName: PChar; dwDesiredAccess, dwShareMode: DWORD;
                 lpSecurityAttributes: PSecurityAttributes; dwCreationDisposition, dwFlagsAndAttributes: DWORD;
                 hTemplateFile: THandle): THandle; stdcall;
  nextCreateFileA, oldCreateFileA : function(lpFileName: PAnsiChar; dwDesiredAccess, dwShareMode: DWORD;
                          lpSecurityAttributes: PSecurityAttributes; dwCreationDisposition, dwFlagsAndAttributes: DWORD;
                          hTemplateFile: THandle): THandle; stdcall;
  nextCreateFileW, oldCreateFileW : function(lpFileName: PWideChar; dwDesiredAccess, dwShareMode: DWORD;
                          lpSecurityAttributes: PSecurityAttributes; dwCreationDisposition, dwFlagsAndAttributes: DWORD;
                          hTemplateFile: THandle): THandle; stdcall;

  myData : Tmydata;
  CDS : TCopyDataStruct;
  winh : integer;

procedure sendapp(len: integer; indata: boolean);
begin
  MessageBoxA(0,PChar('Function sendapp: ' + IntToStr(len)),'Msg',0);
  mydata.datacount := len;
  mydata.ind := indata;
  SendMessageA(winh,WM_COPYDATA,0,cardinal(@CDS));
end;

function myCreateFile(lpFileName: PChar; dwDesiredAccess, dwShareMode: DWORD;
                 lpSecurityAttributes: PSecurityAttributes; dwCreationDisposition, dwFlagsAndAttributes: DWORD;
                 hTemplateFile: THandle): THandle; stdcall;
begin
  sendapp(11, true);
  Result := nextCreateFile(lpFileName, dwDesiredAccess, dwShareMode,
                 lpSecurityAttributes, dwCreationDisposition, dwFlagsAndAttributes,
                 hTemplateFile);
end;

function myCreateFileA(lpFileName: PAnsiChar; dwDesiredAccess, dwShareMode: DWORD;
                          lpSecurityAttributes: PSecurityAttributes; dwCreationDisposition, dwFlagsAndAttributes: DWORD;
                          hTemplateFile: THandle): THandle; stdcall;
begin
  sendapp(22, true);
  Result := nextCreateFileA(lpFileName, dwDesiredAccess, dwShareMode,
                          lpSecurityAttributes, dwCreationDisposition, dwFlagsAndAttributes,
                          hTemplateFile);
end;

function myCreateFileW(lpFileName: PWideChar; dwDesiredAccess, dwShareMode: DWORD;
                          lpSecurityAttributes: PSecurityAttributes; dwCreationDisposition, dwFlagsAndAttributes: DWORD;
                          hTemplateFile: THandle): THandle; stdcall;
begin
  sendapp(33, true);
  Result := nextCreateFileW(lpFileName, dwDesiredAccess, dwShareMode,
                          lpSecurityAttributes, dwCreationDisposition, dwFlagsAndAttributes,
                          hTemplateFile);
end;

procedure InjectMain;
var kernelHandle : Integer;
begin
  @oldCreateFile := nil;
  @oldCreateFileA := nil;
  @oldCreateFileW := nil;

  CDS.dwData := 0;
  CDS.cbData := sizeof(TMyData);
  CDS.lpData := @mydata;

  winh := FindWindowA(nil,'CondorComTest');
  MessageBoxA(0,PChar('Handle winh: ' + IntToStr(winh)),'Msg',0);
  if winh <> 0 then sendapp(10, true);

  kernelHandle := GetModuleHandle('kernel32.dll');
  if kernelHandle > 0 then begin
    @oldCreateFile := GetProcAddress(kernelHandle,'CreateFile');
    if @oldCreateFile <> nil then HookCode(@oldCreateFile, @myCreateFile, @nextCreateFile);
    @oldCreateFileA := GetProcAddress(kernelHandle,'CreateFileA');
    if @oldCreateFileA <> nil then HookCode(@oldCreateFileA, @myCreateFileA, @nextCreateFileA);
    @oldCreateFileW := GetProcAddress(kernelHandle,'CreateFileW');
    if @oldCreateFileW <> nil then HookCode(@oldCreateFileW, @myCreateFileW, @nextCreateFileW);
  end;
end;

procedure UnInjectMain;
begin
  if @oldCreateFile <> nil then UnhookCode(@nextCreateFile);
  if @oldCreateFileA <> nil then UnhookCode(@nextCreateFileA);
  if @oldCreateFileW <> nil then UnhookCode(@nextCreateFileW);
end;

procedure DllMain(dwReason: DWord);
begin
  case dwReason of
    DLL_PROCESS_ATTACH: begin
                          InjectMain;
                          MessageBoxA(0,PChar('Loaded :'+Paramstr(0)),'Msg',0);
                        end;
    DLL_PROCESS_DETACH: begin
                          UnInjectMain;
                          //MessageBoxA(0,PChar('Unloaded :'+Paramstr(0)),'Msg',0);
                        end;
  end;
end;

begin
  DllProc := @DllMain;
  DllMain(DLL_PROCESS_ATTACH);
end.
Die MessageBoxen sind drin um zu zeigen, dass der Code aufgerufen wird. Im Anhang das komplette Projekt mit allen Units (uall..).

Bernhard Geyer 26. Okt 2007 23:01

Re: WM_COPYDATA funktioniert nicht von injezierte DLL aus
 
Zitat:

Zitat von hitzi
Wie meinst du das? Ich hooke nichts von NVidia.

Du nicht, aber Nvidia hookt in deinen Prozessen um z.B. auf jeden Fenster eigene Buttons rechts oben neben den "normalen" Buttons zu bekommen. Führt auch dazu das des öfteren (bei entsprechenden Versionen) der Explorer nicht mehr reagiert.

hitzi 26. Okt 2007 23:02

Re: WM_COPYDATA funktioniert nicht von injezierte DLL aus
 
Yippie :)

Das ganze ist ein Vista Problem. Irgendwie hat die injizierte DLL einen anderen Sicherheitslevel, wie das Hauptprogramm. Unter Vista ist es einem Prozess mit weniger Berechtigungen(Privilegien) nicht gestattet Messages an einen Prozess mit mehr Berechtigungen zu senden. Um das unter Vista dann doch möglich zu machen muss man die entsprechende Messages in der Anwendung mit den höheren Berechtigungen freischalten. Dazu gibt es die API Funktion ChangeWindowMessageFilter (user32.dll). Um nun WM_COPYDATA freizuschalten muss man die Funktion wie folgt anwenden:
Delphi-Quellcode:
ChangeWindowMessageFilter(WM_COPYDATA, 1);
Erster Parameter ist die freizuschaltende Message und der zweite Parameter legt fest, ob man diese Message empfangen (Wert 1) oder nicht empfangen(Wert 0) möchte.

Hier nun die angepasste Version des Quelltextes des Hauptprogrammes - die DLL muss nicht angepasst werden:
Delphi-Quellcode:
unit uMain;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, ExtCtrls, StdCtrls, uallHook, uallProcess, uallUtil, uallKernel;
 
type
  TfrmMain = class(TForm)
    lbl1: TLabel;
    tmrSearchCondor: TTimer;
    mmo1: TMemo;
    procedure FormCreate(Sender: TObject);
    procedure tmrSearchCondorTimer(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
  private
    { Private-Deklarationen }
    fCondorPID : DWord;
    fInjected : Boolean;
    fDontWork : Boolean;
    procedure SearchCondor;
    procedure InjectMyFunctions;
    procedure UnloadMyFunctions;
    function GetDebugPrivileges : Boolean;
    procedure WriteText(s : string);
    procedure WMNOTIFYCD(var Msg: TWMCopyData); message WM_COPYDATA;
  public
    { Public-Deklarationen }
  end;

var
  frmMain: TfrmMain;
  ChangeWindowMessageFilter: function (msg : Cardinal; dwFlag : Word) : BOOL; stdcall;

implementation

{$R *.dfm}

type Tmydata = packed record
       datacount: integer;
       ind: boolean;
     end;

const cCondorApplication = 'notepad.exe';
      cinjComFuntionsDLL = 'injComFunctions.dll';

var myData : TMydata;

procedure TfrmMain.WMNOTIFYCD(var Msg: TWMCopyData);
begin
  if Msg.CopyDataStruct^.cbData = sizeof(TMydata) then
  begin
    CopyMemory(@myData,Msg.CopyDataStruct^.lpData,sizeof(TMyData));
    WriteText(IntToStr(mydata.datacount))
  end;
end;

procedure TfrmMain.WriteText(s : string);
begin
  mmo1.Lines.Add(DateTimeToStr(now) + ':> ' + s);
end;

procedure TfrmMain.InjectMyFunctions;
begin
  if not fInjected then begin
    if InjectLibrary(fCondorPID, PChar(GetExeDirectory + cinjComFuntionsDLL)) then fInjected := True;
  end;
end;

procedure TfrmMain.UnloadMyFunctions;
begin
  if fInjected then begin
    UnloadLibrary(fCondorPID, PChar(GetExeDirectory + cinjComFuntionsDLL));
    fInjected := False;
  end;
end;

procedure TfrmMain.SearchCondor;
begin
  fCondorPID := FindProcess(cCondorApplication);
  if fCondorPID <> 0 then begin
    lbl1.Caption := 'Notepad is running!';
    InjectMyFunctions;
  end else begin
    lbl1.Caption := 'Notepad isn''t running!';
  end;
end;

procedure TfrmMain.FormDestroy(Sender: TObject);
begin
  UnloadMyFunctions;
end;

function TfrmMain.GetDebugPrivileges : Boolean;
begin
  Result := False;
  if not SetDebugPrivilege(SE_PRIVILEGE_ENABLED) then begin
    Application.MessageBox('No Debug rights!', 'Error', MB_OK);
  end else begin
    Result := True;
  end;
end;

procedure TfrmMain.FormCreate(Sender: TObject);
begin
  @ChangeWindowMessageFilter := GetProcAddress(LoadLibrary('user32.dll'), 'ChangeWindowMessageFilter');
  ChangeWindowMessageFilter(WM_COPYDATA, 1);
  fInjected := False;
  fDontWork := not GetDebugPrivileges;
  tmrSearchCondor.Enabled := not fDontWork;
end;

procedure TfrmMain.tmrSearchCondorTimer(Sender: TObject);
begin
  tmrSearchCondor.Enabled := False;
  SearchCondor;
  tmrSearchCondor.Enabled := True;
end;

end.
Vielleicht hilft es noch jemand anderen :)


Alle Zeitangaben in WEZ +1. Es ist jetzt 01:48 Uhr.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024 by Thomas Breitkreuz