Solved

How to write a self-modifying executable

Posted on 1998-11-19
4
278 Views
Last Modified: 2008-03-04
Is it possible to write an executable which is able to change itself at runtime? Specifically, I'd like to store some customization data in the executable file itself, so that if that exe is passed to someone else, the data can be retrieved.

I'm thinking that it might be possible to modify the exe's resources somehow, but have no idea if that would work as a scheme for persistence.

The essential point to this problem is that the information MUST be stored in the executable, not in the registry or in a separate file.

If the executable cannot modify itself while it is running, can it spawn another process and close itself? Could the other process then go and make the necessary changes to the exe? Just a thought...

Thanks for your help,
Brett
0
Comment
Question by:circuit
4 Comments
 
LVL 2

Expert Comment

by:cyrilbdt
Comment Utility
you can use BeginUpdateResource, ...,  for modifying resource but this function works only on NT 3.51+.

You can spwan another process, close the parent and then in this new process to modify resource.
0
 
LVL 22

Expert Comment

by:nietod
Comment Utility
In windows, unlike DOS,  executables are opened read-only.  This makes this sort of thing nearly impossible to do.  There are ways around it, like using another process to do the writing, but did you ever stop to consider why they have the executable opened read-only?  The reason is that this sort of design tends to be dissasterious on a multi-processing system like windows.  You may be able to circumvent window's protections against writing to an executable, but they you run into the problems that windows is trying to prevent.  For example, what if you have two copies of your program running?  When one is closed so that another process can write to it, the EXE will still be open read only.  You won't be able to write to it.  What happens then?

I recommend you rethink your design.  Is it really that important that this information be written to the EXE?
0
 

Author Comment

by:circuit
Comment Utility
Sorry, it is most important that I am able to perform this task on windows 95/98.  Thanks for your suggestion though.  

In response to nietod: I don't expect users to run two copies of my executable.  If they try, I can have the second one simply bring the first to top.  And yes, as I said in my post, modifying the executable is cornerstone to the idea of my project.

Thanks,
Brett
0
 
LVL 8

Accepted Solution

by:
Answers2000 earned 100 total points
Comment Utility
If you must do it

(a) make a 2nd executable do the writing back to the 1st, while the 1st isn't running
(b) if you don't want to distribute 2 exe's, the 1st exe spits out the 2nd automatically.

e.g.
Program A wants to write to itself, therefore it does
(a) Program A finds where it's running (e.g. GetModuleFileName)
(b) Program A writes out program B to the same directory
(c) Program A kicks off program B ***
(d) Program A ends ***
(e) Program B waits till A has ended
(f) Program B writes to A's disk image
(g) Program B runs A
(h) program B ends ***
(i) Program A waits till B has finished ***
(j) Program A deletes program B disk image

The steps marked *** occur in parallel (c & d, AND,  h & i) hence the "wait" steps to make sure the last program terminated before modifying/deleting it's disk image.

A tip to make spit out stuff easier, is to embed binary resources into your EXE's, you can simply insert them in Visual Studio, or manually include files into your resource files.

Program A would include the binary image of a compiled program B as one of it's resources.

Program A could include additional binary resources (e.g. some data you can easily find)

Program B can then seek to this data, and write to it when working on A's disk image.



0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now