-
Notifications
You must be signed in to change notification settings - Fork 40
/
Copy pathsafe.py
56 lines (41 loc) · 2.06 KB
/
safe.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# SAFE TEAM
# Copyright (C) 2019 Luca Massarelli, Giuseppe Antonio Di Luna, Fabio Petroni, Leonardo Querzoni, Roberto Baldoni
from asm_embedding.FunctionAnalyzerRadare import RadareFunctionAnalyzer
from argparse import ArgumentParser
from asm_embedding.FunctionNormalizer import FunctionNormalizer
from asm_embedding.InstructionsConverter import InstructionsConverter
from neural_network.SAFEEmbedder import SAFEEmbedder
from utils import utils
class SAFE:
def __init__(self, model):
self.converter = InstructionsConverter("data/i2v/word2id.json")
self.normalizer = FunctionNormalizer(max_instruction=150)
self.embedder = SAFEEmbedder(model)
self.embedder.loadmodel()
self.embedder.get_tensor()
def embedd_function(self, filename, address):
analyzer = RadareFunctionAnalyzer(filename, use_symbol=False, depth=0)
functions = analyzer.analyze()
instructions_list = None
for function in functions:
if functions[function]['address'] == address:
instructions_list = functions[function]['filtered_instructions']
break
if instructions_list is None:
print("Function not found")
return None
converted_instructions = self.converter.convert_to_ids(instructions_list)
instructions, length = self.normalizer.normalize_functions([converted_instructions])
embedding = self.embedder.embedd(instructions, length)
return embedding
if __name__ == '__main__':
utils.print_safe()
parser = ArgumentParser(description="Safe Embedder")
parser.add_argument("-m", "--model", help="Safe trained model to generate function embeddings")
parser.add_argument("-i", "--input", help="Input executable that contains the function to embedd")
parser.add_argument("-a", "--address", help="Hexadecimal address of the function to embedd")
args = parser.parse_args()
address = int(args.address, 16)
safe = SAFE(args.model)
embedding = safe.embedd_function(args.input, address)
print(embedding[0])