SWLUG/์›น ํ•ดํ‚น

[Dreamhack/๋“œ๋ฆผํ•ต] command-injection-1

waterproof 2023. 11. 20. 18:26

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

 

command-injection-1

ํŠน์ • Host์— ping ํŒจํ‚ท์„ ๋ณด๋‚ด๋Š” ์„œ๋น„์Šค์ž…๋‹ˆ๋‹ค. Command Injection์„ ํ†ตํ•ด ํ”Œ๋ž˜๊ทธ๋ฅผ ํš๋“ํ•˜์„ธ์š”. ํ”Œ๋ž˜๊ทธ๋Š” flag.py์— ์žˆ์Šต๋‹ˆ๋‹ค. Reference Introduction of Webhacking

dreamhack.io

 

 


[1] ๋ฌธ์ œ ๋ถ„์„

 

# ๋ฌธ์ œ ์›น ํŽ˜์ด์ง€ ๋ถ„์„

 

 

์ฒซ ํ™”๋ฉด์ด๋‹ค.

 

 

 

์ƒ๋‹จ์— 'Home' ์˜†์— 'Ping'์„ ๋ˆ„๋ฅด๋ฉด ์ด๋™ํ•˜๋Š” ํŽ˜์ด์ง€์ด๋‹ค.

๋„ค๋ชจ ์นธ์— IP ์ฃผ์†Œ๋ฅผ ์ž…๋ ฅํ•  ์ˆ˜ ์žˆ๊ฒŒ ๋˜์–ด์žˆ๋Š” ๊ฒƒ ๊ฐ™์•˜๊ณ , "Ping!" ๋ฒ„ํŠผ์„ ๋ˆŒ๋Ÿฌ ๊ฒฐ๊ณผ๋ฅผ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋Š” ๊ฒƒ ๊ฐ™์•˜๋‹ค.

 

 

โš™๏ธ /ping.html

 

<!doctype html>
<html>
  <head>
    <link rel="stylesheet" href="/static/css/bootstrap.min.css">
    <link rel="stylesheet" href="/static/css/bootstrap-theme.min.css">
    <link rel="stylesheet" href="/static/css/non-responsive.css">
    <title>ping | Dreamhack Ping Tester</title>
    
  

  </head>
<body>

    <!-- Fixed navbar -->
    <nav class="navbar navbar-default navbar-fixed-top">
      <div class="container">
        <div class="navbar-header">
	<a class="navbar-brand" href="/">Home</a>
        </div>
        <div id="navbar">
          <ul class="nav navbar-nav">
            <li><a href="/ping">Ping</a></li>
          </ul>

        </div><!--/.nav-collapse -->
      </div>
    </nav>

    <div class="container">
      
<h1>Let's ping your host</h1><br/>
<form method="POST">
  <div class="row">
    <div class="col-md-6 form-group">
      <label for="Host">Host</label>
      <input type="text" class="form-control" id="Host" placeholder="8.8.8.8" name="host" pattern="[A-Za-z0-9.]{5,20}" required>
    </div>
  </div>

  <button type="submit" class="btn btn-default">Ping!</button>
</form>

    </div> <!-- /container -->

    <!-- Bootstrap core JavaScript -->
    <script src="/static/js/jquery.min.js"></script>
    <script src="/static/js/bootstrap.min.js"></script> 
</body>
</html>

 

์ด ํŽ˜์ด์ง€์˜ ์†Œ์Šค ์ฝ”๋“œ์ด๋‹ค.

 

ํผ ๋ถ€๋ถ„์„ ์œ„์ฃผ๋กœ ๋ถ„์„ํ•ด๋ดค์„ ๋•Œ,

 

1. ์„œ๋ฒ„๋กœ ์ „์†ก๋  ๋•Œ "host"๋ผ๋Š” ์ด๋ฆ„์„ ์‚ฌ์šฉํ•˜์—ฌ ์ „์†กํ•œ๋‹ค.

2. ์•ŒํŒŒ๋ฒณ ๋Œ€์†Œ๋ฌธ์ž, ์ˆซ์ž, ๊ทธ๋ฆฌ๊ณ  ์ (.)์ด 5๋ถ€ํ„ฐ 20๊ฐœ๊นŒ์ง€ ํ—ˆ์šฉ๋œ๋‹ค.

