ghkdtlwns987

FSOB(File Stream Oriented Programming) 1 본문

시스템

FSOB(File Stream Oriented Programming) 1

2020/03/31 2020. 10. 10. 17:51

FSOB 에 대해 공부해보도록 하자. 

사실 이전에 _IO_File_vtable2 , _IO_File_vtable_check 글에서 Exploit 했던 것이 바로 FSOB 이다. 

 

하지만, FSOB 라고 따로 언급하지도 않았고, 그다지 자세히 설명하지 않았기 때문에, 이번글에서 정리해보고자 한다.

다음은 FILE * fp 구조체이다.

struct _IO_FILE {
  int _flags;           /* High-order word is _IO_MAGIC; rest is flags. */
#define _IO_file_flags _flags

  /* The following pointers correspond to the C++ streambuf protocol. */
  /* Note:  Tk uses the _IO_read_ptr and _IO_read_end fields directly. */
  char* _IO_read_ptr;   /* Current read pointer */
  char* _IO_read_end;   /* End of get area. */
  char* _IO_read_base;  /* Start of putback+get area. */
  char* _IO_write_base; /* Start of put area. */
  char* _IO_write_ptr;  /* Current put pointer. */
  char* _IO_write_end;  /* End of put area. */
  char* _IO_buf_base;   /* Start of reserve area. */
  char* _IO_buf_end;    /* End of reserve area. */
  /* The following fields are used to support backing up and undo. */
  char *_IO_save_base; /* Pointer to start of non-current get area. */
  char *_IO_backup_base;  /* Pointer to first valid character of backup area */
  char *_IO_save_end; /* Pointer to end of non-current get area. */

  struct _IO_marker *_markers;

  struct _IO_FILE *_chain;

  int _fileno;
#if 0
  int _blksize;
#else
  int _flags2;
#endif
  _IO_off_t _old_offset; /* This used to be _offset but it's too small.  */

#define __HAVE_COLUMN /* temporary */
  /* 1+column number of pbase(); 0 is unknown. */
  unsigned short _cur_column;
  signed char _vtable_offset;
  char _shortbuf[1];

  /*  char* _save_gptr;  char* _save_egptr; */

  _IO_lock_t *_lock;
#ifdef _IO_USE_OLD_IO_FILE
};

libc에서, __IO_FILE_ 구조체들은 모두 단일 연결 리스트로 연결 되어있는데, *_chain은 리스트의 다음 __IO_FILE의 주소를 가지고 있다. 연결 리스트의 꼭대기는 __IO_list_all_에 저장되어진다. _IO_list_all 은 전에 설명했으니 넘어가도록 하겠다. 여기서 FILE * fp 구조체에서 0xd8 만큼 떨어져 있는 곳에 vtable 의 주소가 저장되어 있다. 

다음은 __IO_FILE_plus_ 구조체이다.

struct _IO_FILE_plus
{
  _IO_FILE file;
  const struct _IO_jump_t *vtable;
};

struct _IO_jump_t
{
    JUMP_FIELD(size_t, __dummy);
    JUMP_FIELD(size_t, __dummy2);
    JUMP_FIELD(_IO_finish_t, __finish);
    JUMP_FIELD(_IO_overflow_t, __overflow);
    JUMP_FIELD(_IO_underflow_t, __underflow);
    JUMP_FIELD(_IO_underflow_t, __uflow);
    JUMP_FIELD(_IO_pbackfail_t, __pbackfail);
    /* showmany */
    JUMP_FIELD(_IO_xsputn_t, __xsputn);
    JUMP_FIELD(_IO_xsgetn_t, __xsgetn);
    JUMP_FIELD(_IO_seekoff_t, __seekoff);
    JUMP_FIELD(_IO_seekpos_t, __seekpos);
    JUMP_FIELD(_IO_setbuf_t, __setbuf);
    JUMP_FIELD(_IO_sync_t, __sync);
    JUMP_FIELD(_IO_doallocate_t, __doallocate);
    JUMP_FIELD(_IO_read_t, __read);
    JUMP_FIELD(_IO_write_t, __write);
    JUMP_FIELD(_IO_seek_t, __seek);
    JUMP_FIELD(_IO_close_t, __close);
    JUMP_FIELD(_IO_stat_t, __stat);
    JUMP_FIELD(_IO_showmanyc_t, __showmanyc);
    JUMP_FIELD(_IO_imbue_t, __imbue);
#if 0
    get_column;
    set_column;
#endif
};

 

그렇다면 이를 이용해 Exploit 하는 예를 하나 들어보겠다.

만약 File *fp; 의 값을 덮어쓸 수 있다면 fake fp를 만들어(이 때 _IO_list_all, 전역변수가 선언되어 있으면 이를 사용할 수 있다.) 포인터 값을 옮기고 fake vtable으로 위치시켜 file stream 함수를 이용할때 프로그램의 실행 흐름을 control하는 방법이 존재한다. 

 

그럼 다음과 같은 코드가 있다고 가정하자.

이번엔 _IO_overflow 가 아닌, fake fp구조체에서 0x602060위치를 가리키도록 하고, 

0x602138위치 ( 0x602060 + 0xd8 )(_IO_file_jumps)에 fake vtable의 주소를 세팅해준다.

그리고 이제 fake vtable(== fake _IO_file_jumps )에서 원하는 부분을 원하는 걸로 바꿔주면 된다.

 

맨 마지막에 fclose() 함수로 fp 를 close() 해 주는데, 

fake_vtable 에 있는 __GI__IO_file_close() 를 one_gadget 을 덮을 수도 있다. 혹은 

fclose(fp) 이므로 fp 값에 '/bin/sh' 값을 주어 system('/bin/sh')를 실행시킬 수 있다.

'시스템' 카테고리의 다른 글

FSOB(_IO_flush_all_lockp )  (0) 2020.10.10
flose() 분석  (0) 2020.10.10
_IO_FILE_vtable_check  (0) 2020.10.10
_IO_FILE_vtable 2  (0) 2020.10.10
_IO_FILE_vtable 1  (0) 2020.10.10
Comments