Exetools  

Go Back   Exetools > General > Source Code

Notices

Reply
 
Thread Tools Display Modes
  #1  
Old 07-01-2019, 01:24
phroyt phroyt is offline
Friend
 
Join Date: May 2018
Posts: 60
Rept. Given: 0
Rept. Rcvd 7 Times in 3 Posts
Thanks Given: 22
Thanks Rcvd at 87 Times in 34 Posts
phroyt Reputation: 7
Lightbulb [Delphi] Search Memory

Hi,

Some time ago I found a piece of code to SearchBytes.
After a little I come out with this.

Code:
//XE10 Compatible

uses
  System.SysUtils,
  System.Classes,
  Winapi.PsApi,
  Winapi.Windows;

Function SearchMemory(Module: hModule; wildcard: Byte; searchCode: Array of Byte; size: Integer; Offset: Cardinal = 0) : Pointer;

Implementation

//Search Memory
Function SearchMemory(Module: hModule; wildcard: Byte; searchCode: Array of Byte; size: Integer; Offset: Cardinal = 0) : Pointer;
Const
  UCHAR_MAX = 255;
Var
  scan,lastByte,defaultSkip, searchEnd,searchSuccess : NativeUInt;
  skipLength : ARRAY of Integer;
  dllInfo : TModuleInfo;
  p : pointer;
  OldProtect: DWORD;
begin

  searchSuccess := 0;
  lastByte := size - 1;
  result := nil;

  while searchCode[lastByte] = wildcard do
    Dec(lastByte);

  defaultSkip := lastByte;

  for scan:= 0 to lastByte - 1 do
  begin
    if searchCode[scan] = wildcard then
      defaultSkip := lastByte - scan;
  end;

  if defaultSkip > 1 then
    defaultSkip := defaultSkip - 1;

  //Is just setting the default skip length
  SetLength(skipLength, UCHAR_MAX);
  for scan:=0 to UCHAR_MAX do
    skipLength[scan] := defaultSkip;

  for scan := 0 to lastByte-1 do
    if searchCode[scan] <> wildcard then
      begin
        skipLength[searchCode[scan]] := lastByte - scan;
      end;

  if GetModuleInformation(GetCurrentProcess, Module, @dllInfo, sizeof(dllInfo)) then
    begin

      p := dllInfo.lpBaseOfDll;


      try

        searchEnd := Cardinal(dllInfo.lpBaseOfDll) + dllInfo.SizeOfImage;
        searchEnd := searchEnd - (lastByte + 1);

        if ((Cardinal(p)+offset) <= searchEnd) then
          p := Pointer(Cardinal(p)+offset);


        //FM: remove the write protect on Code Segment
        VirtualProtect(p, searchEnd, PAGE_EXECUTE_READWRITE, OldProtect);

        while Cardinal(p) <= searchEnd do
          begin
            scan := lastByte;

            while ((searchCode[scan] = wildcard) or (PByte(Cardinal(p)+scan)^ = searchCode[scan])) do
            begin
              if scan = 0 then
              begin
                Result := P;
                Inc(searchSuccess);
                Break;
              end;
              scan := scan-1;
            end;

            if ( searchSuccess <> 0 ) then
              break;

            p := Pointer(NativeInt(p)+skipLength[PByte(NativeUInt(p)+lastByte)^]);
          end;

        if ( searchSuccess = 0 ) then
          result := nil;

        if ( searchSuccess > 1 ) then
          result := nil;
      finally
        VirtualProtect(P, searchEnd, OldProtect, @OldProtect);
      end;
    end;
end;
Even without writing anything to Module memory, sometimes I got a wrong Byte value. Changing the protection solved this.


Any thoughts to improve?


Sample use:


Code:
const
//SFNTQueryFeature
Const SFNTQueryFeature_SIG :   Array [0..15] of Byte = ($55, $8B, $EC, $83, $EC,
                          $24, $C7, $45, $DC, $02, $00, $00, $00, $51, $31, $C9);

var
  base: hModule;
  proc1,
  proc2: Pointer;
  offset: Cardinal;

begin

  base := GetModuleHandle(nil);

  proc1 := SearchMemory(base, $00, SFNTGetLicense_SIG, length(SFNTGetLicense_SIG));

  if (proc1 <> nil) then
    offset := Cardinal(proc1)+1 - base;
  
  proc2 := SearchMemory(base, $00, SFNTGetLicense_SIG, length(SFNTGetLicense_SIG), offset);
Reply With Quote
The Following 7 Users Say Thank You to phroyt For This Useful Post:
chessgod101 (01-04-2020), Gladiyator (07-04-2019), Indigo (07-19-2019), Mahmoudnia (07-01-2019), niculaita (07-01-2019), ontryit (10-13-2019), Rasmus (04-11-2020)
  #2  
Old 04-10-2020, 22:39
kjms's Avatar
kjms kjms is offline
VIP
 
Join Date: Jun 2011
Posts: 193
Rept. Given: 184
Rept. Rcvd 320 Times in 79 Posts
Thanks Given: 18
Thanks Rcvd at 75 Times in 31 Posts
kjms Reputation: 300-399 kjms Reputation: 300-399 kjms Reputation: 300-399 kjms Reputation: 300-399
Hi any one please compile as file, i am not familiar in delphi.
Reply With Quote
The Following User Says Thank You to kjms For This Useful Post:
Cabdulahi Sharif (04-26-2020)
  #3  
Old 04-10-2020, 23:29
chessgod101's Avatar
chessgod101 chessgod101 is online now
Co-Administrator
 
Join Date: Jan 2011
Location: United States
Posts: 499
Rept. Given: 2,099
Rept. Rcvd 667 Times in 208 Posts
Thanks Given: 490
Thanks Rcvd at 688 Times in 143 Posts
chessgod101 Reputation: 500-699 chessgod101 Reputation: 500-699 chessgod101 Reputation: 500-699 chessgod101 Reputation: 500-699 chessgod101 Reputation: 500-699 chessgod101 Reputation: 500-699
This is a library for searching memory of a loaded module in your delphi application. It isn't something you can just compile directly into a working program. It you need a means to search bytes in memory, use your favorite hex editor.
__________________
"Real knowledge is to know the extent of one's ignorance." Confucius
Reply With Quote
The Following 2 Users Say Thank You to chessgod101 For This Useful Post:
kjms (04-11-2020), Rasmus (04-11-2020)
Reply

Tags
delphi

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Problem with search dedificator General Discussion 11 08-19-2009 13:33


All times are GMT +8. The time now is 01:30.


Always Your Best Friend: Aaron, JMI, ahmadmansoor, ZeNiX
( 1998 - 2020 )