def runner(nomeFunc, mainString, testResult, fun_name): # Executa tudo no arquivo test.py com o comando "python3 test.py" grafo = Grafo() #walker = Ast_walker(grafo) codeAst = ast.parse(inspect.getsource(nomeFunc)) ##listCoverage = getCoverage(nomeFunc, mainString,fun_name, mod_name) #walker.visit(codeAst) #dentro do walker fazer com que o nó possua as informações #da Sintaxe Abstrata do código para poder gerar dados com base nisso cfg = CFGBuilder().build_from_file('foo.py', './foo.py') grafo = cfg._build_visual() #grafo.render(filename='g1.dot') paths = []#ACOSymple.createGraph(grafo) dataGenerator(paths, ast.dump(codeAst)) #del grafo
from staticfg import CFGBuilder userCfg = CFGBuilder().build_from_file('user.py', './auction/user.py') bidCfg = CFGBuilder().build_from_file('bid.py', './auction/bid.py') auctionCfg = CFGBuilder().build_from_file('auction.py', './auction/auction.py') #auctionEventCfg = CFGBuilder().build_from_file('auction_event.py','./auction/auction_event.py') bidCfg.build_visual('bidCfg', 'pdf') auctionCfg.build_visual('auctionCfg', 'pdf') #auctionEventCfg.build_visual('auctionEventCfg.pdf', 'pdf')
#!usr/bin/python3 import argparse from staticfg import CFGBuilder parser = argparse.ArgumentParser(description='Generate the control flow graph\ of a Python program') parser.add_argument('input_file', help='Path to a file containing a Python\ program for which the CFG must be generated') parser.add_argument('output_file', help='Path to a file where the produced\ visualisation of the CFG must be saved') args = parser.parse_args() cfg_name = args.input_file.split('/')[-1] cfg = CFGBuilder().build_from_file(cfg_name, args.input_file) cfg.build_visual(args.output_file, format='pdf', calls=True)
def main(argv): if len(argv) == 0: #se non ho passato nulla è un errore #print('Error: invalid use.') #print('python3.6 pythonCowObfuscator.py <source.py>') #sys.exit(2) raise IOError( 'Error: invalid use. Please use : "python3.6 pythonCowObfuscator.py <source.py>" ' ) try: opt, arg = getopt.getopt(argv, " ", ["idir="]) except getopt.GetoptError: #print('Error: invalid use.') #print('python3.6 pythonCowObfuscator.py <source.py>') #sys.exit(2) raise IOError( 'Error: invalid use. Please use : "python3.6 pythonCowObfuscator.py <source.py>" ' ) source = arg[0] #Nome del file passato #check se il file è un file .py extension = os.path.splitext(source)[1] #take extension of file if (extension != ".py"): raise IOError('Invalid input file. Please enter python file') #check il file è nella cartella if os.path.isfile(source): print("-----------") print("File exist") print("-----------") print('') print('') else: raise IOError('File not exist') # create dir result if not exists if not os.path.exists('result'): os.makedirs('result') print('-----------------') print(' ') print(' Obfuscate ') print(' ') print('-----------------') print('') print('') print('...In esecuzione...') print('') print('') # 0) create cfg cfg = CFGBuilder().build_from_file('Before_Obfuscate', source) a = cfg.build_visual('CFG/1)Before_Obfuscate', format='pdf', calls=True) # 1) dead code dead_code.start(source) # 1.1) create cfg cfg = CFGBuilder().build_from_file('After_Insertion_Dead_Code', './result/output.py') a = cfg.build_visual('CFG/2)After_Insertion_Dead_Cod', format='pdf', calls=True) # 2) gen sequence source = './result/output.py' #Apro l'output che ho creato prima nel dead code with open('./result/result1.py', 'w') as res: #creo il primo risultato for line in gen.replace_instructions( source ): #nel file "generate_equivalent_instructions_sequence.py" res.write(line) # 2.1) create cfg cfg = CFGBuilder().build_from_file('After_Change_Sequence', './result/result1.py') a = cfg.build_visual('CFG/3)After_Change_Sequence', format='pdf', calls=True) # 3) replace constants source = './result/result1.py' with open('./result/result2.py', 'w') as res: for line in rc.replace_constants(source): res.write(line) # 3.1) create cfg cfg = CFGBuilder().build_from_file('After_Replace_Constants', './result/result2.py') a = cfg.build_visual('CFG/4)After_Replace_Constants', format='pdf', calls=True) # 4) replace variables (nomi di variabili e nomi di funzioni con nomi a caso) source = './result/result2.py' with open('./result/result3.py', 'w') as res: lines, dic = ov.obfuscate( source ) #ritorna anche il dizionario delle variabile da cui fare il replace for line in lines: res.write(line) # 4.1) create cfg cfg = CFGBuilder().build_from_file('After_Replace_Variables', './result/result3.py') a = cfg.build_visual('CFG/5)After_Replace_Variables', format='pdf', calls=True) # 5) replace function source = './result/result3.py' with open('./result/obfuscated.py', 'w') as res: #obfuscated.py è il file finale obfuscato for line in of.obfuscate( source, dic ): #da anche il dizionario delle variabile da cui fare il replace #in questo caso interesseranno solo i nomi delle funzioni res.write(line) # 5.1) create cfg cfg = CFGBuilder().build_from_file('After_Replace_Function', './result/obfuscated.py') a = cfg.build_visual('CFG/6)After_Replace_Function', format='pdf', calls=True) print('----------------------') print(' ') print(' Text Execution ') print(' ') print('----------------------') print('') '''Test del tempo di esecuzione''' start_time = time.time() #faccio partire il cronometro os.system('python ' + arg[0]) #faccio partire il programma print("Tempo trascorso del file iniziale: %s" % (time.time() - start_time)) start_time = time.time() #faccio partire il cronometro os.system('chmod 777 ./result/obfuscated.py') #faccio partire il programma os.system('python ./result/obfuscated.py') print("Tempo trascorso del file offuscato: %s" % (time.time() - start_time)) print('') print('')
def generateSaltyCFG(): cfgObject = CFGBuilder().build_from_file('SALTY', 'salt.parser.py') print(dir(cfgObject), type(cfgObject)) cfgObject.build_visual('SALTY_CFG', 'pdf')
def generateTriangleCFG(): cfgObject = CFGBuilder().build_from_file('TRIANGLE', 'triangle.py') print(dir(cfgObject), type(cfgObject)) cfgObject.build_visual('TRIANGLE_CFG', 'pdf')
""" Return dictionary of K:tuple(source, target) to V:condition """ def getConditions(graph): # Get Cutset condDict = {} for node in graph: reLi = re.findall("\d -> \d", node) if reLi: arr = reLi[0].split("->") stTuple = (int(arr[0]),int(arr[1])) condDict[stTuple] = re.findall("\"[^\"]*\"", node)[0].split("\"")[1] return condDict cfg = CFGBuilder().build_from_file('z3_test.py', './z3_test.py') graph = cfg._build_visual() # graph.render('z3_graph.gv', view=True) # print(graph.graph_attr) # first pass - save nodes with location # second pass - find back-edges and put cut points at locations # Get Code Frag definitions cDict = getCodeDef(graph) print(cDict) # Get condition statements parsed condDict = getConditions(graph) cutSet = set()
from pathlib import Path from staticfg import CFGBuilder FILEPATH = Path(__file__).parent.joinpath("syntax_tree.py") cfg = CFGBuilder().build_from_file('test', FILEPATH) cfg.build_visual('test', 'png')