3. required ์†์„ฑ์€ ์ด ํ•„๋“œ๊ฐ€ ๋น„์–ด์žˆ์œผ๋ฉด ์ œ์ถœ์ด ๋ถˆ๊ฐ€๋Šฅํ•˜๋„๋ก ๋งŒ๋“ ๋‹ค.

 

์ด ์ •๋„ ๋‚ด์šฉ์„ ์•Œ ์ˆ˜ ์žˆ์—ˆ๋‹ค.

 

 

 

"8.8.8.8"์„ ์ ์–ด์„œ ํ•‘ ํ…Œ์ŠคํŠธ๋ฅผ ํ•ด๋ณธ ๊ฒฐ๊ณผ ํ™”๋ฉด์ด๋‹ค.

์‹ค์ œ ping ๋ช…๋ น๊ณผ ๋™์ผํ•œ ๊ธฐ๋Šฅ์„ ์ˆ˜ํ–‰ํ•˜๊ณ , ๊ทธ ๊ฒฐ๊ณผ๋ฅผ ์ถœ๋ ฅํ•˜๋Š” ๊ฒƒ์„ ์•Œ ์ˆ˜ ์žˆ๋‹ค.

 

 

 

์ด ํŽ˜์ด์ง€์˜ ์†Œ์Šค ์ฝ”๋“œ๋ฅผ ๋ณด์•˜๋Š”๋ฐ ๋ณ„ ๊ฑด ์—†์—ˆ๋‹ค.

.

.

.

 

์ด์ œ ๋‹ค์šด๋กœ๋“œํ•œ ๋ฌธ์ œ ํŒŒ์ผ์„ ๋ถ„์„ํ•ด๋ณด๋„๋ก ํ•˜๊ฒ ๋‹ค.

 

 

# ๋‹ค์šด๋กœ๋“œํ•œ ๋ฌธ์ œ ํŒŒ์ผ ๋ถ„์„

 

 

โš™๏ธ app.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 "{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')


if __name__ == '__main__':
    APP.run(host='0.0.0.0', port=8000)

 

์ด app.py์—์„œ ์ฃผ๋ชฉํ• ๋งŒํ•œ ๋ถ€๋ถ„์€,

 

 

 

์ด ๋ถ€๋ถ„์ธ๋ฐ, ์ด ์ฝ”๋“œ๋ฅผ ํ†ตํ•ด {host} ๋ถ€๋ถ„์— ๋‚ด๊ฐ€ ์ž…๋ ฅํ•œ ๊ฐ’์ด ๋“ค์–ด๊ฐ„๋‹ค๋Š” ๊ฒƒ์„ ์•Œ ์ˆ˜ ์žˆ์—ˆ๋‹ค.

 

 

 


โญ โญ  [2] ๊ณต๊ฒฉ โญ โญ 

 

 

command injection ๊ณต๊ฒฉ์€ ์ฃผ๋กœ "๋ฉ”ํƒ€ ๋ฌธ์ž"๋ฅผ ์ด์šฉํ•˜์—ฌ ๋ฐœ์ƒ์ด ๋œ๋‹ค.

 

๊ทธ๋Ÿฌ๋‚˜ /ping.html์˜ pattern="[A-Za-z0-9.]{5,20}"๋ถ€๋ถ„์„ ๋ณด๋ฉด,

์•ŒํŒŒ๋ฒณ ๋Œ€๋ฌธ์ž, ์†Œ๋ฌธ์ž, 0-9๊นŒ์ง€์˜ ์ˆซ์ž ์ด์™ธ์˜ ๋ฌธ์ž๋Š” ํ•„ํ„ฐ๋ง์ด ๋จ์„ ์•Œ ์ˆ˜ ์žˆ๋‹ค.

 

์ฆ‰, ๋ฉ”ํƒ€ ๋ฌธ์ž๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์—†๋Š” ๊ฒƒ์ด๋‹ค.

 

 

 

๊ฐœ๋ฐœ์ž ๋„๊ตฌ๋ฅผ ์—ด์–ด์„œ ํ•„ํ„ฐ๋ง ๊ธฐ๋Šฅ์„ ๋งŒ๋“œ๋Š” ๋„ค๋ชจ ๋ฐ•์Šค ๋ถ€๋ถ„์„ ์ง€์šด ๋‹ค์Œ ๋ช…๋ น์–ด๋ฅผ ์ž…๋ ฅํ•ด๋ณด์•˜๋‹ค.

 

 

 

