https://dreamhack.io/wargame/challenges/44/
command-injection-1
ํน์ Host์ ping ํจํท์ ๋ณด๋ด๋ ์๋น์ค์ ๋๋ค. Command Injection์ ํตํด ํ๋๊ทธ๋ฅผ ํ๋ํ์ธ์. ํ๋๊ทธ๋ flag.py์ ์์ต๋๋ค. Reference Introduction of Webhacking
dreamhack.io
๋ฌธ์
ํน์ Host์ ping ํจํท์ ๋ณด๋ด๋ ์๋น์ค์
๋๋ค.
Command Injection์ ํตํด ํ๋๊ทธ๋ฅผ ํ๋ํ์ธ์. ํ๋๊ทธ๋ flag.py์ ์์ต๋๋ค.
๋ฌธ์ ํ์ผ
#!/usr/bin/env python3
import subprocess
from flask import Flask, request, render_template, redirect
from flag import FLAG
APP = Flask(__name__)
@APP.route('/')
def index():
return render_template('index.html')
@APP.route('/ping', methods=['GET', 'POST'])
def ping():
if request.method == 'POST':
host = request.form.get('host')
cmd = f'ping -c 3 ""; cat "flag.py"'
try:
output = subprocess.check_output(['/bin/sh', '-c', cmd], timeout=5)
return render_template('ping_result.html', data=output.decode('utf-8'))
except subprocess.TimeoutExpired:
return render_template('ping_result.html', data='Timeout !')
except subprocess.CalledProcessError:
return render_template('ping_result.html', data=f'an error occurred while executing the command. -> {cmd}')
return render_template('ping.html')
if __name__ == '__main__':
APP.run(host='0.0.0.0', port=8000)
ํ์ด
์ ์ ํ ping ํญ์ ๋ค์ด๊ฐ๋ฉด ์ธํ์ ํธ์คํธ๋ฅผ ์ ๋ ฅํด์ ping์ ๋ณด๋ผ ์ ์๋ค.
๋น์ฐํ ping์ ๋ณด๋ผ๋๋ ์์คํ ๋ช ๋ น์ด๋ฅผ ์ฌ์ฉํ ๊ฒ์์ ์ ์ ์๋๋ฐ, ์ฐ์ app.py ํ์ผ์ ํ๋ฒ ๋ฏ์ด๋ณด์
@APP.route('/ping', methods=['GET', 'POST'])
def ping():
if request.method == 'POST':
host = request.form.get('host')
cmd = f'ping -c 3 "{host}"'
try:
output = subprocess.check_output(['/bin/sh', '-c', cmd], timeout=5)
return render_template('ping_result.html', data=output.decode('utf-8'))
except subprocess.TimeoutExpired:
return render_template('ping_result.html', data='Timeout !')
except subprocess.CalledProcessError:
return render_template('ping_result.html', data=f'an error occurred while executing the command. -> {cmd}')
return render_template('ping.html')
์ ์ ๊ฐ ์ธํ์ ๋ฃ์ ๊ฐ์ host๋ก ๋ฐ์์ ํฌํจ์ํจ ์์คํ ๋ช ๋ น์ด ping์ ์คํํ๊ณ ๊ทธ ๊ฒฐ๊ณผ๋ฅผ ์ถ๋ ฅํ๋ ๋ก์ง์ด๋ค
์ฐ๋ฆฌ๊ฐ flag.py์ ์๋ ํ๋๊ทธ๋ฅผ ๊ฒฐ๊ณผ๋ก์ ์ถ๋ ฅ์ํค๋ ค๋ฉด
cat flag.py
์ ๋ช ๋ น์ด๋ฅผ ์ํํ๋ฉด ๋ ๊ฒ ๊ฐ๋ค.
"; cat "flag.py"
if request.method == 'POST':
host = request.form.get('host')
cmd = f'ping -c 3 ""; cat "flag.py"'
try:
ํน์๋ชฐ๋ผ "๋ก ํฐ๋ฐ์ดํ๋ฅผ ๋ซ์์ฃผ๊ณ
; ๋ก ์ ๋ช ๋ น์ด์ ๊ตฌ๋ถํด์ฃผ๊ณ
๋ช ๋ น์ด๋ฅผ ๋ฃ์ด์ฃผ๋๋ฐ, ์ด๋ ๋ฌด์กฐ๊ฑด ์ ์ ๊ฐ ์ ๋ ฅํ ๊ฐ์ ๋งจ ๋ค์ "๊ฐ ํฌํจ๋์ด ์คํ๋๊ธฐ ๋๋ฌธ์ flag.py ์์ ํฐ ๋ฐ์ดํ๋ฅผ ์ถ๊ฐํด์คฌ๋ค.
์๊ทธ๋ฌ๋ฉด ping -c 3 ""; cat flag.py" ๊ฐ ๋์ด๋ฒ๋ ค์ ์ค๋ฅ ๋ธ
์ด์ ์คํํด์ฃผ์
Oh.
์์ฒญํ ํ์๊ณผ ์ผ์น์ํค๋ผ๋ ๊ฒฝ๊ณ ์ ํจ๊ป ๋ด๊ฐ ์์ฑํ ์ ๋ ฅ๊ฐ์ด ํผ์ ์ ์ถ๋์ง ์๋๋ค.
ํ๋ก ํธ์๋ ๊ฐ๋ฐ์ ํ๋ค๋ณด๋ ์ ๊ฒฝ๊ณ ๋ฌธ์ ๋์์ธ์ด ๋งค์ฐ ์ต์ํ๋๋ฐ, html์์ input ํ๊ทธ์ ์์ฑ์ผ๋ก ์กฐ๊ฑด์ ๊ฑธ๋ฉด ๋์ค๋ ๊ฒฝ๊ณ ๋ฌธ์ด๋ค
๊ฐ๋ฐ์๋๊ตฌ(Command + F12)๋ฅผ ์ผ์ ํ์ผ์ ์ดํด๋ณด๊ณ ๊ฐ๋จํ๊ฒ ์กฐ์ํด๋ณด์
์์๋๋ก <input/> ํ๊ทธ์ patter ์์ฑ์ด ๊ฑธ๋ ค์๋ค
์ ๋ถ๋ถ๋ง ์ญ์ ํด๋ฒ๋ฆฌ๋ฉด ํจํด ์กฐ๊ฑด ๊ฒ์ฌ ์์ด ์ ์ถ์ด ๊ฐ๋ฅํ๋ค
์ด์ ๊ฒฝ๊ณ ์๋ฆผ๋ ์๋จ๊ณ ์ ์ถ์ด ๋๋ค
์ฑ๊ณต!
ํ์ผ๋ด
๋์ค์ ๋ค๋ฅธ ์ ๋ต ๋ธ๋ก๊ทธ๋ฅผ ์ดํด๋ณด๋ ์ ํจํด๊ฒ์ฌ ์ฐํ๋ฅผ ์ํด burpsuit ๋ฑ์ ์ฌ์ฉํ๋ ๊ฒฝ์ฐ๋ ๋ง์๋๋ฐ,,
๊ฐ๋จํ ๋ณ๊ฒฝ์ ๋๋ผ ๊ฐ๋ฐ์ ๋ชจ๋์์ ํ๋ฒ์ ๋๋ฑ ํ๊ธด ํ์ง๋ง ๋ณต์กํ ๋ณ๊ฒฝ์ผ ๊ฒฝ์ฐ ํด๋น ํด์ ์ฌ์ฉํด๋ณด๋ ๊ฒ๋ ์ข์ ๊ฒ ๊ฐ๋ค.
'๐ CTF (Dreamhack) > Web Hacking (์นํดํน)' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[DreamHack] ๋๋ฆผํต ์นํดํน: web-ssrf (0) | 2023.07.17 |
---|---|
[DreamHack] ๋๋ฆผํต ์นํดํน : file-download-1 (0) | 2023.07.14 |
[DreamHack] ๋๋ฆผํต ์นํดํน: csrf-2 (0) | 2022.11.09 |
[DreamHack] ๋๋ฆผํต ์นํดํน: csrf-1 (0) | 2022.11.08 |
[DreamHack] ๋๋ฆผํต ์นํดํน: simple-sqli (0) | 2022.09.22 |