views:

371

answers:

3

Hi

I looking for example of program, that modify string inside his exe.

I work with C++, Visual Studio under Windows.

I searched in internet for working examples in Windows, but I doesn't find any working code.

I need simple code, that will ask user for string:

string strTest = "";
(if strTest != "")
{
   cout << "Modified: " << strTest << endl;
}
cin >> strText;

And code should rewrite:

string strTest = "";

To string that typed user:

 string strTest = "SomeStringFromUser";

How program in C++ can modify string in itselfs exe (from string strTest = ""), to string, what user typed? (for example to strTest = "foo").

If you know, please write sample code, that will show modyfing strings.

Thanks

Jasmin25

+15  A: 

When an EXE is running on a Windows machine, the exe file is held open as a CreateFileMapping object with pages marked either as READONLY or COPY_ON_WRITE.

So when the exe writes to itself, the file is not modified. It just creates a new page backed by the swap file. But since the file is kept open, no-one else can open the EXE file and write to it either.

Other than hacking the page protection to turn off COPY_ON_WRITE - Which I'm not sure is even possible. The only way I can think to do this would be to write a little program that runs after your exe finishes and opens the .exe file and writes to it.

I've gotta believe that whatever you are trying to do, there is a better way to go about it.

--- Later ----

Ok, I get it now. you are looking to watermark your exe. Here's the thing, this is pretty easy to do in your installer before the exe starts. But once the .exe is running it's MUCH harder to do.

Here's what I would do.

  • declare a global string variable of the necessary size, say const char g_szWatermark[100] = "";
  • Build my exe, then look in the map file to find the address of the variable within its segment (remember about C++ name decoration)
  • parse the EXE header to find the where the segment begins in the exe.
  • add these two numbers to get the location of the string within the exe file, give this number to my installer
  • have the installer run a little program that asks the user for information and then writes it into the .exe. Note: you have do do this before the exe runs or it won't work!
John Knoeller
+1 for your last line.
David Thornley
I like your edit too, but since I already upvoted you I can't do it again.
David Thornley
+2  A: 

A licensing scheme based on some open, established cryptographic mechanism is going to be your most robust solution. Something using standard PKI should be much simpler and more secure than attempting to implement self-modifying code.

To be honest though, there are a lot of companies that spend a lot of money on R&D creating copy protection and those systems are cracked within days of release. So if you're trying to thwart crackers then you have a long, hard road ahead.

If you just want to keep the honest people honest, a simple online activation using a GUID as the "license key" would be quite effective.

joshperry
A: 

How about this:

#include <string>
#include <iostream>

int main()
{
    std::string strTest = "";
    std::getline(std::cin,strTest);

    if (strTest != "")
    {
        std::cout << "Modified String: " << strTest << "\n";
    }
    else
    {
        std::cout << "Not modified\n";
    }
 }
Martin York