-
Write binary file
Hello,
I really cannot think of how to do this. I want to embed an .exe into my .exe and copy it out and run it at runtime.
Basically I have the following C code...
#include <stdio.h>
int main(void)
{
FILE *myfile = fopen("out.exe", "wb");
putc(0x00, myfile);
putc(0xA8, myfile);
putc(0xFE, myfile);
fclose(myfile);
return 0;
}
That will write 3 characters into out.exe
Will I need to go through each character and put it in my source or is there a shortcut? Any applications I can use to write that bit of the source for me?
Any help would be greatly appreciated.
Thanks
-
> Will I need to go through each character and put it in my source or is there a shortcut?
you will need to go through each character and copy it, but the task is trivial with a loop.
for example, this snippet would copy 16 KB starting at an offset of 8 KB from srce to dest.
Code:
#include <fstream>
int main()
{
std::ifstream srce( "/usr/local/bin/curl", std::ios_base::binary ) ;
std::ofstream dest( "/tmp/curl_slice", std::ios_base::binary ) ;
const std::streampos from = 1024U * 8U ; // start from this offset
const std::size_t nbytes = 1024U * 16U ; // upto a max of nbytes
srce.seekg(from) ;
char c ;
for( std::size_t i=0 ; (i<nbytes) && srce.get(c) ; ++i ) dest << c ;
}
-
hmm, thats pretty cool even though it isnt exactly what I meant.. for example, the .exe isn't embedded into my app as such so that I can get a position of it. more like i want to manually build it up using something like millions of putc(0xFE, myfile);
Is there a better way to do this?
How would you embedd an .exe or even a .bmp in your compiled .exe and then extract it at runtime?
-
> How would you embedd an .exe or even a .bmp in your compiled .exe and then extract it at runtime?
on a microsoft windows platform, you could embed it as a 'resource file'.
microsoft resource files are just part of the const data section of the executable image, so the feature is easy to implement on other platforms.
for example, the following code will create an object file called embedded_bytes.o which will contain an array with external linkage called file_contents. this array will contain every byte in the executable image /bin/sh. all you have to do now is to link in the object file embedded_bytes.o into your executable. and dump the array file_contents to a target file at runtime. (to execute it you would also need to do a chmod.)
Code:
#include <fstream>
#include <cstdlib>
using namespace std ;
int main()
{
{
ofstream embedded_data( "embedded_bytes.cc" ) ;
ifstream binay_file( "/bin/sh", ios_base::binary ) ;
embedded_data << "extern const char file_contents[] = {\n" ;
embedded_data << showbase << hex ;
char byte ;
int n = 0 ;
while( binay_file.get(byte) )
{
embedded_data << int( (unsigned char)byte ) << ", " ;
if( ++n%72 == 0 ) embedded_data << '\n' ;
}
embedded_data << "};\n" ;
}
system( "/usr/bin/g++ -Wall -std=c++98 -pedantic -Werror -c "
"embedded_bytes.cc -o embedded_bytes.o " ) ;
}
Last edited by vijayan; 01-11-2008 at 02:33 AM.
-
The easy way is just to make a bin to hex tool, and then write each byte of the embedded program as a string that c++ will accept in an array:
//generate something that looks like this from your tool as a .h or .cpp file
//and just include it
unsigned char exe[] = {
0x12, 0x34, ...
};
then build it, and do a write binary of the entire buffer:
fwrite(file, ....) or OOPfile.write(...)
-
you can also cheat in the following way:
make your executable the true executable if the program's name matches some string, else copy your program to the location with the correct rename. You do that using free first command line parameter, which contains the program's name, and simply change how the program runs based on the name of the program. Pretty cool, eh?
-
Hey, all those ideas are great. I have been wondering about this before and was always tied to .NET so i could embed as resource and use
System.IO.FileStream fs = new System.IO.FileStream(".\\out.exe", System.IO.FileMode.Create);
fs.Write(MemoryRun.Properties.Resources.<resource name>, 0, MemoryRun.Properties.Resources.<resource name>.Length);
fs.Close();
but now I can finally use MinGW g++
Thanks a lot guys, for your help.
Similar Threads
-
Replies: 5
Last Post: 06-25-2007, 08:26 AM
-
By extreme in forum VB Classic
Replies: 0
Last Post: 06-13-2007, 09:51 PM
-
Replies: 1
Last Post: 06-13-2007, 03:40 PM
-
By Phil Weber in forum .NET
Replies: 3
Last Post: 06-16-2002, 10:50 PM
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Development Centers
-- Android Development Center
-- Cloud Development Project Center
-- HTML5 Development Center
-- Windows Mobile Development Center
|