촉촉한초코칩
Dreamhack - login filtering 본문
코드
<?php
if (isset($_GET['view-source'])) {
show_source(__FILE__);
exit();
}
/*
create table user(
idx int auto_increment primary key,
id char(32),
ps char(32)
);
*/
if(isset($_POST['id']) && isset($_POST['ps'])){
include("./lib.php"); # include for $FLAG, $DB_username, $DB_password.
$conn = mysqli_connect("localhost", $DB_username, $DB_password, "login_filtering");
mysqli_query($conn, "set names utf8");
$id = mysqli_real_escape_string($conn, trim($_POST['id']));
$ps = mysqli_real_escape_string($conn, trim($_POST['ps']));
$row=mysqli_fetch_array(mysqli_query($conn, "select * from user where id='$id' and ps=md5('$ps')"));
if(isset($row['id'])){
if($id=='guest' || $id=='blueh4g'){
echo "your account is blocked";
}else{
echo "login ok"."<br />";
echo "FLAG : ".$FLAG;
}
}else{
echo "wrong..";
}
}
?>
<!DOCTYPE html>
<style>
* {margin:0; padding:0;}
body {background-color:#ddd;}
#mdiv {width:200px; text-align:center; margin:50px auto;}
input[type=text],input[type=[password] {width:100px;}
td {text-align:center;}
</style>
<body>
<form method="post" action="./">
<div id="mdiv">
<table>
<tr><td>ID</td><td><input type="text" name="id" /></td></tr>
<tr><td>PW</td><td><input type="password" name="ps" /></td></tr>
<tr><td colspan="2"><input type="submit" value="login" /></td></tr>
</table>
<div><a href='?view-source'>get source</a></div>
</form>
</div>
</body>
<!--
you have blocked accounts.
guest / guest
blueh4g / blueh4g1234ps
-->
아이디랑 패스워드 입력 받음
id는 null값이 아니여야 하고, guest/blueh4g를 제외한 값을 찾아야 한다.
select문을 리턴해주는 함수는 mysqli_fetch_array() 함수를 사용하고 있는데, 이 함수는 한번에 한 개의 데이터 행을 배열 형태로 가져온다. 더이상 배열의 형태로 반환할 데이터가 없을 때까지 true를 반환하여 조건문을 이용하면 모든 배열 값을 호출할 수 있다.
공격
sql 취약점을 이용해서 id에 or 조건문을 넣어서 뒤에는 주석처리 되게 하려고 했지만 → 실패
a' or 1=1-- |
문제에 bypass filtering이라고 해서 sql bypass filtering에 대해 알아봄 (https://hg2lee.tistory.com/entry/SQL-Injection-filtering-Bypass) → 실패
싱글쿼터 우회 방법 1. "" 사용 select * from users where id='' or 1=1# 2. \ 사용 -> 앞 문자를 문자 그 자체로 인식 이를 통해 패스워드를 md5인 해시값으로 저장하는 행위를 우회할 수 있음 select * from users whers id ='\' and pw =md5(") --> select * from users where id='' |
select문에서 id 값이 테이블에 없다면 wrong이 나오고 있다...
그러면 select문에서 모든 값이 다 출력될 수 있도록 where 조건을 만족시켜야 하는데......
그래서 or 조건도 쓴 건데 잘 안됨..
결국 검색했는데..
php에서는 대소문자 구분을 안 해서 guest를 Guest로 쓰면 flag가 나온다....
'Study > Web Hacking' 카테고리의 다른 글
wargame.kr type confusion (0) | 2024.09.13 |
---|---|
wargamme.kr - tmitter (1) | 2024.09.13 |
Dreamhack - baby-union (0) | 2024.08.25 |
Dreamhack - command-injection-chatgpt (0) | 2024.08.25 |
Dreamhack - error based sql injection (0) | 2024.08.15 |