290 likes | 576 Views
Code Injection. Cable Johnson. Overview Common Injection Types Sanitization Techniques Developer Prevention. Code Injection. “username” stored as string constant. I nsert source code into existing application Single command Entire s cript Used by worms to propagate . Overview.
E N D
Code Injection Cable Johnson
Overview • Common Injection Types • Sanitization Techniques • Developer Prevention Code Injection
Insert source code into existing application • Single command • Entire script • Used by worms to propagate Overview
SQL injection • Web injection/XSS • Shell injection Common Injection Types
Infiltrate database • Dump data, alter data • Done at database level • Easily Automated • Attempted constantly • Average: 71 attempts/hr • Peak: 800-1300 attempts/hr SQL Injection
SQL: database level • XSS: web level • PHP/ASP injection: server infiltration • HTML/Script injection: browser infiltration Web
Targets machine rather than db or webpage • Done at shell (command line) level • Windows and UNIX • Typically used to escalate privileges Shell Injection
Design • Input sanatization Prevention
Blacklisting • Minimize use of user input • Limit database use • Disable unnecessary database functionality Design
Character exclusion • Signature exclusion • Prepared statements Sanitization
( ‘ ), ( \ ), ( ` ), require alphanumeric only • Easy to implement • Easily recognizable Character Exclusion
UNION SELECT • OR 1=1 • EXEC SP_ (or EXEC XP_) • False positives come with large signature sets • Easily avoidable Signature Exclusion
OR 1 = 1 • OR ‘str’ = ‘str’ • OR ‘str’ = ‘st’+’r’ • OR ‘str’ = N’str’ • OR ‘s’ IN (‘str’) • O/**/R ‘s’ < ‘z’ • Unreasonable to keep signatures for countless possible inputs Signature Weakness
Efficient method of sanatization • Also a query optimization • Build the sql statement with minimal syntax • Run partial query (“prepare”) • Fill in user input after preparation Prepared Statements
username = input() password = input() sql = “SELECT * FROM users WHERE username=$1 AND password=$2” statement = db.prepare(sql) statement.execute(username, password) Pseudo Code
Seth • Amanda • George Bad Sanatization
function checkForBadSql($sqlcode) • { • global $CONTEXT, $ERROR_TEXT; • $badSqlCode[] = 'create'; • $badSqlCode[] = 'database'; • $badSqlCode[] = 'table'; • $badSqlCode[] = 'insert'; • $badSqlCode[] = 'update'; • $badSqlCode[] = 'rename'; • $badSqlCode[] = 'replace'; • $badSqlCode[] = 'select'; • $badSqlCode[] = 'handler'; • $badSqlCode[] = 'delete'; • $badSqlCode[] = 'truncate'; • $badSqlCode[] = 'drop'; • $badSqlCode[] = 'where'; • $badSqlCode[] = 'or'; • $badSqlCode[] = 'and'; • $badSqlCode[] = 'values'; • $badSqlCode[] = 'set'; • //test if sql code is bad • if (preg_match('/\s['.implode('|',$badSqlCode).']+\s/i', $sqlcode)) • { • //bad sql found -- hack attept! Abort • $ERROR_TEXT = "Invalid text was entered. Please correct."; • return 0; • } • return 1; • }