[BUUOJ] reverse3



  • 32位IDA打开,F5一下main函数

     __int64 __cdecl main_0()
     {
      size_t v0; // eax
      const char *v1; // eax
      size_t v2; // eax
      int v3; // edx
      __int64 v4; // ST08_8
      signed int j; // [esp+DCh] [ebp-ACh]
      signed int i; // [esp+E8h] [ebp-A0h]
      signed int v8; // [esp+E8h] [ebp-A0h]
      char Dest[108]; // [esp+F4h] [ebp-94h]
      char Str; // [esp+160h] [ebp-28h]
      char v11; // [esp+17Ch] [ebp-Ch]
    
      for ( i = 0; i < 100; ++i )
      {
        if ( (unsigned int)i >= 0x64 )
          j____report_rangecheckfailure();
        Dest[i] = 0;
      }
      sub_41132F("please enter the flag:");
      sub_411375("%20s", &Str);
      v0 = j_strlen(&Str);
      v1 = (const char *)sub_4110BE(&Str, v0, &v11);
      strncpy(Dest, v1, 0x28u);
      v8 = j_strlen(Dest);
      for ( j = 0; j < v8; ++j )
        Dest[j] += j;
      v2 = j_strlen(Dest);
      if ( !strncmp(Dest, Str2, v2) )
        sub_41132F("rigth flag!\n");
      else
        sub_41132F("wrong flag!\n");
      HIDWORD(v4) = v3;
      LODWORD(v4) = 0;
      return v4;
    }
    

    可以看出输入的flag经过sub_4110BE函数的处理,再经过for循环最后与Str2比较。
    打开Str2

    .data:0041A034 Str2            db '[email protected]@dH',0 ; DATA XREF: _main_0+142↑o
    

    把Str2反向经历for循环是容易的
    再看sub_4110BE函数

    void *__cdecl sub_411AB0(char *a1, unsigned int a2, int *a3)
    {
      int v4; // STE0_4
      int v5; // STE0_4
      int v6; // STE0_4
      int v7; // [esp+D4h] [ebp-38h]
      signed int i; // [esp+E0h] [ebp-2Ch]
      unsigned int v9; // [esp+ECh] [ebp-20h]
      int v10; // [esp+ECh] [ebp-20h]
      signed int v11; // [esp+ECh] [ebp-20h]
      void *Dst; // [esp+F8h] [ebp-14h]
      char *v13; // [esp+104h] [ebp-8h]
    
      if ( !a1 || !a2 )
        return 0;
      v9 = a2 / 3;
      if ( (signed int)(a2 / 3) % 3 )
        ++v9;
      v10 = 4 * v9;
      *a3 = v10;
      Dst = malloc(v10 + 1);
      if ( !Dst )
        return 0;
      j_memset(Dst, 0, v10 + 1);
      v13 = a1;
      v11 = a2;
      v7 = 0;
      while ( v11 > 0 )
      {
        byte_41A144[2] = 0;
        byte_41A144[1] = 0;
        byte_41A144[0] = 0;
        for ( i = 0; i < 3 && v11 >= 1; ++i )
        {
          byte_41A144[i] = *v13;
          --v11;
          ++v13;
        }
        if ( !i )
          break;
        switch ( i )
        {
          case 1:
            *((_BYTE *)Dst + v7) = aAbcdefghijklmn[(signed int)(unsigned __int8)byte_41A144[0] >> 2];
            v4 = v7 + 1;
            *((_BYTE *)Dst + v4++) = aAbcdefghijklmn[((byte_41A144[1] & 0xF0) >> 4) | 16 * (byte_41A144[0] & 3)];
            *((_BYTE *)Dst + v4++) = aAbcdefghijklmn[64];
            *((_BYTE *)Dst + v4) = aAbcdefghijklmn[64];
            v7 = v4 + 1;
            break;
          case 2:
            *((_BYTE *)Dst + v7) = aAbcdefghijklmn[(signed int)(unsigned __int8)byte_41A144[0] >> 2];
            v5 = v7 + 1;
            *((_BYTE *)Dst + v5++) = aAbcdefghijklmn[((byte_41A144[1] & 0xF0) >> 4) | 16 * (byte_41A144[0] & 3)];
            *((_BYTE *)Dst + v5++) = aAbcdefghijklmn[((byte_41A144[2] & 0xC0) >> 6) | 4 * (byte_41A144[1] & 0xF)];
            *((_BYTE *)Dst + v5) = aAbcdefghijklmn[64];
            v7 = v5 + 1;
            break;
          case 3:
            *((_BYTE *)Dst + v7) = aAbcdefghijklmn[(signed int)(unsigned __int8)byte_41A144[0] >> 2];
            v6 = v7 + 1;
            *((_BYTE *)Dst + v6++) = aAbcdefghijklmn[((byte_41A144[1] & 0xF0) >> 4) | 16 * (byte_41A144[0] & 3)];
            *((_BYTE *)Dst + v6++) = aAbcdefghijklmn[((byte_41A144[2] & 0xC0) >> 6) | 4 * (byte_41A144[1] & 0xF)];
            *((_BYTE *)Dst + v6) = aAbcdefghijklmn[byte_41A144[2] & 0x3F];
            v7 = v6 + 1;
            break;
        }
      }
      *((_BYTE *)Dst + v7) = 0;
      return Dst;
    }
    

    很明显看到输入的字符串经过了数组aAbcdefghijklmn[]的变换,打开这个数组

    .rdata:00417B30 aAbcdefghijklmn db 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/='
    

    从'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/='可以看出,这个函数应该是base64的加密函数,因此只需要解密即可。

    脚本

    import base64
    
    a= '[email protected]@dH'
    b = list(a)
    flag = ''
    
    for i in range(0,16):
        b[i] = chr(ord(b[i])-i)
    
    c = ''.join(b)
    
    flag = base64.b64decode(c)
    flag = flag.decode('ASCII')
    print(flag)
    

    flag

    {i_l0ve_you}
    

Log in to reply