old-37 -- Router Port Forwarding

URL: https://webhacking.kr/challenge/web-18/arrow-up-righthttps://webhacking.kr/challenge/web-18/?view_source=1arrow-up-right

old-37.png
<?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>

The challenge is a bit tricky, you are supposed to open a port on your server and catch the connection that way. Since I don't have server I did port forwarding via router.

First setup firewall rule to allow connections to this port.

old-37-1.png

Router Configuration Page > Advanced Setup > NAT > Virtual Server

old-37-2.png

Note: This may only be specific to CABSAT router.

The file which your host is being read from is created by current timestamp value, current as in when request was made. Since server time may not be 1:1 add some wiggle room by 5 seconds and hopefully catch the request.

Make sure to cleanup!

Last updated