촉촉한초코칩

[Webhacking.kr] old-39 본문

Study/Web Hacking

[Webhacking.kr] old-39

햄친구베이컨 2025. 2. 25. 21:03

<?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>

 

id에 \\가 들어가면 아예 없어지는 걸로? 대체되고 '는 ''로 대체된다. 길이는 0~14글자로 한정된다. 

select문은 다음과 같다. 
select 1 from member where length(id)<14 and id='{$_POST['id']}
이 구문이 있어야 통과된다.
(여기서 1은 그냥 반환하는 값인 것 같다..)

 

풀이 찾아보다가..ㅎㅎ

mysql에서는 'a' = 'a '의 결과가 1로 나온다. https://techblog.woowahan.com/2559/

char에서는 문자열을 비교할 때 공백을 채워서 비교한다. 
짧은 쪽 끝에 공백을 추가하여 2개의 데이터가 같은 길이가 되도록 한다. 그리고 앞에서부터 한 문자씩 비교한다. 

varchar은 맨 처음부터 한 문자씩 비교하고 공백도 하나의 문자로 취급하여 끝 공백이 다르면 다른 문자로 인식한다. 

그렇다면 문자열 길이는 15니까, admin을 쓰고 맨 마지막 14번째에는 '를 쓰면 id=admin이 맞춰져서 true가 된다. 

 

admin을 입력하면 단순히 admin 값으로 처리하는데, 

admin   '을 입력하면 SQL에서 의도하지 않은 동작을 실행하게 해서..? 통과가 되는 것 같다.. 

 

아무튼 뒤에 공백이 추가되어도 같은 문자열로 인식한다는 점..!

'Study > Web Hacking' 카테고리의 다른 글

[Webhacking.kr] old-16  (0) 2025.02.25
[Webhacking.kr] old-26  (0) 2025.02.25
Webhacking.kr old-38  (0) 2024.11.08
Webhacking.kr - old-15  (1) 2024.11.08
Webhaking.kr old-18  (1) 2024.11.08