Autor Beitrag
VaNaTiC
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 30



BeitragVerfasst: Fr 17.11.06 22:04 
Hallo,

ich habe das Beispiel TrafficCounter aus der uallCollection zum Testen ein bisschen erweitert und musste
feststellen, dass sobald ich in den gehookten Funktionen meine angepassten IPC-Funktionen aufrufe um die
Daten weiterzuleiten Verbindungsprobleme bekomme.

Hier der Code der DLL, die ich genau wie im Beispiel per InjectLibrary einklinke, wobei meine IPC-Funktionen funktionieren.
ausblenden volle Höhe 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:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:
102:
103:
104:
105:
106:
107:
108:
109:
110:
111:
112:
113:
114:
115:
116:
117:
118:
119:
120:
121:
122:
123:
124:
125:
126:
127:
128:
129:
130:
131:
132:
133:
134:
135:
136:
137:
138:
139:
140:
141:
142:
143:
144:
145:
146:
147:
148:
149:
150:
151:
152:
153:
154:
155:
156:
157:
158:
159:
160:
161:
162:
163:
164:
165:
166:
167:
168:
169:
170:
171:
172:
173:
174:
175:
176:
177:
178:
179:
180:
181:
182:
183:
184:
185:
186:
187:
188:
189:
190:
191:
192:
193:
194:
195:
196:
197:
198:
199:
library TESTdll;

uses
  Windows,
  uallHook, uallUtil, uallProcess;

//************************************************************************
// declaration of used tyopes without using of unit WinSock
//************************************************************************
type
  u_char = Char;
  u_short = Word;
  u_int = Integer;
  u_long = Longint;
  TSocket = u_int;
  SunB = packed record
    s_b1, s_b2, s_b3, s_b4: u_char;
  end;
  SunW = packed record
    s_w1, s_w2: u_short;
  end;
  in_addr = record
    case integer of
      0: (S_un_b: SunB);
      1: (S_un_w: SunW);
      2: (S_addr: u_long);
  end;
  PInAddr = ^TInAddr;
  TInAddr = in_addr;
  sockaddr_in = record
    case Integer of
      0: (sin_family: u_short;
          sin_port: u_short;
          sin_addr: TInAddr;
          sin_zero: array[0..7] of Char);
      1: (sa_family: u_short;
          sa_data: array[0..13] of Char)
  end;
  PSockAddrIn = ^TSockAddrIn;
  TSockAddrIn = sockaddr_in;
  PSockAddr = ^TSockAddr;
  TSockAddr = sockaddr_in;
  TRecvProc= function (s: TSocket; var Buf; len, flags: Integer): Integer; stdcall;
  TRecvFromProc= function ( s: TSocket; var Buf; len, flags: Integer; var from: TSockAddr; var fromlen: Integer): Integer; stdcall;
  TSendProc= function (s: TSocket; var Buf; len, flags: Integer): Integer; stdcall;
  TSendToProc= function (s: TSocket; var Buf; len, flags: Integer; var addrto: TSockAddr; tolen: Integer): Integer; stdcall;
var
  OldSend, NextSend: TSendProc;
  OldSendTo, NextSendTo: TSendToProc;
  OldRecv, NextRecv: TRecvProc;
  OldRecvFrom, NextRecvFrom: TRecvFromProc;


//************************************************************************
// the IPC stuff
//************************************************************************
const
  WM_COPYDATA     = $004A;
  SIZEOF_SOCKADDR = 16;
var
  hAPP: HWND;

procedure SendAPP(typ: DWORD; pBuf: PChar; nSize: Integer);
var
  CDS: TCopyDataStruct;
begin
  CDS.dwData := typ;
  CDS.cbData := nSize;
  CDS.lpData := pBuf;
  SendMessageA(hAPP, WM_COPYDATA, 0, Cardinal(@CDS));
end;

procedure SendAPPnet(typ: DWORD; fromsock, tosock: PSockAddr; pBuf: PChar; nSize: Integer);
var
  pNewBuf: PChar;
  nNewSize: Integer;
  dwPID: DWORD;
begin
  nNewSize := nSize+2*SIZEOF_SOCKADDR+4;
  GetMem(pNewBuf, nNewSize);
  ZeroMemory(pNewBuf, nNewSize);
  dwPID := GetCurrentProcessId;
  CopyMemory(@pNewBuf[0], @dwPID, 4);
  if fromsock <> nil then
    CopyMemory(@pNewBuf[4], fromsock, SIZEOF_SOCKADDR);
  if tosock <> nil then
    CopyMemory(@pNewBuf[4+SIZEOF_SOCKADDR], tosock, SIZEOF_SOCKADDR);
  if (pBuf <> nil) and (nSize > 0) then
    CopyMemory(@pNewBuf[4+2*SIZEOF_SOCKADDR], pBuf, nSize);
  SendAPP(typ, pNewBuf, nNewSize);
  FreeMem(pNewBuf);
