Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to write a program that modifies itself? 2

Status
Not open for further replies.

BatVanko

Technical User
Jun 1, 2000
9
US
I'm tring to write an aplication that could write in it's .exe file (i.e. some configuration data). Any sugestions?

Regards
 
...Of course NOT. I want to write an application that don't need of any configuration files or registry entries.
 
Hello You mean more of a appliction program with a set dir for config file, ect.?
Now I am new to this I have a exe that is like that and am trying to learn how to veiw the source so that I remove some java within it, I own the exe and have promission to
rewrite to it, but have no clue on opening it.
But My answer would be Yes to Your question and if I could see how it was done. I'd tell more lol


MAWarGod
Plain well for the day, Who's knows about tomorrow
 
Hi,
It would seem that changing the program would be easy.
The problem would be saving the .exe and thus the changes to the disk.
Something to think about.
Pappy
You learn something everyday.
 
Modifying the .exe file directly, i wouldn't think, would be a good approach. Just my opinion. I would just do an api call to the ini file funcitons like

GetProfileInt
WriteProfileInt
etc...

Yes it is an external file which is what you dont want but if you can look at the binary data of an exe and rewrite it all the power to you!!! Just post the code cause I am curious how to do it.

Matt
 
I think a configuration file is better than what you are trying to do, but you might want to look to the Image Help library, which contains functions needed to manipulate PE-format files. Include <imagehlp.h> and link with imagehlp.lib. Type &quot;ImageHlp&quot; in the index part of the MSDN library and you will find the info I think you need. I did never try one of these routines myself, so I can not give any more help on this issue.

Marcel
 
Without changing Windows, I know only one way to do it from a program, for example, my.exe:
1. Copy Your my.exe - File with another name, for example, mynew.exe in the same folder or in another folder (You can use then the same name) - You must do it programmatically with CopyFile() or another Function You like.
2. Change mynew.exe how do You wish.
3. Start mynew.exe.
4. End the Process You started first (my.exe).
5. Delete my.exe.
If You wish to use the same file name my.exe in the old folder, You must then repeate 1-5 from mynew.exe (copy mynew.exe to my.exe etc.).
 
There is no way you can overwrite the exe image on the hard drive, while it is running, because the OS puts a lock on the file.

It is a posibility to modify the exe image in memory though, because I read this somewhere. It is something with MapViewOfFile API.

HTH, s-)

Blessed is he who in the name of justice and goodwill, sheperds the weak through the valley of darkness...
 
If you hard code a character array with data, you can find that in the .exe as long as you can guarantee either the location of the data or that the flag you put at the front is a unique pattern.

You should then be able to edit that &quot;data area&quot;.
I used
Code:
void main()
{
  char sztemp[128] = {0};
  //using qwertyuiop to flag the start of the data area
  strcpy(sztemp,&quot;qwertyuiopxxxxxx...xxx&quot;);
  cout<<sztemp;
}
[\code]
to check that VC6 didnt do anything clever with the data like compressing it.

copying the file and then editing it using &quot;qwertyuiop&quot; to find the start of the &quot;data area&quot; should be fairly easy.
Obviously you'll have to build up your flag rather than hard code it or you may find the wrong one...
 
You can do this in .NET with the System.Reflection classes.

Chip H.
 
See WDJ:
the code delete themself. editing no more dificult.

#include <windows.h>

int main()
{
HMODULE module = GetModuleHandle(0);

CHAR buf[MAX_PATH];

GetModuleFileName(module, buf, sizeof buf);

CloseHandle(HANDLE(4));

__asm {
lea eax, buf
push 0 // argument to
ExitProcess
push 0 // return address of
ExitProcess
push eax // argument to
DeleteFile
push ExitProcess // return address of
DeleteFile
push module // argument to
UnmapViewOfFile
push DeleteFile // return address of
UnmapViewOfFile
push UnmapViewOfFile
ret
}

return 0;
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top