依靠

  1. IDA7.7
  2. Ubuntu20.04

作者:hans774882968以及hans774882968以及hans774882968

本文52pojie:www.52pojie.cn/thread-1686…

本文juejin:/post/714231…

本文csdn:blog.csdn.net/hans7748829…

思路

64位ELF。用IDA打开即可看到main函数:

__int64 __fastcall main(int a1, char **a2, char **a3)
{
  if ( a1 > 1 )
  {
    if ( !strcmp(a2[1], "zer0pts{********CENSORED********}") )
      puts("Correct!");
    else
      puts("Wrong!");
  }
  else
  {
    printf("Usage: %s <FLAG>\n", *a2);
  }
  return 0LL;
}

会这么简单嘛?咱们应该关注a2[1]是否被修改了。因而看看start函数:

// positive sp value has been detected, the output may be wrong!
void __fastcall __noreturn start(__int64 a1, __int64 a2, void (*a3)(void))
{
  __int64 v3; // rax
  int v4; // esi
  __int64 v5; // [rsp-8h] [rbp-8h] BYREF
  char *retaddr; // [rsp+0h] [rbp+0h] BYREF
  v4 = v5;
  v5 = v3;
  _libc_start_main(main, v4, &retaddr, init, fini, a3, &v5);
  __halt();
}

查阅_libc_start_main的材料(参考链接1)可知init函数会在main函数之前被调用。

void __fastcall init(unsigned int a1, __int64 a2, __int64 a3)
{
  signed __int64 v4; // rbp
  __int64 i; // rbx
  v4 = &off_200DF0 - &funcs_889;
  init_proc();
  if ( v4 )
  {
    for ( i = 0LL; i != v4; ++i )
      ((void (__fastcall *)(_QWORD, __int64, __int64))*(&funcs_889 + i))(a1, a2, a3);
  }
}

有这些函数被调用了:

.init_array:0000000000200DE0 E0 06 00 00 00 00 00 00       funcs_889 dq offset sub_6E0
.init_array:0000000000200DE8 95 07 00 00 00 00 00 00       dq offset sub_795

看了看sub_6E0啥也没有,那重点肯定是sub_795了:

// write access to const memory has been detected, the output may be wrong!
int (**sub_795())(const char *s1, const char *s2)
{
  int (**result)(const char *, const char *); // rax
  result = &strcmp;
  qword_201090 = (__int64)&strcmp;
  off_201028 = sub_6EA;
  return result;
}

点击检查off_201028,发现是strcmp函数的地址,也就是说off_201028 = sub_6EAstrcmp的地址篡改为sub_6EA的地址。因而main函数调用strcmp的时候会调用sub_6EA

__int64 __fastcall sub_6EA(__int64 a1, __int64 a2)
{
  int i; // [rsp+18h] [rbp-8h]
  int v4; // [rsp+18h] [rbp-8h]
  int j; // [rsp+1Ch] [rbp-4h]
  for ( i = 0; *(_BYTE *)(i + a1); ++i )
    ;
  v4 = (i >> 3) + 1;
  for ( j = 0; j < v4; ++j )
    *(_QWORD *)(8 * j + a1) -= qword_201060[j];
  return qword_201090(a1, a2);
}

这儿a1, a2类型不对,应该是const char *。回想一下sub_795这句qword_201090 = (__int64)&strcmp,可知qword_201090(a1, a2)就是调用原有的strcmp函数。至此咱们现已知道a2[1]被修改的进程。

总结:一种hook,很有趣。

代码

CPP完成

考虑到这儿要把字符串当_QWORD运用,python不方便完成,因而咱们用IDA装置目录下可找到的defs.h,赋能cpp,打出一套组合。

#include <bits/stdc++.h>
#include "defs.h"
using namespace std;
typedef long long LL;
#define rep(i,a,b) for(int i = (a);i <= (b);++i)
#define re_(i,a,b) for(int i = (a);i < (b);++i)
#define dwn(i,a,b) for(int i = (a);i >= (b);--i)
void dbg() {
    puts ("");
}
template<typename T, typename... R>void dbg (const T &f, const R &... r) {
    cout << f << " ";
    dbg (r...);
}
int main() {
    char a1[40] = "zer0pts{********CENSORED********}";
    _QWORD qword_201060[4] = {0, 0x410A4335494A0942, 0x0B0EF2F50BE619F0, 0x4F0A3A064A35282B};
    int v4 = strlen (a1);
    re_ (j, 0, 4) {
        * (_QWORD *) (8 * j + a1) += qword_201060[j];
    }
    dbg (a1);
    return 0;
}

Python+libnum库

libnum能够让咱们方便地把char数组当成整数来进行内存操作,同时支撑一些根本的数论操作(如:求逆元、分化质因数)。装置libnum(留意:只支撑python3):

pip config set global.index-url https://pypi.douban.com/simple # 留意现在pip只支撑https的源了
pip install libnum

代码

from libnum import s2n, n2s
def main():
    key = [0, 0x410A4335494A0942, 0x0B0EF2F50BE619F0, 0x4F0A3A064A35282B]
    a = b'zer0pts{********CENSORED********}'
    ans = b''
    for i in range(4):
        j = s2n(a[i * 8: i * 8 + 8][::-1]) + key[i]
        ans += n2s(j)[::-1]
    ans += b'}'
    print(ans)
if __name__ == "__main__":
    main()

IDA动态调试

IDA动态调试装备戳这儿qwq

动调时sub_795main函数长这样(因为没有去掉ASLR):

// write access to const memory has been detected, the output may be wrong!
__int64 (__fastcall *sub_561199200795())()
{
  __int64 (__fastcall *result)(); // rax
  result = sub_5611992005C6;
  qword_561199401090 = (__int64)sub_5611992005C6;
  off_561199401028 = sub_5611992006EA;
  return result;
}
__int64 __fastcall sub_5588DE0007C7(int a1, __int64 a2)
{
  if ( a1 > 1 )
  {
    if ( (unsigned int)sub_5588DE0005C0(*(_QWORD *)(a2 + 8), "zer0pts{********CENSORED********}") )
      sub_5588DE0005A0("Wrong!");
    else
      sub_5588DE0005A0("Correct!");
  }
  else
  {
    sub_5588DE0005B0("Usage: %s <FLAG>\n", *(const char **)a2);
  }
  return 0LL;
}

即使不知道sub_795hook了strcmp也不要紧,咱们断点断在main函数,然后“步进”strcmp函数也能找到要害代码。

sub_6ea点击a1变量,能够定位输入字符串,进而看到它被修改的进程。

【reverse】buu-[Zer0pts2020]easy_strcmp——main函数的启动过程+IDA动态调试ELF

【reverse】buu-[Zer0pts2020]easy_strcmp——main函数的启动过程+IDA动态调试ELF

参考材料

  1. zhuanlan.zhihu.com/p/52054044
  2. 一篇写得不错的wp:blog.csdn.net/Tokameine/a…