-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathquatitative_analysis.py
84 lines (65 loc) · 3.31 KB
/
quatitative_analysis.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
import json
import re
import csv
# List of special opcodes to analyze
special_opcodes = ["jmp", "call", "mov", "xor", "push", "pop", "add", "sub"]
def extract_opcode_frequency(code_block):
opcode_frequency = {opcode: 0 for opcode in special_opcodes}
opcode_frequency["ds:"] = 0
opcode_frequency["hex_values"] = 0
hex_pattern = re.compile(r'\b[0-9a-fA-F]+h\b')
# Count opcode occurrences
for instruction in code_block:
for opcode in special_opcodes:
if opcode in instruction:
opcode_frequency[opcode] += 1
# Count ds: occurrences
if "ds:" in instruction:
opcode_frequency["ds:"] += 1
# Count hexadecimal value occurrences
if hex_pattern.search(instruction):
opcode_frequency["hex_values"] += 1
return opcode_frequency
def normalize_frequency(opcode_frequency, total_instructions):
normalized_frequency = {}
for opcode, count in opcode_frequency.items():
normalized_frequency[opcode] = count / total_instructions
return normalized_frequency
def analyze_sample(sample_json):
# Extract code blocks from JSON
sample_code_blocks = sample_json['CPID_Features']
# Initialize variables
total_opcode_frequency = {opcode: 0 for opcode in special_opcodes}
total_opcode_frequency["ds:"] = 0 # Add this line
total_opcode_frequency["hex_values"] = 0 # Add this line
total_instructions = 0
# Analyze each code block
for block_name, code_block in sample_code_blocks.items():
opcode_frequency = extract_opcode_frequency(code_block)
total_instructions += len(code_block)
# Accumulate opcode frequencies
for opcode, count in opcode_frequency.items(): # Modify this line
total_opcode_frequency[opcode] += count # Modify this line
# Normalize opcode frequency
normalized_frequency = normalize_frequency(total_opcode_frequency, total_instructions)
return total_opcode_frequency, normalized_frequency
def save_to_csv(opcode_frequency, normalized_frequency, filename):
with open(filename, mode='w', newline='') as file:
writer = csv.writer(file)
# Write header
writer.writerow(["Opcode/Operand", "Frequency", "Normalized Frequency"])
# Write data
for opcode, freq in opcode_frequency.items():
norm_freq = normalized_frequency[opcode]
writer.writerow([opcode, freq, norm_freq])
# Example usage:
if __name__ == '__main__':
subgraph_file = "C:\\Users\\Saqib\\PycharmProjects\\GAGE\\expriment_out\\benign\\result_with_nodes_extracted_ex__1_benign_0a925bae354e3a051366c460f22821eeb15ea4730a4c904c24360da897bb3331__v1.tmp0.json"
subgraph_file_quantitative = "C:\\Users\\Saqib\\PycharmProjects\\GAGE\\expriment_out\\benign\\result_with_nodes_extracted_ex__1_benign_0a925bae354e3a051366c460f22821eeb15ea4730a4c904c24360da897bb3331__v1.tmp0.csv"
with open(subgraph_file, 'r') as file:
data = json.load(file)
opcode_frequency, normalized_frequency = analyze_sample(data)
# Output the results
print(f"Opcode Frequency: {opcode_frequency}")
print(f"Normalized Frequency: {normalized_frequency}")
save_to_csv(opcode_frequency, normalized_frequency, subgraph_file_quantitative)