Top Banner
Taking on the King: Killing Injection Vulnerabilities Taking on the King: Killing Injection Vulnerabilities Justin Collins @presidentbeef AppSec California 2018
57

Taking on the King: Killing Injection Vulnerabilities Scripting A4 A1 A2 A3 A7. ... Some SQL Injection Python ... SQL Injection Prevention ORM User.where(email: params[:email])

Jun 10, 2018

Download

Documents

dinhthu
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Taking on the King: Killing Injection Vulnerabilities Scripting A4 A1 A2 A3 A7. ... Some SQL Injection Python ... SQL Injection Prevention ORM User.where(email: params[:email])

Taking on the King:Killing Injection Vulnerabilities

Taking on the King:Killing Injection Vulnerabilities

Justin Collins@presidentbeef

AppSec California 2018

Page 2: Taking on the King: Killing Injection Vulnerabilities Scripting A4 A1 A2 A3 A7. ... Some SQL Injection Python ... SQL Injection Prevention ORM User.where(email: params[:email])

About Me

I’m this guy over here

Page 3: Taking on the King: Killing Injection Vulnerabilities Scripting A4 A1 A2 A3 A7. ... Some SQL Injection Python ... SQL Injection Prevention ORM User.where(email: params[:email])

About Me

Or maybe over there

Page 4: Taking on the King: Killing Injection Vulnerabilities Scripting A4 A1 A2 A3 A7. ... Some SQL Injection Python ... SQL Injection Prevention ORM User.where(email: params[:email])

This is a “Thought” Talk

Page 5: Taking on the King: Killing Injection Vulnerabilities Scripting A4 A1 A2 A3 A7. ... Some SQL Injection Python ... SQL Injection Prevention ORM User.where(email: params[:email])

Injection in the OWASP Top 10

2004 2009 2010 2013 2017

Injection A6 A2 A1 A1 A1

Cross-Site Scripting A4 A1 A2 A3 A7

Page 6: Taking on the King: Killing Injection Vulnerabilities Scripting A4 A1 A2 A3 A7. ... Some SQL Injection Python ... SQL Injection Prevention ORM User.where(email: params[:email])

Injection in the OWASP Top 10

Page 7: Taking on the King: Killing Injection Vulnerabilities Scripting A4 A1 A2 A3 A7. ... Some SQL Injection Python ... SQL Injection Prevention ORM User.where(email: params[:email])

Injection in the Wild

HackerOne Hacker-Powered Security Report 2017

Page 8: Taking on the King: Killing Injection Vulnerabilities Scripting A4 A1 A2 A3 A7. ... Some SQL Injection Python ... SQL Injection Prevention ORM User.where(email: params[:email])

Injection in the Wild

“...the average payout for SQLi was the highest at $1,058”

BugCrowd State of Bug Bounty Report 2017

Page 9: Taking on the King: Killing Injection Vulnerabilities Scripting A4 A1 A2 A3 A7. ... Some SQL Injection Python ... SQL Injection Prevention ORM User.where(email: params[:email])

What is an Injection Vulnerability?

DATA interpreted as CODE

Page 10: Taking on the King: Killing Injection Vulnerabilities Scripting A4 A1 A2 A3 A7. ... Some SQL Injection Python ... SQL Injection Prevention ORM User.where(email: params[:email])

What is an Injection Vulnerability?

DATA interpreted as CODEQuery Parameters

Form ValuesHeader ValuesUploaded Files

Database Values...

SQLHTMLJavaScriptBash ScriptXML Entities...

Page 11: Taking on the King: Killing Injection Vulnerabilities Scripting A4 A1 A2 A3 A7. ... Some SQL Injection Python ... SQL Injection Prevention ORM User.where(email: params[:email])

What is an Injection Vulnerability?

Developer Code Attacker Code

Interpreter

Page 12: Taking on the King: Killing Injection Vulnerabilities Scripting A4 A1 A2 A3 A7. ... Some SQL Injection Python ... SQL Injection Prevention ORM User.where(email: params[:email])

What is an Injection Vulnerability?

Developer Code Attacker Code

Interpreter

Developer Code

Page 13: Taking on the King: Killing Injection Vulnerabilities Scripting A4 A1 A2 A3 A7. ... Some SQL Injection Python ... SQL Injection Prevention ORM User.where(email: params[:email])

What is an Injection Vulnerability?

Developer Code Attacker Code

Interpreter

Developer Code

Database DriverBrowserWeb ServerMail ServerShellTemplating LibraryXML ParserLDAP Parsereval()...

Page 14: Taking on the King: Killing Injection Vulnerabilities Scripting A4 A1 A2 A3 A7. ... Some SQL Injection Python ... SQL Injection Prevention ORM User.where(email: params[:email])

SQL Injection Example

query = "SELECT * FROM users WHERE email='" + email + "'"

db.execute(query)

Page 15: Taking on the King: Killing Injection Vulnerabilities Scripting A4 A1 A2 A3 A7. ... Some SQL Injection Python ... SQL Injection Prevention ORM User.where(email: params[:email])

SQL Injection Example

query = "SELECT * FROM users WHERE email='" + email + "'"

db.execute(query)

Expected to be DATA

Page 16: Taking on the King: Killing Injection Vulnerabilities Scripting A4 A1 A2 A3 A7. ... Some SQL Injection Python ... SQL Injection Prevention ORM User.where(email: params[:email])

SQL Injection Example

query = "SELECT * FROM users WHERE email='" + email + "'"

db.execute(query)

Expected to be DATALooks like DATA,but is CODE

Page 17: Taking on the King: Killing Injection Vulnerabilities Scripting A4 A1 A2 A3 A7. ... Some SQL Injection Python ... SQL Injection Prevention ORM User.where(email: params[:email])

SQL Injection Example

email = request.params["email"]

query = "SELECT * FROM users WHERE email='" + email + "'"

db.execute(query)

User Input

Page 18: Taking on the King: Killing Injection Vulnerabilities Scripting A4 A1 A2 A3 A7. ... Some SQL Injection Python ... SQL Injection Prevention ORM User.where(email: params[:email])

SQL Injection Example

email = "' OR 1=1;--"

query = "SELECT * FROM users WHERE email='" + email + "'"

db.execute(query)

Attacker Input

Page 19: Taking on the King: Killing Injection Vulnerabilities Scripting A4 A1 A2 A3 A7. ... Some SQL Injection Python ... SQL Injection Prevention ORM User.where(email: params[:email])

SQL Injection Example

email = "' OR 1=1;--"

query = "SELECT * FROM users WHERE email='' OR 1=1;--'"

db.execute(query)

DATA now interpreted as CODE

Page 20: Taking on the King: Killing Injection Vulnerabilities Scripting A4 A1 A2 A3 A7. ... Some SQL Injection Python ... SQL Injection Prevention ORM User.where(email: params[:email])

Some SQL Injection

Python

query = "SELECT * FROM users WHERE email = '%s'" % (request.POST.get('email'))

cursor.execute(query)

Ruby / ActiveRecord

query = "SELECT * FROM users WHERE email = '#{params[:email]}'"ActiveRecord::Base.connection.execute(query)

Java

String user = request.getParameter("email");Statement st = conn.createStatement();String query = "SELECT * FROM user where userId='" + email + "'";st.executeQuery(query)

Go

db.Query("SELECT *FROM users WHERE email='" + req.FormValue("email") + "'")

Page 21: Taking on the King: Killing Injection Vulnerabilities Scripting A4 A1 A2 A3 A7. ... Some SQL Injection Python ... SQL Injection Prevention ORM User.where(email: params[:email])

Some Command Injection

Python

cmd = "rm -rf /tmp/%s" % request.POST.get("username")

os.system(cmd)

Ruby

`rm -rf #{params[:username]}`

Java

String cmd = String.format("sh -c rm -rf /tmp/%s", request.getParameter("username"))Runtime.getRuntime().exec(cmd)

Go

