iksikd

导航

DASCTF X HDCTF 2024 公开赛|生而无畏战 FinalEncrypt

打开附件,发现给了三个,一个两个.enc,一个elf文件,猜测可能通过elf加密这两个文件 ida打开elf文件: 直接来到main函数

int __cdecl main(int argc, const char **argv, const char **envp)
{
  char v3; // r15
  char v4; // r14
  int v5; // eax
  unsigned int v7; // eax
  __int128 *v8; // rbx
  char *v9; // rax
  unsigned __int8 *v10; // rax
  char *v11; // rax
  char *v12; // rax
  char v13; // [rsp+0h] [rbp-292h]
  char v14; // [rsp+1h] [rbp-291h]
  __m128i v15[4]; // [rsp+Ah] [rbp-288h] BYREF
  __int128 v16[16]; // [rsp+4Ah] [rbp-248h] BYREF
  char file[16]; // [rsp+14Ah] [rbp-148h] BYREF
  __int128 v18; // [rsp+15Ah] [rbp-138h]
  __int128 v19; // [rsp+16Ah] [rbp-128h]
  __int128 v20; // [rsp+17Ah] [rbp-118h]
  __int128 v21; // [rsp+18Ah] [rbp-108h]
  __int128 v22; // [rsp+19Ah] [rbp-F8h]
  __int128 v23; // [rsp+1AAh] [rbp-E8h]
  __int128 v24; // [rsp+1BAh] [rbp-D8h]
  __int128 v25; // [rsp+1CAh] [rbp-C8h]
  __int128 v26; // [rsp+1DAh] [rbp-B8h]
  __int128 v27; // [rsp+1EAh] [rbp-A8h]
  __int128 v28; // [rsp+1FAh] [rbp-98h]
  __int128 v29; // [rsp+20Ah] [rbp-88h]
  __int128 v30; // [rsp+21Ah] [rbp-78h]
  __int128 v31; // [rsp+22Ah] [rbp-68h]
  __int128 v32; // [rsp+23Ah] [rbp-58h]
  unsigned __int64 v33; // [rsp+252h] [rbp-40h]

  v3 = 0;
  v4 = 0;
  v33 = __readfsqword(0x28u);
  memset(v16, 0, sizeof(v16));
  *file = 0LL;
  v18 = 0LL;
  v19 = 0LL;
  v20 = 0LL;
  v21 = 0LL;
  v22 = 0LL;
  v23 = 0LL;
  v24 = 0LL;
  v13 = 0;
  v14 = 0;
  v25 = 0LL;
  v26 = 0LL;
  v27 = 0LL;
  v28 = 0LL;
  v29 = 0LL;
  v30 = 0LL;
  v31 = 0LL;
  v32 = 0LL;
  while ( 1 )
  {
    v5 = getopt(argc, argv, ":d:e:rk:h");
    if ( v5 == -1 )
      break;
    if ( v5 <= 114 )
    {
      if ( v5 > 99 )
      {
        switch ( v5 )
        {
          case 'd':
            v3 = 1;
            __strcpy_chk(file, optarg, 256LL);
            continue;
          case 'e':
            v4 = 1;
            strncpy(file, optarg, 0xFFuLL);
            continue;
          case 'h':
            goto LABEL_26;
          case 'k':
            strncpy(v16, optarg, 0x100uLL);
            v13 = 1;
            continue;
          case 'r':
            v14 = 1;
            continue;
          default:
            continue;
        }
      }
      if ( v5 == ':' )
      {
        puts("option needs a value");
      }
      else if ( v5 == '?' )
      {
        __printf_chk(1LL, "unknown option: %c\n", optopt);
      }
    }
  }
  if ( v14 == 1 )
  {
    if ( v13 )
      goto LABEL_26;
  }
  else if ( v13 != 1 )
  {
    goto LABEL_26;
  }
  if ( v4 != (v3 ^ 1) )
    goto LABEL_26;
  if ( v4 != 1 )
    goto LABEL_18;
  if ( v14 != 1 )
LABEL_26:
    printHelp();
  v7 = time(0LL);
  v8 = v15;
  srand(v7);
  do
  {
    v8 = (v8 + 4);
    *(v8 - 1) = rand();
  }
  while ( v16 != v8 );
  v9 = binToHex(v15, 64LL);
  __printf_chk(1LL, "Random key: %s\n", v9);
  puts("Keep this key safe. It is needed to decrypt the file.");
  fileEncrypt(file, v15);
LABEL_18:
  if ( v3 == 1 )
  {
    v10 = hexToBin(v16, 0x80uLL);
    v11 = binToHex(v10, 64LL);
    __printf_chk(1LL, "Key: %s\n", v11);
    v12 = hexToBin(v16, 0x80uLL);
    fileDecrypt(file, v12);
  }
  return 0;
}

