Jump to content

Read/Write Binary File...


Recommended Posts

i've been playing with this for awhile and have finally gotten where i can read the file i want, but i still can't WRITE the characters i need to write. i can open file "Contoso.vuw" and process to the end of file, displaying both the character position number and associated character, IF i open the file as read only. If i open the file as FileWrite("contoso.vuw", 1) the file runs to the end, but displays nothing. If i open as FileWrite("contoso.vuw", 16) i get a binary representation of all characters. i'm stuck.

i need to replace characters 17, 18 and 19 with whatever the user types in (i can do that part), a 3-character userid. If i can get these three positions done i can do the other positions further down in the file (characters 2247, 2248, and 2249, all represented as "abc").

Can someone point me in the right direction please.

Thanks,

Dave

BinaryWrite.zip

Link to comment
Share on other sites

Something like this maybe?

Local $sUserID = "A27"
Local $aiPosArr[3] = [2, 17, 2247]
Local $hFile = FileOpen("contoso.vuw", 17)
If $hFile = -1 Then
    MsgBox(0, "Error", "Unable to open file.")
    Exit
EndIf
For $i = 1 To $aiPosArr[0]
    FileSetPos($hFile, $aiPosArr[$i], 0)
    FileWrite($hFile, $sUserID)
Next
FileClose($hFile)

Hope this helps :mellow:

Link to comment
Share on other sites

Good to see there is a thread like what i was searching for, i really want to know it's possible with AutoIt or not.

Take a look at this screen shots.

It's a file that i opened it with Ollydbg:

Then i patched this for some reasona, so it will be like this:

Now what i want to do in AutoIt is to write this data (here is six 90) in the address that you can see (here is 00467B52) and then save a copy of patched file. It don't seems to be so difficult but i just can figure it out. Maybe some experienced AutoIt scripter can help me.

Sorry if it's off topic.

Link to comment
Share on other sites

Good to see there is a thread like what i was searching for, i really want to know it's possible with AutoIt or not.

Take a look at this screen shots.

It's a file that i opened it with Ollydbg:

Then i patched this for some reasona, so it will be like this:

Now what i want to do in AutoIt is to write this data (here is six 90) in the address that you can see (here is 00467B52) and then save a copy of patched file. It don't seems to be so difficult but i just can figure it out. Maybe some experienced AutoIt scripter can help me.

Sorry if it's off topic.

If its a exe you want to patch and save, why dont you just do it through ollydbg? but yes its possible if you know what you are doing

Ever wanted to call functions in another process? ProcessCall UDFConsole stuff: Console UDFC Preprocessor for AutoIt OMG

Link to comment
Share on other sites

Of course it's a exe file and i can do it easily with Ollydbg but when i want to share the pathed file the size is about 4 mb, if i can do it with AutoIt (i mean make some patcher that get the file from user and patch it) the file size will reduced to maximum of 1 mb or even less i think.

any working code will be great for me.

I know where i want write data (i mean the exact byte when the file is in binary mode) and i know what i want to write.

Link to comment
Share on other sites

Holy Cats smartee!! That worked like a champ!!

Now i need to understand why. FileOpen() seems like the flags are "additive." If i open the file with "16" as a binary file then adding the "1" to make it open writable would make "17."

i've never quite gotten the array thing. It looks like You're creating a two-dimensional array and storing the data from position 17 and 2247 for 3 characters in each array element.

Sorry if i seem dumb, but i gotta put it in "Dave-speak" so i can assure myself it's not magic.

Thanks Loads!

Dave

Link to comment
Share on other sites

@Obviator:

You're welcome. :blink:

You are right, the 17 comes from 16(Binary mode) + 1(Write mode) :mellow:

As for the array, it is a one dimensional array of integers with 3 elements hence the 3 in Local $aiPosArr[3]. The first element of the array, $aiPosArr[0], contains the number 2 indicating there are two positions, and the 2nd and 3rd elements are the positions where the user's ID are to be inserted.

For more help in using arrays, you can check Arrays - AutoIt Wiki :)

Note however that using an array wasn't necessary, I only used it to avoid repetitive code, this would work just as fine:

Local $sUserID = "A27"
Local $hFile = FileOpen("contoso.vuw", 17)
If $hFile = -1 Then
    MsgBox(0, "Error", "Unable to open file.")
    Exit
EndIf
FileSetPos($hFile, 17, 0)
FileWrite($hFile, $sUserID)
FileSetPos($hFile, 2247, 0)
FileWrite($hFile, $sUserID)
FileClose($hFile)

