PDA

View Full Version : Any code to create another executable?



Nutshell
03-29-2002, 12:31 AM
Hi,

Is there any way or code to create another .exe file during program execution? If it's possible, can i hard-code all the codings needed in the new executable?

DavidP
03-29-2002, 01:14 AM
guess what a compiler is an example of...

Nutshell
03-29-2002, 01:19 AM
What about packing all the binary in the code, and then output it as a file and rename it as .exe ? or are there any other ways?

Magos
03-29-2002, 04:19 PM
Well, theoretically you could write a file within a program (containing lots of MQ@% characters) and save it as binary and naming it XXX.exe. However I highly doubt there is an easy way to do this.

Nutshell
03-29-2002, 04:22 PM
i got all these unterminated characters errors during compile ii think it's because the binary is too long.

I tried save the funny character into a text file, read it one character at a time and output it as .exe but output is incorrect.

Hillbillie
03-29-2002, 05:49 PM
>I tried save the funny character into a text file, read it one character at a time and output it as .exe but output is incorrect.<

Did you open the new file in text or binary mode? Anyway, to answer your first question, you could have an array of hexidecimal numbers, each representing an ASCII character. Just write these to a file (that was opened in binary mode). Give it the appropriate extension and viola.

Nutshell
03-29-2002, 07:04 PM
i dunno how to do hex. And i did open the new file using mode "wb"

thnx

sean
03-29-2002, 07:17 PM
Unless you're going to be modifying the way the file is written, and its all constants, just copy and paste each line of the file (the best option would be EDIT.com from Dos,), and then use each of the lines in between ""'s in a printf or cout statement.

Hillbillie
03-29-2002, 08:18 PM
>i dunno how to do hex. And i did open the new file using mode "wb"<

If you can't do hex, just do an array of ASCII characters (decimal form). I don't even know why I said hex in the first place - decimal would do just as good.

>then use each of the lines in between ""'s in a printf or cout statement.<

That wouldn't work because an executable file has characters that don't like to be treated as C/C++ strings (null characters, quotes, those weird looking characters, etc.). That's why hexadecimal or decimal would be a good choice.

Hillbillie
03-29-2002, 09:11 PM
Okay, here's a sample I whipped up. I hope this is what you were talking about. Sorry I used hex, but it was the easiest for me. If it's too much of a problem, I can fix it. :)

You'll notice (when you compile and run it) that it creates a file named "program.exe". Run it. It's the winhelp.exe program found in Windows, so you'll need that devil to run it. Hope this helps.

Nutshell
03-29-2002, 11:04 PM
hey that worked perfectly. One question. How do u convert decimal stuff to hex, in other words, how do u know the hex equivalent. ALso, do u havta use array? And did u copy and paste each element? Coz there are commas between each one. Anyway that was exactly what i wanted and i'll appreciate it if u tell me how u did it.

thnx

Hillbillie
03-29-2002, 11:48 PM
First a wrote simple program that converts a program into a text file that is hexadecimal with commas. Then I just copied that into an array.

This is the program that converts an executable to user readable hexadecimal:

#include <stdio.h>

int main()
{
FILE *file;
FILE *file2;
signed short int ch = 0;

file = fopen("program.exe", "rb");
file2 = fopen("program.txt", "w");

while(ch != EOF)
{
ch = fgetc(file);
fprintf(file2, "%x, ", ch);
}

fcloseall();

return 0;
}

You have to open the file it creates and delete ", ffff, " at the end. Then copy the whole file into an array.

Nutshell
03-30-2002, 12:24 AM
why is there ffff?

also, is it possible to make a program delete itself after executation?

Fordy
03-30-2002, 01:43 AM
Originally posted by Nutshell
also, is it possible to make a program delete itself after executation?

Aghh....so that's what your your up to........

Nutshell
03-30-2002, 01:52 AM
Just out of curiosity..........

But i do hav one application. Say i want to let my friend have a look at a project i've done - a normal school project in a word file. But i don't want him to copy and paste anything in it. SO if i convert the text file into an image file and display in a program which deletes itself after say 5 minutes then he won't have time to do anything except having a look?

