rootx

replace hex value in a file

3 posts in this topic

Cattura.thumb.PNG.25bf352df15018ec68f46c

I want to replace this value with autoit, usually I do that with HxD editor and replace selection utility.

I'm able to find the exact value but I do not understand how to replace it, can someone explain me how to do it?

Thx.

My code

#include <String.au3>
#include <Array.au3>
$file = "myfile" 
$read = FileRead($file)
$filhex = _StringToHex($read)
$split = StringRegExp($filhex, "(?s).{1,2}", 3)

$kb = 0
$sp = ""
$val = ""
$row = 0

For $i = 0 To UBound($split) - 1
    $kb += 1
    If $kb < 16 Then
        $sp = "  "
        $val &= $split[$i] & $sp

    ElseIf $kb = 16 Then
        $sp = @CRLF
        $val &= $split[$i] & $sp
        $kb = 0
        $row += 1
    EndIf
Next

ConsoleWrite($split[53]&@LF)

 

Share this post


Link to post
Share on other sites



local $hFile=FileOpen("1.exe",512)
Local $sData=FileRead($hFile)
FileClose($hFile)

Local $hFile2=FileOpen("2.exe",514)
FileWrite($hFile2,StringReplace($sData,Chr(0x2B),Chr(0x2A),1))
FileClose($hFile2)

That change the first found 2B for 2A.

 

Saludos

Share this post


Link to post
Share on other sites
local $hFile=FileOpen("1.exe",512)
Local $sData=FileRead($hFile)
FileClose($hFile)

Local $hFile2=FileOpen("2.exe",514)
FileWrite($hFile2,StringReplace($sData,Chr(0x2B),Chr(0x2A),1))
FileClose($hFile2)

That change the first found 2B for 2A.

 

Saludos

Thx one step forward...

I used one array because I need to replace a lot of values dynamically without knowing the value inside the file but only the exactly position.

For example at the position of the Array [12][22].......N replace with [AE][2B] ....N

In your example the target is the first occurrence... but I need to replace multiples values in one loop. Usually I used _FileReadToArray but Im not sure with Hex values the right way to do it.

:>

Share this post


Link to post
Share on other sites

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

  • Similar Content

    • rootx
      By rootx
      I need help to read one file in a HEX mode and go to one offset and than read the value. THX

    • Altor
      By Altor
      Hi all:
      I nedd to convert a word hex number to an integer 16
      Ex: 0xFFE9 convert to -23
      I would appreciate if they had any idea how I can get it
    • toasterking
      By toasterking
      I was just working on a project that involved decoding a stream of binary data from a serial port in AutoIt.  It took me a few hours to figure out how to process the data efficiently in AutoIt and I did not find any helpful examples on how to do so, so I thought I would share my core example and maybe save someone else some time.  There may be a more efficient way to do this, but this works well for me.
       
      #cs Author: ToasterKing This is an example of a way to parse streaming binary data that follows a strict format with a header and footer. In this example, each frame is 5 bytes with a 2-byte header of 0xD5AA and a 1-byte footer of 0xAD. The _BinaryParse() function accumulates incoming data in a buffer. Once a footer is found, it searches backward for the header, and if it is in the right position, it extracts the remaining 2 bytes in the middle, then moves on to looking for the next frame. #ce ; The data source might be something asynchronous like serial or TCP, but since this is just an example, I'm just putting the data in a variable. Local $fSomeData $fSomeData = Binary("0xD5AA24B1") ; Binary data constituting almost a complete frame. _BinaryParse($fSomeData) ; Call the function with the received data. It isn't a complete frame, so it is just stored in the buffer until more data is received. $fSomeData = Binary("0xAD62D5AA92E7AD") ; Remainder of the previous frame, one garbage byte (0x62) which should be skipped, and a complete additional frame. _BinaryParse($fSomeData) ; The function should be able to parse both frames now. Func _BinaryParse($fNewData) Local Static $fBinaryReceived = Binary("") ; Buffer for received data ConsoleWrite("Hey, the function is called!" & @CRLF) ; Add new data to the buffer. ; This ridiculous monstrosity is the only way I could find to append binary data to binary data in AutoIt. It must be converted to strings first. ; Both, one, or no substrings will begin with "0x" depending on whether they contained binary data. To be converted back to binary properly, only one instance ; of "0x" must exist at the beginning of the string. $fBinaryReceived = Binary("0x" & StringReplace(String($fBinaryReceived) & String($fNewData),"0x","")) ConsoleWrite("Data in the buffer: " & String($fBinaryReceived) & @CRLF) Local $iLength = BinaryLen($fBinaryReceived) ; Count the bytes in the data If $iLength > 0 Then Local $fBinaryReceivedTemp = $fBinaryReceived ; Create temporary copy to work on Local $fByte1,$fByte2 For $i = 1 To $iLength If BinaryMid($fBinaryReceivedTemp,$i,1) = 0xAD Then ; If the 1-byte footer found ConsoleWrite("Footer found at end of " & $i & " of " & $iLength & " bytes!" & @CRLF) If BinaryMid($fBinaryReceivedTemp,$i - 4,1) = 0xD5 And BinaryMid($fBinaryReceivedTemp,$i - 3,1) = 0xAA Then ; and the 2-byte header is found 4 bytes before that ConsoleWrite("Header found before the footer!" & @CRLF) $fByte1 = BinaryMid($fBinaryReceivedTemp,$i - 2,1) ; Get 1st byte in the body (between header and footer) $fByte2 = BinaryMid($fBinaryReceivedTemp,$i - 1,1) ; Get 2nd byte in the body (between header and footer) ConsoleWrite("Here is the critical data: " & String($fByte1) & " " & String($fByte2) & @CRLF) ; Just display the 2 bytes for demonstration purposes. Normally, you'd do something more useful with it here. EndIf $fBinaryReceived = BinaryMid($fBinaryReceivedTemp,$i + 1) ; Truncate the original data to remove all of the bytes just processed, then continue processing $fBinaryReceivedTemp EndIf Next EndIf EndFunc  
    • TheDcoder
      By TheDcoder
      Hello Guys!
      I want to convert the binary hash returned by _Crypt_Hash* function to string.
      Thanks in Advance! TD
    • yucatan
      By yucatan
      i have an external hex editor in which i can edit something in the hex of a file.
      is it possible do edit the hex of a file with autoit?
      if this is possible how this can be done?
      Thanks in advance.