House of Force 분석

msh1307·2022년 11월 15일
0

etc

목록 보기
2/21

House of force

top chunk의 size를 조작함으로써 임의의 주소에 힙 청크를 할당 할 수 있는 공격 기법이다.

Conditions

  1. Top chunk의 size를 조작할 수 있어야함.
  2. 임의의 크기로 malloc 할 수 있어야함.

Analysis

static void *
_int_malloc (mstate av, size_t bytes)
{
  INTERNAL_SIZE_T nb;               /* normalized request size */
  ...
  mchunkptr remainder;              /* remainder from a split */
  unsigned long remainder_size;     /* its size */
...
    use_top:
      victim = av->top;
      size = chunksize (victim);
      if ((unsigned long) (size) >= (unsigned long) (nb + MINSIZE))
        {
          remainder_size = size - nb;
          remainder = chunk_at_offset (victim, nb);
          av->top = remainder;
          set_head (victim, nb | PREV_INUSE |
                    (av != &main_arena ? NON_MAIN_ARENA : 0));
          set_head (remainder, remainder_size | PREV_INUSE);
          check_malloced_chunk (av, victim, nb);
          void *p = chunk2mem (victim);
          alloc_perturb (p, bytes);
          return p;
        }
    ...
      else
        {
          void *p = sysmalloc (nb, av);
          if (p != NULL)
            alloc_perturb (p, bytes);
          return p;
        }
    }
}

Top chunk size가 nb + MINSIZE보다 크면 위 로직이 실행된다.
remainder_sizesize - nb가 된다.

#define chunk_at_offset(p, s)  ((mchunkptr) (((char *) (p)) + (s)))

chunk_at_offset 매크로는 위와 같다.

remainder은 (mchunkptr)((char *)victim + nb)가 된다.
그 다음 av->top에 넣어준다.
Top chunknb만큼 올라간다.


0x30 만큼 올라간 것을 확인할 수 있다.

Exploitation

Top chunk size를 2^32-1(32 bit) 혹은 2^64-1(64 bit)로 채워주면 아래 조건문을 통과할 수 있다.

if ((unsigned long) (size) >= (unsigned long) (nb + MINSIZE))
        {
          remainder_size = size - nb;
          remainder = chunk_at_offset (victim, nb);
          av->top = remainder;

그리고 임의의 size를 malloc 하면, nb를 컨트롤 할 수 있다.
임의의 주소 - top chunk 주소 - 0x10 크기만큼 malloc 해주면, chunk_at_offset 매크로에 의해서 임의의 주소에 청크를 할당할 수 있게 된다.
안 맞으면 그때 가서 직접 보고 잘 빼줘서 맞춰주면 된다.

profile
https://msh1307.kr

0개의 댓글