Edit Binary File

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Message
Author
stondesign
Posts: 13
Joined: 23 Oct 2018 17:28

Edit Binary File

#1 Post by stondesign » 10 Mar 2019 12:08

Hello friends! I need a script ...
Who could help me?

Basically I would like to create a program to edit a binary file.
That is, it should look for the hexadecimal in the offsets and replace them with the ones I set them.
Obviously you do not have to give a search command, but you have to go looking for it.
I have created this! but I do not search because I set the offsets

In practice it is a program to remove EGR control on cars! But unfortunately with the same ECU sometimes the offsets change and set an offset is not good because I would like the program to be able to find the bits to change ...

OPEN PROGRAM TO CLICK ON SELECT.BAT

Code: Select all

<# : chooser.bat

@echo off
setlocal

for /f "delims=" %%I in ('powershell -noprofile "iex (${%~f0} | out-string)"') do (
    start C:\SolutionDPFErasePack\Fiat\mjd6f3\egroff.bat %%~I
    exit
)
goto :EOF

: end Batch portion / begin PowerShell hybrid chimera #>

Add-Type -AssemblyName System.Windows.Forms
$f = new-object Windows.Forms.OpenFileDialog
$f.InitialDirectory = pwd 
$f.Filter = "File Binari (*.bin)|*.bin|File Ori (*.ori*)|*.ori*|Tutti i File (*.*)|*.*"
$f.ShowHelp = $false
$f.Multiselect = $false
[void]$f.ShowDialog()
if ($f.Multiselect) { $f.FileNames } else { $f.FileName }
EGROFF.BAT

Code: Select all

MKDIR "%userprofile%\desktop\File DPFErasePack"
if exist "%userprofile%\desktop\File DPFErasePack" Erase /F /S /Q "%userprofile%\desktop\File DPFErasePack"
xcopy %1 "%userprofile%\desktop\File DPFErasePack"

@echo off
setlocal EnableDelayedExpansion

rem FILE TRASCINATO SUL BAT
set "file=%1"
for %%a in ("%file%") do set "hex=%%~Na.hex"

rem Specifica i valori Offset da cambiare
set "changes=84750:B9 84751:92"

rem Codifica il file in HEX
certutil -f -encodehex "%file%" "%hex%"

rem Crea una lista di line-offsets per cercare i valori Offset da Modificare
set "offsets="
for %%c in (%changes%) do for /F "tokens=1,2 delims=:" %%a in ("%%c") do (
   set "offset=%%a"
   set "offsets=!offsets! !offset:~0,-1!"
)

rem Converti line-Offsets in un elenco di numeri in riga
set "lines="
for /F "delims=:" %%a in ('findstr /N /I "%offsets%" "%hex%"') do set "lines=!lines! %%a"

rem Elabora il file esadecimale ed esegue le modifiche desiderate
set "tokens=%%A %%B %%C %%D %%E %%F %%G %%H  %%I %%J %%K %%L %%M %%N %%O %%P %%Q"
set "letter=ABCDEFGHIJKLMNOPQ"
for /F "tokens=1*" %%a in ("%changes%") do set "change=%%a" & set "changes=%%b"
set "last=1"
< "%hex%" (

   rem Elabora le linee di destinazione
   for %%l in (%lines%) do (

      rem Copia le linee tra quelle di destinazione
      set /A skip=%%l-last, last=%%l+1
      for /L %%i in (1,1,!skip!) do (
         set /P "line="
         echo !line!
      )

      rem Elabora la linea di destinazione
      set /P "line="
      for /F "tokens=1*" %%x in ("!line!") do (
         set "ofs=%%x" & set "values=%%y"
         for /L %%# in (1,1,16) do for /F "tokens=1,2 delims=:" %%a in ("!change!") do (
            set "offset=%%a"
            if /I "!offset:~0,-1!" equ "!ofs!" (
               set /A "token=0x!offset:~-1!"
               for /F %%x in ("!token!") do for /F %%y in ("!letter:~%%x,1!") do call :ChangeValue %%y
               if defined changes (
                  for /F "tokens=1*" %%x in ("!changes!") do set "change=%%x" & set "changes=%%y"
               ) else (
                  set "change="
               )
            )
         )
      )
      echo !ofs!	!values!
   )

   rem Copia le linee dopo l'ultima destinazione
   findstr "^"

) > temp.hex
del "%hex%"

set "name=%~n1"

rem Codifica file HEX in file NOEGR.bin
certutil -f -decodehex temp.hex %name%NOEGR.bin
xcopy %name%NOEGR.bin "%userprofile%\desktop\File DPFErasePack"
del temp.hex
del %name%NOEGR.bin

