Jump to content
Tuts 4 You

Read Data Write Data (Delphi)[Help]


eXec0d3

Recommended Posts

I need to Read data from .exe file for example this is OEP (virtual address) 00000023 I've converted it to physical and now I need to check if data on this address = $55 then WriteFile(H, buf, SizeOf(buf), written, nil);

How to do that? Sorry for my bad English language :(

Link to comment

I need to Read data from .exe file for example this is OEP (virtual address) 00000023 I've converted it to physical and now I need to check if data on this address = $55 then WriteFile(H, buf, SizeOf(buf), written, nil);

How to do that? Sorry for my bad English language :(

Use ReadFile Api, has the same params as WriteFile.

You can read file into memory all at once, or use SetFilePointer api to choose where to read from ..

Also you can use FileStream / MemoryStream Delphi classes, but they are lame and you get much smaller code using apis .. :)

  • Like 1
Link to comment

Check if this works for you. Of course, you can modify it to add checks and everything you need...

type
BufferByte = array of Byte;procedure ReadBufferFromFile(PathToMyFile : AnsiString; Offset : Integer; var buf : BufferByte);
var
MyFile : TFileStream; begin
try
MyFile := TFileStream.Create(PathToMyFile,fmOpenRead);
MyFile.Seek(Offset,SoFromBeginning);
MyFile.Read(buf[0],Length(buf));
finally
MyFile.Free;
end;
end;procedure WriteBufferToFile(PathToMyFile : AnsiString; Offset : Integer; var buf : BufferByte);
var
MyFile : TFileStream; begin
try
MyFile := TFileStream.Create(PathToMyFile,fmOpenWrite);
MyFile.Seek(Offset,SoFromBeginning);
MyFile.Write(buf[0],Length(buf));
finally
MyFile.Free;
end;
end;procedure CheckOEPAndFix;
var
Offset : Integer;
PathToMyFile : AnsiString;
bufRead, bufWrite : BufferByte;begin
Offset := $1523; // Example of address of your OEP, it must be offset of a file, not Virtual Address
PathToMyFile := 'C:\Program Files\MyExe.exe'
SetLength(bufRead,1);
ReadBufferFromFile(PathToMyFile,Offset,bufRead);
if bufRead[0] = $55 then
begin
........ // Treatment of your buffer to be written
........
WriteBufferToFile(PathToMyFile,Offset,bufWrite);
end;
end;
  • Like 1
Link to comment

thanks it works !! :)

but,

 PathToMyFile := 'C:\Program Files\MyExe.exe' <-- here must be ";" this
PathToMyFile := 'C:\Program Files\MyExe.exe';
Link to comment

Nice! Now you can try to find how to improve your techniques, by modifying that code for your needings.

About the missing ';', hehe, when you code this happens for sure! :^

Luckily there are good coompilers warning you about things like that...

Best regards

Nacho_dj

Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...