HexRaysCodeXplorer: object oriented RE for fun and profit 
Alexander Matrosov 
@matrosov 
Eugene Rodionov 
@vxradius
C++ Code Reconstruction Problems 
Show problems on real examples (Flamer) 
HexRaysCodeXplorerv1.5 [H2HC Edition] Agenda
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 
Vftablereconstruction 
Associate vftableobject with exact object 
Class hierarchy reconstruction
C++ Code Reconstruction Problems Class AvfPtra1() a2() A::vfTablemetaA::a1() A::a2() RTTI Object LocatorsignaturepTypeDescriptorpClassDescriptor
C++ Code Reconstruction Problems
REconstructingFlamer Framework
An overview of the Flamer Framework 
Vector<Command Executor> 
DB_Query ClanCmd 
Vector<Task> 
IDLER CmdExec 
Vector<DelayedTasks> 
Euphoria 
Share 
Supplier 
Vector<Consumer> 
Mobile 
Consumer 
Cmd 
Consumer 
Sniffer Munch FileFinder 
FileCollect Driller GetConfig 
LSS 
Sender 
Frog Beetlejuice 
Lua 
Consumer 
Media 
Consumer 
http://www.welivesecurity.com/2012/08/02/flamer-analysis-framework-reconstruction/
An overview of the Flamer Framework 
Vector<Command Executor> 
DB_Query ClanCmd 
Vector<Task> 
IDLER CmdExec 
Vector<DelayedTasks> 
Euphoria 
Share 
Supplier 
Vector<Consumer> 
Mobile 
Consumer 
Cmd 
Consumer 
Sniffer Munch FileFinder 
FileCollect Driller GetConfig 
LSS 
Sender 
Frog Beetlejuice 
Lua 
Consumer 
Media 
Consumer 
http://www.welivesecurity.com/2012/08/02/flamer-analysis-framework-reconstruction/
An overview of the Flamer Framework 
Vector<Command Executor> 
DB_Query ClanCmd 
Vector<Task> 
IDLER CmdExec 
Vector<DelayedTasks> 
Euphoria 
Share 
Supplier 
Vector<Consumer> 
Mobile 
Consumer 
Cmd 
Consumer 
Sniffer Munch FileFinder 
FileCollect Driller GetConfig 
LSS 
Sender 
Frog Beetlejuice 
Lua 
Consumer 
Media 
Consumer 
http://www.welivesecurity.com/2012/08/02/flamer-analysis-framework-reconstruction/
Identify Smart Pointer Structure 
oSmart pointers 
oStrings 
oVectors to maintain the objects 
oCustom data types: 
wrappers 
tasks, 
triggers 
and etc.
Data Types Being Used: Smart pointers 
typedefstructSMART_PTR 
{ 
void *pObject;// pointer to the object 
int*RefNo;// reference counter 
};
Identify Smart Pointer Structure
Data Types Being Used: Vectors 
structVECTOR 
{ 
void *vTable;// pointer to the table 
intNumberOfItems;// self-explanatory 
intMaxSize;// self-explanatory 
void *vector;// pointer to buffer with elements 
}; 
oUsed to handle the objects: 
tasks 
triggers 
etc.
Identify Exact Virtual Function Call in Vtable
Identify Exact Virtual Function Call in Vtable
Identify Custom Type Operations
Data Types Being Used: Strings 
structUSTRING_STRUCT 
{ 
void *vTable;// pointer to the table 
intRefNo;// reference counter 
intInitialized; 
wchar_t*UnicodeBuffer;// pointer to unicodestring 
char *AsciiBuffer;// pointer to ASCII string 
intAsciiLength;// length of the ASCII string 
intReserved; 
intLength;// Length of unicodestring 
intLengthMax;// Size of UnicodeBuffer 
};
Identify Objects Constructors
Identify Objects Constructors
REconstructingObject’s Attributes
REconstructingObject’s Attributes
REconstructingObject’s Methods
REconstructingObject’s Methods
HexRaysCodeXplorer
HexRaysCodeXplorerv1.0: released in 2013 at REcon
HexRaysCodeXplorerFeatures 
oHex-Rays decompilerplugin 
oThe plugin was designed to facilitate static analysis of: 
object oriented code 
position independent code 
oThe plugin allows to: 
navigate through decompiled virtual methods 
partially reconstruct object type
Hex-Rays DecompilerPlugin SDK 
oAt the heart of the decompilerlies ctreestructure: 
syntax tree structure 
consists of citem_tobjects 
there are 9 maturity levels of the ctreestructure
Hex-Rays DecompilerPlugin SDK 
oAt the heart of the decompilerlies ctreestructure: 
syntax tree structure 
consists of citem_tobjects 
there are 9 maturity levels of the ctreestructure
Hex-Rays DecompilerPlugin SDK 
oType citem_tis a base class for: 
cexpr_t–expression type 
cinsn_t–statement type 
oExpressions have attached type information 
oStatements include: 
block, if, for, while, do, switch, return, goto, asm 
oHex-Rays provides iterators for traversing the citem_tobjects within ctreestructure: 
ctree_visitor_t 
ctree_parentee_t 
citem_tcexpr_tcinsn_t
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 ctree 
structure: 
 ctree_visitor_t 
 ctree_parentee_t 
