HexRaysCodeXplorer: object oriented RE for fun and profit

46
HexRaysCodeXplorer: object oriented RE for fun and profit Alexander Matrosov @matrosov Eugene Rodionov @vxradius

description

HexRaysCodeXplorer - Hex-Rays Decompiler plugin for easier code navigation. Here are the main features of the plugin: - Automatic type REconstruction for C++ objects. - C-tree graph visualization - a special tree-like structure representing a decompiled routine in c_itemt terms. Useful feature for understanding how the decompiler works. - Navigation through virtual function calls in HexRays Pseudocode window. - Object Explorer - useful interface for navigation through virtual tables (VTBL) structures. In this presentation, the authors of HexRaysCodeXplorer will be discussing main functionality of the plugin and its application for reverse engineering. The authors will be presenting the algorithm for C++ type REconstruction. Also a special version of HexRaysCodeXplorer (H2HC edition) will be released with new features developed specially for H2Cconference. New features will be committed to GitHub from the stage.

Transcript of HexRaysCodeXplorer: object oriented RE for fun and profit

Page 1: HexRaysCodeXplorer: object oriented RE for fun and profit

HexRaysCodeXplorer: object oriented RE for fun and profit

Alexander Matrosov@matrosov

Eugene Rodionov@vxradius

Page 2: HexRaysCodeXplorer: object oriented RE for fun and profit

C++ Code Reconstruction Problems

Show problems on real examples (Flamer)

HexRaysCodeXplorer v1.5 [H2HC Edition]

Agenda

Page 3: HexRaysCodeXplorer: object oriented RE for fun and profit

C++ Code Reconstruction Problems

Object identification Type reconstruction

Class layout reconstruction Identify constructors/destructors

Identify class members

Local/global type reconstruction

Associate object with exact method calls

RTTI reconstruction Vftable reconstruction

Associate vftable object with exact object

Class hierarchy reconstruction

Page 4: HexRaysCodeXplorer: object oriented RE for fun and profit

C++ Code Reconstruction Problems

Class A

vfPtr

a1()

a2()A::vfTable

meta

A::a1()

A::a2()

RTTI Object Locator

signature

pTypeDescriptor

pClassDescriptor

Page 5: HexRaysCodeXplorer: object oriented RE for fun and profit

C++ Code Reconstruction Problems

Page 6: HexRaysCodeXplorer: object oriented RE for fun and profit

REconstructing Flamer Framework

Page 7: HexRaysCodeXplorer: object oriented RE for fun and profit

An overview of the Flamer Framework

Vector<Command Executor>

DB_Query ClanCmd

Vector<Task>

IDLER CmdExec

Vector<DelayedTasks>

EuphoriaShare

Supplier

Vector<Consumer>

MobileConsumer

CmdConsumer

MunchSniffer FileFinder

FileCollect Driller GetConfig

LSSSender

Frog Beetlejuice

LuaConsumer

MediaConsumer

http://www.welivesecurity.com/2012/08/02/flamer-analysis-framework-reconstruction/

Page 8: HexRaysCodeXplorer: object oriented RE for fun and profit

An overview of the Flamer Framework

Vector<Command Executor>

DB_Query ClanCmd

Vector<Task>

IDLER CmdExec

Vector<DelayedTasks>

EuphoriaShare

Supplier

Vector<Consumer>

MobileConsumer

CmdConsumer

MunchSniffer FileFinder

FileCollect Driller GetConfig

LSSSender

Frog Beetlejuice

LuaConsumer

MediaConsumer

http://www.welivesecurity.com/2012/08/02/flamer-analysis-framework-reconstruction/

Page 9: HexRaysCodeXplorer: object oriented RE for fun and profit

An overview of the Flamer Framework

Vector<Command Executor>

DB_Query ClanCmd

Vector<Task>

IDLER CmdExec

Vector<DelayedTasks>

EuphoriaShare

Supplier

Vector<Consumer>

MobileConsumer

CmdConsumer

MunchSniffer FileFinder

FileCollect Driller GetConfig

LSSSender

Frog Beetlejuice

LuaConsumer

MediaConsumer

http://www.welivesecurity.com/2012/08/02/flamer-analysis-framework-reconstruction/

