作为练手的 160 个 CrackMe 系列整理分析

CrackMe 来源:【反汇编练习】160个CrackME索引目录1~160建议收藏备用

第二版的 CrackMe,有了 052 作铺垫后,053 就稍微轻松点了(并不)。053 的过程和前一个类似,同样先取用户名、产品 ID 等生成一个字符串并计算散列; 取序列号进行计算后两者进行对比,相同则通过。

散列这里就直接跳过不说了,这次不是 MD5,但是看看生成的结果是 128 位的似乎又不是 SHA-1 之类的,可能是修改了的 MD5。

直接看序列号的处理,看看能否找到算法的逆(应该要能)。计算的函数位于 0x00401AD0,其中 0x00401AB0 这个小函数的作用是计算 32 位整数比特位为 1 的 数量。这里直接贴上稍作整理后的 c 代码。

unsigned char countb(DWORD dw)
{
    int count = 0;
    while (dw)
    {
        ++count;
        dw &= dw - 1;
    }
    return count;
}

void func_401AD0(int *key, DWORD *addr1, DWORD *addr2)
{
    int k0 = 0, k1 = 0;
    for (int i = 0; i < 32; ++i)
    {
        unsigned char c1 = countb(*addr1 & key[0]);
        unsigned char c2 = countb(*addr2 & key[1]);
        ++addr1;
        ++addr2;
        k0 = (c1 ^ c2) & 1 ^ 2 * k0;
    }

    for (int i = 0; i < 32; ++i)
    {
        unsigned char c1 = count_bits(*addr1 & key[0]);
        unsigned char c2 = count_bits(*addr2 & key[1]);
        ++addr1;
        ++addr2;
        k1 = (c1 ^ c2) & 1 ^ 2 * k1;
    }

    key[0] = k0;
    key[1] = k1;
}

这次的算法并不直观,首先是一对 key 分别和固定数值(addr1、addr2)作与运算,两者的结果计算非零位的数量后再异或。整体来看就是计算结果(两个 dword)的 64 个 bit 分别由 key 和 64 个固定的 dword 计算后填充,每次结果填充一个位,互不干扰。

一开始我盯着看了好久都没个想法……后来感觉这 64 个位独立计算和矩阵有点类似。可以这样看 countb(d1 & d2):
例如 d1 = 0x12345678,d2 = 0x9abcdef0,那么

d1  00010010001101000101011001111000
&
d2  10011010101111001101111011110000
--------------------------------------
    00010010001101000101011001110000



注意到 & 运算等价于模 2 乘,同样 ^ 运算等价于模 2 加,见模 2 运算。而 countb 则将结果中的所有 1 和 0 相加。

然后就有 countb(d1 & d2) = ∑(i=0,31){d1:i * d2:i},其中 d1:i 表示 d1 的第 i 位。
所以

k1:m = ∑(i=0,31){key0:i * addr1[m]:i} + 
       ∑(i=0,31){key1:i * addr2[m]:i}

如果把 key0,key1 看作一个 64 位整数,那么根据上面的公式可以这样安排一个矩阵:

x = [ x0:31, x0:30, ..., x0:0, x1:31, ..., x1:0 ]
A = ┌                                             ┐
    │ addr1[0]:31, addr1[1]:31, ..., addr1[63]:31 │
    │ addr1[0]:30, addr1[1]:30, ..., addr1[63]:30 │
    │     .                                .      │
    │     .                                .      │
    │ addr1[0]:1 ,        ...,       addr1[63]:1  │
    │ addr1[0]:0 ,        ...,       addr1[63]:0  │
    │ addr2[0]:31,        ...,       addr2[63]:31 │
    │     .      ,        ...,             .      │
    │ addr2[0]:0 ,        ...,       addr2[63]:0  │
    └                                             ┘
b = [ key0:31, key0:30, ..., key0:0, key1:31, ..., key1:0 ]

因为 key 是最后的计算结果,所以这样安排计算式:x * A = b,
x0,x1 即代码中初始的 key[0],key[1]。根据矩阵的乘法公式,这样 x 对应位置上的比特便能与 addr 上对应的比特相乘。

最后照着顺序(先 key[1]key[2],再 key[0]key[1])解出矩阵的解后即得到初始的序列号。矩阵运算找一个合适的数学库就好了。