可以看到是通过时间戳生成的key来实现文件加密与解密。

查看文件属性,获取连个enc文件的时间戳,ida动调elf。注意设置parameters,例如(-r -e testfile.txt),依次得到对应的key。

分别是Encryption.exe.enc:507CD82354B1A821ED46A45FAF06D53D0F941C24E085D511F244410B2666056462E126287107616B308DDB193B62036C89C7112C8E713828BC8E8C5079ED221A

flag.md.enc:CB0C24457D0BE9695EFDD94C533DE2036E0E6E706C1B06471DBE334DA3195C32F46C8078C7311D068270A10EAE446D0553FA1F628CE4336A39DA852730625324

解密得到一个exe文件和一个flag文件,flag文件内容为e07816e1dba1da61536634bef2c3b6346d533cc3b6b834e3beb634c80264143c34e36400bb4daa6902ff643414e3b8344dff6634b8b66db6bbc33834143461ab147e04。猜测exe文件继续对flag进行加密。故ida打开exe文件

int __cdecl main(int argc, const char **argv, const char **envp)
{
  unsigned int v3; // eax
  char Str[8]; // [rsp+20h] [rbp-60h] BYREF
  char v6[56]; // [rsp+28h] [rbp-58h] BYREF
  int v7; // [rsp+60h] [rbp-20h]
  char v8[260]; // [rsp+70h] [rbp-10h] BYREF
  int v9; // [rsp+174h] [rbp+F4h]
  int v10; // [rsp+178h] [rbp+F8h]
  int v11; // [rsp+17Ch] [rbp+FCh]
  int v12; // [rsp+180h] [rbp+100h]
  int v13; // [rsp+184h] [rbp+104h]
  int v14; // [rsp+188h] [rbp+108h]
  int v15; // [rsp+18Ch] [rbp+10Ch]
  int v16; // [rsp+190h] [rbp+110h]
  char v17; // [rsp+195h] [rbp+115h]
  char v18; // [rsp+196h] [rbp+116h]
  char v19; // [rsp+197h] [rbp+117h]
  int i; // [rsp+198h] [rbp+118h]
  unsigned __int8 v21; // [rsp+19Ch] [rbp+11Ch]
  char v22; // [rsp+19Dh] [rbp+11Dh]
  char v23; // [rsp+19Eh] [rbp+11Eh]
  char v24; // [rsp+19Fh] [rbp+11Fh]

  _main(argc, argv, envp);
  memset(v8, 0, 0x100ui64);
  v3 = time(0i64);
  srand(v3);
  do
    v19 = rand();
  while ( !v19 );
  memset(v8, 0, 0x100ui64);
  v8[0] = v19;
  v22 = 1;
  v21 = 1;
  do
  {
    if ( v22 < 0 )
      v24 = 27;
    else
      v24 = 0;
    v22 ^= v24 ^ (2 * v22);
    v18 = (2 * v21) ^ (4 * ((2 * v21) ^ v21)) ^ v21;
    v17 = v18 ^ (16 * v18);
    if ( v17 < 0 )
      v23 = 9;
    else
      v23 = 0;
    v21 = v23 ^ v17;
    v16 = (unsigned __int8)(v23 ^ v17);
    v16 = __ROR1__(v23 ^ v17, 7);
    v15 = v16 ^ (unsigned __int8)(v21 ^ v8[0]);
    v14 = v21;
    v14 = __ROR1__(v21, 6);
    v13 = v15 ^ v14;
    v12 = v21;
    v12 = __ROR1__(v21, 5);
    v11 = v13 ^ v12;
    v10 = v21;
    v10 = __ROR1__(v21, 4);
    v9 = v11 ^ v10;
    v8[v22] = v11 ^ v10;
  }
  while ( v22 != 1 );
  puts("Please input the flag:");
  *(_QWORD *)Str = 0i64;
  memset(v6, 0, sizeof(v6));
  v7 = 0;
  scanf("%s", Str);
  puts("The encrypted flag is:");
  for ( i = 0; i < strlen(Str); ++i )
    printf("%02x", (unsigned __int8)v8[Str[i]]);
  return 0;
}