citem_t 
cexpr_t cinsn_t
DEMO time :)
HexRaysCodeXplorer: GapzPosition Independent Code
HexRaysCodeXplorer: Virtual Methods 
The IDA’s ‘Local Types’ is used to represent object type
Hex-Rays decompilerplugin is used to navigate through the virtual methods 
HexRaysCodeXplorer: Virtual Methods
Hex-Rays decompilerplugin is used to navigate through the virtual methods 
HexRaysCodeXplorer: Virtual Methods
HexRaysCodeXplorer: Object Type REconstruction 
oHex-Rays’sctreestructure may be used to partially reconstruct object type based on its initialization routine (constructor) 
oInput: 
pointer to the object instance 
object initialization routine entry point 
oOutput: 
C structure-like object representation
HexRaysCodeXplorer: Object Type REconstruction 
citem_tobjects to monitor: 
memptr 
idx 
memref 
call (LOBYTE, etc.)
HexRaysCodeXplorer: Object Type REconstruction 
//reference of DWORD at offset 12 in buffer a1 
*(DWORD *)(a1 + 12) = 0xEFCDAB89;
HexRaysCodeXplorerv1.5 [H2HC Edition] 
oNew citem_tobjects to monitor: 
memptr 
idx 
memref 
call (LOBYTE, etc.) 
ptr, asg, …
HexRaysCodeXplorerv1.5 [H2HC Edition] 
oNew citem_tobjects to monitor: 
memptr 
idx 
memref 
call (LOBYTE, etc.) 
ptr, asg, … 
oType propagation for nested function calls
HexRaysCodeXplorerv1.5 [H2HC Edition] 
oFeatures of v1.5 [H2HC Edition] : 
Better Type Reconstruction 
•Improvements for parsing citem_tobjects with PTR andASG statements 
•Recursive traversal of Ctreeto reconstruct Types hierarchy 
Navigate from Pseudo code window to Disassembly line 
Hints for Ctreeelements which point to Disassembly line 
Support for x64 version of Hex-Rays Decompiler 
Some bug fixes by user requests
DEMO time :)
HexRaysCodeXplorer: -> What are the next goals? 
oDevelop the next version on IdaPython 
oFocus on the following features: 
Type reconstruction(C++, Objective-C) 
Type Navigation (C++, Objective-C) 
Vtablesparsing based on Hex-Rays API 
Ctreegraph navigation improvements 
Patterns for possible vulndetection
Why python?
Python Arsenal Contesthttp://2014.zeronights.org/contests/python-arsenal-contest.html 
Best exploit devtool/plugin/lib 
Best forensics tool/plugin/lib 
Best reversing tool/plugin/lib 
Best fuzzing tool/plugin/lib 
Best malware analysis tool/plugin/lib
Thank you for your attention! 
HexRaysCodeXplorer 
http://REhints.com@REhints 
https://github.com/REhints/HexRaysCodeXplorer

