3

I looking for example of program, that modifies a string inside its exe.

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

I searched working examples in Windows, but I can'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, in C++, do you modify a string (from string strTest = ""), to string, what a user typed? (for example to strTest = "foo")?

Nathaniel Ford
  • 20,545
  • 20
  • 91
  • 102
Jasmin25
  • 177
  • 1
  • 7
  • Is this assuming all correct permissions etc? Because a typical program would be installed in "Program Files", and normal user account wouldn't have permission to change binaries there... – Pavel Minaev Dec 23 '09 at 18:08
  • Can you describe *why* you want to do this? It smells like there is a better way. – Drew Dormann Dec 23 '09 at 18:12
  • @Jasmin25: "simple" and "anti-piracy" don't really mix. – Joe Dec 23 '09 at 18:20
  • Shmoopty: I want to create simple anti piracy function in my program. Program in first run will take MAC from user, and add it to string in exe. If user copy this exe to another computer (that have other MAC from first computer) - program will not run. – Jasmin25 Dec 23 '09 at 18:21
  • 1
    maybe you'd be better off storing the MAC address separately, but encrypting it with a key that's hardcoded in your binary? – philsquared Dec 23 '09 at 18:23
  • What if the user kept the executable before first run? By the way, opening a .exe in write mode will flash all the red lights in most good antivirus software. – Nicolás Dec 23 '09 at 18:25
  • @Jasmin25: What you want has nothing to do with the C++ code. You need to provide an area (allocating the bytes) to write the MAC into the executable and then write it in the .exe file. This is not covered by the standard, and may well not be possible, depending on platform. – David Thornley Dec 23 '09 at 18:28
  • Phil Nash: I want to store this MAC in exe, because user can't copy this exe to another computer and run it. Nicolás: I got this solved (partially). About antiviruses - I know, that I can send my program to AV companies, and told them, that my program isn't mallware. – Jasmin25 Dec 23 '09 at 18:31
  • 2
    And what happens if the user changes network cards? Would that invalidate the license? – Timo Geusch Dec 23 '09 at 18:37
  • 4
    @Jasmin25: I wouldn't count on all AV companies cooperating. In fact, if you find some that will just take your word it's not malware, let me know so I can never use that AV software again. Nor would I want to depend on all users having updated AV software. – David Thornley Dec 23 '09 at 18:40

3 Answers3

14

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
  • 33,512
  • 4
  • 61
  • 92
2

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
  • 41,167
  • 16
  • 88
  • 103
0

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
  • 257,169
  • 86
  • 333
  • 562