end;

procedure SendAPPtxt(text: string);
begin
  SendAPPnet(1, nil, nil, PChar(text+#0), Length(text)+1);
end;


//************************************************************************
// my hooked network functions
//************************************************************************

function MyRecv(s: TSocket; var Buf; len, flags: Integer): Integer; stdcall;
begin
  //SendAPPtxt('recv()');
  Result := NextRecv(s, Buf, len, flags);
  //SendAPPnet(2, nil, nil, PChar(Buf), Result);
end;

function MyRecvFrom(s: TSocket; var Buf; len, flags: Integer;
  var from: TSockAddr; var fromlen: Integer): Integer; stdcall;
begin
  //SendAPPtxt('recvfrom()');
  Result := NextRecvFrom(s, Buf, len, flags, from, fromlen);
  //SendAPPnet(3, @from, nil, PChar(Buf), Result);
end;

function MySend(s: TSocket; var Buf; len, flags: Integer): Integer; stdcall;
begin
  //SendAPPtxt('send()');
  //SendAPPnet(4, nil, nil, PChar(Buf), len);
  Result := NextSend(s, Buf, len, flags);
end;

function MySendTo(s: TSocket; var Buf; len, flags: Integer; var addrto: TSockAddr;
  tolen: Integer): Integer; stdcall;
begin
  //SendAPPtxt('sendto()');
  //SendAPPnet(5, nil, @addrto, PChar(Buf), len);
  Result := NextSendTo(s, Buf, len, flags, addrto, tolen);
end;

//************************************************************************
// (Un)Injecting
//************************************************************************

procedure InjectMain;
var
  hWS, hUSR: Integer;
begin
  @OldSendTo := nil;
  @OldSend := nil;
  @OldRecvFrom := nil;
  @OldRecv := nil;

  hUSR := GetModuleHandle('user32.dll');
  hWS :=  GetModuleHandle('ws2_32.dll');

  if (hWS > 0) and (hUSR > 0) then
  begin
    hAPP := FindWindowA(nil, 'TESTman');

    @OldSendTo := GetProcAddress(hWS, 'sendto');
    if @OldSendTo <> nil then
      uallHook.HookCode(@OldSendTo, @MySendTo, @NextSendTo);

    @OldSend := GetProcAddress(hWS, 'send');
    if @OldSend <> nil then
      uallHook.HookCode(@OldSend, @MySend, @NextSend);

    @OldRecv := GetProcAddress(hWS, 'recv');
    if @OldRecv <> nil then
      uallHook.HookCode(@OldRecv, @MyRecv, @NextRecv);

    @OldRecvFrom := GetProcAddress(hWS, 'recvfrom');
    if @oldrecvfrom <> nil then
      uallHook.HookCode(@OldRecvFrom, @MyRecvFrom, @NextRecvFrom);
    SendAPPtxt('hooked');
  end;
end;

procedure UnInjectMain;
begin
  if @OldSendTo <> nil then
    uallHook.UnhookCode(@NextSendTo);
  if @OldSend <> nil then
    uallHook.UnhookCode(@NextSend);
  if @OldRecv <> nil then
    uallHook.UnhookCode(@NextRecv);
  if @OldRecvFrom <> nil then
    uallHook.UnhookCode(@NextRecvFrom);
  SendAPPtxt('unhooked');
end;

procedure DLLMain(dwReason: DWORD);
begin
  case dwReason of
    DLL_PROCESS_ATTACH:
      Injectmain;
    DLL_PROCESS_DETACH:
      UnInjectmain;
  end;
end;

begin
  DLLProc := @DLLMain;
  DLLMain(DLL_PROCESS_ATTACH);
end.

Wenn ich in den gehookten Funktionen, die auskommentierten IPC-Funktionen reinnehme, dann klappt die Verbidung nicht. Jenachdem wie die gehookte Anwendung programmiert ist, reagiert es mit Abbruch oder
Neuverbindung.


Und hier das aufrufende Programm, welches die IPC bekommt und den Inject durchführt
ausblenden volle Höhe 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:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:
102:
103:
104:
105:
106:
107:
108:
109:
110:
111:
112:
113:
114:
115:
116:
117:
unit MainTEST;

interface

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

type
  TForm1 = class(TForm)
    Button1: TButton;
    Button2: TButton;
    Timer1: TTimer;
    Memo1: TMemo;
    procedure WMNOTIFYCD(var Msg: TWMCopyData); message WM_COPYDATA;
    procedure FormDestroy(Sender: TObject);
    procedure Timer1Timer(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
  private
    { Private declarations }
    mAppInst: TIntPtrMap;
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

uses
  WinSock,
  uallProcess, uallHook, uallUtil;

type
  TIPCData = record
    Typ: DWORD;
    PID: DWORD;
    FromSockAddr, ToSockAddr: TSockAddr;
    Data: PChar;
    Size: Integer;
  end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  mAppInst := TIntPtrMap.Create;
end;

procedure TForm1.FormDestroy(Sender: TObject);
begin
  mAppInst.Free;
end;

procedure TForm1.WMNOTIFYCD(var Msg: TWMCopyData);
const
  SIZEOF_SOCKADDR = 16;
var
  pData, P: PChar;
  dwTyp, dwPID, nSize, n: DWORD;
  F, T: TSockAddrIn;
begin
  dwTyp := Msg.CopyDataStruct^.dwData;
  nSize := Msg.CopyDataStruct^.cbData;
  pData := PChar(Msg.CopyDataStruct^.lpData);
  Move(pData[0], dwPID, 4);
  Move(pData[4], F, SIZEOF_SOCKADDR);
  Move(pData[4+SIZEOF_SOCKADDR], T, SIZEOF_SOCKADDR);
  n := nSize - 4 - 2*SIZEOF_SOCKADDR;

  case dwTyp of
    1: begin
      Memo1.Lines.Add('['+IntToStr(dwPID)+'] '+(pData+4+2*SIZEOF_SOCKADDR));
    end;
    2: begin // recv
      Memo1.Lines.Add('['+IntToStr(dwPID)+'] recv:'+IntToStr(n));
    end;
    3: begin // recvfrom
      Memo1.Lines.Add('['+IntToStr(dwPID)+'] recvfrom:'+IntToStr(n));
    end;
    4: begin // send
      Memo1.Lines.Add('['+IntToStr(dwPID)+'] send:'+IntToStr(n));
    end;
    5: begin // sendto
      Memo1.Lines.Add('['+IntToStr(dwPID)+'] sendto:'+IntToStr(n));
    end;
  end;
end;

procedure TForm1.Timer1Timer(Sender: TObject);
var
  dwPID: DWORD;
begin
  dwPID := uallProcess.FindProcess('IrgendEinNetzwerkTool');
  if (dwPID <> 0) and (mAppInst[dwPID] = nil) then
  begin
    if uallHook.InjectLibrary(dwPID, PChar(uallUtil.GetExeDirectory+'\TESTdll.dll')) then
      mAppInst[dwPID] := Pointer(dwPID);
  end;
  dwPID := uallProcess.FindProcess('EinAnderesNetzwerkTool');
  if (dwPID <> 0) and (mAppInst[dwPID] = nil) then
  begin
    if uallHook.InjectLibrary(dwPID, PChar(uallUtil.GetExeDirectory+'\TESTdll.dll')) then
      mAppInst[dwPID] := Pointer(dwPID);
  end;
end;


procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  uallHook.GlobalUnloadLibrary('TESTdll.dll');
end;

end.


Vielleicht hatte schon ein anderer ein ähnliches Problem und kann mir helfen wo ich ansetzen könnte.

Vielen Dank
VaNaTiC Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 30



BeitragVerfasst: Mi 22.11.06 00:30 
Keiner eine Idee?
Oder hab ich meine Problematik schlecht beschrieben?
uall@ogc
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 1826
Erhaltene Danke: 11

Win 2000 & VMware
Delphi 3 Prof, Delphi 7 Prof
BeitragVerfasst: Do 23.11.06 18:11 
Keine Ahnung warum das so ist.
Nimm mal statt SendMessage PostMessage.
Ansonsten schau mal bei madshi.net im Forum da hat sowas 100% jemand schonmal gemacht.

_________________
wer andern eine grube gräbt hat ein grubengrabgerät
- oder einfach zu viel zeit
VaNaTiC Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 30



BeitragVerfasst: So 26.11.06 17:14 
OK, ich schau mal bei madshi.
Aber das Problem lässt sich mit dem Quellcode ganz einfach reproduzieren.
Evtl. ist irgendwas in der von mir angepaßten IPC-Funktion zur Laufzeit nicht bekannt und stürzt dann ab.