Page 10: HexRaysCodeXplorer: object oriented RE for fun and profit

Identify Smart Pointer Structure

o Smart pointers

o Strings

o Vectors to maintain the objects

o Custom data types: wrappers tasks, triggers and etc.

Page 11: HexRaysCodeXplorer: object oriented RE for fun and profit

Data Types Being Used: Smart pointers

typedef struct SMART_PTR

{

void *pObject; // pointer to the object

int *RefNo; // reference counter

};

Page 12: HexRaysCodeXplorer: object oriented RE for fun and profit

Identify Smart Pointer Structure

Page 13: HexRaysCodeXplorer: object oriented RE for fun and profit

Data Types Being Used: Vectors

struct VECTOR

{

void *vTable; // pointer to the table

int NumberOfItems; // self-explanatory

int MaxSize; // self-explanatory

void *vector; // pointer to buffer with elements

};

o Used to handle the objects: tasks

triggers

etc.

Page 14: HexRaysCodeXplorer: object oriented RE for fun and profit

Identify Exact Virtual Function Call in Vtable

Page 15: HexRaysCodeXplorer: object oriented RE for fun and profit

Identify Exact Virtual Function Call in Vtable

Page 16: HexRaysCodeXplorer: object oriented RE for fun and profit

Identify Custom Type Operations

Page 17: HexRaysCodeXplorer: object oriented RE for fun and profit

Data Types Being Used: Strings

struct USTRING_STRUCT{

void *vTable; // pointer to the tableint RefNo; // reference counterint Initialized;wchar_t *UnicodeBuffer; // pointer to unicode stringchar *AsciiBuffer; // pointer to ASCII stringint AsciiLength; // length of the ASCII stringint Reserved;int Length; // Length of unicode stringint LengthMax; // Size of UnicodeBuffer

};

Page 18: HexRaysCodeXplorer: object oriented RE for fun and profit

Identify Objects Constructors

Page 19: HexRaysCodeXplorer: object oriented RE for fun and profit

Identify Objects Constructors

Page 20: HexRaysCodeXplorer: object oriented RE for fun and profit

REconstructing Object’s Attributes

Page 21: HexRaysCodeXplorer: object oriented RE for fun and profit

REconstructing Object’s Attributes

Page 22: HexRaysCodeXplorer: object oriented RE for fun and profit

REconstructing Object’s Methods

Page 23: HexRaysCodeXplorer: object oriented RE for fun and profit

REconstructing Object’s Methods

Page 24: HexRaysCodeXplorer: object oriented RE for fun and profit

HexRaysCodeXplorer

Page 25: HexRaysCodeXplorer: object oriented RE for fun and profit

HexRaysCodeXplorer v1.0:released in 2013 at REcon

Page 26: HexRaysCodeXplorer: object oriented RE for fun and profit

HexRaysCodeXplorer Features

o Hex-Rays decompiler plugin

o The plugin was designed to facilitate static analysis of: object oriented code position independent code

o The plugin allows to: navigate through decompiled virtual methods partially reconstruct object type

Page 27: HexRaysCodeXplorer: object oriented RE for fun and profit

Hex-Rays Decompiler Plugin SDK

o At the heart of the decompiler lies ctree structure:

syntax tree structure

consists of citem_t objects

there are 9 maturity levels of the ctree structure

Page 28: HexRaysCodeXplorer: object oriented RE for fun and profit

Hex-Rays Decompiler Plugin SDK

o At the heart of the decompiler lies ctree structure:

syntax tree structure

consists of citem_t objects

there are 9 maturity levels of the ctree structure

Page 29: HexRaysCodeXplorer: object oriented RE for fun and profit

Hex-Rays Decompiler Plugin SDK

o Type citem_t is a base class for: cexpr_t – expression type cinsn_t – statement type

o Expressions have attached type information

o Statements include: block, if, for, while, do, switch, return, goto, asm

o Hex-Rays provides iterators for traversing the citem_t objects within ctreestructure: ctree_visitor_t ctree_parentee_t

citem_t

cexpr_t cinsn_t

Page 30: HexRaysCodeXplorer: object oriented RE for fun and profit

Hex-Rays Decompiler Plugin SDK

o Type citem_t is a base class for: cexpr_t – expression type cinsn_t – statement type