goto :EOF

:ChangeValue token
set "newVals=!tokens:%1=b!"
for /F "tokens=1-16*" %%A in ("%values%") do set "values=%newVals%"

exit /B
Can you help me create this little program? :lol:

stondesign
Posts: 13
Joined: 23 Oct 2018 17:28

BIN FILE

#2 Post by stondesign » 10 Mar 2019 12:10

I have shared the BIN file

Fiat 500 1.3 Mjt 75CV
Attachments
mjd6f3.zip
(320.82 KiB) Downloaded 6 times

aGerman
Expert
Posts: 3522
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Edit Binary File

#3 Post by aGerman » 10 Mar 2019 14:36

I would like the program to be able to find the bits to change
Which hex sequence (how many bytes) would uniquely mark the position in your file? How does it look like before and after you changed it?

Steffen

stondesign
Posts: 13
Joined: 23 Oct 2018 17:28

Re: Edit Binary File

#4 Post by stondesign » 10 Mar 2019 14:50

Example
Attachments
Screenshot_20190310-215001.jpg
Screenshot_20190310-215001.jpg (566.75 KiB) Viewed 233 times

aGerman
Expert
Posts: 3522
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Edit Binary File

#5 Post by aGerman » 10 Mar 2019 15:30

Okay, you marked 24 bytes. Is this the number of bytes that is sufficient to identify the sequence?
But still
How does it look like … after you changed it?

stondesign
Posts: 13
Joined: 23 Oct 2018 17:28

Re: Edit Binary File

#6 Post by stondesign » 10 Mar 2019 15:39

change

84750:37
84751:AC

to

84750:B9
84751:92

aGerman
Expert
Posts: 3522
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Edit Binary File

#7 Post by aGerman » 10 Mar 2019 16:25

I suggest to use dbenham's JREPL.BAT

Code: Select all

call JREPL.BAT "\x00\x32\x00\x4d\x00\x8c\x40\xf3\x37\xac\x01\x55\x0d\xac\x08\x55\x00\x00\x00\xf0\x07\xd0\xff\xb0" "\x00\x32\x00\x4d\x00\x8c\x40\xf3\xb9\x92\x01\x55\x0d\xac\x08\x55\x00\x00\x00\xf0\x07\xd0\xff\xb0" /XSEQ /M /F "mjd6f3.bin" /O "-"

stondesign
Posts: 13
Joined: 23 Oct 2018 17:28

Re: Edit Binary File

#8 Post by stondesign » 10 Mar 2019 16:51

...and if I had to give more replacement commands? that is, give 2 or more changes looking for more parts?

Example

First change
x00\x32\x00\x4d\x00\x8c\x40\xf3\x37\xac\x01\x55\x0d\xac\x08\x55\x00\x00\x00\xf0\x07\xd0\xff\xb0
to
x00\x32\x00\x4d\x00\x8c\x40\xf3\xb9\x92\x01\x55\x0d\xac\x08\x55\x00\x00\x00\xf0\x07\xd0\xff\xb0


Second change that is in another position?
x01\x02\x03\xac\x10\x20\x30
to
x02\x03\x04\xac\x11\x22\x33

what would be the multi script to use?
Last edited by stondesign on 10 Mar 2019 17:08, edited 2 times in total.

aGerman
Expert
Posts: 3522
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Edit Binary File

#9 Post by aGerman » 10 Mar 2019 16:58

Just call JREPL.BAT twice. Option /O "-" is there to change the original file. That is, after the first call the first replacement has been applied and you can continue with the next replacement.
EDIT: Also pass /P and /PFLAG "" to replace only the first match.
But actually I asked you for a unique byte sequence. Unique means that you can make sure it doesn't appear twice.

stondesign
Posts: 13
Joined: 23 Oct 2018 17:28

Re: Edit Binary File

#10 Post by stondesign » 10 Mar 2019 17:09

is it possible to have a written example?

aGerman
Expert
Posts: 3522
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Edit Binary File

#11 Post by aGerman » 10 Mar 2019 17:13

stondesign wrote:
10 Mar 2019 17:09
is it possible to have a written example?
Same line again with just the other strings. Why do you need me to write it?

stondesign
Posts: 13
Joined: 23 Oct 2018 17:28

Re: Edit Binary File

#12 Post by stondesign » 10 Mar 2019 17:20

I would like to write the automatic changes in a single script However, thank you was very kind and helpful! Thank you for what you did ...

dbenham
Expert
Posts: 2199
Joined: 12 Feb 2011 21:02
Location: United States (east coast)

