20180613
0x00 前言
注意:此解法非常暴力,如果你想通过这道题学习神经网络相关的数学与算法知识,建议点击返回并查看其它大佬的WP。所以请版主大大千万别加精,要脸。。。
0x01 字符长度与范围
长度限定为10,但他计算长度的方法非常的魔性,我是没看懂,是猜+调试弄出来的。
iter = input;
do
{
v5 = *(_DWORD *)iter;
iter += 4;
v6 = ~v5 & (v5 - 0x1010101) & 0x80808080;
}
while ( !v6 ); // iter = length as dword + base pointer, round to 4
if ( !(~v5 & (v5 - 0x1010101) & 0x8080) )
v6 >>= 16;
if ( !(~v5 & (v5 - 0x1010101) & 0x8080) )
iter += 2;
if ( &iter[-__CFADD__((_BYTE)v6, (_BYTE)v6) - 3] - input != 10 )// length == 10
{
//error
}
然后是字符范围,要求[A-F0-9]
,并且会通过这个把它转换成数值
if ( (unsigned int)capitical_hex_to_num(input, &input_num, 10) != 5 )
{
//error
}
如果范围不对,capitical_hex_to_num
会返回0
然后在后面,有一个input_num & 0xF0000
就会错误的检查,最后得出flag为XXXXX0XXXX
的形式,其中X
为[0-9A-F]
PS:这个坑了我整整一天,一直看错了以为flag是XXXX00XXXX
,然后一直跑不出解,还以为是反调试了,调试的时候的训练数据跟不调试的不一样,还对程序做了补丁输出了所有不加调试时候的数据,然后发现一样怀疑人生。。。也还是自己犯傻了。。。
0x02 暴力破解
暴力破解也很简单了,基本上就是把前面三个函数nop掉,因为要么会crash(可能是因为爆破只调用了main,没有调用程序的预处理,而0x401F00
很可能是编译器生成的call,要么会太慢(训练的时候用GetTickCount
规定了必须1秒)。然后,把预先训练好的数据dump下来,直接memcpy
过去,接下来就是暴力跑了。。。
代码轻喷。。。
#include <stdio.h>
#include <stdint.h>
#include <windows.h>
#include <set>
#include <string>
#include <vector>
using namespace std;
char train_res[] =
"\xF4\xC9\x5C\x82\x72\xA7\xDB\x3F\x77\x7A\x2F\xBB\xCA\xE6\xE3\x3F"
"\x34\xD1\x6F\x19\xAF\x87\xDA\x3F\x4A\x15\xD0\x5D\xF6\x81\xE0\x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D0\x3F\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF"
"\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\x02";
set<string> outputs;
vector<uint32_t> goons;
char* module_addr = 0;
size_t main_off = 0x3880;
size_t printf_iat_off = 0xD3B4;
size_t scanf_iat_off = 0xD3C4;
bool success = false;
bool goon = false;
uint32_t number_now;
typedef int (__cdecl *main_t)();
main_t program_main;
void check(uint32_t a)
{
number_now = a;
memcpy(module_addr + 0xC980, train_res, sizeof(train_res) - 1);
program_main();
if (success)
{
printf("%hx", (uint16_t)(number_now & 0xffff));
printf("00");
printf("%hx", (uint16_t)(number_now >> 16));
system("pause");
}
if (goon)
{
//goons.push_back(a);
printf("%.8X\n", a);
goon = false;
}
}
void __fastcall hook_printf(char* content)
{
if (strcmp(content, "Congratulation\n") == 0)
{
success = true;
}
else if (strcmp(content, "Come on, go on\n") == 0)
{
goon = true;
}
//outputs.insert(content);
//printf("%s\n", content);
}
void __fastcall hook_scanf(char* format, char* buf)
{
sprintf_s(buf, 5 , "%.4hX", (uint16_t)(number_now & 0xffff));
buf[4] = 'E';
buf[5] = '0';
sprintf_s(buf + 6, 5, "%.4hX", (uint16_t)(number_now >> 16));
//printf("%s\n", buf);
}
void init()
{
module_addr = (char*)LoadLibraryA("NNCrackme.dll");
if (module_addr == 0)
{
printf("fail to open dll\n");
system("pause");
exit(-1);
}
*(void**)(module_addr + printf_iat_off) = hook_printf;
*(void**)(module_addr + scanf_iat_off) = hook_scanf;
program_main = (main_t)(module_addr + main_off);
}
int main()
{
init();
for (size_t i = 0x00400000; i < 0x100000000; i+=0x1000000)
{
//printf("%zx\n", i);
for (size_t j = 0; j < 0x100000; j++)
{
check((uint32_t)(i + j));
}
}
for (uint32_t g : goons)
{
printf("0x%.8X,", g);
}
system("pause");
return 0;
}
然后这个其实是有猜的成分,之前我是从0-0xffffffff
然后开12个进程一直在跑的,然后发现好像只有0xXX4XXXXX
会产生go on的信息,所以索性换成了以上的爆破方式,结果就在E的时候出结果了。。。
然后方法用的是把exe变成dll的老办法,为啥不用unicorn那套东西?因为还在学,没学会(逃