๐๏ธ Access
Admin์ KEY๊ฐ ํ์ํฉ๋๋ค! ์๋ง์ KEY๊ฐ์ ์ ๋ ฅํ์ฌ ํ๋๊ทธ๋ฅผ ํ๋ํ์ธ์.
ํ๋๊ทธ ํ์์ DH{...} ์ ๋๋ค.
๐พ Exploit Algorithm & Payload
> ./app.py
#!/usr/bin/env python3
import subprocess
from flask import Flask, request, render_template, redirect, url_for
import string
import os
import hashlib
app = Flask(__name__)
try:
FLAG = open("./flag.txt", "r").read()
except:
FLAG = "[**FLAG**]"
KEY = hashlib.md5(FLAG.encode()).hexdigest()
guest_key = hashlib.md5(b"guest").hexdigest()
# filtering
def filter_cmd(cmd):
alphabet = list(string.ascii_lowercase)
alphabet.extend([' '])
num = '0123456789'
alphabet.extend(num)
command_list = ['flag','cat','chmod','head','tail','less','awk','more','grep']
for c in command_list:
if c in cmd:
return True
for c in cmd:
if c not in alphabet:
return True
@app.route('/', methods=['GET', 'POST'])
def index():
# GET request
return render_template('index.html')
@app.route('/flag', methods=['POST'])
def flag():
# POST request
if request.method == 'POST':
key = request.form.get('key', '')
cmd = request.form.get('cmd_input', '')
if cmd == '' and key == KEY:
return render_template('flag.html', txt=FLAG)
elif cmd == '' and key == guest_key:
return render_template('guest.html', txt=f"guest key: {guest_key}")
if cmd != '' or key == KEY:
if not filter_cmd(cmd):
try:
output = subprocess.check_output(['/bin/sh', '-c', cmd], timeout=5)
return render_template('flag.html', txt=output.decode('utf-8'))
except subprocess.TimeoutExpired:
return render_template('flag.html', txt=f'Timeout! Your key: {KEY}')
except subprocess.CalledProcessError:
return render_template('flag.html', txt="Error!")
return render_template('flag.html')
else:
return redirect('/')
else:
return render_template('flag.html')
if __name__ == '__main__':
app.run(host='0.0.0.0', port=8000, debug=True)
> ./templates/base.html
<!doctype html>
<html>
<head>
<link rel="stylesheet" href="{{ url_for('static', filename='css/bootstrap.min.css') }}">
<link rel="stylesheet" href="{{ url_for('static', filename='css/bootstrap-theme.min.css') }}">
<link rel="stylesheet" href="{{ url_for('static', filename='css/non-responsive.css') }}">
<title>{% block title %}{% endblock %} | Dreamhack </title>
{% block head %}{% endblock %}
</head>
<body>
<!-- Fixed navbar -->
<nav class="navbar navbar-default navbar-fixed-top">
<div class="container">
<div class="navbar-header">
<a class="navbar-brand" href="/">BypassIF</a>
</div>
<div id="navbar">
<ul class="nav navbar-nav">
<li><a href="/">index page</a></li>
</ul>
</div><!--/.nav-collapse -->
</div>
</nav><br/><br/><br/>
<div class="container">
{% block content %}{% endblock %}
</div> <!-- /container -->
<!-- Bootstrap core JavaScript -->
<script src="{{ url_for('static', filename='js/jquery.min.js')}}"></script>
<script src="{{ url_for('static', filename='js/bootstrap.min.js') }}"></script>
</body>
</html>
> ./templates/index.html
{% extends "base.html" %}
{% block title %}Index{% endblock %}
{% block head %}
{{ super() }}
{% endblock %}
{% block content %}
<h1>hello dream</h1>
<form action="/flag" method="POST">
<div class="row">
<div class="col-md-6 form-group">
<br/><input type="text" class="form-control" placeholder="your key" name="key" pattern="[A-Za-z0-9\s]{2,35}" required>
</div>
</div>
<button type="submit" class="btn btn-default">Submit</button>
</form>
<br/><br/>
{% if txt %}
<pre>{{ txt }}</pre>
{% endif %}
{% endblock %}
> ./templates/flag.html
{% extends "base.html" %}
{% block title %}Flag{% endblock %}
{% block head %}
{{ super() }}
{% endblock %}
{% block content %}
<h1>hello admin</h1>
<form action="/flag" method="POST">
<div class="row">
<div class="col-md-6 form-group">
<br/><input type="text" class="form-control" placeholder="ls" name="cmd_input" required>
</div>
</div>
<button type="submit" class="btn btn-default">Submit</button>
</form>
<br/><br/>
{% if txt %}
<pre>{{ txt }}</pre>
{% endif %}
{% endblock %}
> ./templates/guest.html
{% extends "base.html" %}
{% block title %}Index{% endblock %}
{% block head %}
{{ super() }}
{% endblock %}
{% block content %}
<h1>hello guest</h1>
<br/><br/>
{% if txt %}
<pre>{{ txt }}</pre>
{% endif %}
{% endblock %}
...
#1
: '/' ํ์ด์ง์์ key๋ฅผ ์ ๋ ฅํ๊ณ ์ ์กํ๋ฉด ๋ฐ์ํ ๊ฒ์ผ๋ก ๋ณด์ธ๋ค.
#2
https://tophix.com/ko/development-tools/encrypt-text
: app.py์์ guest_key๊ฐ 'hashlib.md5(b"guest").hexdigest()'๋ค์๊ณผ ๊ฐ์ด ๋์ํจ์ ํ์ธํ๊ธฐ ๋๋ฌธ์ md5 ๋ณตํธํ ํด์ guest์ key ๊ฐ์ ํ์ธํ ์ ์๋ค.
: ์ฝ๋๋ฅผ ์ถ๊ฐ๋ก ํด์ํ๋ค๋ณด๋ฉด ํ๋ผ๋ฏธํฐ๊ฐ ๋๊ฐ์์ ์ ์ ์๋ค. (key, cmd_input)
#3
: /index ํ์ด์ง์์ /flag ํ์ด์ง๋ก ๋์ด๊ฐ ๋, ์ ๋ฌํ๋ ๊ฐ์ด key ๋ฟ์ด์์ง BurpSuite ๋๊ตฌ๋ฅผ ํ์ฉํ์ฌ ํจํท์ ๊ฐ๋ก์ฑ์ cmd_input๋ ์ ๋ฌํ ์ ์๊ฒ ์์ฑํ์๋ค.
: ๋ค์๊ณผ ๊ฐ์ด ๋ช ๋ น์ด๊ฐ ์ ๋์ํจ์ ํ์ธํ ์ ์๋ค.
: ๊ทธ๋ฌ๋ ํ์ผ์ ํ์ธํ ์ ์๋ ์ฐํ ๋ฐฉ๋ฒ์ ์ฐพ๊ธฐ ์ฝ์ง ์์๋ค.(์์์ง ์์์ง๋ ํ์คํ์ง ์๋ค.)
- ํํฐ๋ง: 'flag','cat','chmod','head','tail','less','awk','more','grep'
- ๊ธฐ๋ณธ์ ์ผ๋ก ์ํ๋ฒณ ์๋ฌธ์, ๊ณต๋ฐฑ ๋๋ ์ซ์๋ง ๊ฐ๋ฅ. ์ฆ, ๋ฌธ์๋ ์ฌ์ฉ ๋ถ๊ฐ.
- ๋ค๋ฅธ ์ฐํ ๋ฐฉ๋ฒ์ ํ์ฉํด๋ดค์ผ๋ ๋จนํ์ง ์์์.
ex) fold, tac, nl, rev, sort, diff, /b?n/c?t, ...
#4
...
if cmd != '' or key == KEY:
if not filter_cmd(cmd):
try:
output = subprocess.check_output(['/bin/sh', '-c', cmd], timeout=5)
return render_template('flag.html', txt=output.decode('utf-8'))
except subprocess.TimeoutExpired:
return render_template('flag.html', txt=f'Timeout! Your key: {KEY}')
except subprocess.CalledProcessError:
return render_template('flag.html', txt="Error!")
return render_template('flag.html')
...
: ์ฝ๋๋ฅผ ์์ธํ ํ์ธํ๋ ๋ค์๊ณผ ๊ฐ๋ค.
: cmd๊ฐ ํํฐ๋ง ํจ์์ ํด๋นํ๋ ๊ฒ์ด ์๋์ง ํ์ธํ๊ฒ ๋๋ค. ํต๊ณผ๋๋ค๋ฉด try๋ฌธ์ผ๋ก ์ด๋ํ๋ค.
: try๋ฌธ์์ cmd์์ ์์ฑํ๋ ์ ๋ช ๋ น์ด๊ฐ ์คํ๋๊ณ ์ถ๋ ฅ ๊ฒฐ๊ณผ๋ฅผ ๊ฐ์ ธ์ค๊ฒ ๋๋ค. ์ด๋, ๋ช ๋ น์ด ์คํ์ 5์ด ์ด์ ์ง์ฐ์ด ๋ฐ์ํ๋ฉด ์๋ฌ๋ฌธ์ ๋ฐ์ํ๋ ๊ฒ์ ํ์ธํ ์ ์๋ค.
: except subprocess.TimeoutExpired๋ timeout ์๋ฌ๊ฐ ๋ฐ์ํ๋ฉด flag.html ํ์ด์ง๋ฅผ ๋ ๋๋งํ๊ณ KEY(Admin์ KEY)๋ฅผ ์ ๋ฌํจ์ ์ ์ ์๋ค.
: ๋ฐ๋ผ์ ์ง์ฐ์ํค๋ฉด Admin์ KEY๋ฅผ ์ป์ ์ ์์ ๊ฒ์ด๋ค.
๐Analysis and results for obtaining the Flag DH{…}
'[Dreamhack]WebHacking > Wargame&CTF' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[Dreamhack] Level1: [wargame.kr] strcmp (1) | 2024.02.25 |
---|---|
[Dreamhack] Level2: login-1 (2) | 2024.02.25 |
[Dreamhack] Level2: baby-sqlite (0) | 2024.02.23 |
[Dreamhack] Level4: KeyCat (0) | 2024.02.23 |
[Dreamhack] Level1:Beginner blue-whale (1) | 2024.02.07 |