Re: Edit Binary File

#13 Post by dbenham » 11 Mar 2019 08:50

aGermans' regular expression find/replace pair can be modified a bit to make it easier/shorter to write by using capture groups and lookahead groups.

Something of the form:

Code: Select all

 Capture Group 1                              Look-ahead
(BytesToMatchAndPreserve)ByteOrBytesToChange(?=AdditionalBytesToMatchAndPreserve)  $1ReplacementBytes

Where Capture Group 1 is substituted for $1
So for your case:

Code: Select all

call JREPL.BAT (\x00\x32\x00\x4D\x00\x8C\x40\xF3)\x37\xAC(?=\x01\x55\x0D\xAC\x08\x55\x00\x00\x00\xF0\x07\xD0\xFF\xB0)  $1\xB9\x92  /XSEQ /M /F "mjd6f3.bin" /O -
You want to do multiple find/replace.

Your find/replace pairs don't have any white space or poison characters, so the find/replace arguments don't need to be quoted.

Also, your find/replace pairs don't have any wild cards.

So you can use a FOR loop with each pair in quotes, and then in the DO clause use the ~ modifier to get rid of the quotes.

Code: Select all

@echo off
for %%A in (
  "(\x00\x32\x00\x4D\x00\x8C\x40\xF3)\x37\xAC(?=\x01\x55\x0D\xAC\x08\x55\x00\x00\x00\xF0\x07\xD0\xFF\xB0)  $1\xB9\x92"
  "x01\x02\x03\xAC\x10\x20\x30  x02\x03\x04\xAC\x11\x22\x33"
) do call JREPL.BAT %%~A /XSEQ /M /F "mjd6f3.bin" /O -
The above strategy is a bit inefficient since the file must be read, searched, and written multiple times. It would be better if the file only needed to be read and written once. This is possible with the /T option if and only if the regions to be found do not overlap.

The command line is easier to maintain if you define the find/replace values in variables:

Code: Select all

@echo off
setlocal

set "F1=(\x00\x32\x00\x4D\x00\x8C\x40\xF3)\x37\xAC(?=\x01\x55\x0D\xAC\x08\x55\x00\x00\x00\xF0\x07\xD0\xFF\xB0)"
set "R1=$1\xB9\x92"

set "F2=\x01\x02\x03\xAC\x10\x20\x30"
set "R2=\x02\x03\x04\xAC\x11\x22\x33"

call JREPL.BAT "%F1% %F2%" "%R1% %R2% /T " " /XSEQ /M /F "mjd6f3.bin" /O -
Or you could use a FOR loop and a FOR /F loop to build the FIND and REPL strings, and make a single call

Code: Select all

@echo off
setlocal enableDelayedExpansion
set "find="
set "repl="
for %%A in (
  "(\x00\x32\x00\x4D\x00\x8C\x40\xF3)\x37\xAC(?=\x01\x55\x0D\xAC\x08\x55\x00\x00\x00\xF0\x07\xD0\xFF\xB0)  $1\xB9\x92"
  "\x01\x02\x03\xAC\x10\x20\x30  \x02\x03\x04\xAC\x11\x22\x33"
) do for /f "tokens=1,2" %%B in (%%A) do (
  set "find=!find! %%B"
  set "repl=!repl! %%C"
)
call JREPL.BAT "!find:~1!" "!repl:~1"  /T " " /XSEQ /M /F "mjd6f3.bin" /O -
Or you can store the find/replace values in a pair of files like so:

find.txt

Code: Select all

(\x00\x32\x00\x4D\x00\x8C\x40\xF3)\x37\xAC(?=\x01\x55\x0D\xAC\x08\x55\x00\x00\x00\xF0\x07\xD0\xFF\xB0)
\x01\x02\x03\xAC\x10\x20\x30
replace.txt

Code: Select all

$1\xB9\x92
\x02\x03\x04\xAC\x11\x22\x33
And then run the following command

Code: Select all

call JREPL find.txt replace.txt /T FILE /XSEQ /M /F "mjd6f3.bin" /O -

All of the strategies above have the advantage that it should be easy to add additional find/replace pairs.


Dave Benham

catalinnc
Posts: 39
Joined: 12 Jan 2015 11:56

Re: Edit Binary File

#14 Post by catalinnc » 11 Mar 2019 11:53

very nice examples...
_

aGerman
Expert
Posts: 3522
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Edit Binary File

#15 Post by aGerman » 11 Mar 2019 12:30

@Dave
Phew! I was hoping you would come in. I still have only a poor understanding of the possibilities JREPL provides.

Steffen

Post Reply