Nsec 2015
Nsec 2015
HexRaysCodeXplorer
Eugene Rodionov Alex Matrosov
@vxradius @matrosov
Agenda
* Object Oriented Code Reversing Challenges
-- virtual methods
-- templates
* HexRaysCodeXplorer in use
Modern C++ Malware for Targeted Attacks
Why reversing C++ code
is a hard problem?
Virtual Methods & Templates
Virtual Methods class Animal {
protected:
int _weight;
public:
class Cat { Animal(int weight) : _weight(weight) {};
private: virtual int eat(int food) = 0;
int _weight; };
public:
Cat(int weight) : _weight(weight) {}; class Cat : Animal {
public:
int eat(int food) { Cat(int weight) : Animal(weight) {};
};
return _weight += food; vs
virtual int eat(int food) {
}; return _weight += food;
};
int _tmain(int argc, _TCHAR* argv[]) };
{
Cat* cat = new Cat(130); int _tmain(int argc, _TCHAR* argv[])
int newWeigth = cat->eat(20); {
} Animal* cat = new Cat(130);
int newWeight = cat->eat(20);
}
Virtual Methods class Animal {
protected:
int _weight;
public:
class Cat { Animal(int weight) : _weight(weight) {};
private: virtual int eat(int food) = 0;
int _weight; };
public:
Cat(int weight) : _weight(weight) {}; class Cat : Animal {
public:
int eat(int food) { Cat(int weight) : Animal(weight) {};
};
return _weight += food; vs
virtual int eat(int food) {
}; return _weight += food;
};
int _tmain(int argc, _TCHAR* argv[]) };
{
Cat* cat = new Cat(130); int _tmain(int argc, _TCHAR* argv[])
int newWeigth = cat->eat(20); {
} Animal* cat = new Cat(130);
int newWeight = cat->eat(20);
}
Virtual Function Tables
Class A meta
* initialized in constructors
-- need to track back object creation
C++ Templates
* extra code to analyze
-- another way to create polymorphic types
std::vector<int> std::vector<char>
std::vector<std::string> std::vector<custom_type>
* RTTI reconstruction
-- vftable reconstruction
-- Associate vftable object with exact object
-- class hierarchy reconstruction
Reversing Object
Oriented Malware
Practical Approaches: REconstructing Flamer Framework
REconstructing Flamer Framework
Vector<Consumer> Vector<Command Executor>
Cmd Vector<Task>
Consumer
IDLER CmdExec Sniffer Munch FileFinder
Lua
Consumer
Vector<DelayedTasks>
Media Share LSS
Consumer Euphoria Frog Beetlejuice
Supplier Sender
http://www.welivesecurity.com/2012/08/02/flamer-analysis-framework-reconstruction/
REconstructing Flamer Framework
Vector<Consumer> Vector<Command Executor>
Cmd Vector<Task>
Consumer
IDLER CmdExec Sniffer Munch FileFinder
Lua
Consumer
Vector<DelayedTasks>
Media Share LSS
Consumer Euphoria Frog Beetlejuice
Supplier Sender
http://www.welivesecurity.com/2012/08/02/flamer-analysis-framework-reconstruction/
Identifying Used Types
* Smart pointers
* Strings
* Reconstruct Object
Attributes
Control Flow Graph
Reconstruction
* Reconstruct Object Methods
Identifying Object Constructors
REconstructing Object’s Attributes
REconstructing Object’s Attributes
REconstructing Object’s Methods
REconstructing Object’s Methods
REconstructing Object’s Methods
Reversing Object
Oriented Malware
Practical Approaches: REconstructing XAgent Framework
XAgent Framework
Agent Modules
Vector<IAgentModule>
AgentKernel
AgentKernel Local Channel
Cryptor DNameNode
Storage Controller
Module
FileSystem
Module
Remote
KeyLogger Communication Channels
Vector<IAgentChannel>
Process
Retranslator WinHttp
Module
http://www.welivesecurity.com/2014/10/08/sednit-espionage-group-now-using-custom-exploit-kit/
Object Interconnection: IAgentModule
struct IAgentModule {
LPVOID receiveMessage;
LPVOID sendMessage;
LPVOID getModuleId;
LPVOID setModuleId; IAgentModule
LPVOID executeModule;
};
Module Process
Module
AgentKernel Remote Retranslator
FileSystem
Keylogger Module
Exploring RTTI*
* recover type names
Local
DataStorage
Registry File
reader/writer reader/writer
XAgent: Cryptor
XAgent: Cryptor
salt
encrypted message (4 bytes)
key RC4
plain text
XAgent: IReservedApi
XAgent: Identifying Used Types
* Strings: std::string
* Strings: std::string
* Statements include:
-- block, if, for, while, do, switch, return, goto, asm
* Statements include:
-- block, if, for, while, do, switch, return, goto, asm
* Input:
-- pointer to the object instance
-- object initialization routine entry point
* Output:
-- C structure-like object representation
HexRaysCodeXplorer: Object Type REconstruction
* citem_t objects:
-- memptr, idx, memref
-- call, ptr, asg
HexRaysCodeXplorer: Object Type REconstruction
* citem_t objects:
-- memptr, idx, memref
-- call, ptr, asg
HexRaysCodeXplorer: Object Type REconstruction
// reference of DWORD at offset 12 in buffer a1
*(DWORD *)(a1 + 12) = 0xEFCDAB89;
HexRaysCodeXplorer: v1.7 [NSEC Edition]
+
Type reconstruction
HexRaysCodeXplorer: v1.7 [NSEC Edition]
* Bugfixes
DEMO time :)
HexRaysCodeXplorer: Next plans
* Switch to IdaPython
Why python?
HexRaysCodeXplorer: Next plans
* Switch to IdaPython
http://REhints.com
@Rehints
https://github.com/REhints/HexRaysCodeXplorer