hgame Week2 Reverse WP:fakeshell 和 __attribute__((constructor))
fakeshell
唉,这道题不仅shell是fake的,连key都是fake的。。。
理解了题意后我们直奔验证 sudo 密码的函数,发现是以aHappyhg4me指向的字符串为秘钥的RC4加密算法,


但是直接用aHappyhg4me指向的字符串解密只有乱码。


之后我就卡住了,看了 WP 才知道原来在 c 语言中,被__attribute__((constructor)) 分配属性的函数会先于main()函数被调用,IDA 将其反编译到 init 中。不过我直接看也看 init 我也看不出来什么不对劲,只是能看出它好像是调用了其他函数的指针。
不过我们只要在aHappyhg4me处按下 X 查看其被交叉引用的记录,就可以较容易地发现明显这个字符串是被修改过的。
双击 mov 指令,成功找到修改秘钥字符串的函数。

解密得到flag。

关于 __attribute__
__attribute__可用于为函数或者数据声明赋属性值。给函数分配属性的主要目的是为了让编译程序可以优化处理。分配给函数的属性位于函数原型的声明中。
__attribute__((constructor))先于main()函数调用;__attribute__((destructor))在main()函数后调用。
#include <stdio.h>
#include <stdlib.h>
static void before(void) __attribute__((constructor));
static void after(void) __attribute__((destructor));
static void before()
{
printf("before main\n");
}
static void after(void)
{
printf("after main\n");
}
int main()
{
printf("main\n");
return 0;
}

还可以通过参数设置优先级关系:
#include <stdio.h>
#include <stdlib.h>
static void before(void) __attribute__((constructor));
static void before3(void) __attribute__((constructor(103)));
static void before2(void) __attribute__((constructor(102)));
static void before1(void) __attribute__((constructor(101)));
static void before2()
{
printf("before 102\n");
}
static void before1()
{
printf("before 101\n");
}
static void before3()
{
printf("before 103\n");
}
static void before()
{
printf("before main\n");
}
int main()
{
printf("main\n");
return 0;
}


浙公网安备 33010602011771号