저장소

[webhacking.kr] 39번 풀이 본문

IT/웹 해킹

[webhacking.kr] 39번 풀이

huiu 2021. 8. 23. 12:01

https://webhacking.kr/

 

Webhacking.kr

Index Welcome Stranger! Chatting Notice(en) [2021-04-16] Score of old challenges has been adjusted to 1/10. For a while now, new challenges will be added "without" notice. [2020-08-12] Replace chat feature with Discord. Please do not cause inconvenience to

webhacking.kr

39번 문제를 풀기 시작하면 가장 먼저 볼 수 있는 화면이다. view-source를 눌러서 소스코드를 확인해보면 아래와 같다.

<?php
  include "../../config.php";
  if($_GET['view_source']) view_source();
?><html>
<head>
<title>Chellenge 39</title>
</head>
<body>
<?php
  $db = dbconnect();
  if($_POST['id']){
    $_POST['id'] = str_replace("\\","",$_POST['id']);
    $_POST['id'] = str_replace("'","''",$_POST['id']);
    $_POST['id'] = substr($_POST['id'],0,15);
    $result = mysqli_fetch_array(mysqli_query($db,"select 1 from member where length(id)<14 and id='{$_POST['id']}"));
    if($result[0] == 1){
      solve(39);
    }
  }
?>
<form method=post action=index.php>
<input type=text name=id maxlength=15 size=30>
<input type=submit>
</form>
<a href=?view_source=1>view-source</a>
</body>
</html>

소스코드에서 알 수 있는 것은

  1. \\를 빈문자열로 바꿔버리고 '를 ' '로 바꾼다.
  2. id 값에서 인덱스 0부터 14번째까지의 문자열을 추출해온다. (15자)
  3. id 값은 최대 15자 입력할 수 있다.

실제로 입력 폼에 15자 초과 입력을 시도해보았지만 더이상 입력이 되지 않았다.

$result = mysqli_fetch_array(mysqli_query($db,"select 1 from member where length(id)<14 and id='{$_POST['id']}"));

다시 소스코드를 확인하자 id를 받는 부분의 따옴표 쌍이 존재하지 않음을 알게 되었다.

SQL문은 따옴표(') 쌍을 잘 맞춰주어야 하므로 admin'과 같이 입력되어야 할 것이다.

$_POST['id'] = str_replace("'","''",$_POST['id']);
$_POST['id'] = substr($_POST['id'],0,15);

여기서 '를 ' '로 바꾸는 작업을 수행하고 있음에 주의해야한다.

또한, 15자까지만 입력을 받아오고 있으므로 아래와 같이 마지막 15번 째 문자를 따옴표로 주었다.

=> admin         '

 

이렇게 된다면 admin         ' '으로 입력되겠지만 php가 15자까지만 입력을 받아오도록 되어있으므로 admin         '와 같이 전송된다. SQL문은 단순한 공백을 무시하므로 admin'을 입력한 것과 같은 결과가 나온다.

 

 

'IT > 웹 해킹' 카테고리의 다른 글

[webhacking.kr] 24번 풀이  (0) 2021.08.27
[webhacking.kr] 12번 풀이  (0) 2021.08.23
[webhacking.kr] 35번 풀이  (0) 2021.08.20
[webhacking.kr] 3번 풀이  (0) 2021.08.20
[webhacking.kr] 54번 풀이  (0) 2021.08.13
Comments