Autor Beitrag
TmpDir
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 53
Erhaltene Danke: 3

Win7HP Win8.1 Deb/Ubuntu
C# [Delphi 6 Pro, Delphi 2005 PE, TurboDelphi PE]
BeitragVerfasst: So 04.06.06 21:23 
Hi,

ich hab mal wieder ein Problem....

folgende Routine benutze ich um einen Text im Speicher des IE zu suchen.
Kurioserweise funktionierte diese Routine mit IE6 bisher ganz gut.
Bei IE7beta2 habe ich jetzt aber nur manchmal (eher selten) ein Suchergebnis zurückbekommen.

Wenn ich den HxD (super Progi!) benutze finde ich die Daten immer noch im Speicher und Sie sehen aus wie immer.

Liegt das jetzt einfach nur am IE7 (was ich mal nicht hoffe) oder meinem Kauderwelsch Code :) oder bin ich sogar verwanzt und spielen meine Augen mir streiche...

[edit] Evtl. nicht genutzte Variablen, hab ich vergessen nach dem Testen wieder zu entfernen.[/edit]

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:
unit uSearchGameData;

interface

uses Windows,Classes,tlhelp32, SysUtils, ComCtrls;

  function SearchGameDataMemoryWide (PID: Cardinal;StartString,EndString:String; var PBar:TProgressBar; HexPos :Boolean=False):String;
  function SearchInMemStream(Buffer, Search: TMemoryStream;var PositionFound:Integer; StartPos:Integer=0):Boolean;

implementation

function SearchGameDataMemoryWide (PID: Cardinal; StartString,EndString:String;
        var PBar:TProgressBar; HexPos :Boolean=False):String;
var
  GameDataStart,GameDataEnd : WideString;
  hProcess : Cardinal;
  MemBasicInfo : TMemoryBasicInformation;
  SysInfo : TSystemInfo;
  lpMinAddr: Pointer;
  ret : DWORD;
  sBuffer : WideString;
  lpReadBytes : DWORD;
  lPos        : Integer;
  CalcAddress : DWORD;
  ByteContent : array of Byte;
  lpOldProtect: Cardinal;
  NewStartPos, NewStartPos2, FoundPos,FoundPos2 : Integer;
  cmpstring : widestring;
  BufferStream :TMemoryStream;
  SearchStream :TMemoryStream;
  SearchStream2:TMemoryStream;
  wsGameData : WideString;