If there is anything else you don't understand feel free to post back :)

Oh no! You don't seem dumb, you seem new to the language, and nobody starts off knowing everything :party:

Maybe you can peruse the Wiki and help-file at your leisure to get familiar with AutoIt.

Hope this helps :)

Link to comment
Share on other sites

Well, here i go exposing myself to ridicule...

i've been working in and around computers since the mid-80s; i can't tell You how many different OSs, programs, and languages i've worked with in that time; i couldn't tell You enough about any of them to ruin my amature status :mellow: . Arrays are just one of those things that i've never "grokked," no matter how much reading i do or which language i try. i think of arrays as a sort of spreadsheet with a fixed number of cells, "fixed" being determined by the programmer.

i've always been more of a "brute force" type of programmer, using what knowledge i have to solve a particular problem then moving on to the next issue. i've always admired the way some folks have elegant solutions to problems, like Your solution, but i just don't have that elegant streak. Thank God there's folks who do.

Thanks again!

Dave

Link to comment
Share on other sites

@Obviator:

You're welcome. ;)

You are right, the 17 comes from 16(Binary mode) + 1(Write mode) :mellow:

As for the array, it is a one dimensional array of integers with 3 elements hence the 3 in Local $aiPosArr[3]. The first element of the array, $aiPosArr[0], contains the number 2 indicating there are two positions, and the 2nd and 3rd elements are the positions where the user's ID are to be inserted.

For more help in using arrays, you can check Arrays - AutoIt Wiki :)

Note however that using an array wasn't necessary, I only used it to avoid repetitive code, this would work just as fine:

Local $sUserID = "A27"
Local $hFile = FileOpen("contoso.vuw", 17)
If $hFile = -1 Then
    MsgBox(0, "Error", "Unable to open file.")
    Exit
EndIf
FileSetPos($hFile, 17, 0)
FileWrite($hFile, $sUserID)
FileSetPos($hFile, 2247, 0)
FileWrite($hFile, $sUserID)
FileClose($hFile)

If there is anything else you don't understand feel free to post back :)

Oh no! You don't seem dumb, you seem new to the language, and nobody starts off knowing everything :party:

Maybe you can peruse the Wiki and help-file at your leisure to get familiar with AutoIt.

Hope this helps :)

you are the best, let me kiss you :blink: !!! ^_^

i did it, now i can do in autoit what i can do in ollydbg , i made my patcher.

thank you guys.

Edited by D4RKON3
Link to comment
Share on other sites

Guys you can also enter the address in hex like this:

FileSetPos($File, 0x67B52, 0)
FileWrite($File, "Ú¯")

Ú¯ charactor that i used here in hex is 90 that it mean NOP in ollydbg, but how can i enter this value as hex? i mean maybe:

FileSetPos($File, 0x67B52, 0)
FileWrite($File, 0x90)

but it doesn't work coz also affcet some byte after the address.

Link to comment
Share on other sites

there is any way to write 1 byte?

FileSetPos($File, 0x67B52, 0)
$BinCon = FileRead($File,4)
$Contents = Binary("0x90" & StringTrimLeft($BinCon,4)) ;Forcing cast to string, need to trim "0xXX" off
FileSetPos($File, 0x67B52, 0)
FileWrite($File, $Contents)

Of course it's a exe file and i can do it easily with Ollydbg but when i want to share the pathed file the size is about 4 mb, if i can do it with AutoIt (i mean make some patcher that get the file from user and patch it) the file size will reduced to maximum of 1 mb or even less i think.

Then you're probably doing it wrong...

Ever wanted to call functions in another process? ProcessCall UDFConsole stuff: Console UDFC Preprocessor for AutoIt OMG

Link to comment
Share on other sites

Then you're probably doing it wrong...

Why?

I'm going to do it this way:

FileSetPos($File, 0x9FFE8, 0)
    FileWrite($File, _HexToString("83"))
    FileSetPos($File, 0x9FFE9, 0)
    FileWrite($File, _HexToString("F8"))
    FileSetPos($File, 0x9FFEA, 0)
    FileWrite($File, _HexToString("00"))
    FileSetPos($File, 0x9FFEB, 0)
    FileWrite($File, _HexToString("90"))
    FileSetPos($File, 0x9FFEC, 0)
    FileWrite($File, _HexToString("90"))
Link to comment
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
 Share

×
×
  • Create New...