• No results found

Hunting Vulnerabilities with Graph Databases

N/A
N/A
Protected

Academic year: 2021

Share "Hunting Vulnerabilities with Graph Databases"

Copied!
28
0
0

Loading.... (view fulltext now)

Full text

(1)

GEORG-AUGUST-UNIVERSITÄT GÖTTINGEN

Hunting Vulnerabilities with Graph Databases

Fabian ‘fabs’ Yamaguchi

Nico Golde (Qualcomm)

INBOT’14

(2)

GEORG-AUGUST-UNIVERSITÄT GÖTTINGEN

CVE-2013-6381:

qeth buffer overflow in snmp ioctl

(3)

GEORG-AUGUST-UNIVERSITÄT GÖTTINGEN

CVE-2013-6381:

qeth buffer overflow in snmp ioctl

3

First arg of copy_from_user propagates to third

arg of memcpy without being checked.

(4)

GEORG-AUGUST-UNIVERSITÄT GÖTTINGEN

CVE-2013-6381:

qeth buffer overflow in snmp ioctl

4

Goal: make searching for bugs as easy as talking

about them.

(5)

GEORG-AUGUST-UNIVERSITÄT GÖTTINGEN

Building a Bug Search Engine

»

It’s only really useful it it’s generic enough to express lots of

different kinds of bugs

» We want to be able to query the database for

»

What the code looks like (syntax)

»

Statement execution order (control flow)

»

How data flows through the program (data flow)

» Long story short: We built it, let’s talk about it.

(6)

GEORG-AUGUST-UNIVERSITÄT GÖTTINGEN

How compilers analyze syntax: ASTs

(7)

GEORG-AUGUST-UNIVERSITÄT GÖTTINGEN

How compilers analyze control flow

(8)

GEORG-AUGUST-UNIVERSITÄT GÖTTINGEN

How compilers analyze data flow: dependence graphs

(9)

GEORG-AUGUST-UNIVERSITÄT GÖTTINGEN

Merge: Code Property Graphs

(10)

GEORG-AUGUST-UNIVERSITÄT GÖTTINGEN

Graph databases! Big Data! Cloud-era!

10

»

Designed to store social networks

(11)

GEORG-AUGUST-UNIVERSITÄT GÖTTINGEN

GraphDBs: Not limited to storage of useless crap

» You can store code property graphs in graph databases

(12)

GEORG-AUGUST-UNIVERSITÄT GÖTTINGEN

Overview of the System (Joern)

12

Query

Response

Robust Parser Source Code

(13)

GEORG-AUGUST-UNIVERSITÄT GÖTTINGEN

Querying the Database: Sending in the Gremlin

» Imperative language to traverse graphs by Marko Rodriguez

»

Turing complete, embedded groovy

»

Feels functional

»

Runs on top of Blueprints

» Traversals

»

Find starting nodes using an index

»

Describe how to walk the graph from the starting node by

chaining elementary traversals (“pipes”)

»

Return all nodes reached

(14)

GEORG-AUGUST-UNIVERSITÄT GÖTTINGEN

Gremlin – Chaining Pipes

14

From: http://markorodriguez.com/2011/08/03/on-the-nature-of-pipes/

» A pipe maps sets of nodes to

new sets of nodes

» Pipes are chained to describe

traversals in the graph

» Chained pipes can be

(15)

GEORG-AUGUST-UNIVERSITÄT GÖTTINGEN

Gremlin in a Nuttshell

15

>> g.v(1).out(‘knows’).name vadas, josh

>> g.v(1).out(‘knows’).filter{ it.age > 30}.name josh

>> g.v(1).out().loop(1){ it.loops < 2}.name ripple

(16)

GEORG-AUGUST-UNIVERSITÄT GÖTTINGEN

Custom steps – Basis for code analysis with Gremlin

16 Gremlin.defineStep(‘twoHopsAway’, [Vertex,Pipe], { _().out().out() }) >> g.v(1).twoHopsAway().name ripple

(17)

GEORG-AUGUST-UNIVERSITÄT GÖTTINGEN

Steps for code analysis: python-joern

» Start node selection:

»

getArgs, getParameters, getAssignments…

» Elementary AST-steps

»

children(), parents(), ithChild(), astNodes()

» Node-specific utility steps for ASTs

»

callToIthArgument(), assignmentToLval(), …

» Data flow and control flow steps

»

sources(), definitions(), unsanitized(), paths() …

»

A “language” for bug description

(18)

GEORG-AUGUST-UNIVERSITÄT GÖTTINGEN

Simple example: multiplications inside args to malloc

18

$ echo "getArguments('malloc', '0')

.astNodes().filter{it.type == 'MultiplicativeExpression'}.code“ | lookup.py -g

» Syntax-only:

»

Get all first arguments to malloc

»

Get all nodes of trees rooted at these (astNodes)

»

Select only multiplicative expressions

»

Get rid of ‘sizeof’

(19)

GEORG-AUGUST-UNIVERSITÄT GÖTTINGEN

Building block for tainting – The “unsanitized” pipe

»

It’s possible to build very complex pipes

»

Inject arbitrary groovy code into the DB!

»

Meet the pipe ‘unsanitized(sanitizers)’

» All CFG paths from sources to a sink where

»

A symbol is propagated from source to sink (data flow)

»

(No node on the path re-define the symbol)

»

No node on the path matches a sanitizer-description

(20)

GEORG-AUGUST-UNIVERSITÄT GÖTTINGEN

Determining symbols used and possible sources

20