begin
  GameDataStart := StartString;
  GameDataEnd := EndString;

  BufferStream := TMemoryStream.Create;
  BufferStream.Clear;
  BufferStream.SetSize($700000);  // Allocate a bigger BLock for ReSize...
  SearchStream := TMemoryStream.Create;

  SearchStream.Clear;
  SearchStream.SetSize(length(GameDataStart)*2);
  CopyMemory(SearchStream.Memory,@GameDataStart[1],length(GameDataStart)*2);

  SearchStream2 := TMemoryStream.Create;
  SearchStream2.Clear;
  SearchStream2.SetSize(length(GameDataEnd)*2);
  CopyMemory(SearchStream2.Memory,@GameDataEnd[1],length(GameDataEnd)*2);


  hProcess := OpenProcess(PROCESS_ALL_ACCESS, false, PID);
  GetSystemInfo(SysInfo);
  lpMinAddr := SysInfo.lpMinimumApplicationAddress;
  PBar.Min := (integer(SysInfo.lpMinimumApplicationAddress) AND $FFFF0000) DIV 10000;
  PBar.Max := (integer(SysInfo.lpMaximumApplicationAddress) AND $FFFF0000) DIV 10000;
  try
  while Cardinal(lpMinAddr) < Cardinal(SysInfo.lpMaximumApplicationAddress) do
  begin
    PBar.Position:=(integer(lpMinAddr)AND $FFFF0000) DIV 10000;  // Da Size Int64

    ret := VirtualQueryEx(hProcess, lpMinAddr, MemBasicInfo, SizeOf(MemBasicInfo));
    VirtualProtectEx(hProcess,lpMinAddr,MemBasicInfo.RegionSize, PAGE_READWRITE, lpOldProtect);

    if ret = SizeOf(MemBasicInfo) then
      if  ((MemBasicInfo.Type_9 = MEM_PRIVATE) Or
          (MemBasicInfo.Type_9 = MEM_MAPPED) OR
          (MemBasicInfo.Type_9 = MEM_IMAGE)) AND
          (MemBasicInfo.State  = MEM_COMMIT)}  THEN
      begin
        BufferStream.SetSize(MemBasicInfo.RegionSize);
        BufferStream.Position := 0;

        ReadProcessMemory(hProcess, lpMinAddr,BufferStream.Memory,MemBasicInfo.RegionSize, lpReadBytes);
        // For more Debug info....
        if hexpos = true then
            Result := Result + Format('[%8.8x]',[DWORD(lpMinAddr)])+#13#10;

        FoundPos := 0; NewStartPos := 0;
        while SearchInMemStream(BufferStream,SearchStream,FoundPos,NewStartPos) = True do
        begin
          if SearchInMemStream(BufferStream,SearchStream2,FoundPos2,FoundPos) = True then
            if FoundPos2-FoundPos < 100000 then
            begin
              // For more Debug info....
              if hexpos = true then
              Result := Result + (inttohex(Cardinal(lpMinAddr),8)
                  +':'+inttohex(Cardinal(lpMinAddr)+FoundPos,8)
                  +':'+inttohex(Cardinal(lpMinAddr)+FoundPos2,8)
                  +#13#10);
              SetLength(wsGameData,FoundPos2-FoundPos);
              CopyMemory(@wsGameData[1],ptr(cardinal(BufferStream.Memory)+FoundPos),FoundPos2-FoundPos);
              Result := Result + wsGameData + '*|*#13#10';
            end;
          NewStartPos := FoundPos+1;
          FoundPos := 0;
        end;

     end;
    VirtualProtectEx(hProcess,lpMinAddr,MemBasicInfo.RegionSize, lpOldProtect, lpOldProtect);
    lpMinAddr := Ptr(Cardinal(MemBasicInfo.BaseAddress)+MemBasicInfo.RegionSize);
  end;
 finally
   CloseHandle(hProcess);
   BufferStream.Free;
 end;
end;

function  SearchInMemStream(Buffer, Search: TMemoryStream;var PositionFound:Integer; StartPos:Integer=0):Boolean;
var
  posfound : integer;
  i,j: integer;
  SearchByte,BufferByte,SrchByte : Byte;
  pSearchByte,pBufferByte,pSrchByte, pBufrByte:PByte;
  Found:Boolean;

begin
  Search.Position := 0;
  Search.Read(SearchByte,1);

  pSearchByte := Search.Memory;
  pBufferByte := ptr(Cardinal(Buffer.Memory)+StartPos);
  pSrchByte  := pSearchByte;

  result := false;
  for i:= StartPos to Buffer.Size do
  begin
    if pBufferByte^ = pSearchByte^ then  // Erste Übereinstimmung ?
    begin                                // Dann Rest überprüfen...
      Found := True;
      pSrchByte := pSearchByte;          // Save for later...
      pBufrByte := pBufferByte;        
      for j:= 0 to Search.Size-1 do    
      begin
        if pBufrByte^ <> pSrchByte^ then
        begin
          Found := False;
          break;
        end;
        inc(pBufrByte);  // Pointer je um ein Byte weiterschieben
        inc(pSrchByte);
      end;
      if Found = TRUE then  // Wenn der ganze Search-Memory passt...
      begin
        PositionFound := startpos+i;
        result := TRUE;
        exit;
      end;
    end;
    inc(pBufferByte);
  end;
end;

end.

_________________
repeat :autsch: until :think:
TmpDir Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 53
Erhaltene Danke: 3

Win7HP Win8.1 Deb/Ubuntu
C# [Delphi 6 Pro, Delphi 2005 PE, TurboDelphi PE]
BeitragVerfasst: Mo 05.06.06 08:20 
Ergänzende Beobachtung:

Nach dem Starten von Windows, hatte ich heute einmal das Glück, das die Routine die Daten findet.

Dann gab es einen Ausnahmefehler während des Suchvorgangs. Nach dem Schließen des IE und neustart konnte nichts mehr gefunden werden. Und selbst nach einem ReBoot(?) konnten die Daten in der Applikation kein zweites mal gefunden werden.


Das klingt jetzt irgendwie nach Virenbefall. Ist aber, soweit das Kasperski sehen kann, kein Viri auf'm Rechner.

mfg

_________________
repeat :autsch: until :think:
TmpDir Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 53
Erhaltene Danke: 3

Win7HP Win8.1 Deb/Ubuntu
C# [Delphi 6 Pro, Delphi 2005 PE, TurboDelphi PE]
BeitragVerfasst: Mi 07.06.06 17:14 
Das Problem hat sich erledigt, nach dem ich die Routine nochmal komplett neu geschrieben hat....

Ich weiss aber immer noch nicht genau woran es lag....


mfg

_________________
repeat :autsch: until :think:
DOCa Cola
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 64

Vista x64
D2007
BeitragVerfasst: Mi 07.06.06 17:21 
kannst sie ja mal posten, wenn du magst, würde mich auch interessieren :)
TmpDir Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 53
Erhaltene Danke: 3

