2018湖湘杯逆向
0x00:介绍HighwayHash64这道题放了很久一直没有去做,最近看了一下发现这道题比较特殊,重新整理了一下思路,Replace这道题比较简单,属于签到题难度,还有一道题环境配置比较麻烦,我就说一下大概的思路。
0x01:题目
题目1:Replace
题目链接:
简单的…密码学~
http://hxb2018.oss-cn-beijing.aliyuncs.com/reserves/Replace_B21DA8B2F172C13764989DF0F99B890A.rar
解题过程:首先查壳是UPX壳,我们直接用脱壳机就可以了(当然也可以手动)
https://img-blog.csdnimg.cn/20190204093728517.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L0NoYXJsZXNHb2RY,size_16,color_FFFFFF,t_70
我们用IDA分析后发现,就是一个简单的算法,里面有两处比较关键的数组,我们将其提取出来
https://img-blog.csdnimg.cn/20190204094840666.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L0NoYXJsZXNHb2RY,size_16,color_FFFFFF,t_70
我们提取出第一处关键数组,这里可以不包括最后一个0x00:
https://img-blog.csdnimg.cn/20190204094904393.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L0NoYXJsZXNHb2RY,size_16,color_FFFFFF,t_70
第二处关键数组:
https://img-blog.csdnimg.cn/20190204095113156.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L0NoYXJsZXNHb2RY,size_16,color_FFFFFF,t_70
提取完之后,就可以写爆破脚本了:
a = [
0x63, 0x7C, 0x77, 0x7B, 0xF2, 0x6B, 0x6F, 0xC5, 0x30, 0x01,
0x67, 0x2B, 0xFE, 0xD7, 0xAB, 0x76, 0xCA, 0x82, 0xC9, 0x7D,
0xFA, 0x59, 0x47, 0xF0, 0xAD, 0xD4, 0xA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0x3E,
0xB5, 0x66, 0x48, 0x03, 0xF6, 0x0E, 0x61, 0x35, 0x57, 0xB9,
0x86, 0xC1, 0x1D, 0x9E, 0xE1, 0xF8, 0x98, 0x11, 0x69, 0xD9,
0x8E, 0x94, 0x9B, 0x1E, 0x87, 0xE9, 0xCE, 0x55, 0x28, 0xDF,
0x8C, 0xA1, 0x89, 0x0D, 0xBF, 0xE6, 0x42, 0x68, 0x41, 0x99,
0x2D, 0x0F, 0xB0, 0x54, 0xBB, 0x16
]
b = [
0x32, 0x61, 0x34, 0x39, 0x66, 0x36, 0x39, 0x63, 0x33, 0x38,
0x33, 0x39, 0x35, 0x63, 0x64, 0x65, 0x39, 0x36, 0x64, 0x36,
0x64, 0x65, 0x39, 0x36, 0x64, 0x36, 0x66, 0x34, 0x65, 0x30,
0x32, 0x35, 0x34, 0x38, 0x34, 0x39, 0x35, 0x34, 0x64, 0x36,
0x31, 0x39, 0x35, 0x34, 0x34, 0x38, 0x64, 0x65, 0x66, 0x36,
0x65, 0x32, 0x64, 0x61, 0x64, 0x36, 0x37, 0x37, 0x38, 0x36,
0x65, 0x32, 0x31, 0x64, 0x35, 0x61, 0x64, 0x61, 0x65, 0x36,
0x00
]
flag = ""
v4 = 0
while(1):
v8 = b
if (v8 < 48 | v8 > 57):
v9 = v8 - 87
else:
v9 = v8 - 48
v10 = b
v11 = 16 * v9
if ( v10 < 48 | v10 > 57 ):
v12 = v10 - 87
else:
v12 = v10 - 48
for v5 in range(0,127):
v6 = (v5 >> 4) % 16
v7 = (16 * v5 >> 4) % 16
if a == (v11 + v12) ^ 0x19:
flag += chr(v5)
break
v4 += 1
if (v4 >= 35):
break
print(flag)
运行得到flag
https://img-blog.csdnimg.cn/2019020409533460.png
题目2:HighwayHash64
题目链接:
口算哈希说的就是你吧~
http://hxb2018.oss-cn-beijing.aliyuncs.com/reserves/reverse_1CE475F54D2A3264A8ED743FDFEF24A8.zip
解题过程:拿到题运行之,提示要我们输入flag,随便输入之后程序自动退出,放入IDA中分析一下
https://img-blog.csdnimg.cn/20190204100546890.png
找到main函数,这两处有明显的比较,结合题目意思,这里应该是hash摘要计算,我们需要爆破hash的值
https://img-blog.csdnimg.cn/20190204100610218.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L0NoYXJsZXNHb2RY,size_16,color_FFFFFF,t_70
我们逐个分析,可以分析得出第一个函数负责计算flag长度,第二个函数负责计算flag括号中的内容,既然是自定义的hash函数,我们可以考虑新写一个exe程序调用这个程序的函数,既然是调用那我们肯定要将exe文件修改为dll文件,工具使用010Editor,修改的方法主要是:
.IMAGE_FILE_HEADER->Characteristics(文件属性)->2102h(DLL文件一般是2102h)
.IMAGE_OPTIONAL_HEADER->AddressOfEntryPoint(程序执行入口RVA)->0000h
我们先通过MS-DOS头部找到PE文件头,然后在通过偏移找到上面的两处位置,如下图:
https://img-blog.csdnimg.cn/20190204100728547.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L0NoYXJsZXNHb2RY,size_16,color_FFFFFF,t_70
第一处修改:
https://img-blog.csdnimg.cn/20190204100746314.png
第二处修改:
https://img-blog.csdnimg.cn/20190204100758946.png
修改之后我们将后缀改为.dll然后就可以实现链接了,根据汇编我们可以看出是__fastcall的调用约定,因为是用rcx和rdx寄存器传的参数,编译的时候也需要用64位编译,因为我们调用的dll是64位
https://img-blog.csdnimg.cn/2019020410083810.png
然后我们就可以开始实现链接了:
typedef __int64(__fastcall *f)(__int64 buff, unsigned __int64 len);
int main()
{
HINSTANCE hdll;
hdll = LoadLibrary(TEXT("F:\\reverse.dll"));
if (hdll == NULL)
{
printf("Load dll Error: %d\n", GetLastError());
return 0;
}
printf("Dll base is %llx\n", hdll);
f func = ((f)((char*)hdll + 0x17A0));
}
第一处是需要我们爆破长度,长度最后减去hxb2018{}这9位:
https://img-blog.csdnimg.cn/20190204101010127.png
代码如下:
int i;
unsigned long longresult;
for (i = 0; i<50; i++)
{
result = func((long long)&i, 4);
if (result == 0xD31580A28DD8E6C4)
{
printf("Len is %d\n", i - 9);
}
}
运行可以看到长度为10
https://img-blog.csdnimg.cn/20190204101132772.png
第二次是需要我们爆破内容,通过sprintf快速制作10个字节的十进制数,然后穷举:
unsigned long long j;
unsigned long long result2;
char buff;
for (j = 0; j < 10000000000; j++)
{
sprintf_s(buff, "%0.10llu", j);
if (j % 100000 == 0)
{
printf("%0.10llu\n", j);
}
result2 = func((long long)buff, 10);
if (result2 == 0xE3BE26AF8730545A)
{
printf("flag is %lld\n", j);
return 0;
}
}
最后爆破一段时间得到flag
https://img-blog.csdnimg.cn/20190204101250746.png
总的脚本如下:
#include "stdafx.h"
#include<Windows.h>
typedef __int64(__fastcall *f)(__int64 buff, unsigned __int64 len);
int main()
{
HINSTANCE hdll;
hdll = LoadLibrary(TEXT("F:\\reverse.dll"));
if (hdll == NULL)
{
printf("Load dll Error: %d\n", GetLastError());
return 0;
}
printf("Dll base is %llx\n", hdll);
f func = ((f)((char*)hdll + 0x17A0));
int i;
unsigned long longresult;
for (i = 0; i<50; i++)
{
result = func((long long)&i, 4);
if (result == 0xD31580A28DD8E6C4)
{
printf("Len is %d\n", i - 9);
}
}
unsigned long long j;
unsigned long long result2;
char buff;
for (j = 0; j < 10000000000; j++)
{
sprintf_s(buff, "%0.10llu", j);
if (j % 100000 == 0)
{
printf("%0.10llu\n", j);
}
result2 = func((long long)buff, 10);
if (result2 == 0xE3BE26AF8730545A)
{
printf("flag is %lld\n", j);
return 0;
}
}
return 0;
}
Tips:每个人的flag在复现的时候可能不同哦
0x02:总结
HighwayHash64这道题可以帮助我们熟悉PE文件结构,是一道很不错的题目,还有一道题目More efficient than JS我没有记录,大概是关于WebAssembly逆向的,可以使用idawasm插件在IDA中进行分析,也可以使用wasmdec 生成伪 c 代码分析,就是环境搭建比较麻烦。
参考的wp:
https://www.anquanke.com/post/id/164604#h2-6
https://impakho.com/post/hxb-2018-writeup
你擅长用IDA还是OD? Shin 发表于 2019-2-23 12:58
当然是IDA和OD一起用,IDA静态分析,OD动态调试,不过现在动态调试我在用×64dbg Thunder_J 发表于 2019-2-23 14:05
x64bug的确好用,不过你可以试试Mdbg,全新体验,我IDA是短板,我很多东西都是先靠动态来跑,然后静态来还原具体的,正好跟你们的步骤反过来,看来我得多练习IDA和Python 我感觉这个帖子应该是有图的,但是图你好像没有上传成功,有空的话可以补一下{:9_320:}
页:
[1]