pls i wanna know, and i have no bad intention what so ever

Fordy
03-30-2002, 02:03 AM
Originally posted by Nutshell
Just out of curiosity..........

But i do hav one application. Say i want to let my friend have a look at a project i've done - a normal school project in a word file. But i don't want him to copy and paste anything in it. SO if i convert the text file into an image file and display in a program which deletes itself after say 5 minutes then he won't have time to do anything except having a look?

pls i wanna know, and i have no bad intention what so ever

There are Modify permissions with M$Word......that would stop your friend cutting & pasting........

shtarker
03-30-2002, 02:03 AM
>>also, is it possible to make a program delete itself after executation?

In windows, the way to do this is to put it in the Temp directory.
I don't think there is a platform independant way to do it.

Nutshell
03-30-2002, 03:51 AM
Hi can u be kind enough to tell me howta do this in word? Any extra plugins and stuff needed?

Nutshell
03-30-2002, 05:14 AM
Hillbillie i modified ur code but it doesn't work, here is the code thnx. All the output file contains is many many "1, "s......



/* Takes a binary file and output its hex equivalent in a text file */

#include <stdio.h>

int main( int argc, char *argv[] )
{
FILE *fpBinary, *fpHex;
unsigned short int c;

if ( argc == 1 ) {
printf( "%s\n\n%s\n%s\n\n\n", "Usage: bin2hex.exe < filename >",
"It takes a binary file and output its hexidecimal equivalent",
"in a text file ( hex.txt ) in the current directory." );
system( "PAUSE" );
return 0;
}

if ( ( fpBinary = fopen( argv[ 1 ], "rb" ) ) != NULL ) {
if ( ( fpHex = fopen( "hex.txt", "w" ) ) != NULL ) {
while ( c = fgetc( fpBinary ) != EOF ) {
fprintf( fpHex, "%x, ", c );
}
}
else
printf( "\aError\n\n" );
}
else
printf( "\aError\n\n" );


system("PAUSE");
return 0;

}

(TNT)
03-30-2002, 06:04 AM
Hey,

I am fairly interested in this sort of area, and i understand how its been done using hex.

But, i would like to know how to edit the exe output with user input. For example say in the main program i asked the user to input a text string that will be the window caption of the new exe. How would you then set the window title to this string then output the exe. Dunno if that sounds correct lol.... I guess you could modify the hex bit by bit but it would be very tricky, is there a simple way of doing this?

TNT

Nutshell
03-30-2002, 06:09 AM
anyone can answer my question above the above post pls thnx.

(TNT) i think u're talking about resource hackers again.

Fordy
03-30-2002, 06:51 AM
If you want to change a windows caption then look at WM_SETTEXT as sending this message to a window will do what you wish......

If you are interested in Hex editing........take some time out to learn ASM

Nutshell
03-30-2002, 06:57 AM
what i want to do is relatively. Can't u answer it in a simple way?

thnx

Hillbillie
03-30-2002, 01:33 PM
My my, this thread has exploded...

>why is there ffff?<

That is hexadecimal for -1, which is what most standard C libraries use for EOF. It could be fixed easily to not print that out to the text file, but I'm lazy. :p

>Hillbillie i modified ur code but it doesn't work, here is the code thnx. All the output file contains is many many "1, "s......<

I received a few errors when compiling that code you posted. Do you want help on the errors or does yours compile fine and you need help on bugs? Please be more specfic.

Nutshell
03-30-2002, 05:57 PM
I couldn't compile. It compiles fine but when i use execute it, all the text file contains are many many "1, "s.

Hillbillie
03-30-2002, 06:46 PM
You _really_ should figure this out on your own. I don't like giving people that are having problems source code because you don't usually learn that way, but oh well. Here's what you were trying to do, but working ;):


#include <stdio.h>