还是时间戳生成数组,然后将flag作为数组的index来加密。然而我们不知道这个时间戳是多少。我尝试了FinalEncrypt这个文件的时间戳,但是失败了。

因此想到了爆破时间戳

#include <stdio.h>
#include <string.h>
#include <time.h>
#include <stdlib.h>
#include <stdbool.h>

// Rotate right function
unsigned char __ROR1__(unsigned char value, int count) {
    return ((value >> count) | (value << (8 - count))) & 0xFF;
}

void custom_algorithm(unsigned int v3, unsigned char *v8) {
    unsigned int v19, v22, v21, v18, v17, v23, v16, v15, v14, v13, v12, v11, v10, v9;
    int v24;

    memset(v8, 0, 256);
    srand(v3); // Use the provided seed

    do {
        v19 = rand() % 256; // Ensure v19 fits in unsigned char
    } while (!v19);

    v8[0] = (unsigned char)v19;
    v22 = 1;
    v21 = 1;
    int count = 0;
    do {
        if (v22 > 127)
            v24 = 27;
        else
            v24 = 0;
        v22 = (v22 ^ v24 ^ (2 * v22)) & 0xFF;
        v18 = ((2 * v21) ^ (4 * ((2 * v21) ^ v21)) ^ v21) & 0xFF;
        v17 = (v18 ^ (16 * v18)) & 0xFF;
        if (v17 > 127)
            v23 = 9;
        else
            v23 = 0;
        v21 = (v23 ^ v17) & 0xFF;
        v16 = __ROR1__((unsigned char)(v23 ^ v17), 7);
        v15 = (v16 ^ (v21 ^ v8[0])) & 0xFF;
        v14 = __ROR1__((unsigned char)v21, 6);
        v13 = (v15 ^ v14) & 0xFF;
        v12 = __ROR1__((unsigned char)v21, 5);
        v11 = (v13 ^ v12) & 0xFF;
        v10 = __ROR1__((unsigned char)v21, 4);
        v9 = (v11 ^ v10) & 0xFF;
        v8[v22] = v9;
        count++;
    } while (count < 300);
}

bool match_v8_with_b(const unsigned char *v8, const char *b, unsigned char *indices) {
    size_t b_len = strlen(b);
    bool all_found = true;
    size_t idx = 0;

    for (size_t i = 0; i < b_len; i += 2) {
        char hex_str[3] = {b[i], b[i + 1], '\0'};
        unsigned char byte = (unsigned char)strtol(hex_str, NULL, 16);
        bool found = false;

        for (int j = 0; j < 128; j++) {
            if (v8[j] == byte) {
                indices[idx++] = j; // Store index of v8 where match is found
                found = true;
                break;
            }
        }
        if (!found) {
            all_found = false;
            break;
        }
    }
    indices[idx] = 0xFF; // Terminate the indices array
    return all_found;
}

int main() {
    const char *b = "e07816e1dba1da61536634bef2c3b6346d533cc3b6b834e3beb634c80264143c34e36400bb4daa6902ff643414e3b8344dff6634b8b66db6bbc33834143461ab147e04";
    unsigned char v8[256];
    unsigned char indices[128]; // Large enough to store all indices needed
    unsigned int start_time =1715153796;
    unsigned int end_time = 1715156519;

    for (unsigned int v3 = start_time; v3 <= end_time; v3++) {
        custom_algorithm(v3, v8);
        if (match_v8_with_b(v8, b, indices)) {
            printf("Match found for seed %u\n", v3);
            printf("Matching indices in v8: ");
            for (int i = 0; indices[i] != 128; i++) {
                printf("%c", indices[i]);
            }
            printf("\n");
        }
    }

    return 0;
}

取一开始和最后的时间戳,爆破得到有多个值满足,且都能还原出flag

flag为DASCTF{7ou_h@ve_5o1ved_4he_fina1_4ncrypti0n_a4d_y0u_de5erv3_a_7lag}

posted on 2024-06-04 14:33  iksikd  阅读(125)  评论(0)    收藏  举报