NShiftKey-Rule-Guide logo NShiftKey-Rule-Guide

1. Vulnerability Description

<Figure 1. Normal Scenario>

<Figure 2. Attack Scenario>

(1) Upload files using procedures of file I/O + WebShell

(2) Reverse connection by using shell exeacution

<Figure 3. Showing the attack and the Business impact of SQLi (OWASP 2013)>

Case of personal information breaches

- Marriott International(stolen data on approximately 500 million customers)
- eBay (exposed its entire account list of 145 million users)
- Equifax(exposed about 147.9 million consumers)
- Sina Weibo(538 million accounts are impacted)
- Not well-known, but big and small accidents frequently occur.
- Internal Cases: data breaches via the Manager page of a service

2. How to check vulnerability

2.1. Line-Comments

Comment syntax MySQL MSSQL Oracle
-- comment YES YES YES
# comment YES NO NO
/* comment */ YES YES YES

<Table 1. Comment syntax by DBMS>

2.2. Error-Base

2.3. Boolean

2.4. Interger Based

3. Vulnerability Countermeasure

3.1. Use Prepared Statements

3.2. Use Stored Procedure

3.3. Exposure common error page when DBMS error occur

3.4. Input Validation(Logic & Filtering & escape)

Filtering characters

--, #, \@, \@@, /*, */, table, sys, char, varchar, nvarchar, create, declare, alter, exec, insert, delete, drop, end, sys, table, update

4. Sample Code

4.1. Using Prepared Statements

String custname = request.getparameter("customerName");
String query = "SELECT account_balance FROM user_data WHERE user_name = ?";
PreparedStatement pstmt = connection.prepareStatement(query);
pstmt.setString(1, custname);
ResultSet results = pstmt.executeQuery();
   <sqlMap namespace="Student">
   ...
   <delete id="delStudent" parameterClass="Student">
   DELETE STUDENTS
   WHERE Name = #name#
   <mapper namespace="Student">
   ...
   <delete id="delStudent" parameterClass="Student">
  DELETE STUDENTS
  WHERE Name = '#{name}'

4.2. Using Stored Procedure

String custname = request.getParameter("customerName");
try {
   CallableStatement cs = connection.prepareCall("{call sp_getAccountBalance(?)}");
   cs.setString(1, custname);
   ResultSet results = cs.executeQuery();     
} catch (SQLException se) {           
   // … logging and error handling
}