i have been experimenting with CreateFile and WriteFile and have sucessfully created an HTML document, a batch file and .txt etc. however, what beats me is how to create an .exe file or another file type that you cant just write the text to. It must be possible, but how the hell do i do it??? also, how would i go about compressing this file within an .exe ? any info appreciated. skud.
i'm confused what by you said above... maybe next time you cou- ld slow down abit. ;) what is your objective? are you trying to create your own file- type? such as *.BAT where each time you right-click, there's an option for you to EDIT. Or perhaps your own *.ext file??? hmmm...
sorry :P my objective is to make an .exe that when run creates another .exe, if you see what i mean. i can create a file ok; thats not a prob (a blank one). i can then write text to it using writefile. However when it comes to creating an .exe or something that does not contain readable text then the problems begin. for instance, if you open say a batch file (.bat) in notepad and copy the entire contents to the clipboard and then paste it in another instance of notepad and then save this as whatever.bat then the file will be identical. However, if you do this with an .exe then it doesnt work and some error comes up. what i want to do is make a prog that creates a file and then writes a string to it and it will then be a working executable. simple idea, hard to explain :) thanks for the reply, skud.
Go to hutch's web site and download the file pe1.zip in the PE section. It shows how to create an executable file from scratch.
Do you want to manually create an EXE file, or do you just want to "write one out", from data in your application? The first thing is pretty hard, the second is easy-peasy. You'll need to conver the EXE file to a series of "db" statements - as you state yourself, copy+paste will not do the trick. There are tools available for doing the data->db conversion, find one of those (search for bin2db or bin2hex or binhex).
skud, I think what you really want to do is to recreate an existing EXE and not create a new different EXE, am i right? well, you could convert the contents of an EXE or any binary into readable form using the base64 algorithm or uuencode (algos used in many programs like e-mail clients and http clients). nce you've encoded a binary into readable form using, let's say, base64 you can then convert back it to binary using it's decoder and write it to a file for example program.exe. Check out the algorithms section on this board, Zynaps had his source posted for base64. - clip
If you want to "bundle" data inside an executable, create a resource file with "RCDATA" elements: BLAH RCDATA "MyExe.EXE" Then use "LoadResource", "LockResource", "FreeResource", and "GlobalSize" to put it in memory. Then simply write it out! If you want to do a "create an exe" type of program, you could do a very similar thing.... Create a "blank" exe, and then the resource you want to add to it, and inject that into the "blank" exe, the only difference from the above is that you would also need "FindResouce" Mirno
wow. great response. sorry im so bad at explaining what i want to do :) f0dder - spot on. thats exactly what i want to do (the second thing). i want to "write one out", from data in my application. i have had a look round for those files (took me 2 seconds to find, top result on yahoo when searching for "bin2db"). i have now made an application that creates another .exe when it is run or "writes one out". however, this makes the file size pretty big. is their any simple way i can compress the file within it or compress it some other way? thanks. skud.
i would like to know how to for example write a patch engine that creates a exe file (actually the patch). the only thing that would change are thy bytes to change in another exe file.
If you just want a "file dropper" here is some code that works, is simple, and easy to update!
####### With this rsrc.rc file #######
.data FileName db "Gump.exe",0 AppName db "Test file dropper",0 MessageStr db "FileSize = %u",0 .data? hResource dd ? hOutFile dd ? ResSize dd ? Written dd ? buffer db 512 dup(?) .code start: invoke FindResource, NULL, 1, RT_RCDATA .IF eax != 0 mov hResource, eax invoke SizeofResource, NULL, eax mov ResSize, eax invoke CreateFile, ADDR FileName, GENERIC_WRITE, NULL, NULL, \ CREATE_NEW, FILE_ATTRIBUTE_NORMAL, NULL .IF eax != INVALID_HANDLE_VALUE mov hOutFile, eax invoke LoadResource, NULL, hResource invoke LockResource, eax invoke WriteFile, hOutFile, eax, ResSize, ADDR Written, NULL invoke CloseHandle, hOutFile .ENDIF .ENDIF invoke ExitProcess, 0 end start
It holds in the resource section of the file you create the exact binary file specified in the rsrc.rc file. It outputs the file to "gump.exe", as specified by FileName in the .data section of the code. Be warned, it will pad any file it creates to a 4byte aligned number! This shouldn't affect any PE files as they are already 512 byte aligned (or 32 byte if you're skimping on your NT PE files, that you don't want to work on 95.... :D ). Mirno
1 RCDATA "test.exe"