blueforce4116 Posted May 2, 2009 Share Posted May 2, 2009 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. Link to comment Share on other sites More sharing options...
Moderators SmOke_N Posted May 2, 2009 Moderators Share Posted May 2, 2009 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 modeFileRead() ; Use the handle from FileOpen()StringInStr() or StringRegExp() ; for searching stringStringToBinary() ; 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 stringFileWrite() ; using handle from binary fileopenFileClose() ; 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. Link to comment Share on other sites More sharing options...
blueforce4116 Posted May 2, 2009 Author Share Posted May 2, 2009 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) Link to comment Share on other sites More sharing options...
blakel Posted May 2, 2009 Share Posted May 2, 2009 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 Link to comment Share on other sites More sharing options...
blueforce4116 Posted May 2, 2009 Author Share Posted May 2, 2009 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. Link to comment Share on other sites More sharing options...
Moderators Melba23 Posted May 2, 2009 Moderators Share Posted May 2, 2009 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 Open spoiler to see my UDFs: Spoiler ArrayMultiColSort ---- Sort arrays on multiple columnsChooseFileFolder ---- Single and multiple selections from specified path treeview listingDate_Time_Convert -- Easily convert date/time formats, including the language usedExtMsgBox --------- A highly customisable replacement for MsgBoxGUIExtender -------- Extend and retract multiple sections within a GUIGUIFrame ---------- Subdivide GUIs into many adjustable framesGUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView itemsGUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeViewMarquee ----------- Scrolling tickertape GUIsNoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxesNotify ------------- Small notifications on the edge of the displayScrollbars ----------Automatically sized scrollbars with a single commandStringSize ---------- Automatically size controls to fit textToast -------------- Small GUIs which pop out of the notification area  Link to comment Share on other sites More sharing options...
blueforce4116 Posted May 3, 2009 Author Share Posted May 3, 2009 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 Link to comment Share on other sites More sharing options...
Mobius Posted May 3, 2009 Share Posted May 3, 2009 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 Link to comment Share on other sites More sharing options...
blueforce4116 Posted May 3, 2009 Author Share Posted May 3, 2009 (edited) 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 Edited May 3, 2009 by blueforce4116 Link to comment Share on other sites More sharing options...
Mobius Posted May 3, 2009 Share Posted May 3, 2009 (edited) 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 May 4, 2009 by Mobius Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now