Win7HP Win8.1 Deb/Ubuntu
C# [Delphi 6 Pro, Delphi 2005 PE, TurboDelphi PE]
BeitragVerfasst: Mi 07.06.06 17:34 
Ich denke mal das Problem lag daran, wenn VirtualQueryEx kein Ergebnis zurückgeliefert hat, das der neue Offset nicht korrekt berechnet wurde.

Dazu mal hier www.delphi-forum.de/viewtopic.php?t=56143 mit :flehan: BenBe's Routine vergleichen. Dann ging's. :)

Hab auch die ProgressBar Events erstmal rausgeschmissen der Übersicht wegen.
Den Code kann ich nur Posten wenn die anderen Funktionen in der Komponente noch Integeriert sind.
z.B. Search for INT SHORTINT WORD DWORD Unicode ANSI etc...
Ansonsten ist das nur was halbes.


greets

_________________
repeat :autsch: until :think:
TmpDir Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 53
Erhaltene Danke: 3

Win7HP Win8.1 Deb/Ubuntu
C# [Delphi 6 Pro, Delphi 2005 PE, TurboDelphi PE]
BeitragVerfasst: Mi 07.06.06 17:41 
ok. ich bin mal nicht so....
obwohl das ja nun nix fertiges ist.
Alles was nicht in der Funktion deklariert ist, ist in der Komponente deklariert.

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:
procedure TMemSearch.FindInMem;
var
  hProcess : DWORD;
  SysInfo  : TSystemInfo;
  ret : DWORD;
  lpMinAddr : Pointer;
  MemBasicInfo : TMemoryBasicInformation;
  OldProtect: DWORD;
  ReadBytes : DWORD;
  PosFound : DWORD;
  StartPos : DWORD;
begin

  FBufferStream.Clear;
  FBufferStream.SetSize($700000);  // Allocate a bigger BLock for ReSize...

  hProcess := OpenProcess(PROCESS_ALL_ACCESS, TRUE, PID);
  GetSystemInfo(SysInfo);
  lpMinAddr := 0;

  try
   while Cardinal(lpMinAddr) <= Cardinal(SysInfo.lpMaximumApplicationAddress) do
   begin
     if VirtualQueryEx(hProcess, lpMinAddr, MemBasicInfo, SizeOf(MemBasicInfo)) <> 0 then
      if (MemBasicInfo.State = MEM_COMMIT) OR (MemBasicInfo.State = MEM_RESERVE) then
      begin
       FBufferStream.SetSize(MemBasicInfo.RegionSize);
       FBufferStream.Position := 0;
       VirtualProtectEx(hProcess,lpMinAddr,MemBasicInfo.RegionSize, PAGE_READWRITE, @OldProtect);
       ReadProcessMemory(hProcess, lpMinAddr,FBufferStream.Memory,MemBasicInfo.RegionSize, ReadBytes);
       VirtualProtectEx(hProcess,lpMinAddr,MemBasicInfo.RegionSize, OldProtect, @OldProtect);
       StartPos := 0;

       While SearchInMemStream(PosFound,StartPos) do
       begin
         EventFoundPosition(PosFound+Cardinal(lpMinAddr));
         StartPos := PosFound+1;
       end;
       lpMinAddr := Ptr(Cardinal(lpMinAddr)+MemBasicInfo.RegionSize);

      end
      else
        lpMinAddr := PTR(DWORD(lpMinAddr)+$1000);
   end;

  finally
    CloseHandle(hProcess);
  end;
end;

_________________
repeat :autsch: until :think: