Augmenting Binary Analysis with Python and Pin January 14th, 2014

Shmoocon 2015

@ancat, @1blankwall1

Who are we?

Shmoocon 2015

@ancat, @1blankwall1

About Us • Omar • Recent graduate of NYU • Security engineer at Etsy • Tyler • Studies at NYU • Security researcher at SilverSky

Shmoocon 2015

@ancat, @1blankwall1

What is binary analysis?

Shmoocon 2015

@ancat, @1blankwall1

What is binary analysis? • Binary: A file containing all the resources and native code

needed for a program to execute • Analysis: To make sense of an application when the original intentions are not clear or known

Shmoocon 2015

@ancat, @1blankwall1

Using a debugger (WinDbg, GDB, Immunity, etc)

Shmoocon 2015

@ancat, @1blankwall1

Simply observing the execution of a binary $  ./bomb   Welcome  to  my  fiendish  little  bomb.  You  have  6  phases  with   which  to  blow  yourself  up.  Have  a  nice  day!   qwertyuiop   !

BOOM!!!   The  bomb  has  blown  up.   $  ./bomb   Welcome  to  my  fiendish  little  bomb.  You  have  6  phases  with   which  to  blow  yourself  up.  Have  a  nice  day!   Public  speaking  is  very  easy.   Phase  1  defused.  How  about  the  next  one? Shmoocon 2015

@ancat, @1blankwall1

Reading disassembly output (IDA, objdump, etc)

Shmoocon 2015

@ancat, @1blankwall1

Running /usr/bin/strings on a binary $  strings  ./elysium   /lib/ld-­‐linux.so.2   libcrypto.so.1.0.0   EVP_DecryptFinal_ex   EVP_aes_128_cbc   EVP_DecryptInit_ex   RAND_pseudo_bytes   EVP_EncryptFinal_ex   EVP_CIPHER_CTX_init   EVP_DecryptUpdate   EVP_EncryptInit_ex   SHA1   EVP_EncryptUpdate   libc.so.6   _IO_stdin_used   setuid   socket   strcpy   exit   htons   [-­‐]  Send  Fail   1)  Get  informations     2)  List  units   3)  Add  medical  units     4)  Add  military  units     5)  Add  social  units    

Shmoocon 2015

@ancat, @1blankwall1

Static Analysis • Reading disassembly output (IDA, objdump, etc) • Running /usr/bin/strings on a binary

Shmoocon 2015

@ancat, @1blankwall1

Dynamic Analysis • Using a debugger (WinDbg, gdb, Immunity, etc) • Simply observing the execution of a binary

Shmoocon 2015

@ancat, @1blankwall1

Static vs Dynamic • Speed • Level of Understanding • Code Coverage • Static can cover 100% of the code (good or bad?) • Dynamic can be accurate due to run time information

Shmoocon 2015

@ancat, @1blankwall1

Introducing…

Shmoocon 2015

@ancat, @1blankwall1

Dynamic Binary Instrumentation

Shmoocon 2015

@ancat, @1blankwall1

Dynamic Binary Instrumentation • A technique to modify the behavior of programs based on

certain conditions during execution • Sometimes done by modifying the code before starting the program • For example, an INT3 instruction on x86 used by debuggers, or less specifically, trampolines

Shmoocon 2015

@ancat, @1blankwall1

Debugger Scripting • GDB & LLDB • Scriptable using Python - Unix only (mostly) • WinDBG • Scriptable using Python (somewhat) - Windows only • VDB • Entirely Python API - Windows and and Unix support

Shmoocon 2015

@ancat, @1blankwall1

Debugger Scripting define structs! set $target = $root! set $limit = 0! while $target! printf "[0x%x] node.name=0x%x; node.value=0x%x; node.next=0x%x; node.prev=0x%x\n”,! $target, *($target), *($target+4), *($target+8), *($target+0xc)! set $old_target = $target! set $target = *($target+8)! !

if $old_target == $target! set $limit = $limit + 1! end!

NODE.NAME

N O D E . VA L U E

NODE.NEXT

NODE.PREV

NODE.NAME

N O D E . VA L U E

NODE.NEXT

NODE.PREV

NODE.NAME

N O D E . VA L U E

NODE.NEXT

NODE.PREV

! if $limit > 10! printf "Infinite loop?\n"! set $target = 0! end! end! end!

Shmoocon 2015

@ancat, @1blankwall1

DBI Frameworks • Valgrind • GPL'd system for debugging and profiling Linux

programs • Automatically detects many memory management and threading bugs • Works on x86/Linux, AMD64/Linux and PPC32/Linux • Focused on Safe and Reliable Code • Developer tool used for finding code errors

Shmoocon 2015

@ancat, @1blankwall1

DBI Frameworks

A S S E M B LY

VEX IR V A L G R I N D I N S T R U M E N TAT I O N FRAMEWORK

C U S T O M VA L G R I N D T O O L S

(MEMCHECK, KCACHEGRIND, HELGRIND, ETC)

A S S E M B LY

Shmoocon 2015

@ancat, @1blankwall1

DBI Frameworks • Address Sanitizer • Fast memory error detector • The tool consists of a compiler instrumentation module

(currently, an LLVM pass) and a run-time library which replaces the malloc function • Works on x86 Linux, and Mac, and ARM Android • Focused on bugs • Heap/Stack Buffer overflows and Use After Free

Shmoocon 2015

@ancat, @1blankwall1

Address Sanitizer Algorithm 8 BYTE BLOCKS! PROGRAM MEMORY

1 BYTE! S H A D O W M E M O R Y ( M E TA D ATA )

ALL UNPOISONED

0

Mapping

ALL POISONED

N E G AT I V E V A L U E

K BYTES POISONED

K

Shmoocon 2015

@ancat, @1blankwall1

DBI Frameworks

Shmoocon 2015

@ancat, @1blankwall1

DBI Frameworks • DynamoRIO • Runtime code manipulation system that supports code

transformations on any part of a program at runtime • Works on x86/AMD64 Linux Mac, and Windows • Transparent, and comprehensive manipulation of unmodified applications running on stock operating systems • Direct Competitor to Pin :-!

Shmoocon 2015

@ancat, @1blankwall1

What is Pin? • Pin allows user to insert arbitrary code into an executable

right after it is loaded into memory • Generates code from a “PinTool” used to “hook” instructions and calls • Pin is the framework • PinTools are the interface • The mechanism that decides where and what code is inserted • The code to execute at insertion points

Shmoocon 2015

@ancat, @1blankwall1

Why Pin?

Shmoocon 2015

@ancat, @1blankwall1

Intel’s Pin • • • •

Amazing documentation Same exact API works for Windows and Unix Extremely popular Nothing needs to be recompiled to be used with Pin

Shmoocon 2015

@ancat, @1blankwall1

It’s easy to get started • Large repo of well commented sample tools come with Pin • Documentation is generally easy to follow • Installation is a piece of cake

Shmoocon 2015

@ancat, @1blankwall1

It can be as granular as you need it to be • Simple hook/callback system • function calls • basic blocks • instructions • and so on

Shmoocon 2015

@ancat, @1blankwall1

Mostly personal preference, though

Shmoocon 2015

@ancat, @1blankwall1

Why not Pin? • The Pin API uses C++ • Not a huge deal, but can be inconvenient during a time

crunch (ctf) • Harder to prototype • Slower than other DBI Frameworks • Not as granular as other solutions • Harder to do more advanced binary analysis techniques such as taint tracing

Shmoocon 2015

@ancat, @1blankwall1

Awesome but what can Pin do?

Shmoocon 2015

@ancat, @1blankwall1

Popular Uses • The Pin API has been used extensively in industry • Most notably Microsoft Blue Hat (2012) Winner kBouncer

(Vasilis Pappas) • Efficient and fully transparent ROP mitigation technique • Very similar to second place ROPGuard (Ivan Fratric) • Used in Microsofts EMET protection system • IDA 6.4 and above includes a pin tool for tracing code in the debugger

Shmoocon 2015

@ancat, @1blankwall1

Cool… WHERE ARE MY BUGS?! • Pin can be used to find many different classes of bugs • Most can be found by using the right kind of

instrumentation • Format Strings • Analyze parameters passed to formatting functions • Buffer Overflows • Analyze memory read and write instructions • Misused Memory Allocation (Double Frees or UAF) • Analyze memory allocation functions (malloc/free) and memory writes Shmoocon 2015

@ancat, @1blankwall1

Misused Heap Allocations • How to find these dynamically? • Keep track of all malloc calls and the addresses returned • Maintain state: Freed or In use and size • When a memory read or write happens, if the target is on

the heap, verify that the memory is a valid place to be read from or written to

Shmoocon 2015

@ancat, @1blankwall1

D-d-d-d-d-demo! • Pin C++ Heap Overflow Demo

Shmoocon 2015

@ancat, @1blankwall1

Pin • Wow, Pin is really cool! • But, wait! Pin is a mess! • Correction, C++ is a mess :P • Lots of necessary boilerplate code • Hard to prototype quickly • Difficult to understand

Shmoocon 2015

@ancat, @1blankwall1

C++ RTN mallocRtn = RTN_FindByName(img, MALLOC);! if (RTN_Valid(mallocRtn))! {! RTN_Open(mallocRtn);! !

// Instrument malloc() to print the input argument value and the return value.! RTN_InsertCall(mallocRtn, IPOINT_BEFORE, (AFUNPTR) Arg1Before,! IARG_ADDRINT, MALLOC,! IARG_FUNCARG_ENTRYPOINT_VALUE, 0,! IARG_END);! RTN_InsertCall(mallocRtn, IPOINT_AFTER, (AFUNPTR) MallocAfter,! IARG_FUNCRET_EXITPOINT_VALUE, IARG_END);! !

RTN_Close(mallocRtn);! }!

Shmoocon 2015

@ancat, @1blankwall1

Python rtn = pin.RTN_FindByName(img, "malloc")! if pin.RTN_Valid(rtn):! pin.RTN_Open(rtn)! pin.RTN_InsertCall(pin.IPOINT_BEFORE, "malloc", rtn, 1, malloc_before)! pin.RTN_InsertCall(pin.IPOINT_AFTER, "malloc", rtn, 1, malloc_after)! pin.RTN_Close(rtn)!

Shmoocon 2015

@ancat, @1blankwall1

C++ vs Python • Python • Simpler • Cleaner • No need for recompilation every time • Extensive libraries and support

Shmoocon 2015

@ancat, @1blankwall1

Python-Pin • Essentially, a python interpreter embedded within a

PinTool • • • •

“Virtual” pin module exposed to the python script Enables access to most of Pin’s functionality from within python Quick and easy to write PinTools Enables seamless integration with other Python modules • Z3py, PIL, SciPy, etc PIN TOOL PIN FRAMEWORK

Shmoocon 2015

PYTHON INTERPRETER

PYTHON CODE

@ancat, @1blankwall1

Python-Pin Demo • Use after free and heap overflow detection • Transparent socket logging • Basic utility demos

Shmoocon 2015

@ancat, @1blankwall1

Basic Heap Overflow and UAF Protection POISONED GUARD



USER CALLS MALLOC (CALLOC, REALLOC ETC…) !

• P I N H O O KS A L L O C AT I O N F U N C T I O N S

AND ADJUST REQUESTED SIZE TO A L L O W F O R C A N A RY A L L O C A T I O N S

Allocated Block

!

• H O O K S R E T U R N VA L U E A N D A D J U S T S

THE SIZE AS WELL AS SETTING A D D R E S S ’ S W I T H C A N A RY VA L U E !

• POISONED GUARD

Shmoocon 2015

CHECKS HEAP READS AND WRITES T O E N S U R E C A N A RY VA L U E I S N O T PRESENT @ancat, @1blankwall1