cmd := exec.Command(fmt.String("rm -rf /tmp/%s", req.FormValue("username"))

err := cmd.Run()

Page 22: Taking on the King: Killing Injection Vulnerabilities Scripting A4 A1 A2 A3 A7. ... Some SQL Injection Python ... SQL Injection Prevention ORM User.where(email: params[:email])

Some Server-Side Template Injection

Ruby on Rails

render inline: "Hello, #{params[:name]}!"

Python / Jinja

from jinja2 import Environment

name = request.GET.get('name')Environment().from_string('Hello ' + name + '!').render()

Page 23: Taking on the King: Killing Injection Vulnerabilities Scripting A4 A1 A2 A3 A7. ... Some SQL Injection Python ... SQL Injection Prevention ORM User.where(email: params[:email])

Some Cross-Site Scripting?

<html> <head> <title>{{ title }} - My Site</title> <style> body { color: {{ theme['color'] }}; } </style> </head> <body> <script type="text/javascript"> var init = {{ data }}; </script>

<a href={{ home_url }}>Home</a>

</body></html>

Page 24: Taking on the King: Killing Injection Vulnerabilities Scripting A4 A1 A2 A3 A7. ... Some SQL Injection Python ... SQL Injection Prevention ORM User.where(email: params[:email])

Some Cross-Site Scripting?

<html> <head> <title>{{ title }} - My Site</title> <style> body { color: {{ theme['color'] }}; } </style> </head> <body> <script type="text/javascript"> var init = {{ data }}; </script>

<a href={{ home_url }}>Home</a>

</body></html>

HTML Context

CSS Context

JavaScript Context

HTML Attribute Context

Page 25: Taking on the King: Killing Injection Vulnerabilities Scripting A4 A1 A2 A3 A7. ... Some SQL Injection Python ... SQL Injection Prevention ORM User.where(email: params[:email])

Web Programming is Metaprogramming

Page 26: Taking on the King: Killing Injection Vulnerabilities Scripting A4 A1 A2 A3 A7. ... Some SQL Injection Python ... SQL Injection Prevention ORM User.where(email: params[:email])

Web Programming is Metaprogramming

“Writing Code that Writes Code”

Page 27: Taking on the King: Killing Injection Vulnerabilities Scripting A4 A1 A2 A3 A7. ... Some SQL Injection Python ... SQL Injection Prevention ORM User.where(email: params[:email])

Web Programming is Metaprogramming

SQL / NoSQL

HTTP, HTML, JavaScript, JSON, CSS, ...

HTTP, JSON, XML, Form Data...

HTTP, JSON, XML, ...HTML, JavaScript, CSS, ...

Page 28: Taking on the King: Killing Injection Vulnerabilities Scripting A4 A1 A2 A3 A7. ... Some SQL Injection Python ... SQL Injection Prevention ORM User.where(email: params[:email])

Web ProgrammingIs

Compiler Construction

Page 29: Taking on the King: Killing Injection Vulnerabilities Scripting A4 A1 A2 A3 A7. ... Some SQL Injection Python ... SQL Injection Prevention ORM User.where(email: params[:email])

Web ProgrammingIs

Compiler ConstructionWith Untrusted Values!

Page 30: Taking on the King: Killing Injection Vulnerabilities Scripting A4 A1 A2 A3 A7. ... Some SQL Injection Python ... SQL Injection Prevention ORM User.where(email: params[:email])

Web Programming is Compiler Construction

SQL / NoSQL

HTTP, HTML, JavaScript, JSON, CSS, ...

HTTP, JSON, XML, ...

HTTP, JSON, XML, ...HTML, JavaScript, CSS, ...

Page 31: Taking on the King: Killing Injection Vulnerabilities Scripting A4 A1 A2 A3 A7. ... Some SQL Injection Python ... SQL Injection Prevention ORM User.where(email: params[:email])

Compiler Components

Lexer

Parser

Semantic Analyzer

Code Generator(s)

Source Code

Token Stream

Abstract Syntax Tree

Semantic Graph

IntermediateRepresentations

Compiled Code

Page 32: Taking on the King: Killing Injection Vulnerabilities Scripting A4 A1 A2 A3 A7. ... Some SQL Injection Python ... SQL Injection Prevention ORM User.where(email: params[:email])

We Are Using String Manipulationto Write Complex Compilers

Page 33: Taking on the King: Killing Injection Vulnerabilities Scripting A4 A1 A2 A3 A7. ... Some SQL Injection Python ... SQL Injection Prevention ORM User.where(email: params[:email])

We Are Using String Manipulationto Write Complex Compilers

With Untrusted Values!

Page 34: Taking on the King: Killing Injection Vulnerabilities Scripting A4 A1 A2 A3 A7. ... Some SQL Injection Python ... SQL Injection Prevention ORM User.where(email: params[:email])

Web Programming is Compiler Construction

SQL / NoSQL

HTTP, HTML, JavaScript, JSON, CSS, ...

HTTP, JSON, XML, ...

HTTP, JSON, XML, ...HTML, JavaScript, CSS, ...

Via String ManipulationWith Untrusted Values!

Page 35: Taking on the King: Killing Injection Vulnerabilities Scripting A4 A1 A2 A3 A7. ... Some SQL Injection Python ... SQL Injection Prevention ORM User.where(email: params[:email])

Back to the Real World

Page 36: Taking on the King: Killing Injection Vulnerabilities Scripting A4 A1 A2 A3 A7. ... Some SQL Injection Python ... SQL Injection Prevention ORM User.where(email: params[:email])

SQL Injection Prevention

ORM

User.where(email: params[:email])

Query Parameterization

User.where(["email = ?", params[:email]])

Manual escaping

email = ActiveRecord::Base.connection.quote_string(params[:email])

query = "SELECT * FROM users WHERE email = #{email}"

ActiveRecord::Base.connection.execute(query)

Page 37: Taking on the King: Killing Injection Vulnerabilities Scripting A4 A1 A2 A3 A7. ... Some SQL Injection Python ... SQL Injection Prevention ORM User.where(email: params[:email])

Preventing Injection(Some Suggestions)

Page 38: Taking on the King: Killing Injection Vulnerabilities Scripting A4 A1 A2 A3 A7. ... Some SQL Injection Python ... SQL Injection Prevention ORM User.where(email: params[:email])

Remove Unsafe Interfaces

Page 39: Taking on the King: Killing Injection Vulnerabilities Scripting A4 A1 A2 A3 A7. ... Some SQL Injection Python ... SQL Injection Prevention ORM User.where(email: params[:email])

Remove Unsafe Interfaces

db.execute(...) NO!

system.run(...) NO!

blah.html_safe NO!

{{ … | safe }} NO!

blah.innerHTML = ... NO!

Page 40: Taking on the King: Killing Injection Vulnerabilities Scripting A4 A1 A2 A3 A7. ... Some SQL Injection Python ... SQL Injection Prevention ORM User.where(email: params[:email])

Stop Providing String Interfaces

Page 41: Taking on the King: Killing Injection Vulnerabilities Scripting A4 A1 A2 A3 A7. ... Some SQL Injection Python ... SQL Injection Prevention ORM User.where(email: params[:email])

ARel

users = Arel::Table.new(:users)

users.where(users[:name].eq('bob').or(users[:age].lt(25)))

Page 42: Taking on the King: Killing Injection Vulnerabilities Scripting A4 A1 A2 A3 A7. ... Some SQL Injection Python ... SQL Injection Prevention ORM User.where(email: params[:email])

But I Want To...

schemas = schema_search_path.split(/,/).map { |p| quote(p) }.join(',')

result = query(<<-SQL, name)

SELECT distinct i.relname, d.indisunique, d.indkey, t.oid

FROM pg_class t, pg_class i, pg_index d

WHERE i.relkind = 'i'

AND d.indexrelid = i.oid

AND d.indisprimary = 'f'

AND t.oid = d.indrelid

AND t.relname = '#{table_name}'

AND i.relnamespace IN (SELECT oid FROM pg_namespace WHERE nspname IN (#{schemas}) )

ORDER BY i.relname

SQL

Page 43: Taking on the King: Killing Injection Vulnerabilities Scripting A4 A1 A2 A3 A7. ... Some SQL Injection Python ... SQL Injection Prevention ORM User.where(email: params[:email])

Types to the Rescue?

User.where("name = '#{params[:email]}'")

SecurityError in UsersController#search:

Cannot add dangerous input to String

Page 44: Taking on the King: Killing Injection Vulnerabilities Scripting A4 A1 A2 A3 A7. ... Some SQL Injection Python ... SQL Injection Prevention ORM User.where(email: params[:email])

Is This a Good Idea?

mab = Markaby::Builder.new

mab.html do

head { title "Boats.com" }

body do

h1 "Boats.com has great deals"

ul do

li "$49 for a canoe"

li "$39 for a raft"

li "$29 for a huge boot that floats and can fit 5 people"

end

end

end

Page 45: Taking on the King: Killing Injection Vulnerabilities Scripting A4 A1 A2 A3 A7. ... Some SQL Injection Python ... SQL Injection Prevention ORM User.where(email: params[:email])

Restrict Accepted Language

Page 46: Taking on the King: Killing Injection Vulnerabilities Scripting A4 A1 A2 A3 A7. ... Some SQL Injection Python ... SQL Injection Prevention ORM User.where(email: params[:email])

Restrict Accepted Language

Do you really need a Turing-complete language..?

Page 47: Taking on the King: Killing Injection Vulnerabilities Scripting A4 A1 A2 A3 A7. ... Some SQL Injection Python ... SQL Injection Prevention ORM User.where(email: params[:email])

Provide Context-Aware Escaping

Page 48: Taking on the King: Killing Injection Vulnerabilities Scripting A4 A1 A2 A3 A7. ... Some SQL Injection Python ... SQL Injection Prevention ORM User.where(email: params[:email])

Context-Aware Auto-Escaping

<html> <head> <title>{{ title }} - My Site</title> <style> body { color: {{ theme['color'] }}; } </style> </head> <body> <script type="text/javascript"> var init = {{ data }}; </script>

<a href={{ home_url }}>Home</a>

</body></html>

HTML Context

CSS Context

JavaScript Context

HTML Attribute Context

Page 49: Taking on the King: Killing Injection Vulnerabilities Scripting A4 A1 A2 A3 A7. ... Some SQL Injection Python ... SQL Injection Prevention ORM User.where(email: params[:email])

HTML Context-Aware Auto-Escaping

Go Templates

CTemplates

Latte (PHP)

SecureHandlebars

Page 50: Taking on the King: Killing Injection Vulnerabilities Scripting A4 A1 A2 A3 A7. ... Some SQL Injection Python ... SQL Injection Prevention ORM User.where(email: params[:email])

SQL Context-Aware Auto-Escaping

Page 51: Taking on the King: Killing Injection Vulnerabilities Scripting A4 A1 A2 A3 A7. ... Some SQL Injection Python ... SQL Injection Prevention ORM User.where(email: params[:email])

Shell Context-Aware Auto-Escaping

Page 52: Taking on the King: Killing Injection Vulnerabilities Scripting A4 A1 A2 A3 A7. ... Some SQL Injection Python ... SQL Injection Prevention ORM User.where(email: params[:email])

“But, but, framework XYZ does this!”

Page 53: Taking on the King: Killing Injection Vulnerabilities Scripting A4 A1 A2 A3 A7. ... Some SQL Injection Python ... SQL Injection Prevention ORM User.where(email: params[:email])

https://www.flickr.com/photos/cogdog/6706527857

Page 54: Taking on the King: Killing Injection Vulnerabilities Scripting A4 A1 A2 A3 A7. ... Some SQL Injection Python ... SQL Injection Prevention ORM User.where(email: params[:email])

EVERY Framework Needs to Do This

Page 55: Taking on the King: Killing Injection Vulnerabilities Scripting A4 A1 A2 A3 A7. ... Some SQL Injection Python ... SQL Injection Prevention ORM User.where(email: params[:email])

How?

Quality, simple, portable libraries (e.g. libnacl, libpasta)

Make it “standard” in new web frameworks

Have the “cool kids” (Google, Facebook, etc.) push it

Page 56: Taking on the King: Killing Injection Vulnerabilities Scripting A4 A1 A2 A3 A7. ... Some SQL Injection Python ... SQL Injection Prevention ORM User.where(email: params[:email])

How?

Quality, simple, portable libraries (e.g. libnacl, libpasta)

Make it “standard” in new web frameworks

Have the “cool kids” (Google, Facebook, etc.) push it

Page 57: Taking on the King: Killing Injection Vulnerabilities Scripting A4 A1 A2 A3 A7. ... Some SQL Injection Python ... SQL Injection Prevention ORM User.where(email: params[:email])

Thank You

@presidentbeef