#include <array>
#include "NTL/ZZ.h"              // 用了 NTL 库
#include "NTL/vec_GF2.h"
#include "NTL/mat_GF2.h"

using namespace NTL;

const std::array<const unsigned char, 256> addr_0x41c2c0 = {
    0x35, 0xEF, 0xCF, 0x0B, 0x6B, 0xDE, 0x9F, 0x17, 0x98, 0xE7, 0xE7, 0xC5, 0xAC, 0x79, 0x7F, 0x5E,
    0x59, 0xF3, 0xFE, 0xBC, 0xB7, 0xC6, 0xFD, 0xF9, 0x6A, 0xAD, 0xFB, 0x73, 0xD6, 0xBC, 0x3F, 0x2F,
    0xD5, 0x5A, 0xF7, 0xE7, 0xAF, 0x95, 0xEE, 0x4F, 0x5E,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, 0x6D, 0x75, 0xFA, 0x4E, 0xDB, 0xEA,
    0x80, 0x8F, 0xB5, 0x2D, 0x00, 0x1F, 0x6B, 0x5B, 0x01, 0x3E, 0xD6, 0xB6, 0xF1, 0xBD, 0xB6, 0x55,
    0xE3, 0x7B, 0x6D, 0xAB, 0xC2, 0xD7, 0xDA, 0xD6, 0x08, 0x98, 0x58, 0x5B, 0x06, 0x5C, 0xAC, 0xED,
};

const std::array<const unsigned char, 256> addr_0x41c3c0 = {
    0x49, 0x13, 0x4D, 0x9B, 0x92, 0x26, 0x9A, 0x36, 0xA4, 0x89, 0xA6, 0xCD, 0x48, 0x9A, 0x68, 0xDA,
    0x90, 0x34, 0xD1, 0xB4, 0x21, 0x69, 0xA2, 0x69, 0x43, 0xD2, 0x44, 0xD3, 0x24, 0x4D, 0x34, 0x6D,
    0x86, 0xA4, 0x89, 0xA6, 0x0D, 0x49, 0x13, 0x4D, 0x1A, 0x92, 0x26, 0x9A, 0x6B, 0x48, 0x9A, 0x68,
    0x35, 0x24, 0x4D, 0x34, 0xD7, 0x90, 0x34, 0xD1, 0xAE, 0x21, 0x69, 0xA2, 0xBA, 0x86, 0xA4, 0x89,
    0x5D, 0x43, 0xD2, 0x44, 0x75, 0x0D, 0x49, 0x13, 0xEA, 0x1A, 0x92, 0x26, 0xD5, 0x35, 0x24, 0x4D,
    0xAA, 0x6B, 0x48, 0x9A, 0x54, 0xD7, 0x90, 0x34, 0xA9, 0xAE, 0x21, 0x69, 0x52, 0x5D, 0x43, 0xD2,
    0xA5, 0xBA, 0x86, 0xA4, 0x4B, 0x75, 0x0D, 0x49, 0x96, 0xEA, 0x1A, 0x92, 0x2C, 0xD5, 0x35, 0x24,
    0x58, 0xAA, 0x6B, 0x48, 0xB1, 0x54, 0xD7, 0x90, 0x62, 0xA9, 0xAE, 0x21, 0xC4, 0x52, 0x5D, 0x43,
    0x89, 0xA5, 0xBA, 0x86, 0x24, 0x96, 0xEA, 0x1A, 0x12, 0x4B, 0x75, 0x0D, 0x48, 0x2C, 0xD5, 0x35,
    0x90, 0x58, 0xAA, 0x6B, 0x20, 0xB1, 0x54, 0xD7, 0x81, 0xC4, 0x52, 0x5D, 0x02, 0x89, 0xA5, 0xBA,
    0x40, 0x62, 0xA9, 0xAE, 0x0A, 0x24, 0x96, 0xEA, 0x05, 0x12, 0x4B, 0x75, 0x2A, 0x90, 0x58, 0xAA,
    0x55, 0x20, 0xB1, 0x54, 0xAA, 0x40, 0x62, 0xA9, 0x54, 0x81, 0xC4, 0x52, 0x15, 0x48, 0x2C, 0xD5,
    0xA9, 0x02, 0x89, 0xA5, 0x53, 0x05, 0x12, 0x4B, 0xA7, 0x0A, 0x24, 0x96, 0x4E, 0x15, 0x48, 0x2C,
    0x9D, 0x2A, 0x90, 0x58, 0x3A, 0x55, 0x20, 0xB1, 0x75, 0xAA, 0x40, 0x62, 0xEA, 0x54, 0x81, 0xC4,
    0xAB, 0x4E, 0x15, 0x48, 0x56, 0x9D, 0x2A, 0x90, 0xAC, 0x3A, 0x55, 0x20, 0xD5, 0xA9, 0x02, 0x89,
    0xAA, 0x53, 0x05, 0x12, 0x55, 0xA7, 0x0A, 0x24, 0xB3, 0xEA, 0x54, 0x81, 0x59, 0x75, 0xAA, 0x40,
};