o Expressions have attached type information

o Statements include: block, if, for, while, do, switch, return, goto, asm

o Hex-Rays provides iterators for traversing the citem_t objects within ctreestructure: ctree_visitor_t ctree_parentee_t

citem_t

cexpr_t cinsn_t

Page 31: HexRaysCodeXplorer: object oriented RE for fun and profit

DEMO time :)

Page 32: HexRaysCodeXplorer: object oriented RE for fun and profit

HexRaysCodeXplorer: Gapz Position Independent Code

Page 33: HexRaysCodeXplorer: object oriented RE for fun and profit

HexRaysCodeXplorer: Virtual Methods

The IDA’s ‘Local Types’ is used to represent object type

Page 34: HexRaysCodeXplorer: object oriented RE for fun and profit

Hex-Rays decompiler plugin is used to navigate through the virtual methods

HexRaysCodeXplorer: Virtual Methods

Page 35: HexRaysCodeXplorer: object oriented RE for fun and profit

Hex-Rays decompiler plugin is used to navigate through the virtual methods

HexRaysCodeXplorer: Virtual Methods

Page 36: HexRaysCodeXplorer: object oriented RE for fun and profit

HexRaysCodeXplorer: Object Type REconstruction

o Hex-Rays’s ctree structure may be used to partially reconstruct object type based on its initialization routine (constructor)

o Input: pointer to the object instance

object initialization routine entry point

o Output: C structure-like object representation

Page 37: HexRaysCodeXplorer: object oriented RE for fun and profit

HexRaysCodeXplorer: Object Type REconstruction

citem_t objects to monitor: memptr

idx

memref

call (LOBYTE, etc.)

Page 38: HexRaysCodeXplorer: object oriented RE for fun and profit

HexRaysCodeXplorer: Object Type REconstruction

// reference of DWORD at offset 12 in buffer a1

*(DWORD *)(a1 + 12) = 0xEFCDAB89;

Page 39: HexRaysCodeXplorer: object oriented RE for fun and profit

HexRaysCodeXplorer v1.5 [H2HC Edition]

o New citem_t objects to monitor: memptr

idx

memref

call (LOBYTE, etc.) ptr, asg, …

Page 40: HexRaysCodeXplorer: object oriented RE for fun and profit

HexRaysCodeXplorer v1.5 [H2HC Edition]

o New citem_t objects to monitor: memptr

idx

memref

call (LOBYTE, etc.) ptr, asg, …

o Type propagation for nested function calls

Page 41: HexRaysCodeXplorer: object oriented RE for fun and profit

HexRaysCodeXplorer v1.5 [H2HC Edition]

o Features of v1.5 [H2HC Edition] :

Better Type Reconstruction• Improvements for parsing citem_t objects with PTR and ASG statements

• Recursive traversal of Ctree to reconstruct Types hierarchy

Navigate from Pseudo code window to Disassembly line

Hints for Ctree elements which point to Disassembly line

Support for x64 version of Hex-Rays Decompiler

Some bug fixes by user requests

Page 42: HexRaysCodeXplorer: object oriented RE for fun and profit

DEMO time :)

Page 43: HexRaysCodeXplorer: object oriented RE for fun and profit

HexRaysCodeXplorer: -> What are the next goals?

o Develop the next version on IdaPython

o Focus on the following features: Type reconstruction (C++, Objective-C)

Type Navigation (C++, Objective-C)

Vtables parsing based on Hex-Rays API

Ctree graph navigation improvements

Patterns for possible vuln detection

Page 44: HexRaysCodeXplorer: object oriented RE for fun and profit

Why python?

Page 45: HexRaysCodeXplorer: object oriented RE for fun and profit

Python Arsenal Contest

http://2014.zeronights.org/contests/python-arsenal-contest.html

Best exploit dev tool/plugin/lib

Best forensics tool/plugin/lib

Best reversing tool/plugin/lib

Best fuzzing tool/plugin/lib

Best malware analysis tool/plugin/lib

Page 46: HexRaysCodeXplorer: object oriented RE for fun and profit

Thank you for your attention!

HexRaysCodeXplorerhttp://REhints.com @REhints

https://github.com/REhints/HexRaysCodeXplorer