HexRaysCodeXplorer: object oriented RE for fun and profit

  • 1.
    HexRaysCodeXplorer: object orientedRE for fun and profit Alexander Matrosov @matrosov Eugene Rodionov @vxradius
  • 2.
    C++ Code ReconstructionProblems Show problems on real examples (Flamer) HexRaysCodeXplorerv1.5 [H2HC Edition] Agenda
  • 3.
    C++ Code ReconstructionProblems 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 Vftablereconstruction Associate vftableobject with exact object Class hierarchy reconstruction
  • 4.
    C++ Code ReconstructionProblems Class AvfPtra1() a2() A::vfTablemetaA::a1() A::a2() RTTI Object LocatorsignaturepTypeDescriptorpClassDescriptor
  • 5.
  • 6.
  • 7.
    An overview ofthe Flamer Framework Vector<Command Executor> DB_Query ClanCmd Vector<Task> IDLER CmdExec Vector<DelayedTasks> Euphoria Share Supplier Vector<Consumer> Mobile Consumer Cmd Consumer Sniffer Munch FileFinder FileCollect Driller GetConfig LSS Sender Frog Beetlejuice Lua Consumer Media Consumer http://www.welivesecurity.com/2012/08/02/flamer-analysis-framework-reconstruction/
  • 8.
    An overview ofthe Flamer Framework Vector<Command Executor> DB_Query ClanCmd Vector<Task> IDLER CmdExec Vector<DelayedTasks> Euphoria Share Supplier Vector<Consumer> Mobile Consumer Cmd Consumer Sniffer Munch FileFinder FileCollect Driller GetConfig LSS Sender Frog Beetlejuice Lua Consumer Media Consumer http://www.welivesecurity.com/2012/08/02/flamer-analysis-framework-reconstruction/
  • 9.
    An overview ofthe Flamer Framework Vector<Command Executor> DB_Query ClanCmd Vector<Task> IDLER CmdExec Vector<DelayedTasks> Euphoria Share Supplier Vector<Consumer> Mobile Consumer Cmd Consumer Sniffer Munch FileFinder FileCollect Driller GetConfig LSS Sender Frog Beetlejuice Lua Consumer Media Consumer http://www.welivesecurity.com/2012/08/02/flamer-analysis-framework-reconstruction/
  • 10.
    Identify Smart PointerStructure oSmart pointers oStrings oVectors to maintain the objects oCustom data types: wrappers tasks, triggers and etc.
  • 11.
    Data Types BeingUsed: Smart pointers typedefstructSMART_PTR { void *pObject;// pointer to the object int*RefNo;// reference counter };
  • 12.
  • 13.
    Data Types BeingUsed: Vectors structVECTOR { void *vTable;// pointer to the table intNumberOfItems;// self-explanatory intMaxSize;// self-explanatory void *vector;// pointer to buffer with elements }; oUsed to handle the objects: tasks triggers etc.
  • 14.
    Identify Exact VirtualFunction Call in Vtable
  • 15.
    Identify Exact VirtualFunction Call in Vtable
  • 16.
  • 17.
    Data Types BeingUsed: Strings structUSTRING_STRUCT { void *vTable;// pointer to the table intRefNo;// reference counter intInitialized; wchar_t*UnicodeBuffer;// pointer to unicodestring char *AsciiBuffer;// pointer to ASCII string intAsciiLength;// length of the ASCII string intReserved; intLength;// Length of unicodestring intLengthMax;// Size of UnicodeBuffer };
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
    HexRaysCodeXplorerFeatures oHex-Rays decompilerplugin oThe plugin was designed to facilitate static analysis of: object oriented code position independent code oThe plugin allows to: navigate through decompiled virtual methods partially reconstruct object type
  • 27.
    Hex-Rays DecompilerPlugin SDK oAt the heart of the decompilerlies ctreestructure: syntax tree structure consists of citem_tobjects there are 9 maturity levels of the ctreestructure
  • 28.
    Hex-Rays DecompilerPlugin SDK oAt the heart of the decompilerlies ctreestructure: syntax tree structure consists of citem_tobjects there are 9 maturity levels of the ctreestructure
  • 29.
    Hex-Rays DecompilerPlugin SDK oType citem_tis a base class for: cexpr_t–expression type cinsn_t–statement type oExpressions have attached type information oStatements include: block, if, for, while, do, switch, return, goto, asm oHex-Rays provides iterators for traversing the citem_tobjects within ctreestructure: ctree_visitor_t ctree_parentee_t citem_tcexpr_tcinsn_t
  • 30.
    Hex-Rays Decompiler PluginSDK 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 ctree structure:  ctree_visitor_t  ctree_parentee_t citem_t cexpr_t cinsn_t
  • 31.
  • 32.
  • 33.
    HexRaysCodeXplorer: Virtual Methods The IDA’s ‘Local Types’ is used to represent object type
  • 34.
    Hex-Rays decompilerplugin isused to navigate through the virtual methods HexRaysCodeXplorer: Virtual Methods
  • 35.
    Hex-Rays decompilerplugin isused to navigate through the virtual methods HexRaysCodeXplorer: Virtual Methods
  • 36.
    HexRaysCodeXplorer: Object TypeREconstruction oHex-Rays’sctreestructure may be used to partially reconstruct object type based on its initialization routine (constructor) oInput: pointer to the object instance object initialization routine entry point oOutput: C structure-like object representation
  • 37.
    HexRaysCodeXplorer: Object TypeREconstruction citem_tobjects to monitor: memptr idx memref call (LOBYTE, etc.)
  • 38.
    HexRaysCodeXplorer: Object TypeREconstruction //reference of DWORD at offset 12 in buffer a1 *(DWORD *)(a1 + 12) = 0xEFCDAB89;
  • 39.
    HexRaysCodeXplorerv1.5 [H2HC Edition] oNew citem_tobjects to monitor: memptr idx memref call (LOBYTE, etc.) ptr, asg, …
  • 40.
    HexRaysCodeXplorerv1.5 [H2HC Edition] oNew citem_tobjects to monitor: memptr idx memref call (LOBYTE, etc.) ptr, asg, … oType propagation for nested function calls
  • 41.
    HexRaysCodeXplorerv1.5 [H2HC Edition] oFeatures of v1.5 [H2HC Edition] : Better Type Reconstruction •Improvements for parsing citem_tobjects with PTR andASG statements •Recursive traversal of Ctreeto reconstruct Types hierarchy Navigate from Pseudo code window to Disassembly line Hints for Ctreeelements which point to Disassembly line Support for x64 version of Hex-Rays Decompiler Some bug fixes by user requests
  • 42.
  • 43.
    HexRaysCodeXplorer: -> Whatare the next goals? oDevelop the next version on IdaPython oFocus on the following features: Type reconstruction(C++, Objective-C) Type Navigation (C++, Objective-C) Vtablesparsing based on Hex-Rays API Ctreegraph navigation improvements Patterns for possible vulndetection
  • 44.
  • 45.
    Python Arsenal Contesthttp://2014.zeronights.org/contests/python-arsenal-contest.html Best exploit devtool/plugin/lib Best forensics tool/plugin/lib Best reversing tool/plugin/lib Best fuzzing tool/plugin/lib Best malware analysis tool/plugin/lib
  • 46.
    Thank you foryour attention! HexRaysCodeXplorer http://REhints.com@REhints https://github.com/REhints/HexRaysCodeXplorer