const std::array<const unsigned char, 256> addr_0x41c4c0 = {
    0xF5, 0xE7, 0x10, 0xAE, 0xB6, 0xFF, 0x87, 0x70, 0x6D, 0xFF, 0x0F, 0xE1, 0xDF, 0xDE, 0x1F, 0x42,
    0xBF, 0xBD, 0x3F, 0x84, 0xEF, 0xEF, 0x21, 0xDC, 0xDB, 0xFF, 0x43, 0x38, 0x7B, 0x5B, 0x7F, 0x88,
    0xF3, 0x96, 0xFE, 0x90, 0x82, 0x57, 0xF4, 0x07, 0x04, 0xAF, 0xE8, 0x0F, 0xE3, 0x0D, 0xFD, 0xA1,
    0xC3, 0x3B, 0xFA, 0xC3, 0x4F, 0xF0, 0x8A, 0xFE, 0x9B, 0xC0, 0x15, 0x7D, 0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    0x38, 0xAF, 0xDF, 0xD0, 0xD1, 0xF9, 0xFD, 0x86, 0xA7, 0xD3, 0xFB, 0x8D, 0x4A, 0x87, 0xF7, 0x9B
};

const std::array<const unsigned char, 256> addr_0x41c5c0 = {
    0xDF, 0x8E, 0xCF, 0xDF, 0xFE, 0x76, 0x7C, 0xFE, 0xFC, 0xED, 0xF8, 0xFC, 0xF9, 0xDB, 0xF1, 0xF9,
    0xF2, 0xB7, 0xE3, 0xF3, 0xBF, 0x1D, 0x9F, 0xBF, 0x7F, 0x3B, 0x3E, 0x7F, 0xE5, 0x6F, 0xC7, 0xE7,
    0xCB, 0xDF, 0x8E, 0xCF, 0x5F, 0xFE, 0x76, 0x7C, 0xBE, 0xFC, 0xED, 0xF8, 0x97, 0xBF, 0x1D, 0x9F,
    0x2F, 0x7F, 0x3B, 0x3E, 0xE0, 0xCB, 0xDF, 0x8E, 0xC1, 0x97, 0xBF, 0x1D, 0xF0, 0xE5, 0x6F, 0xC7,
    0x7C, 0xF9, 0xDB, 0xF1, 0xF8, 0xF2, 0xB7, 0xE3, 0x82, 0x2F, 0x7F, 0x3B, 0x05, 0x5F, 0xFE, 0x76,
    0x56, 0xC1, 0x97, 0xBF, 0x55, 0xF0, 0xE5, 0x6F, 0x15, 0x7C, 0xF9, 0xDB, 0x2A, 0xF8, 0xF2, 0xB7,
    0xAB, 0xE0, 0xCB, 0xDF, 0x0A, 0xBE, 0xFC, 0xED, 0x61, 0x15, 0x7C, 0xF9, 0xC3, 0x2A, 0xF8, 0xF2,
    0x86, 0x55, 0xF0, 0xE5, 0xAC, 0x82, 0x2F, 0x7F, 0x58, 0x05, 0x5F, 0xFE, 0xB0, 0x0A, 0xBE, 0xFC,
    0x0C, 0xAB, 0xE0, 0xCB, 0x19, 0x56, 0xC1, 0x97, 0x32, 0xAC, 0x82, 0x2F, 0x65, 0x58, 0x05, 0x5F,
    0x97, 0x61, 0x15, 0x7C, 0x2E, 0xC3, 0x2A, 0xF8, 0x5C, 0x86, 0x55, 0xF0, 0xB9, 0x0C, 0xAB, 0xE0,
    0x73, 0x19, 0x56, 0xC1, 0xE7, 0x32, 0xAC, 0x82, 0xCB, 0xB0, 0x0A, 0xBE, 0xCE, 0x65, 0x58, 0x05,
    0x9C, 0xCB, 0xB0, 0x0A, 0x72, 0x2E, 0xC3, 0x2A, 0x39, 0x97, 0x61, 0x15, 0xE4, 0x5C, 0x86, 0x55,
    0xC8, 0xB9, 0x0C, 0xAB, 0x23, 0xE7, 0x32, 0xAC, 0x46, 0xCE, 0x65, 0x58, 0x8D, 0x9C, 0xCB, 0xB0,
    0x1B, 0x39, 0x97, 0x61, 0x36, 0x72, 0x2E, 0xC3, 0x6C, 0xE4, 0x5C, 0x86, 0xD9, 0xC8, 0xB9, 0x0C,
    0x91, 0x73, 0x19, 0x56, 0xCB, 0x46, 0xCE, 0x65, 0x96, 0x8D, 0x9C, 0xCB, 0xB2, 0x91, 0x73, 0x19,
    0x65, 0x23, 0xE7, 0x32, 0x2C, 0x1B, 0x39, 0x97, 0x59, 0x36, 0x72, 0x2E, 0xB3, 0x6C, 0xE4, 0x5C
};