Basic Heap Overflow and UAF Protection POISONED GUARD

FREE

FREE LIST

BLOCK_1 &

Allocated Block

• PIN HOOKS FREE FUNCTION !

• POISONED GUARD

!



Shmoocon 2015

A D D S E V E RY F R E E D B L O C K TO THE FREE LIST

BLOCK_2 &

ETC…

VERIFIES HEAP ACCESS AGAINST THE FREE LIST BY HOOKING READS AND WRITES @ancat, @1blankwall1

Basic Heap Overflow and UAF Protection L I M I TAT I O N S : !

POISONED GUARD

Allocated Block POISONED GUARD

Shmoocon 2015





L A RG E C O M P U TAT I O N T I M E T O C H E C K T H E F R E E L I S T E V E RY TIME !

BLOCK_1 &

CHICKEN OR THE EGG PROBLEM • PIN BEGINS HOOKING FREES A N D A L L O C AT I O N S AT A VA R I A B L E P O I N T

BLOCK_2 &

!



FREE LIST

ETC…

T O C O M BAT T H I S O U R A L L O C A T I O N D O E S N O T A C T U A L LY F R E E A N Y B L O C K S S O N O T VA L I D FO R S U S TA I N E D U S E @ancat, @1blankwall1

The Future of Python-Pin • Better memory management • Finish 32-bit support • Instructions for Mac and Windows

Shmoocon 2015

@ancat, @1blankwall1

Acknowledgements Tyler Bohan Kevin Chung Dan Guido Robert Meggs Jonathan Salwan Rich Smith Paolo Soto Alex Sotirov Kai Zhong baszerr.eu

Shmoocon 2015

@ancat, @1blankwall1

Thanks for tuning in! • Slides and pin tools will be posted to twitter, for real this time

• @ancat/@1blankwall1

Shmoocon 2015

@ancat, @1blankwall1

Pin - GitHub

Jan 14, 2014 - Augmenting Binary Analysis with Python and Pin. January ... What is binary analysis? .... The tool consists of a compiler instrumentation module.

1MB Sizes 9 Downloads 287 Views

Recommend Documents

Pin Configuration for BLHeliSuite 4-Way Interfaces (4w-if) - GitHub
C2CK 3 D9. PB1. C2CK 3 D5. PD5. C2CK 4 D8. PB0. C2CK 4 D6. PD6. C2CK 5 A2. PC2. C2CK 5 D7. PD7. C2CK 6 A3. PC3. C2CK 6 D8. PB0. C2CK 7 A4.

PIN DIODE.pdf
There was a problem previewing this document. Retrying... Download. Connect more apps... Try one of the apps below to open or edit this item. PIN DIODE.pdf.

Pin The Bowtie.pdf
Whoops! There was a problem loading this page. Pin The Bowtie.pdf. Pin The Bowtie.pdf. Open. Extract. Open with. Sign In. Details. Comments. General Info.

PIN Campus Map.pdf
Student Life. • TV Studio. 2nd Floor • Instructional Development. Services. • Student Services. Advising. Career Center. Counseling. • Student Services. Disability ... Library. • Media Center. • Student Lounge. 10th Floor • Administrati

juki pin cushion.pdf
Page 1 of 1. Place on fold Wrap-Around Pincushion for Juki 2010Q Sewing Machine Cut 2. © 2017 Monica Curry | cutstitchandpiece.blogspot.ca. Page 1 of 1. juki pin cushion.pdf. juki pin cushion.pdf. Open. Extract. Open with. Sign In. Main menu. Displa

November Pin God 1st.pdf
There was a problem previewing this document. Retrying... Download. Connect more apps... Try one of the apps below to open or edit this item. November Pin ...

PIN 10369.pdf - OPAC UdeA
Page 2 of 55. Pasaje Babilonia 10 Ciudad Mendoza. Tel: 0261 - 4237241. [email protected] www.cpmendoza.com. Cuando, por el contrario, no prestamos tanta atención a nuestra imagen ideal como a. los problemas que obstaculizan nuestro camino, se activ

APPLICATION FOR MIGRATION CERTIFICATE Pin Code ... - alrahiman
If applying for DUPLICATE MIGRATION CERTIFICATE, specify the details of original certificate No………….……………. Date………………... [A declaration (to the effect that the certificate is irrecoverably lost and will be surrendered to

Pin Sucide-5.pdf
Date: Saturday, June 17, 2017. Site: Rockford Lutheran High School – Contacts: Dani Parrotte & Justice Parrotte. 3411 N. Alpine Road. Rockford, IL 61114.

Cheap 2 Ide Dual 4 Pin Molex Ide Male To 6 Pin Female Pci-E Y ...
Cheap 2 Ide Dual 4 Pin Molex Ide Male To 6 Pin Fema ... deo Cards F642D Free Shipping & Wholesale Price.pdf. Cheap 2 Ide Dual 4 Pin Molex Ide Male To 6 ...

Cheap 10Pcs Molex 4 Pin To Pci-E 6 Pin Power Converter Adapter ...
Cheap 10Pcs Molex 4 Pin To Pci-E 6 Pin Power Conver ... Cable Connector Free Shipping & Wholesale Price.pdf. Cheap 10Pcs Molex 4 Pin To Pci-E 6 Pin ...

rs485 pin configuration pdf
There was a problem previewing this document. Retrying... Download. Connect more apps... Try one of the apps below to open or edit this item. rs485 pin ...

pin - 09-25-2013.pdf
ACC is working more closely with UT, Texas State University, and Huston. Tillotson University to offer articulation and co-enrollment programs. The ACC Board of Trustees is planning to ask voters to pass a general obligation. bond to give ACC the fun

GitHub
domain = meq.domain(10,20,0,10); cells = meq.cells(domain,num_freq=200, num_time=100); ...... This is now contaminator-free. – Observe the ghosts. Optional ...

GitHub
data can only be “corrected” for a single point on the sky. ... sufficient to predict it at the phase center (shifting ... errors (well this is actually good news, isn't it?)

PIN generation using EEG: a stability study ...
School of Computer Science and Electronic Engineering,. University ... infrared), iris, retina, signature, ear shape, odour, keystroke entry pattern, gait and voice. (Jain et al. ..... International Journal of Advanced Mechatronic Systems, Vol. 2, No

09-22-14, Approved PIN CAC Meeting Minutes.pdf
Page 2 of 3. 2. Agenda Item 3: Report and Discussion on November 2014 Trustee Presenter: Dr. Mary Hensley. Election, Bond Election, and Tax. Cap Amendment Election. Discussion: Dr. Hensley began with information about ACC, the student population, pat

Date: From, Name(s) (Address) (Address) (City/Town & PIN Code ...
Sl. No. Checklist (Original instruction/guideline from the RBI circular and the .... RBI: The format of the Comprehensive Notice Board is given in the Annex - II.

Cheap Plastic Diy Lithium Battery Box Battery Holder With Pin ...
Cheap Plastic Diy Lithium Battery Box Battery Holder ... ) Lithium Battery Free Shipping & Wholesale Price.pdf. Cheap Plastic Diy Lithium Battery Box Battery ...

10-06-15, PIN CAC Meeting Minutes.pdf
... Trustee- Rita Banninger (for Dr. Carol Cespedes Dr. Mary Faria Jeffrey K. Richard ... Leslie Saldana Judge Susan Steeg Dr. Terry Thomas Denise Anding.

The Pin by Chris Crutcher - full text.pdf
“I think I've found my career,” I say at the dinner table. Family rule. 605 says all table conversation will be solemn: talk of world events,. school issues, anything ...

ePub 100 Pin Loom Squares: 100 Exciting Color ...
P 0884298868883 from amazons book store download and read 100 pin loom squares 100 exciting ... 100 Pin Loom Squares 100 Exciting Color Combinations To Try Plus 15 Stylish Projects books online for ... kinds of ideas for using these.