Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to edit a binary file using Windows command line?

Is there a way in Windows to edit a binary file, from the command line? i.e. a way that could be written into a batch file?

I want to be able to edit a single byte, at a known position, in an existing file.

This existing question[1] is solved, but that's a Linux solution. I'm looking for something similar for Windows.

Background

There's a bug in GTA 1 when downloaded from Steam whereby the save-game data file gets corrupted on exit. As a result, the game can be played fine the first time but subsequently crashes. It turns out this can be fixed by changing the 5th byte in the file (i.e. the byte at address 0x04) from x00 to x06[2].

I can do this in Python easily, e.g.:

with open("PLAYER_A.DAT", "rb") as f:
    bytes = f.read()
bytes = bytes[:4] + '\x06' + bytes[5:]
with open("PLAYER_A.DAT", "wb") as g:
    for b in bytes: g.write(b)

Ideally though I'd rather do this in a batch job that does the following:

  • fixes the data file
  • launches GTA

I could make something that works for me (using Python), but that wouldn't help random other people who don't have Python (yes I know it's easy to get & install, but still). Similarly, there is a freeware available that claims to do just this, but I don't want to run a random .exe on my PC, and I don't think anyone else should either. For that reason, I'd like to present a batch file, that people can inspect, and - if they're happy with what it does - run for themselves.

Thanks for you help!

[1] CLI: Write byte at address (hexedit/modify binary from the command line)

[2] http://forums.steampowered.com/forums/showthread.php?t=1597746

[edit] Fixed up the Python script, as I found it didn't work as-is (file.read() returns an immutable object, so you can't just update one of the values).

like image 485
sam Avatar asked Apr 05 '13 12:04

sam


People also ask

How can I edit a binary file in Windows?

To open the Binary Editor on an existing file, go to menu File > Open > File, select the file you want to edit, then select the drop arrow next to the Open button, and choose Open With > Binary Editor.

Can a binary file be edited?

Binary files can be edited in either (or both) ASCII View or Hexadecimal View encodings, and EmEditor's large file capabilities mean that huge (up to 16 TB—that's 1,099 billion lines!) binary files are no problem (64-bit version).


2 Answers

I think PowerShell is a perfect tool for this task. It's available for XP or higher and is automatically shipped since Windows 7:

Just create a *.ps1 file with this content:

$bytes = [System.IO.File]::ReadAllBytes("PLAYER_A.DAT");
$bytes[4] = 0x06;

[System.IO.File]::WriteAllBytes("PLAYER_A.DAT", $bytes);
& "C:\Path-To-GTA1-Exe-File.exe"

Note that one has to enable unsigned PowerShell scripts:

  1. Start PowerShell as an administrator

  2. Run this command: Set-ExecutionPolicy RemoteSigned


You could also use VBScript but the script will be somewhat longer because it wasn't designed for reading binary files (you have to use ADODB.Stream objects).

Here is a compilation of helper functions: http://www.motobit.com/tips/detpg_read-write-binary-files/

like image 184
ComFreek Avatar answered Nov 09 '22 10:11

ComFreek


What about splitting the original file into three, then merging with your substitute byte in the middle? Split the binary into three pieces (start -> target-1 / target / target+1 -> end) then use COPY to merge the beginning and end chunks with your new byte in the middle.

I've never been able to get DOS (or any Windows Command Prompt) to split a file natively, but the free SPLITS.EXE utility is very good and could be included in your solution. COPY is of course a native command.

I can't find a link to that utility right now, but googling for 'free dos file split utility' yields many hits...

like image 40
Eight-Bit Guru Avatar answered Nov 09 '22 11:11

Eight-Bit Guru