[Natas] Level 10 > Level 11

다음은 natas11의 페이지이다.
#FF0000(빨강)을 입력해주었더니 뒷배경이 빨간색이 되는 것을 확인할 수 있었다.

먼저 View sourcecode를 눌러 소스코드를 확인해주었다.
다음은 전체 소스코드이다.
<html>
<head>
<!-- This stuff in the header has nothing to do with the level -->
<link rel="stylesheet" type="text/css" href="http://natas.labs.overthewire.org/css/level.css">
<link rel="stylesheet" href="http://natas.labs.overthewire.org/css/jquery-ui.css" />
<link rel="stylesheet" href="http://natas.labs.overthewire.org/css/wechall.css" />
<script src="http://natas.labs.overthewire.org/js/jquery-1.9.1.js"></script>
<script src="http://natas.labs.overthewire.org/js/jquery-ui.js"></script>
<script src=http://natas.labs.overthewire.org/js/wechall-data.js></script><script src="http://natas.labs.overthewire.org/js/wechall.js"></script>
<script>var wechallinfo = { "level": "natas11", "pass": "<censored>" };</script></head>
<?
$defaultdata = array( "showpassword"=>"no", "bgcolor"=>"#ffffff");
function xor_encrypt($in) {
$key = '<censored>';
$text = $in;
$outText = '';
// Iterate through each character
for($i=0;$i<strlen($text);$i++) {
$outText .= $text[$i] ^ $key[$i % strlen($key)];
}
return $outText;
}
function loadData($def) {
global $_COOKIE;
$mydata = $def;
if(array_key_exists("data", $_COOKIE)) {
$tempdata = json_decode(xor_encrypt(base64_decode($_COOKIE["data"])), true);
if(is_array($tempdata) && array_key_exists("showpassword", $tempdata) && array_key_exists("bgcolor", $tempdata)) {
if (preg_match('/^#(?:[a-f\d]{6})$/i', $tempdata['bgcolor'])) {
$mydata['showpassword'] = $tempdata['showpassword'];
$mydata['bgcolor'] = $tempdata['bgcolor'];
}
}
}
return $mydata;
}
function saveData($d) {
setcookie("data", base64_encode(xor_encrypt(json_encode($d))));
}
$data = loadData($defaultdata);
if(array_key_exists("bgcolor",$_REQUEST)) {
if (preg_match('/^#(?:[a-f\d]{6})$/i', $_REQUEST['bgcolor'])) {
$data['bgcolor'] = $_REQUEST['bgcolor'];
}
}
saveData($data);
?>
<h1>natas11</h1>
<div id="content">
<body style="background: <?=$data['bgcolor']?>;">
Cookies are protected with XOR encryption<br/><br/>
<?
if($data["showpassword"] == "yes") {
print "The password for natas12 is <censored><br>";
}
?>
<form>
Background color: <input name=bgcolor value="<?=$data['bgcolor']?>">
<input type=submit value="Set color">
</form>
<div id="viewsource"><a href="index-source.html">View sourcecode</a></div>
</div>
</body>
</html>
- xor_encrypt 함수 : 두 문자열을 XOR연산
- loadData 함수 : 쿠키 값의 데이터를 디코드하고 변경된 점을 저장
- saveData 함수 : 다시 암호화하여 쿠키 저장 (json_encode -> xor_encrypt -> base64_encode 순으로 진행)
<h1>natas11</h1>
<div id="content">
<body style="background: <?=$data['bgcolor']?>;">
Cookies are protected with XOR encryption<br/><br/>
<?
if($data["showpassword"] == "yes") {
print "The password for natas12 is <censored><br>";
}
?>
이 부분에서 볼 수 있듯이 $data["showpassword"]가 "yes"일 때, 비밀번호를 획득할 수 있다.
$defaultdata = array( "showpassword"=>"no", "bgcolor"=>"#ffffff");
showpassword의 초기값은 no이고, bgcolor의 초기값은 #ffffff이다.

cookie값 : ClVLIh4ASCsCBE8lAxMacFMZV2hdVVotEhhUJQNVAmhSEV4sFxFeaAw
cookie값을 이용하여 다음과 같이 php코드를 작성해주었다.
function xor_encrypt를 활용하여 작성해주었다.
base64_decode()의 반환값인 text와 json_encode의 반환값인 outText를 ^(xor)연산해주도록 하였따.
<!DOCTYPE html>
<html>
<body>
<?php
$text = base64_decode("ClVLIh4ASCsCBE8lAxMacFMZV2hdVVotEhhUJQNVAmhSEV4sFxFeaAw");
$defaultdata=array("showpassword"=>"no", "bgcolor"=>"#ffffff");
$outText=json_encode($defaultdata);
$key='';
// Iterate through each character
for($i=0;$i<strlen($text);$i++) {
$key .= $outText[$i] ^ $text[$i % strlen($text)];
}
echo $key;
?>
</body>
</html>

이를 입력해주었더니 사진에서 볼 수 있들시 "qw8J"가 반복하여 출력되고 있다.
이 값을 이용하여 새로운 cookie값을 만들 수 있다.
outText에 json_encode반환값과 구한 key값(qw8J)를 xor연산해주었으며, base64_encode를 적용해주었다.
<!DOCTYPE html>
<html>
<body>
<?php
$defaultdata=array("showpassword"=>"yes", "bgcolor"=>"#ffffff");
$key='qw8J';
$text=json_encode($defaultdata);
$outText='';
for($i=0;$i<strlen($text);$i++) {
$outText .= $text[$i] ^ $key[$i % strlen($key)];
}
$outText=base64_encode($outText);
echo $outText;
?>
</body>
</html>
출력값으로 새로운 쿠키값이 생성되었다.
ClVLIh4ASCsCBE8lAxMacFMOXTlTWxooFhRXJh4FGnBTVF4sFxFeLFMK
쿠키값을 바꿔주었더니 아래와 같이 다음단계의 비밀번호를 얻을 수 있었다.
