如何在代码中隐藏文字

发布于 2024-12-07 01:00:10 字数 629 浏览 3 评论 0原文

隐藏代码中文字值的现有主要方法是什么,以便仅使用十六进制转储器或反编译器就不容易追踪它们?

例如,

    static final int MY_VALUE = 100;

我们可以不编写以下代码:

    static final int MY_VALUE = myFunction1();

    private int myFunction1(){
        int i = 23;
        i += 8 << 4;
        for(int j = 0; j < 3; j++){
            i-= (j<<1);
        }
        return myFunction2(i);
    }

    private int myFunction2(int i){
        return i + 19;
    }

这只是我们正在尝试做的一个示例。 (是的,我知道,编译器可能会优化它并预先计算常量)。

免责声明:我知道这根本不会提供任何额外的安全性,但它使代码对于逆向工程来说更加晦涩(或有趣)。这样做的目的只是迫使攻击者调试程序,浪费时间。请记住,我们这样做只是为了好玩。

What are the main existing approaches to hide the value of literals in code, so that they are not easily traced with just an hexdumper or a decompiler?

For example, instead of coding this:

    static final int MY_VALUE = 100;

We could have:

    static final int MY_VALUE = myFunction1();

    private int myFunction1(){
        int i = 23;
        i += 8 << 4;
        for(int j = 0; j < 3; j++){
            i-= (j<<1);
        }
        return myFunction2(i);
    }

    private int myFunction2(int i){
        return i + 19;
    }

That was just an example of what we're trying to do. (Yes, I know, the compiler may optimize it and precalculate the constant).

Disclaimer: I know this will not provide any aditional security at all, but it makes the code more obscure (or interesting) to reverse-engineer. The purpose of this is just to force the attacker to debug the program, and waste time on it. Keep in mind that we're doing it just for fun.

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

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

发布评论

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

评论(3

小忆控 2024-12-14 01:00:10

由于您试图隐藏在程序的简单转储中可见的文本,因此您可以使用某种简单的加密来混淆您的程序并隐藏该文本以防止窥探。

详细说明:

  1. 访问 ROT47.com 并在线编码您的文本。您还可以使用此网站获得更通用的 ROTn 编码。
  2. 将字符串常量的内容替换为编码文本。
  3. 当您需要时,使用代码中的解码器将文本转换回其原始形式。 ROT13 Wikipedia 文章 包含一些有关实现的注释,这里是 StackOverflow 上 ROTn 的 Javascript 实现。使其适应您使用的任何语言都很简单。

为什么要使用众所周知的弱加密 ROT47?

最后,您的代码将如下所示:

decryptedData=decryptStr(MY_ENCRYPTED_CONSTANT)
useDecrypted(decryptedData)

无论您的密码有多强,任何配备调试器的人都可以在 useDecrypted() 上设置断点并恢复明文。因此,密码的强度并不重要。然而,使用像 Rot47 这样的东西有两个明显的优点:

  1. 您可以在线编码文本,无需编写专门的程序来编码文本。
  2. 解密非常容易实现,因此您不会将时间浪费在不会为客户增加任何价值的事情上。
  3. 任何阅读你的代码的人(你的同事或 5 年后的你自己)都会立即知道这不是真正的安全,而是默默无闻的安全。
  4. 对于任何只是窥探你编译的程序的人来说,你的文本仍然会显示为乱码,所以任务完成了。

Since you're trying to hide text, which will be visible in the simple dump of the program, you can use some kind of simple encryption to obfuscate your program and hide that text from prying eyes.

Detailed instuctions:

  1. Visit ROT47.com and encode your text online. You can also use this web site for a more generic ROTn encoding.
  2. Replace contents of your string constants with the encoded text.
  3. Use the decoder in your code to transform the text back into its original form when you need it. ROT13 Wikipedia article contains some notes about implementation, and here is Javascript implementation of ROTn on StackOverflow. It is trivial to adapt it to whatever language you're using.

Why use ROT47 which is notoriously weak encryption?

In the end, your code will look something like this:

decryptedData = decryptStr(MY_ENCRYPTED_CONSTANT)
useDecrypted(decryptedData)

No matter how strong your cypher, anybody equipped with a debugger can set a breakpoint on useDecrypted() and recover the plaintext. So, strength of the cypher does not matter. However, using something like Rot47 has two distinct advantages:

  1. You can encode your text online, no need to write a specialized program to encode your text.
  2. Decryption is very easy to implement, so you don't waste your time on something that does not add any value to your customers.
  3. Anybody reading your code (your coworker or yourself after 5 years) will know immediately this is not a real security, but security by obscurity.
  4. Your text will still appear as gibberish to anyone just prying inside your compiled program, so mission accomplished.
鱼忆七猫命九 2024-12-14 01:00:10

运行一些生命游戏变体进行大量迭代,然后根据最终状态向量做出控制流决策。

如果你的程序实际上是为了做一些有用的事情,你可以提前计划你想要的分支,并选择适合的状态向量位(“我想要一个 true ,位 17 打开,所以使条件...... ”)

Run some game of life variant for a large number of iterations, and then make control flow decisions based on the final state vector.

If your program is meant to actually do something useful, you could have your desired branches planned ahead of time and choose bits of the state vector to suit ("I want a true here, bit 17 is on, so make that the condition..")

公布 2024-12-14 01:00:10

您还可以使用编译代码的某些部分作为数据,然后对其进行一些修改。这在虚拟机执行的程序中很难做到,但在 asm 或 c 等语言中是可行的。

You could also use some part of compiled code as data, then modify it a little. This would be hard to do in a program executed by virtual machine, but is doable in languages like asm or c.

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