Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[정민성/NaraDoSeong]: nginx CVE-2013-4547 분석 코드 및 결과 #186

Open
wants to merge 17 commits into
base: main
Choose a base branch
from
Open
37 changes: 37 additions & 0 deletions Nginx/CVE-2013-4547/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# Nginx 파일 이름 로직 취약점(CVE-2013-4547)



### 요약

- 영향을 받는 버전 Nginx 0.8.41 ~ 1.4.3 / 1.5.0 ~ 1.5.7
- Nginx 정규식은 \0을 만나면 중지함.
- 하지만 \0앞에 공백을 추가하면 Nginx가 \0을 검사하지 않음

<br/>

### 환경 구성 및 실행

- `docker compose up -d`를 실행하여 테스트 환경을 실행
- `http://your-ip:8070/`에 접속하여 Nginx 기본 페이지를 확인
- 이 페이지는 실제로 8070 포트의 내용을 리버스 프록시로 전달
- `exploit.jpg` php기능파일인데 jpg확장자를 가지는 파일
- 업로드된 위치/exploit.jpg.php에서 헤더값을 수정하여 php파일 실행 시킬 수 있었습니다.

<br/>
<img width="567" alt="image" src="https://github.com/NaraDoSeong/kr-vulhub/assets/64023668/93a44445-65f0-460f-9527-c3e99f35fa97">

- Burp Suite를 사용하여 filename = exploit.jpg에 공백을 넣음
<br/>
<img width="333" alt="image" src="https://github.com/NaraDoSeong/kr-vulhub/assets/64023668/814ee6fe-1ec4-482e-8c40-2557e4d61157">

- exploit.jpg.php를 GET할때 g(67)와 .(2e)사이에 20 00바이트 삽입 \0문자
<br/>

### 결과
<img width="354" alt="image" src="https://github.com/NaraDoSeong/kr-vulhub/assets/64023668/3f0ddf8b-364b-4ba4-bd74-ca5629c78b75">
<br/>

### 정리
이 취약점은 서버에서 php파일 업로드를 제한하였을경우 jpg파일로 위장하여 php파일 업로드 후 해당 업로드 위치에서 php파일을 동작시키는 위험이 있습니다.
안전한 웹 서비스 운영을 위해 업로드 위치를 비공개 및 해당 취약점이 패치된 최신 NginX를 사용하여야 합니다.
14 changes: 14 additions & 0 deletions Nginx/CVE-2013-4547/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
version: '2'
services:
nginx:
image: vulhub/nginx:1.4.2
volumes:
- ./nginx.conf:/usr/local/nginx/conf/nginx.conf
- ./www:/usr/local/nginx/html
- ./flagA:/etc/flagA
ports:
- "8070:80"
php:
build: ./php-fpm/
volumes:
- ./www:/var/www/html
7 changes: 7 additions & 0 deletions Nginx/CVE-2013-4547/exploit.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions Nginx/CVE-2013-4547/flagA
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
securitybypass
33 changes: 33 additions & 0 deletions Nginx/CVE-2013-4547/nginx.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
worker_processes 1;

events {
worker_connections 1024;
}


http {
include mime.types;
default_type application/octet-stream;

sendfile on;
keepalive_timeout 65;

server {
listen 80;
server_name localhost;
root html;
index index.php;

charset utf-8;

location ~ \.php$ {
root html;
include fastcgi_params;

fastcgi_pass php:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /var/www/html$fastcgi_script_name;
fastcgi_param DOCUMENT_ROOT /var/www/html;
}
}
}
18 changes: 18 additions & 0 deletions Nginx/CVE-2013-4547/php-fpm/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
FROM vulhub/php:5-fpm

MAINTAINER phithon <[email protected]>

RUN rm -rf /var/www/html/* \
&& mkdir -p /var/www/html/uploadfiles \
&& chmod 777 /var/www/html/uploadfiles \
&& { \
echo "#\!/bin/bash"; \
echo "chmod 0777 /var/www/html/uploadfiles"; \
echo "/usr/local/sbin/php-fpm"; \
echo ; \
} | tee /start.sh \
&& chmod +x /start.sh

COPY www.conf /usr/local/etc/php-fpm.d/www-2.conf

CMD ["/start.sh"]
3 changes: 3 additions & 0 deletions Nginx/CVE-2013-4547/php-fpm/www.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[www]
security.limit_extensions =
php_admin_flag[cgi.fix_pathinfo] = off
33 changes: 33 additions & 0 deletions Nginx/CVE-2013-4547/www/index.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php
if (!empty($_FILES)):

// Check for errors
if($_FILES['file_upload']['error'] > 0){
die('An error ocurred when uploading.');
}

// Check filesize
if(!is_uploaded_file($_FILES['file_upload']['tmp_name'])) {
die('File is not uploaded file');
}

$ext = pathinfo($_FILES['file_upload']['name'], PATHINFO_EXTENSION);
if (empty($ext) || in_array($ext, ['php', 'php3', 'php5', 'phtml'])) {
die('Unsupported filetype uploaded.');
}

$new_name = __DIR__ . '/uploadfiles/' . $_FILES['file_upload']['name'];
if(!move_uploaded_file($_FILES['file_upload']['tmp_name'], $new_name)){
die('Error uploading file - check destination is writeable.');
}

die('File uploaded successfully: ' . $new_name);

else:
?>
<form method="post" enctype="multipart/form-data">
File: <input type="file" name="file_upload">
<input type="submit">
</form>
<?php
endif;
1 change: 1 addition & 0 deletions Nginx/CVE-2013-4547/www/uploadfiles/.gitkeep
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@