Gremlin.defineStep('unsanitized', [Vertex, Pipe], { sanitizer, src = { [1]._() }-> _().sideEffect{ dst = it; }

.uses().sideEffect{ symbol = it.code }

.transform{ dst.producers([symbol]) }.scatter() .transform{

cfgPaths(symbol, sanitizer, it, dst.statements().toList()[0] ) }.scatter()

.firstElem() })

» What this query does

»

Determine symbols used by a statement

»

Determine producers of that symbol

(21)

GEORG-AUGUST-UNIVERSITÄT GÖTTINGEN

Determine CFG paths with a functional program

21

cfgPaths = { symbol, sanitizer, src, dst ->

_cfgPaths(symbol, sanitizer, src, dst, [:], []) }

Object.metaClass._cfgPaths = {symbol, sanitizer, curNode, dst, visited, path ->

if(curNode == dst) return [path + curNode] as Set

if( ( path != [] ) && isTerminationNode(symbol, sanitizer, curNode, visited)) return [] as Set

def X = [] as Set; def x;

def children = curNode._().out(CFG_EDGE).toList() for(child in children){

def curNodeId = curNode.toString()

x = _cfgPaths(symbol, sanitizer, child, dst,

visited + [ (curNodeId) : (visited.get(curNodeId, 0) + 1)], path + curNode)

X += x } X

(22)

GEORG-AUGUST-UNIVERSITÄT GÖTTINGEN

“Functional feel”

22

cfgPaths = { symbol, sanitizer, src, dst ->

_cfgPaths(symbol, sanitizer, src, dst, [:], []) }

Object.metaClass._cfgPaths = {symbol, sanitizer, curNode, dst, visited, path ->

if(curNode == dst) return [path + curNode] as Set

if( ( path != [] ) && isTerminationNode(symbol, sanitizer, curNode, visited)) return [] as Set

def children = curNode._().out(CFG_EDGE).toList() for(child in children){

def curNodeId = curNode.toString()

X += _cfgPaths(symbol, sanitizer, child, dst,

visited + [ (curNodeId) : (visited.get(curNodeId, 0) + 1)], path + curNode)

} X }

(23)

GEORG-AUGUST-UNIVERSITÄT GÖTTINGEN

Taking Joern for a spin on the Linux Kernel

» Crafted queries for four different types of vulnerabilities

»

Buffer overflows

»

Zero-byte allocation

»

Memory mapping bugs

»

Memory disclosure

»

Let’s take a look at the buffer overflow examples

(24)

GEORG-AUGUST-UNIVERSITÄT GÖTTINGEN

Query for buffer overflows in write handlers (Joern 0.2)

24

query1 = “””

getFunctionASTsByName('*_write*')

.getArguments('(copy_from_user OR memcpy)', '2') .sideEffect{ paramName = 'c(ou)?nt'; }

.filter{ it.code.matches(paramName) } .unsanitized( { it._().or( _().isCheck('.*' + paramName + '.*'), _().codeContains('.*alloc.*' + paramName + '.*'), _().codeContains('.*min.*') )} ) .param( '.*c(ou)?nt.*' ) .locations() “””

(25)

GEORG-AUGUST-UNIVERSITÄT GÖTTINGEN

Query for qeth-style buffer overflows (Joern 0.2)

25

query2 = “””

getArguments('(copy_from_user OR memcpy)', '2')

.filter{ !it.argToCall().toList()[0].code.matches('.*(sizeof|min).*') } .sideEffect{ argument = it.code; } /* store argument */

.sideEffect{ dstId = it.statements().toList()[0].id; } /* store id of sink */

.unsanitized({ it._().or( _().isCheck('.*' + Pattern.quote(argument) + '.*') , _().codeContains('.*alloc.*' + Pattern.quote(argument) + '.*'), _().codeContains('.*min.*') ) }, { it._().filter{ it.code.contains('copy_from_user')} } )

.filter{ it.id != dstId } /* filter flows from node to self */

(26)

GEORG-AUGUST-UNIVERSITÄT GÖTTINGEN

Profit: Seven 0-days in eleven hits

26

(27)

GEORG-AUGUST-UNIVERSITÄT GÖTTINGEN

Search queries for four different types of bugs

» 18 zero-days, acknowledged /fixed by developers

(28)

GEORG-AUGUST-UNIVERSITÄT GÖTTINGEN 28

Thank you! Questions?

» Tool:

»

http://mlsec.org/joern

»

http://github.com/fabsx00/joern

» Fabian Yamaguchi

»

[email protected]

»

http://codeexploration.blogspot.de

»

Twitter: @fabsx00

References

Related documents

Preferred citation: [Item name], San Joaquin General Hospital School of Nursing Collection, Ms10, San Joaquin County Historical Society and Museum, Lodi, California..

Background: The goal of the study was to assess the presence of antibodies to mutated citrullinated vimen- tin (anti-MCV) and cyclic citrullinated peptides (anti- CCP) in patients

If community members, the housing authority, housing organizations, and the town leadership could better understand the affordable housing obstacles and personal tolls of

Jeanette Harshman Sandy Empting Bill Empting BUILDING &amp; GROUNDS Walt Munsterman Rich Alickson Ralph Johnson Bill Rogers Shane Mekeland David Darsow

From the results reported in Table 4 for all firms we conclude that the share of exports in total sales matters for the size of the exporter productivity premia in all countries

Panel Organizer: “Between the Streets and the Archives: Grappling with Sources in Francophone Africa in ‘les années 1968,’” and presenter: “Pluralizing Perspectives: Pitfalls

The case study was implemented in the Prague 12 district (approx. 55,000 inhabitants) from September 2016 to November 2017 and it consisted of three mapping activities: a workshop

Per evitare gli inconvenienti tipici di ciascun sistema, cercando di recuperarne le caratteristiche positive e, limitandone al contempo gli effetti negativi, si