<s> from subprocess import Popen , PIPE , STDOUT <EOL> import os , sys <EOL> import xmlrpclib <EOL> import cPickle <EOL> class _Method ( object ) : <EOL> def __init__ ( self , proxy , name ) : <EOL> self . proxy = proxy <EOL> self . name = name <EOL> def __call__ ( self , * args ) : <EOL> z = getattr ( self . proxy , self . name , None ) <EOL> try : <EOL> if len ( args ) == <NUM_LIT:1> : <EOL> ret = z ( cPickle . dumps ( args [ <NUM_LIT:0> ] ) ) <EOL> else : <EOL> ret = z ( cPickle . dumps ( args ) ) <EOL> return cPickle . loads ( ret ) <EOL> except xmlrpclib . ProtocolError : <EOL> return [ ] <EOL> class MyXMLRPC ( object ) : <EOL> def __init__ ( self , proxy ) : <EOL> self . proxy = proxy <EOL> def __getattr__ ( self , name ) : <EOL> return _Method ( self . proxy , name ) <EOL> class BasicBlock ( object ) : <EOL> def __init__ ( self , ins ) : <EOL> self . ins = ins <EOL> def show ( self ) : <EOL> for i in self . ins : <EOL> print i <EOL> class Function ( object ) : <EOL> def __init__ ( self , name , start_ea , instructions , information ) : <EOL> self . name = name <EOL> self . start_ea = start_ea <EOL> self . information = information <EOL> self . basic_blocks = [ ] <EOL> self . instructions = instructions <EOL> r = { } <EOL> idx = <NUM_LIT:0> <EOL> for i in instructions : <EOL> r [ i [ <NUM_LIT:0> ] ] = idx <EOL> idx += <NUM_LIT:1> <EOL> for i in information [ <NUM_LIT:0> ] : <EOL> try : <EOL> start = r [ i [ <NUM_LIT:0> ] ] <EOL> end = r [ i [ <NUM_LIT:1> ] ] + <NUM_LIT:1> <EOL> self . basic_blocks . append ( BasicBlock ( instructions [ start : end ] ) ) <EOL> except KeyError : <EOL> pass <EOL> def get_instructions ( self ) : <EOL> return [ i for i in self . instructions ] <EOL> def run_ida ( idapath , wrapper_init_path , binpath ) : <EOL> os . environ [ "<STR_LIT>" ] = "<STR_LIT:1>" <EOL> pid = os . fork ( ) <EOL> if pid == <NUM_LIT:0> : <EOL> wrapper_path = "<STR_LIT>" + wrapper_init_path <EOL> l = [ idapath , "<STR_LIT>" , wrapper_path , binpath ] <EOL> print l <EOL> compile = Popen ( l , stdout = open ( '<STR_LIT>' , '<STR_LIT:w>' ) , stderr = STDOUT ) <EOL> stdout , stderr = compile . communicate ( ) <EOL> sys . exit ( <NUM_LIT:0> ) <EOL> class IDAPipe ( object ) : <EOL> def __init__ ( self , idapath , binpath , wrapper_init_path ) : <EOL> self . idapath = idapath <EOL> self . binpath = binpath <EOL> self . proxy = None <EOL> run_ida ( self . idapath , self . binpath , wrapper_init_path ) <EOL> while <NUM_LIT:1> : <EOL> try : <EOL> self . proxy = xmlrpclib . ServerProxy ( "<STR_LIT>" ) <EOL> self . proxy . is_connected ( ) <EOL> break <EOL> except : <EOL> pass <EOL> self . proxy = MyXMLRPC ( self . proxy ) <EOL> def quit ( self ) : <EOL> try : <EOL> self . proxy . quit ( ) <EOL> except : <EOL> pass <EOL> def _build_functions ( self , functions ) : <EOL> F = { } <EOL> for i in functions : <EOL> F [ i ] = Function ( functions [ i ] [ <NUM_LIT:0> ] , i , functions [ i ] [ <NUM_LIT:1> : - <NUM_LIT:1> ] , functions [ i ] [ - <NUM_LIT:1> ] ) <EOL> return F <EOL> def get_quick_functions ( self ) : <EOL> functions = self . get_raw ( ) <EOL> return self . _build_functions ( functions ) <EOL> def get_raw ( self ) : <EOL> return self . proxy . get_raw ( ) <EOL> def get_nb_functions ( self ) : <EOL> return len ( self . proxy . Functions ( ) ) <EOL> def get_functions ( self ) : <EOL> for function_ea in self . proxy . Functions ( ) : <EOL> self . get_function_addr ( function_ea ) <EOL> def get_function_name ( self , name ) : <EOL> function_ea = self . proxy . get_function ( name ) <EOL> self . get_function_addr ( function_ea ) <EOL> def get_function_addr ( self , function_ea ) : <EOL> if function_ea == - <NUM_LIT:1> : <EOL> return <EOL> f_start = function_ea <EOL> f_end = self . proxy . GetFunctionAttr ( function_ea , <NUM_LIT:4> ) <EOL> edges = set ( ) <EOL> boundaries = set ( ( f_start , ) ) <EOL> for head in self . proxy . Heads ( f_start , f_end ) : <EOL> if self . proxy . isCode ( self . proxy . GetFlags ( head ) ) : <EOL> refs = self . proxy . CodeRefsFrom ( head , <NUM_LIT:0> ) <EOL> refs = set ( filter ( lambda x : x >= f_start and x <= f_end , refs ) ) <EOL> if refs : <EOL> next_head = self . proxy . NextHead ( head , f_end ) <EOL> if self . proxy . isFlow ( self . proxy . GetFlags ( next_head ) ) : <EOL> refs . add ( next_head ) <EOL> boundaries . update ( refs ) <EOL> for r in refs : <EOL> if self . proxy . isFlow ( self . proxy . GetFlags ( r ) ) : <EOL> edges . add ( ( self . proxy . PrevHead ( r , f_start ) , r ) ) <EOL> edges . add ( ( head , r ) ) <EOL> sorted_boundaries = sorted ( boundaries , reverse = True ) <EOL> end_addr = self . proxy . PrevHead ( f_end , f_start ) <EOL> bb_addr = [ ] <EOL> for begin_addr in sorted_boundaries : <EOL> bb_addr . append ( ( begin_addr , end_addr ) ) <EOL> end_addr = self . proxy . PrevHead ( begin_addr , f_start ) <EOL> while not self . proxy . isCode ( self . proxy . GetFlags ( end_addr ) ) : <EOL> end_addr = self . proxy . PrevHead ( end_addr , f_start ) <EOL> bb_addr . reverse ( ) <EOL> def display_function ( f ) : <EOL> print f , f . name , f . information <EOL> for i in f . basic_blocks : <EOL> print i <EOL> i . show ( ) </s>
