
1. 문제 정보
- 문제 번호 : old -51
- 문제 점수 : 25점
2. 문제 분석

문제에 들어가면 admin page라고 나오고 id와 pw를 입력받는다.
페이지 소스보기를 통해서 소스를 보면 id에는 addslash, pw에는 md5처리를 하고 데이터베이스에 입력한다.
<?php
include "../../config.php";
if($_GET['view_source']) view_source();
?><html>
<head>
<title>Challenge 51</title>
<style>
table{ color:lightgreen;}
</style>
</head>
<body bgcolor=black><br><br>
<font color=silver>
<center><h1>Admin page</h1></center>
</font>
<?php
if($_POST['id'] && $_POST['pw']){
$db = dbconnect();
$input_id = addslashes($_POST['id']);
$input_pw = md5($_POST['pw'],true);
$result = mysqli_fetch_array(mysqli_query($db,"select id from chall51 where id='{$input_id}' and pw='{$input_pw}'"));
if($result['id']) solve(51);
if(!$result['id']) echo "<center><font color=green><h1>Wrong</h1></font></center>";
}
?>
<br><br><br>
<form method=post>
<table border=0 align=center bgcolor=gray width=200 height=100>
<tr align=center><td>ID</td><td><input type=text name=id></td></tr>
<tr align=center><td>PW</td><td><input type=password name=pw></td></tr>
<tr><td colspan=2 align=center><input type=submit></td></tr>
</table>
<font color=silver>
<div align=right><br>.<br>.<br>.<br>.<br><a href=./?view_source=1>view-source</a></div>
</font>
</form>
</body>
</html>
3. 풀이
만약 데이터베이스 설정이 멀티바이트 문자셋을 사용한다면 %aa'를 사용해서 %aa%5c%27에서 %aa%5c'를 GBK 인코딩 환경이라면 窶 (구)라는 한자로 바꿔 addslash를 우회할 수 있다,
하지만 입력을 하면 안된다.


그러면 md5($_POST['pw'],true); 함수를 봐보자 md5는 해쉬 함수이다.
md5(string $string, bool $binary = false): string
함수의 정의로 두번째 인자인 바이너리 옵션을 true로 하면 16bit 바이너리 형태로 반환한다.
pw값으로 129581926211651571912466741651878684928 를 입력하면
b"\x06\xdaT0D\x9f\x8fo#\xdf\xc1'or'8"가 되어서 sql이 항상 참으로 성공하게 된다.
import hashlib
data = "129581926211651571912466741651878684928"
binary_result = hashlib.md5(data.encode()).digest()
# 1. 실제 바이너리 데이터 출력 (깨져 보일 수 있음)
print("Binary Output:", binary_result)
# 2. 16진수로 변환해서 보기 (검증용)
print("Hex Output:", binary_result.hex())
파이썬 코드로 확인해보면 아래처럼 나온다.


4. 요약
- 서버가 멀티바이트 문자셋을 사용하지 않아 ID 필드의 addslashes 우회 공격은 불가능함을 확인하고 PW 필드의 md5(Raw Binary) 취약점으로 대상을 변경했다.
- md5 함수의 결과인 바이너리 데이터가 SQL 쿼리에 그대로 삽입될 때 인젝션 구문이 형성되는 원리를 이용했다.
- 해싱 후 'or'8 패턴이 생성되는 값을 입력하여 쿼리문을 항상 참으로 조작함으로써 관리자 인증에 성공했다.
'워게임 > webhacking.kr' 카테고리의 다른 글
| [webhacking.kr] old-10 문제 (0) | 2026.01.30 |
|---|---|
| [webhacking.kr] old-12 문제 (0) | 2026.01.30 |
| [webhacking.kr] old-43 문제 (0) | 2026.01.30 |
| [webhacking.kr] old-19 문제 (0) | 2026.01.29 |
| [webhacking.kr] old-1문제 (0) | 2026.01.27 |