int main(int argc, char *argv[])
{
FILE *input;
FILE *output;
int ch = 0;

if(argc != 3)
{
printf("\nBin2Hex V0.1\nOutputs a binary file's hexadecimal equivalent..\n");
printf("\nUsage: %s <input file> <output file>\n", argv[0]);
return 0;
}

input = fopen(argv[1], "rb");
output = fopen(argv[2], "w");

while(ch != EOF)
{
ch = fgetc(input);

if(ch != EOF)
{
fprintf(output, "%X, ", ch);
}
}

fcloseall();

return 0;
}

I also fixed that problem with it outputting 0xFFFF.

Nutshell
03-30-2002, 08:40 PM
Ur code worked. BUt i really see no diffference between mine and yours except some minor changes which i think it's related to the problem such as seperating the assignment of 'ch'. THere's no difference! How come ur one works and mine doesn't....

Nutshell
03-31-2002, 01:56 AM
Wait i got it. It's this line:



while ( c = fgetc( fpBinary ) != EOF ) {


should be



while ( ( c = fgetc( fpBinary ) ) != EOF ) {


thnx to all, to confirm, is it really impossible to do it with plain text, instead of hex?

thnx thnx

Hillbillie
03-31-2002, 02:52 AM
>thnx to all, to confirm, is it really impossible to do it with plain text, instead of hex?<

I wouldn't say it's impossible, but it would be difficult to do. Some of the characters that are in binary files won't work well in strings or even as single characters.

Nutshell
03-31-2002, 02:54 AM
You said not impossible. DO u know any tricks to get around the problem? IF not, it's fine. I already got what i wanted to know thnx

Hillbillie
03-31-2002, 03:02 AM
Well, you could (for the characters that aren't "friendly") just work with the ASCII numbers.

But, as far as actual text (and not numbers) goes, I can't think of anything right now. I'm pretty tired (it's 3 AM here), so I'm not thinking very well.

VirtualAce
03-31-2002, 07:11 AM
There is a very simple way for an EXE to delete itself after running. Most programs will call DOS function 4C00 to terminate the program. Prior to running your program, hook int 21h, function 4c00h. Now when the program calls it you will know which program did by looking at either the command line or looking at the PSP/Environment block - I forget which holds the program name or you could compare that to the caller's address/segment (could be called from diff segment, though not likely since will be the end of the program) - which will be on the stack. Then you can search through the FAT, find the cluster number of the file that matches the name, trace the chain marking each cluster as empty( 00h) and placing character 0x229 in the first letter of the directory entry. If you are in DOS 6.20+ you will have to unlock the volume if it is a hard drive or DOS will not allow you to access the hard drive via int 13h. It's possible to delete directories, files, partitions, or restore/create directories, partitions, files, etc. Unfortunately there is not RESTREE command to restore directories in DOS, even though it is possible.

If you are interested I have code that will extract the boot sector off a disk and save it as a binary file. You can also replace the boot sector by specifying a new binary file. Note that this code can destroy your boot sector on your hard drive. Easy way to fix this is to FDISK /MBR which re-writes the bootsector code to the disk. All of the file is in hex like you have been talking about. Each of those hex values corresponds to opcodes and operands, strings, constants, etc. To explain all of it would take lots of time and you would need to be well-versed in assembly language.

But , it is possible to write assembly code in hex and execute it. If you look at the old days of BASIC programming many people did this to write small assembly language functions for their BASIC programs. You just place the values in memory and jump to the start of the code or perform a call to the address of the start of the code.

For instance the opcode for a short jump to a 16 bit offset:segment pair is EA. This would appear in a text file as ASCII 234 which would display some odd character. The actual encodings of the opcodes/operands/SIB byte/MOD r/m byte are extremely complex - thus CISC, but debuggers interpret this data and display it as assembly mneumonics so that we the programmers can read it.

Nutshell
03-31-2002, 08:26 AM
ahh i think the stuff above is a bit complicated for me. However if u show me the binary for the retrieving the boot sector that'll be nice. But i guess there'll be no easy way to a file to delete itself? DOesn't matter.