{host} ๋ถ€๋ถ„์— ๋‚ด๊ฐ€ ์ž…๋ ฅํ•œ ํ…์ŠคํŠธ๊ฐ€ ๋“ค์–ด๊ฐ€๋Š” ๊ฒƒ์„ ์ฐธ๊ณ ํ•˜์—ฌ 8.8.8.8";ls" ๋ฅผ ์ž…๋ ฅํ•˜๊ณ  ์ œ์ถœํ•œ ๊ฒฐ๊ณผ์ด๋‹ค.

 

์ด๋ ‡๊ฒŒ ๋˜๋ฉด, cmd = f'ping -c 3 "8.8.8.8";ls"' ์„ ์ž…๋ ฅํ•œ ๊ฒƒ์ด ๋œ๋‹ค.

 

ํ˜„์žฌ ๋””๋ ‰ํ† ๋ฆฌ์— flag.py๊ฐ€ ์กด์žฌํ•˜๋Š” ๊ฒƒ๊ณผ, ํ•„ํ„ฐ๋ง์„ ํ•ด์ฃผ๋Š” ์ฝ”๋“œ๋ฅผ ์ง€์šฐ๋ฉด ๊ณต๊ฒฉ์ด ๋œ๋‹ค๋Š” ์ ์„ ์ด์šฉํ•ด cat ๋ช…๋ น์–ด๋กœ flag.py ํŒŒ์ผ์„ ์ฝ์–ด์˜ฌ ๊ฒƒ์ด๋‹ค.

 

 

 

(ํ•‘์€ ์•„๋ฌด๊ฑฐ๋‚˜ ์ž…๋ ฅํ•ด๋„ ์ƒ๊ด€์—†๋‹ค.)

 

cat ๋ช…๋ น์–ด๋กœ flag.py์˜ ๋‚ด์šฉ์ธ ํ”Œ๋ž˜๊ทธ ๊ฐ’์„ ์ถœ๋ ฅํ•  ์ˆ˜ ์žˆ์—ˆ๋‹ค.

 

 

 

๋ฌธ์ œ ํ’€์ด ์™„๋ฃŒ!


 

[3] ์ฐธ๊ณ 

 

https://hobbylists.tistory.com/entry/%EB%93%9C%EB%A6%BC%ED%95%B5DreamHack-%EC%BB%A4%EB%A7%A8%EB%93%9C-%EC%9D%B8%EC%A0%9D%EC%85%98-1-Command-Injection-1%ED%92%80%EC%9D%B4

 

๋“œ๋ฆผํ•ต(DreamHack) - ์ปค๋งจ๋“œ ์ธ์ ์…˜ -1, Command Injection-1(ํ’€์ด)

ํ•ด๋‹น ๋ฌธ์ œ๋Š” ๋“œ๋ฆผํ•ต์˜ ์ปค๋งจ๋“œ ์ธ์ ์…˜-1๋ฒˆ ๋ฌธ์ œ์ž…๋‹ˆ๋‹ค. command injection-1 Command injection ๋ง ๊ทธ๋Œ€๋กœ ๋ช…๋ น์–ด ์‚ฝ์ž… ๊ณต๊ฒฉ์ธ ๊ฒƒ ๊ฐ™์€๋ฐ ์ด ๋ฌธ์ œ์—์„œ ํ•ด๋‹น ๊ธฐ๋ฒ•์„ ์ฒ˜์Œ ์ ‘ํ•ด๋ดค์Šต๋‹ˆ๋‹ค. SQL Injection ์ฒ˜๋Ÿผ ๋ช…๋ น

hobbylists.tistory.com

 

'SWLUG > ์›น ํ•ดํ‚น' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

[Dreamhack/๋“œ๋ฆผํ•ต] Web  (2) 2024.04.29
[webhacking.kr] Challange 44  (1) 2023.11.20
[Dreamhack/๋“œ๋ฆผํ•ต] Command Injection Advanced  (1) 2023.11.19
[Dreamhack/๋“œ๋ฆผํ•ต] baby-linux  (0) 2023.11.16
[Dreamhack/๋“œ๋ฆผํ•ต] csrf-2  (1) 2023.11.11