Buffer Overflow Attacks, SQL Injection Attacks
Buffer Overflow Attacks, SQL Injection Attacks
injection attacks
Buffer overflows
HW3
Out shortly…
Buffer overflow attacks
– Will need to use gdb
Using gdb
Input validation
Buffer overflows can be viewed as an example of
improper input validation
Other notable examples:
– Format string vulnerabilities
more later
– SQL injection attacks
Two general mechanisms to prevent attacks
– Better input validation
– Safe programming techniques; techniques for detecting
potential buffer overflows in code; …
Defenses (briefly!)
Secure programming techniques
Penetration testing
Static analysis
Dynamic analysis
Secure programming techniques
Validate all input
Avoid buffer overflows (use safe string
manipulation functions, careful length checking,
etc., …)
Validating input
Determine acceptable input, check for match ---
don’t just check against list of “non-matches”
– Limit maximum length
– Watch out for special characters, escape chars.
Check bounds on integer values
– Check for negative inputs
– Check for large inputs that might cause overflow!
Validating input
Filenames
– Disallow *, .., etc.
Command-line arguments
– Even argv[0]…
Commands
– E.g., SQL (see later)
URLs, http variables
– E.g., cross site scripting, more
– Next lecture
Avoiding buffer overflows
Use arrays instead of pointers
Bad:
… strcpy(record,user);
strcat(record,”:”);
strcat(record,cpw); …
Frame 2 Frame 1
local canary sfp ret str local canary sfp ret str
More methods …
Address obfuscation
– Encrypt return address on stack by XORing with
random string. Decrypt just before returning from
function
– Attacker needs decryption key to set return address to
desired value
More input validation flaws
Format string vulnerabilities
What is the difference between
printf(buf);
and
printf(“%s”, buf);
?
What if buf holds %x ?
Look at memory, and what printf expects…
What happens?
printf(“%x”) expects an additional argument…
“%x”
When the user enters a number and clicks the button, this
generates an http request like
https://www.pizza.com/show_orders?month=10
Example continued…
Upon receiving the request, a java script might
generate an SQL query as follows:
sql_query
= "SELECT pizza, quantity, order_day "
+ "FROM orders "
+ "WHERE userid=" + session.getCurrentUserId()
+ " AND order_month= "
+ request.getParameter("month");
sql_query
= "SELECT pizza, quantity, order_day "
+ "FROM orders "
+ "WHERE userid=" + session.getCurrentUserId()
+ " AND topping= ‘ "
+ request.getParameter(“topping") + “’”
Blacklisting
Whitelisting
Prepared statements/bind variables
Mitigate the impact of SQL injection
Blacklisting?
I.e., searching for/preventing ‘bad’ inputs
E.g., for previous example:
sql_query
= "SELECT pizza, quantity, order_day "
+ "FROM orders "
+ "WHERE userid=" + session.getCurrentUserId()
+ " AND topping= ‘ "
+ kill_chars(request.getParameter(“topping"))
+ “’”
ps.setInt(1, session.getCurrentUserId());
ps.setInt(2,
Integer.parseInt(request.getParameter("month")));
ResultSet res = ps.executeQuery();
Bind variables
Mitigating the impact
Limit privileges
– I.e., allow SELECT queries on the orders database, but
no queries on creditcards database
– Can limit commands, or tables to which access is given
(or both)
– Principle of least privilege
– Not a complete fix, but it helps
Encrypt sensitive data stored in database
– E.g., orders in the clear but credit card numbers
encrypted