SuperVingo

UTCTF 2021 Reversing Peeb Poob Write up 본문

CaptureTheFlag[CTF]/UTCTF2021

UTCTF 2021 Reversing Peeb Poob Write up

SuperVingo 2021. 3. 17. 09:36
728x90

Peeb Poob

ghidra 분석

undefined4 main(void)

{
  size_t len;
  char arr [32];
  undefined4 local_14;
  uint j;
  int i;
  
  local_14 = 0;
  i = 0;
  while (i < 0x20) {
    arr[i] = '\0';
    i = i + 1;
  }
  printf("Enter a string: \n");
  fgets(arr,0x20,stdin);
  encode(arr);
  j = 0;
  while( true ) {
    len = strlen(arr);
    if (len <= j) {
      puts("Nice flag!");
      return 0;
    }
    if (arr[j] != flag[j]) break;
    j = j + 1;
  }
  puts("Wrong!");
                    /* WARNING: Subroutine does not return */
  exit(-1);
}
void encode(char *str)

{
  size_t len;
  uint temp;
  uint i;
  
  i = 0;
  while (len = strlen(str), i < len) {
    temp = (uint)((int)i < 0);
    temp = ((i ^ -temp) + temp & 3 ^ -temp) + temp;
    if (temp < 4) {
      switch(temp) {
      case 0:
        str[i] = str[i] ^ 0x21;
        break;
      case 1:
        str[i] = str[i] ^ 7;
        break;
      case 2:
        str[i] = str[i] ^ 0x23;
        break;
      case 3:
        str[i] = str[i] ^ 5;
      }
    }
    len = strlen(str);
    if (i + 1 < len) {
      str[i + 1] = str[i] ^ str[i + 1];
    }
    i = i + 1;
  }
  return;
}

함수 2개 발견

 

encode (0부터 시작)

1. 특정 연산으로 temp 구한 후, temp값에 의해 xor 연산

2. 다음 문자열을 xor연산된 값으로 미리 xor

3. 다음 문자열도 특정 연산으로 temp 구한 후, temp값에 의해 xor 연산

반복

 

decode (뒷자리부터 시작)

1. 특정 연산으로 temp 구한 후, temp값에 의해 xor 연산

2. 이전 문자열과 xor연산

3. 이전 문자열도 특정 연산으로 temp 구한 후, temp값에 의해 xor 연산

반복

 

특정 연산을 확인해보니

temp = (uint)((int)i < 0);
temp = ((i ^ -temp) + temp & 3 ^ -temp) + temp;
---------------------------------------------------------------
temp = 0;
temp = ((i ^ 0) + 0 & 3 ^ 0) + 0;
---------------------------------------------------------------
temp -> 0 1 2 3 0 1 2 3 0 1 2 3 . . .

i % 4와 같은 연산임

flag 배열 확인

 

Sol

flag = [0x54, 0x27, 0x62, 0x0b, 0x4b, 0x2b, 0x73, 0x14, 0x06, 0x32, 0x61, 0x3b, 0x78, 0x4f, 0x5c, 0x29, 0x57, \
    0x20, 0x30, 0x06, 0x45, 0x1d, 0x4e, 0x7b, 0x6a, 0x0f, 0x51, 0x5e, 0x00, 0x00, 0x00, 0x00]

def encode(str):
    for i in range(0, len(str)):
        if(i % 4 == 0):
            str[i] ^= 0x21
        elif(i % 4 == 1):
            str[i] ^= 0x07
        elif(i % 4 == 2):
            str[i] ^= 0x23
        elif(i % 4 == 3):
            str[i] ^= 0x05
        
        if(i + 1 < len(str)):
            str[i+1] ^= str[i]

def decode(str):
    for i in range(len(str) - 1, -1, -1):
        if(i % 4 == 0):
            str[i] ^= 0x21
        elif(i % 4 == 1):
            str[i] ^= 0x07
        elif(i % 4 == 2):
            str[i] ^= 0x23
        elif(i % 4 == 3):
            str[i] ^= 0x05
        if(i != 0):
            str[i] ^= str[i-1]
    for i in str[0:28]:
        print(chr(i), end="")

decode(flag)

utflag{b33p_b00p_p33b_p00b}

728x90