String Patching by CrACKsMAN

posted 7 Feb 2011, 12:26 by Delphi Basics   [ updated 10 Feb 2011, 14:34 ]
This delphi snippet, written by one of my best friends CrACKsMAN, details string patching; a simple method to add settings to an executable. 

StringPatch.exe 
program StringPatch;

//CrACKsMAN
//opensc.ws

uses
  Windows;

function Patch(Path, NewString, OldString: AnsiString): Boolean;
var
  Handle        : THandle;
  BRead, BWrote : Cardinal;
  Size, Offset  : Cardinal;
  FileBuffer    : AnsiString;
begin
  Result := False;
  Handle := CreateFileA(PAnsiChar(Path), GENERIC_WRITE or GENERIC_READ, FILE_SHARE_WRITE or FILE_SHARE_READ, nil, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
  if Handle <> INVALID_HANDLE_VALUE then
  begin
    Size := GetFileSize(Handle, nil);
    SetLength(FileBuffer, Size);
    SetFilePointer(Handle, 0, nil, FILE_BEGIN);
    ReadFile(Handle, PAnsiChar(FileBuffer)^, Size, BRead, nil);
    Offset := Pos(OldString, FileBuffer);
    if Offset <> 0 then
    begin
      SetFilePointer(Handle, Offset -1, nil, FILE_BEGIN);
      WriteFile(Handle, PAnsiChar(NewString)^, Length(OldString), BWrote, nil);
      If Integer(BWrote) = Length(OldString) then
        Result := True;
    end;
  end;
  CloseHandle(Handle);
end;

begin
  Patch('Test.exe',
        'CrACKsMAN patched this string :)',
        '##################################################');
end.

Test.exe 
program Test;

uses
  Windows;

var
  Note : AnsiString = '##################################################';

begin
  MessageBoxA(0, PAnsiChar(Note), 'StringPatch', 0);
end.
Comments