测试文件:https://static2.ichunqiu.com/icq/resources/fileupload/CTF/BSRC/BSRC-11-3/CrackMe01_97D2BF0DBD2EC13065E7104F24CAAED9.zip

1.准备

“百度杯”CTF比赛 十一月场--CrackMe01-LMLPHP

获取信息

  • 32位文件

2.IDA打开

熟悉Windows API的话,一般组成的就是MessageBox,ShowWindow...这里测试错误答案时,没有窗口提示,也就排除了MessageBox,而且要处理消息事件的话,直接查找默认消息处理DefWindowProc。

在import里面搜索DefWindowProc

“百度杯”CTF比赛 十一月场--CrackMe01-LMLPHP

点击进入之后,Ctrl+X查找用到的地方

“百度杯”CTF比赛 十一月场--CrackMe01-LMLPHP

一个一个进入,F5分析就行。(实际上就是第一个)

找到这么一段代码

 LRESULT __stdcall sub_4027F0(int a1, UINT Msg, WPARAM wParam, LPARAM lParam)
{
UINT v4; // eax
LPARAM v5; // edx
__int16 v6; // bx
void *v7; // edi
HDC v9; // esi
unsigned int v10; // ecx
__int16 v11; // ax
unsigned int v12; // eax
int v13; // edi
int v14; // eax
struct tagRECT v15; // [esp+18h] [ebp-68h]
struct tagRECT Rect; // [esp+28h] [ebp-58h]
struct tagPAINTSTRUCT Paint; // [esp+38h] [ebp-48h] v4 = Msg;
v5 = lParam;
v6 = ;
v7 = (void *)wParam;
if ( Msg > 0xF )
{
if ( Msg != )
return DefWindowProcW((HWND)a1, v4, (WPARAM)v7, v5);
v10 = ;
do
{
v11 = *(_WORD *)(wParam + * v10++);
v6 += v11;
}
while ( v10 <= lParam );
v12 = ;
do
{
*(const WCHAR *)((char *)&chText + v12) ^= v6;
v12 += ;
}
while ( v12 < 0x2C );
GetWindowRect((HWND)a1, &v15);
v13 = (v15.left - v15.right + GetSystemMetrics()) / ;
v14 = GetSystemMetrics();
SetWindowPos((HWND)a1, HWND_MESSAGE|0x2, v13, (v15.top - v15.bottom + v14) / , -, -, 5u);
SetWindowPos((HWND)a1, (HWND)0xFFFFFFFE, , , , , 3u);
if ( (v6 & 0xF00) == 0x400 && (v6 & 0xF0) == 0xB0u && (v6 & ) == )
{
ShowWindow((HWND)a1, );
UpdateWindow((HWND)a1);
}
v7 = (void *)wParam;
LABEL_18:
if ( v7 )
free(v7);
v5 = lParam;
v4 = Msg;
return DefWindowProcW((HWND)a1, v4, (WPARAM)v7, v5);
}
switch ( Msg )
{
case 0xFu:
v9 = BeginPaint((HWND)a1, &Paint);
GetClientRect((HWND)a1, &Rect);
DrawTextW(v9, &chText, -, &Rect, 0x25u);
EndPaint((HWND)a1, &Paint);
goto LABEL_18;
case 1u:
return ;
case 2u:
PostQuitMessage();
return ;
}
return DefWindowProcW((HWND)a1, v4, (WPARAM)v7, v5);
}

3.代码分析

去除掉代码中对窗口消息,创建,我们需要的代码就是

    do
{
*(const WCHAR *)((char *)&chText + v12) ^= v6;
v12 += ;
}
while ( v12 < 0x2C );

这里实际上就是对chText的前0x2c个进行了异或处理,代码中两个字节为一组,总共22组。

chText

F0  DA  D7  D1   8C  FF  F5  FE
E3 F8 E7 FF E3 E9 F0 F3
F2 F4 F3

因此chText为

chText[]={0x4F0, 0x4DA, 0x4D7, 0x4D1, 0x48C, 0x4FF, 0x4F5, 0x4FE,  0x4E3, 0x4F8, 0x4E7, 0x4FF, 0x4E3,  0x4E9, 0x4F0, 0x4F3, 0x485, 0x480, 0x484, 0x4F2, 0x4F4, 0x4F3}

v6的值我们可以通过第44行代码

if ( (v6 & 0xF00) == 0x400 && (v6 & 0xF0) == 0xB0u && (v6 & ) ==  )
{
ShowWindow((HWND)a1, );
UpdateWindow((HWND)a1);
}

得到v6=0x4b6

4.脚本获取

chText = [0x4F0, 0x4DA, 0x4D7, 0x4D1, 0x48C, 0x4FF, 0x4F5, 0x4FE, 0x4E3, 0x4F8, 0x4E7, 0x4FF, 0x4E3, 0x4E9, 0x4F0,
0x4F3, 0x485, 0x480, 0x484, 0x4F2, 0x4F4, 0x4F3]
flag = '' for i in range(22):
chText[i] ^= 0x4b6
flag += chr(chText[i]) print(flag)

5.get flag!

05-11 16:57