首页
社区
课程
招聘
我找到一个解密函数,伪c代码中有一句(_byteswap_ushort(*((_WORD *)a2 + 1))怎么翻译成python呀
发表于: 2024-1-10 20:18 3660

我找到一个解密函数,伪c代码中有一句(_byteswap_ushort(*((_WORD *)a2 + 1))怎么翻译成python呀

2024-1-10 20:18
3660
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
byte_1D9591B[132] = [0x11,0x2B,0x65,0x78,0x17,0x0C,0x0D,0x17,0x15,0x35,0x62,0x65,0x7B,0x62,0x15,0x7F,0x11,0x2C,0x63,0x17,0x4D,0x57,0x0C,0x59,0x0B,0x20,0x65,0x21,0x20,0x63,0x0C,0x7F,0x12,0x75,0x04,0x0A,0x0C,0x7B,0x15,0x10,0x21,0x36,0x37,0x1B,0x23,0x65,0x64,0x0B,0x0F,0x47,0x2F,0x5A,0x24,0x11,0x59,0x41,0x4C,0x5B,0x21,0x66,0x5A,0x31,0x40,0x51,0x7E,0x0D,0x15,0x43,0x2D,0x27,0x58,0x51,0x0F,0x57,0x15,0x13,0x24,0x14,0x0E,0x2B,0x25,0x16,0x1F,0x0E,0x4E,0x0E,0x3F,0x29,0x42,0x52,0x10,0x29,0x0F,0x69,0x65,0x49,0x1C,0x20,0x42,0x54,0x22,0x3D,0x4A,0x07,0x37,0x4D,0x11,0x63,0x69,0x65,0x13,0x4A,0x2D,0x2D,0x4B,0x41,0x59,0x0E,0x73,0x24,0x4E,0x15,0x62,0x77,0x10,0x32,0x3C,0x46,0x50,0x5A,0x64,0x6E]
 
unsigned __int8 *__fastcall cocos2d::FileUtils::decryptYF(
        cocos2d::FileUtils *this,
        unsigned __int8 *a2,
        int a3,
        __int64 *a4)
{
  unsigned __int8 *result; // x0
  int v5; // w8
  int v6; // w13
  int v7; // w10
  int v8; // w9
  int v9; // w11
  signed __int64 v10; // x12
  signed __int64 v11; // x14
  int v12; // w15
  __int64 v13; // x5
  __int64 v14; // x7
  char *v15; // x4
  char v16; // w17
  char v17; // w16
  unsigned __int8 v18; // w16
  __int64 v19; // x14
  char v20; // w15
 
  if ( a3 < 2 )
    return 0LL;
  if ( *a2 == 65 && a2[1] == 66 )
  {
    v5 = a2[13] | (_byteswap_ushort(*((_WORD *)a2 + 1)) << 16) | (a2[12] << 8);
    v6 = _byteswap_ushort(*((_WORD *)a2 + 5)) | (_byteswap_ushort(*((_WORD *)a2 + 2)) << 16);
    v7 = v6 % (a2[15] | (a2[14] << 8)) + v6;
    if ( (v7 & 3) != 0 )
      v8 = v5 % 3;
    else
      v8 = 3;
    if ( v7 >= a3 )
      v9 = a3;
    else
      v9 = v6 % (a2[15] | (a2[14] << 8)) + v6;
    result = a2 + 16;
    if ( v9 < 1 )
      goto LABEL_24;
    if ( v9 == 1 )
    {
      v10 = 0LL;
    }
    else
    {
      v10 = 0LL;
      if ( -17 - v5 >= (unsigned int)(v9 - 1) && !((unsigned __int64)(v9 - 1LL) >> 32) )
      {
        v10 = v9 & 0xFFFFFFFFFFFFFFFELL;
        v11 = v10;
        v12 = v5;
        do
        {
          v13 = (unsigned int)(v12 + 16);
          v14 = (unsigned int)(v12 + 17);
          v15 = &byte_1D9591B[33 * v8];
          v16 = v15[v12 % 33];
          v17 = v15[(v12 + 1) % 33];
          v11 -= 2LL;
          v12 += 2;
          v18 = a2[v14] ^ v17;
          a2[v13] ^= v16;
          a2[v14] = v18;
        }
        while ( v11 );
        if ( v10 == v9 )
          goto LABEL_24;
      }
    }
    do
    {
      v19 = (unsigned int)(v5 + v10 + 16);
      v20 = byte_1D9591B[33 * v8 + (v5 + (int)v10++) % 33];
      a2[v19] ^= v20;
    }
    while ( v10 < v9 );
LABEL_24:
    if ( a4 )
      *a4 = (unsigned int)(a3 - 16);
    return result;
  }
  if ( a4 )
    *a4 = a3;
  return a2;
}

我想知道(_byteswap_ushort(*((_WORD *)a2 + 1))这个是怎么翻译成python的。谢谢大佬们。


[培训]内核驱动高级班,冲击BAT一流互联网大厂工作,每周日13:00-18:00直播授课

收藏
免费 1
支持
分享
最新回复 (9)
雪    币: 294
活跃值: (363)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
2
我的理解是 比如说数据: DD 01 A4 E1
(_byteswap_ushort(*((_WORD *)a2 + 1)) 的意思就是 01DD  E1AA 应该没有问题吧?
2024-1-10 20:39
0
雪    币: 294
活跃值: (363)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
3
```

```
2024-1-10 20:40
0
雪    币: 294
活跃值: (363)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
4
def byteswap_ushort(data, index):
    index = index * 2
    return data[index] << 8 | data[index + 1] << 16
2024-1-10 20:40
0
雪    币: 294
活跃值: (363)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
5

# 给定的byte_1D9591B数组
byte_1D9591B = [
    0x11,0x2B,0x65,0x78,0x17,0x0C,0x0D,0x17,0x15,0x35,0x62,0x65,0x7B,0x62,0x15,0x7F,0x11,0x2C,0x63,0x17,0x4D,0x57,0x0C,0x59,0x0B,0x20,0x65,0x21,0x20,0x63,0x0C,0x7F,0x12,0x75,0x04,0x0A,0x0C,0x7B,0x15,0x10,0x21,0x36,0x37,0x1B,0x23,0x65,0x64,0x0B,0x0F,0x47,0x2F,0x5A,0x24,0x11,0x59,0x41,0x4C,0x5B,0x21,0x66,0x5A,0x31,0x40,0x51,0x7E,0x0D,0x15,0x43,0x2D,0x27,0x58,0x51,0x0F,0x57,0x15,0x13,0x24,0x14,0x0E,0x2B,0x25,0x16,0x1F,0x0E,0x4E,0x0E,0x3F,0x29,0x42,0x52,0x10,0x29,0x0F,0x69,0x65,0x49,0x1C,0x20,0x42,0x54,0x22,0x3D,0x4A,0x07,0x37,0x4D,0x11,0x63,0x69,0x65,0x13,0x4A,0x2D,0x2D,0x4B,0x41,0x59,0x0E,0x73,0x24,0x4E,0x15,0x62,0x77,0x10,0x32,0x3C,0x46,0x50,0x5A,0x64,0x6E
]


def byteswap_ushort(data, index):
    index = index * 2
    # 从a2中读取无符号短整型数据,进行字节顺序交换
    return data[index] << 8 | data[index + 1] << 16

def decryptYF(a2, a3):
    result = None
    if a3 < 2:
        return result

    if a2[0] == 65 and a2[1] == 66:
        v5 = a2[13] | (byteswap_ushort(a2, 1) << 16) | (a2[12] << 8)
        v6 = byteswap_ushort(a2, 5) | (byteswap_ushort(a2, 2) << 16)
        v7 = v6 % (a2[15] | a2[14] << 8) + v6

        if v7 & 3:
            v8 = v5 % 3
        else:
            v8 = 3

        if v7 >= a3:
            v9 = a3
        else:
            v9 = v6 % (a2[15] | a2[14] << 8) + v6

        if v9 < 1:
            return  a2[16:]
        v10 = 0
        if v9 > 1:
            if ((-17 & 0xFFFFFFFF) - v5) >= (v9 - 1) and not ((v9 - 1) >> 32):
                v10 = v9 & 0xFFFFFFFFFFFFFFFE
                v11 = v10
                v12 = v5
                while True:
                    v13 = v12 + 16
                    v14 = v12 + 17
                    v15 = byte_1D9591B[33 * v8:]
                    v16 = v15[v12 % 33]
                    v17 = v15[(v12 + 1) % 33]
                    v11 -= 2
                    v12 += 2
                    v18 = a2[v14] ^ v17
                    a2[v13] ^= v16
                    a2[v14] = v18
                    if not v11:
                        break
                if v10 == v9:
                    return  a2[16:]

        while True:
            v19 = v5 + v10 + 16
            v20 = byte_1D9591B[33 * v8 + (v5 + int(v10)) % 33]
            a2[v19] ^= v20
            v10 += 1
    return a2[16:]
目前翻译出来的结果,但是执行的时候存在数组越界问题,在第一个循环中a2[v14], v14越界了。


2024-1-10 21:24
0
雪    币: 560
活跃值: (1480)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
6

GPT的回答: 

(_byteswap_ushort(*((_WORD *)a2 + 1)) << 16) 是一个 C/C++ 表达式,它涉及到位运算和字节顺序的操作。

 让我们来分解这个表达式: 

(_WORD *)a2 + 1:将 a2 强制转换为 _WORD 类型的指针,然后加上 1。这意味着指针向后移动了 2 个字节的距离,因为 _WORD 通常是 2 个字节。这里的操作可能与处理字节顺序(即小端 vs. 大端)有关。

 *(_WORD *):将上一步得到的指针强制转换回 _WORD 类型,并使用间接运算符 * 取得指针指向的值。这实际上获取了从 a2 开始的第 2 个 _WORD。 _byteswap_ushort:这是一个宏或函数,用于将无符号短整数(_ushort)的字节顺序进行交换。它通常用于处理小端和大端之间的差异。这个函数的作用是将上一步得到的 _WORD 类型值的字节顺序进行交换。 

 << 16:将前一步得到的结果左移 16 位。这通常用于将一个值的高 16 位移到低 16 位的位置。这可能是为了将 _WORD 类型的值转换为 32 位整数。 

 整个表达式的目的可能是将 a2 +1指向的内存区域中的两个字节(根据 _WORD 的大小)进行字节顺序交换,并将结果左移 16 位。这样的操作可能在特定的环境下用于处理一些底层的二进制数据。

2024-1-10 21:51
0
雪    币: 3022
活跃值: (6771)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
7

ChatGPT的回复


以下是将给定的IDA反编译出的C语言代码转换为Python代码并附加注释的结果:

# 这段代码是C++中的一个成员函数,所以在Python中需要将成员函数调整为类方法,加入self参数。
class FileUtils:
    @classmethod
    def decryptYF(cls, a2, a3, a4):
        result = None  # 返回值
        v5 = 0  # 局部变量
        v6 = 0  # 局部变量
        v7 = 0  # 局部变量
        v8 = 0  # 局部变量
        v9 = 0  # 局部变量
        v10 = 0  # 局部变量
        v11 = 0  # 局部变量
        v12 = 0  # 局部变量
        v13 = 0  # 局部变量
        v14 = 0  # 局部变量
        v15 = 0  # 局部变量
        v16 = 0  # 局部变量
        v17 = 0  # 局部变量
        v18 = 0  # 局部变量
        v19 = 0  # 局部变量
        v20 = 0  # 局部变量

        if a3 < 2:
            return None

        # 检查是否以"AB"开头
        if a2[0] == 0x11 and a2[1] == 0x2B:
            v5 = a2[13] | ((a2[1] << 16) | (a2[12] << 8))
            v6 = ((a2[5] << 16) | a2[4]) | ((a2[2] << 16) | a2[3])
            v7 = v6 % ((a2[15] << 8) | a2[14]) + v6

            if v7 & 3 != 0:
                v8 = v5 % 3
            else:
                v8 = 3

            if v7 >= a3:
                v9 = a3
            else:
                v9 = v6 % ((a2[15] << 8) | a2[14]) + v6

            result = a2[16:]  # 设置返回值

            if v9 < 1:
                goto_LABEL_24

            if v9 == 1:
                v10 = 0
            else:
                v10 = 0
                if -17 - v5 >= (v9 - 1) and not (v9 - 1 >> 32):
                    v10 = v9 & 0xFFFFFFFFFFFFFFFE
                    v11 = v10
                    v12 = v5

                    while v11 > 0:
                        v13 = v12 + 16
                        v14 = v12 + 17
                        v15 = byte_1D9591B[33 * v8]
                        v16 = v15[v12 % 33]
                        v17 = v15[(v12 + 1) % 33]
                        v11 -= 2
                        v12 += 2
                        v18 = a2[v14] ^ v17
                        a2[v13] ^= v16
                        a2[v14] = v18

                    if v10 == v9:
                        goto_LABEL_24

            while v10 < v9:
                v19 = v5 + v10 + 16
                v20 = byte_1D9591B[33 * v8 + (v5 + v10) % 33]
                a2[v19] ^= v20
                v10 += 1

        else:
            result = a2

        if a4:
            a4[0] = a3 - 16

        return result

# 注释:由于Python没有goto语句,使用标签和条件跳转来模拟。

请注意,Python语言中没有直接的goto语句,因此上述代码使用了标签和条件跳转来模拟原始C++代码中的goto语句。此外,由于C++中的成员函数在Python中需要调整为类方法,因此添加了@classmethod装饰器和self参数。

2024-1-11 01:08
0
雪    币: 294
活跃值: (363)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
8
const KEYS = [0x11, 0x2B, 0x65, 0x78, 0x17, 0x0C, 0x0D, 0x17, 0x15, 0x35, 0x62, 0x65, 0x7B, 0x62, 0x15, 0x7F, 0x11, 0x2C, 0x63, 0x17, 0x4D, 0x57, 0x0C, 0x59, 0x0B, 0x20, 0x65, 0x21, 0x20, 0x63, 0x0C, 0x7F, 0x12, 0x75, 0x04, 0x0A, 0x0C, 0x7B, 0x15, 0x10, 0x21, 0x36, 0x37, 0x1B, 0x23, 0x65, 0x64, 0x0B, 0x0F, 0x47, 0x2F, 0x5A, 0x24, 0x11, 0x59, 0x41, 0x4C, 0x5B, 0x21, 0x66, 0x5A, 0x31, 0x40, 0x51, 0x7E, 0x0D, 0x15, 0x43, 0x2D, 0x27, 0x58, 0x51, 0x0F, 0x57, 0x15, 0x13, 0x24, 0x14, 0x0E, 0x2B, 0x25, 0x16, 0x1F, 0x0E, 0x4E, 0x0E, 0x3F, 0x29, 0x42, 0x52, 0x10, 0x29, 0x0F, 0x69, 0x65, 0x49, 0x1C, 0x20, 0x42, 0x54, 0x22, 0x3D, 0x4A, 0x07, 0x37, 0x4D, 0x11, 0x63, 0x69, 0x65, 0x13, 0x4A, 0x2D, 0x2D, 0x4B, 0x41, 0x59, 0x0E, 0x73, 0x24, 0x4E, 0x15, 0x62, 0x77, 0x10, 0x32, 0x3C, 0x46, 0x50, 0x5A, 0x64, 0x6E]

function world(data, index) {
    index = index * 2;
    return data[index] << 8 | data[index + 1];
}

function decryptItem(data, length) {
    if (length < 2) return data;
    if (data[0] === 65 && data[1] === 66) {
        let code0 = world(data, 6) | (world(data, 1) << 16)
        let code1 = world(data, 5) | (world(data, 2) << 16)
        let decodeSize = code1 + code1 % (world(data, 7))
        let mul = (decodeSize & 3) !== 0 ? (code0 % 3) : 3;
        decodeSize = Math.min(decodeSize, length);
        if(decodeSize > 0) {
            for(let i = 0; i < decodeSize; ++i) {
                const index = 16 + code0 + i;
                const value = data[index] ^ KEYS[33 * mul + (code0 + i) % 33];
                data[index] = value;
            }
        }
        return data.slice(16);
    }

    return data;
}


function tryDecryptYF(file, outPath) {
    let fileData = fs.readFileSync(file);
    const result = decryptItem(fileData, fileData.length);
    fileUtils.mkdirsSync(outPath);
    console.log(`解密完成:`, file);
    fs.writeFileSync(outPath, result);
}

前段时间解出来了。忘了上来回复一下。被一些代码干扰了。新手,多多担待~~

2024-1-19 21:05
0
雪    币: 252
活跃值: (117)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
9
新手一上来都逆向 cocos引擎了吗?
2024-1-22 09:06
0
雪    币: 252
活跃值: (117)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
10
(_byteswap_ushort(*((_WORD *)a2 + 1))  这个就是 将a2 也转成双字节类型的指针,此时再+1,就会地址偏移就是增加2个字节,如果转成4字节指针如 (_DWORD*)a2 + 1 ,这种情况下加1,地址就会偏移4个字节,明白这个指针偏移量计算是关键,后续是到底是 双字中的高低字节交换,还是4字中的半字交换就依次类推就好了
2024-1-22 09:14
0
游客
登录 | 注册 方可回帖
返回
//