void fill_mat(mat_GF2& dst, unsigned long* t1, unsigned long* t2)
{
    for (int i = 0; i < 64; ++i)
    {
        auto tmp = t1[i];
        for (int k = 0; k < 32; ++k)
        {
            dst[k][i] = !!(tmp & 0x80000000);
            tmp <<= 1;
        }

        tmp = t2[i];
        for (int k = 32; k < 64; ++k)
        {
            dst[k][i] = !!(tmp & 0x80000000);
            tmp <<= 1;
        }
    }
}

unsigned long long vec_2_int(vec_GF2& src, int from, int to)
{
    unsigned long long ret = 0;
    for (int i = from; i < to; ++i)
    {
        ret = (ret << 1) | (unsigned long long)to_int(rep(src[i]));
    }
    return ret;
}


int collapse(int argc, char** argv)
{
    // key 是用户名经过 hash 计算得到的前三个,这里替换掉
    unsigned int key[3] = { 0x83bb467a, 0x82023fba, 0xadd52abe };
    unsigned int answer[3] = { 0,0,0 };

    GF2     d;
    vec_GF2 x, vec_key;
    mat_GF2 m1, m2;

    m1.SetDims(64, 64);
    m2.SetDims(64, 64);
    x.SetLength(64);
    vec_key.SetLength(64);

    fill_mat(m1, (unsigned long*)addr_0x41c4c0.data(), (unsigned long*)addr_0x41c5c0.data());

    for (int i = 0; i < 32; ++i)
    {
        vec_key[i] = !!((key[1] << i) & 0x80000000);
        vec_key[i + 32] = !!((key[2] << i) & 0x80000000);
    }

    solve(d, x, m1, vec_key);    // 计算 x * A = b 中的 x, m1 = A,vec_key = b
    answer[2] = vec_2_int(x, 32, 64, 31);

    for (int i = 0; i < 32; ++i)
    {
        vec_key[i] = !!((key[0] << i) & 0x80000000);
        vec_key[i + 32] = x[i];
    }

    fill_mat(m2, (unsigned long*)addr_0x41c2c0.data(), (unsigned long*)addr_0x41c3c0.data());

    solve(d, x, m2, vec_key);
    answer[1] = vec_2_int(x, 32, 64, 31);
    answer[0] = vec_2_int(x, 0, 32, 31);

    return 0;
}

有点没搞明白的是,当我把矩阵中的 bit 顺序换过来,即低位在前时计算结果会出错,完全变成了别的值,也不是正确结果的逆序,照理说应该都没什么差别才对……

final