修改 exe 内字符串的程序

发布于 2024-08-15 19:26:29 字数 526 浏览 4 评论 0原文

我正在寻找程序示例,该程序修改其 exe 内的字符串。

我在 Windows 下使用 C++、Visual Studio。

我在 Windows 中搜索了工作示例,但找不到任何工作代码。

我需要简单的代码,它将询问用户字符串:

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

并且代码应该重写:

string strTest = "";

到键入用户的字符串:

string strTest = "SomeStringFromUser"; 

在 C++ 中,如何将字符串(从字符串 strTest =“”)修改为字符串,用户键入的内容? (例如 strTest = "foo")?

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")?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

你没皮卡萌 2024-08-22 19:26:29

当 EXE 在 Windows 计算机上运行时,exe 文件作为 CreateFileMapping 对象保持打开状态,其页面标记为 READONLY 或 COPY_ON_WRITE

因此,当 exe 写入自身时,文件不会被修改。它只是创建一个由交换文件支持的新页面。但由于该文件保持打开状态,因此其他人也无法打开该 EXE 文件并对其进行写入。

除了破解页面保护以关闭 COPY_ON_WRITE - 我不确定这是否可能。我能想到的唯一方法是编写一个小程序,在 exe 完成后运行并打开 .exe 文件并向其写入内容。

我必须相信,无论你想做什么,都有更好的方法来实现。

--- 稍后 ----

好的,我现在明白了。你正在寻找给你的exe添加水印。事情是这样的,在 exe 启动之前,这在安装程序中很容易完成。但是一旦 .exe 运行起来就很难了。

这就是我要做的。

  • 声明一个必要大小的全局字符串变量,例如 const char g_szWatermark[100] = "";
  • 构建我的 exe,然后在映射文件中查找该变量在其段内的地址(记住关于 C++ 名称修饰)
  • 解析 EXE 标头以查找该段在 exe 中开始的位置。
  • 将这两个数字相加即可获取 exe 文件中字符串的位置,将此数字提供给我的安装程序,
  • 让安装程序运行一个小程序,询问用户信息,然后将其写入 .exe。注意:您必须在 exe 运行之前执行此操作,否则它将无法工作!

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!
倾其所爱 2024-08-22 19:26:29

基于某种开放的、已建立的加密机制的许可方案将是您最强大的解决方案。使用标准 PKI 的方法应该比尝试实现自修改代码更简单、更安全。

但老实说,有很多公司花费大量资金进行研发以创建复制保护,而这些系统在发布后几天内就被破解了。因此,如果你想阻止破解者,那么你前面的路还很长、很艰难。

如果您只是想让诚实的人保持诚实,使用 GUID 作为“许可证密钥”的简单在线激活将非常有效。

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.

自由如风 2024-08-22 19:26:29

这个怎么样:

#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";
    }
 }

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";
    }
 }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文