Jump to content
Sign in to follow this  
blueforce4116

hex file

Recommended Posts

blueforce4116

I am trying to write to a binary file starting from some hex values stored in a text file. The hex values were originally from an exe file, and once the hex is written to a file the file needs to be fully executable. I cannot find a way to make this work. Any help would be appreciated.

Share this post


Link to post
Share on other sites
SmOke_N

I am trying to write to a binary file starting from some hex values stored in a text file. The hex values were originally from an exe file, and once the hex is written to a file the file needs to be fully executable. I cannot find a way to make this work. Any help would be appreciated.

For the simple approach (Not using Win API Funcs):

FileOpen() ; Look for binary mode

FileRead() ; Use the handle from FileOpen()

StringInStr() or StringRegExp() ; for searching string

StringToBinary() ; A function you may want that will convert binary code to hex readable format (Sort of like _StringToHex)

BinaryToString() ; Converts a binary string to a regular string (sort of like _HexToString)

StringReplace() or StringRegExpReplace() ; to put the string in at a position found or replace a current string

FileWrite() ; using handle from binary fileopen

FileClose() ; to close each handle left open from a FileOpen() call


Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Share this post


Link to post
Share on other sites
blueforce4116

Thanks, but for some reason the file is not writing as binary. here is what I have so far:

#include <file.au3>
$file1 = fileopen("1.txt", 0)
$file2 = fileopen("2.exe", 16)

FileWrite($file2, Binary("0x"))

While 1
    $data = FileReadLine($file1)
    If @error = -1 Then exitloop
    FileWrite($file2, Binary($file1))
Wend

FileClose($file1)
FileClose($file2)

Share this post


Link to post
Share on other sites
blakel

once the hex is written to a file the file needs to be fully executable

executable files must be written in a very specific format which is WAY beyond this forum and most people that use this forum.

Windows [NT] uses PE/COFF

http://en.wikipedia.org/wiki/Portable_Executable

Linux/UNIX typically use ELF and older versions use a.out.

Good luck

Share this post


Link to post
Share on other sites
blueforce4116

executable files must be written in a very specific format which is WAY beyond this forum and most people that use this forum.

Windows [NT] uses PE/COFF

http://en.wikipedia.org/wiki/Portable_Executable

Linux/UNIX typically use ELF and older versions use a.out.

Good luck

well i have the hex for an executable file, and if i dump it into a hex editor and save it, the program will execute. I just need to actually save this data as a binary file through autoit.

Share this post


Link to post
Share on other sites
Melba23

blueforce4116,

I have only ever tried this with .bmps (hence the variable names) but this snippet saved valid files:

$vBmp_Data = Binary($sBmp_Data)
$hFile = FileOpen(@TempDir & $sBmp_Name, 18) ; 18 = 16(binary) + 2(write - erase previous)
FileWrite($hFile, $vBmp_Data)
FileClose($hFile)

The initial $sBmp_Data was created by string concatenation. In your case opening the hex-dump file with FileOpen("filename", 16) should read in the data correctly if, as you say, you have an image of the executable code.

Good luck!

M23


Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______My UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Share this post


Link to post
Share on other sites
blueforce4116

Ok, I got some code to work on smaller files, but in trying to scale this up I am having some issues due to what appears to be variable size limits. Here is is the code that works:

Local Const $TestFile_Path = @ScriptDir & "\test.exe"
    Local $TestFile_ID
   
    $file1 = FileOpen("inputData.txt", 0);make sure to use correct input file name
    $inFile = "0x" & FileReadLine($file1)
    Local Const $data = $inFile

   ;## Create Test File
        $TestFile_ID = FileOpen($TestFile_Path, 14)
        FileWrite($TestFile_ID, Binary($data)); $data variable is too small to contain all the data from what I can tell
        FileClose($TestFile_ID)

Included is the input file I used for a really small executable program (~17K bytes)

and for a large program (~643K bytes)

I have been trying to break down the input into segments, then combine them to write to file. Seems to be straightforward, but so far I have had no luck actually making this work. Any help on this problem would be greatly appreciated.

input_files.zip

Share this post


Link to post
Share on other sites
Mobius

Just quickly adapted this from an old HexDump func I wrote,

it is kind of ugly as it contains no real error checking.

I used this on a reasonably large file (compiled AutoIt3 program) ^_^

and found the speed to be acceptable.

#cs
    SCRIPTNAME <Hexdump.ext>
#ce
GLOBAL CONST $cPOP = "HEX2BIN"
GLOBAL $s_F
IF $Cmdline[0] <> 0 AND FileExists($Cmdline[1]) THEN
    IF NOT _HEX2BIN($Cmdline[1]) THEN EXIT 1
ELSE
    $s_F = FileOpenDialog($cPOP,@WORKINGDIR,"Any (*.*)",3)
    IF @ERROR THEN EXIT
    IF NOT _HEX2BIN($s_F) THEN EXIT 1
ENDIF
EXIT

FUNC _HEX2BIN($sF,$sE = "exe")
    LOCAL $hF, $vD
    $hF = FileOpen($sF,0)
    $vD = "0x"& FileRead($hF)
    FileClose($hF)
    $sF = StringLeft($sF,StringInStr($sF,".",0,-1)) & $sE
    $hF = FileOpen($sF,18)
    $vD = FileWrite($hF,Binary($vD))
    FileClose($hF)
    IF FileExists($sF) THEN
        MsgBox(0,$cPOP,"Convert Succesful.")
        RETURN 1
    ELSE
        MsgBox(0,$cPOP,"Cannot find "& $sF)
        RETURN 0
    ENDIF
ENDFUNC

Share this post


Link to post
Share on other sites
Mobius

EDIT:

It seemed to work at first, but only certain files seem to work. most files still fail. here is a hex dump of a file that does not work:

http://rapidshare.com/files/228491004/largeFile.txt.html

10.5mb (10,967,040) bytes of hex data would slow it down all right...

Ed:

I just checked your large hex file, it converted to binary with no problems.

Btw, it is a Microsoft installer document (Not a Win32pe), try changing the extension to largeFile.msi

and it should work.

or using the HEX2BIN func I posted earlier

_HEX2BIN("C:\largeFile.txt","msi")

Vlad

Edited by Mobius

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
Sign in to follow this  

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.