【问题标题】:I'm stuck trying to reverse this formula我被困在试图扭转这个公式
【发布时间】:2019-06-09 17:06:44
【问题描述】:

我正在尝试对一个游戏函数进行逆向工程,该函数接受一个整数并返回一个 64 位整数,方法是编写一个返回原始值的函数,然后再将其放入游戏函数。我如何做到这一点?

我只是设法扭转了这些步骤:

x = ((1 - x) << 16)

我不确定如何在没有原始值的情况下反转加法。

这里是游戏功能:

int64_t convert(int x) {
  if (x <= 0)
    return (1 - ((((x + 1) >> 31) + x + 1) >> 16));
  else
    return 0;
}

例如,如果原始值为-5175633,则转换后的值为80,我需要从80中获取原始值。

【问题讨论】:

  • sizeof(int) 的值是多少?
  • 使用像Z3这样的SMT求解器来解决此类问题。

标签: c++ reverse-engineering bit-shift


【解决方案1】:

我认为这是不可能的。当您右移一个整数时,位会丢失。这意味着有多个输入值会返回相同的输出值。

抱歉,Victor,但您的解决方案不起作用。你应该比较 r 和 i,而不是 c 和 cc。

【讨论】:

  • 你是对的。我的解决方案只是给出一个具有相同输出的值。正如我所指出的:convert(-5177344) 将给出与 convert(-5175633) 相同的 80,因此您无法在它们之间做出决定。
【解决方案2】:

假设,随着步骤(1 - ((((x + 1) &gt;&gt; 31) + x + 1) &gt;&gt; 16))x 已转换为y,我们得到:

y = 1 - ((((x + 1) >> 31) + x + 1) >> 16)
1 - y = (((x + 1) >> 31) + x + 1) >> 16
(1 - y) << 16 = (x + 1) >> 31 + x + 1

如果x x + 1

如果x + 1(x + 1) >> 31为-1,其中xy为1 - ((((x + 1) &gt;&gt; 31) + x + 1) &gt;&gt; 16),为1 - (x &gt;&gt; 16)

(1 - y) << 16 = -1 + x + 1
(1 - y) << 16 = x

如果x + 1 >= 0,则(x + 1) &gt;&gt; 31 为0,其中x >= -1,而y,即1 - ((((x + 1) &gt;&gt; 31) + x + 1) &gt;&gt; 16),为1。(注意:现在 x 可以只能是 0 或 -1)

(1 - y) << 16 = x + 1
(1 - y) << 16 - 1 = x

所以,把这两个结果加在一起,我们可以得到:

int reverse_convert(int64_t y) {
    if (y == 1)
        return (1 - y) << 16 - 1; // However, either x = 0 or x = 1 can produce this result.
    else
        return (1 - y) << 16;
    // the condition of y == 0, corresponding to the original "else return 0;", is ignored.
}

另外,convert 函数是一个Surjective-only 函数,这意味着多个输入可以得到相同的输出,那么不可能将精确输出反转为输入。

【讨论】:

    【解决方案3】:

    我假设 sizeof(int) 是 4。所有的操作都是在 32 位上完成的。

    #include <iostream>
    
    using namespace std;
    
    int64_t convert(int32_t x) {
      if (x <= 0)
        return (1 - ((((x + 1) >> 31) + x + 1) >> 16));
      else
        return 0;
    }
    
    int32_t revconvert(int64_t r) {
        if (r == 0) return 0;
        if (r == 1) return -1;
        return (1-r) << 16;
    }
    
    int main()
    {
        int32_t i;
        for (i=0;i>-10000000;--i) {
            auto c = convert(i);
            auto r = revconvert(c);
            auto cc = convert(r);
            if( c!=cc) break;
        }
    
        cout << i << endl; // just to see if we got to the end
    
        cout << convert(-5175633) << endl; // Will give 80
        cout << revconvert(80) << endl; // Will give -5177344
        cout << convert(-5177344) << endl; // Will give 80
    
        return 0;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-04-02
      • 1970-01-01
      • 2012-10-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-11
      相关资源
      最近更新 更多