목표 : 코드 해석 및 포트포워딩
파일을 업로드 및 제출할 수 있게 돼있고 뭔가 제출된 파일들의 이름같이 보이는 임시명들이 있다.
<?php
include "../../config.php";
if($_GET['view_source']) view_source();
?><html>
<head>
<title>Challenge 37</title>
</head>
<body>
<?php
$db = dbconnect();
$query = "select flag from challenge where idx=37";
$flag = mysqli_fetch_array(mysqli_query($db,$query))['flag'];
$time = time();
$p = fopen("./tmp/tmp-{$time}","w");
fwrite($p,"127.0.0.1");
fclose($p);
$file_nm = $_FILES['upfile']['name'];
$file_nm = str_replace("<","",$file_nm);
$file_nm = str_replace(">","",$file_nm);
$file_nm = str_replace(".","",$file_nm);
$file_nm = str_replace("/","",$file_nm);
$file_nm = str_replace(" ","",$file_nm);
if($file_nm){
$p = fopen("./tmp/{$file_nm}","w");
fwrite($p,$_SERVER['REMOTE_ADDR']);
fclose($p);
}
echo "<pre>";
$dirList = scandir("./tmp");
for($i=0;$i<=count($dirList);$i++){
echo "{$dirList[$i]}\n";
}
echo "</pre>";
$host = file_get_contents("tmp/tmp-{$time}");
$request = "GET /?{$flag} HTTP/1.0\r\n";
$request .= "Host: {$host}\r\n";
$request .= "\r\n";
$socket = fsockopen($host,7777,$errstr,$errno,1);
fputs($socket,$request);
fclose($socket);
if(count($dirList) > 20) system("rm -rf ./tmp/*");
?>
<form method=post enctype="multipart/form-data" action=index.php>
<input type=file name=upfile><input type=submit>
</form>
<a href=./?view_source=1>view-source</a>
</body>
</html>
PHP
복사
차근차근 보자
$db = dbconnect();
$query = "select flag from challenge where idx=37";
$flag = mysqli_fetch_array(mysqli_query($db,$query))['flag'];
$time = time();
$p = fopen("./tmp/tmp-{$time}","w");
fwrite($p,"127.0.0.1");
fclose($p);
SQL
복사
flag값과 time값을 가져오고 이를 tmp-time이라는 이름 파일을 만들어 그곳에 127.0.0.1을 쓴다.
$file_nm = $_FILES['upfile']['name'];
$file_nm = str_replace("<","",$file_nm);
$file_nm = str_replace(">","",$file_nm);
$file_nm = str_replace(".","",$file_nm);
$file_nm = str_replace("/","",$file_nm);
$file_nm = str_replace(" ","",$file_nm);
SQL
복사
업로드된 파일 이름을 불러오고 < > . / 공백 을 지워서 저장한다.
echo "<pre>";
$dirList = scandir("./tmp");
for($i=0;$i<=count($dirList);$i++){
echo "{$dirList[$i]}\n";
}
echo "</pre>";
SQL
복사
파일 목록을 출력한다.
$host = file_get_contents("tmp/tmp-{$time}");
$request = "GET /?{$flag} HTTP/1.0\r\n";
$request .= "Host: {$host}\r\n";
$request .= "\r\n";
$socket = fsockopen($host,7777,$errstr,$errno,1);
fputs($socket,$request);
fclose($socket);
SQL
복사
현재 시각에 맞는 tmp 파일에서 저장된 IP(원래는 127.0.0.1)를 host에 불러오고, 이를 소켓 통신으로 7777 포트에 flag값을 전달한다.
즉 내가 7777 포트를 열어두고서 내 ip 정보를 담은 파일을 업로드하는데, 파일 이름에 시간 값만 잘 맞춰놓으면 된다.
포트포워딩을 받을 수 있게 공유기 서버 설정을 다시 해놓고
타이밍에 맞춰 파일을 미리 업로드해놓고 (167970030). 새로고침하며 기존 파일을 덮어씌웠다.
플래그를 획득했다.