asm_to_csv/json_feature2json.py

83 lines
3.6 KiB
Python
Raw Normal View History

2024-04-17 15:54:00 +08:00
import os
2024-04-20 13:20:21 +08:00
from my_utils import save_json, load_json, setup_logger, multi_thread_order, THREAD_HALF, THREAD_FULL
2024-04-17 15:54:00 +08:00
from bert.obtain_inst_vec import bb2vec
import multiprocessing
from tqdm import tqdm
import warnings
2024-04-18 16:44:31 +08:00
from datetime import datetime
2024-04-20 13:20:21 +08:00
import concurrent.futures
2024-04-17 15:54:00 +08:00
2024-04-20 13:20:21 +08:00
warnings.filterwarnings("ignore")
2024-04-17 15:54:00 +08:00
def addr2vec(base_file_path):
# 从路径拆分文件名与路径
file_name = str(os.path.basename(base_file_path))
file_path = str(os.path.dirname(base_file_path))
# 如果不是路径则开始转化
if file_name:
2024-04-20 13:20:21 +08:00
# 忽略已生成的文件
if os.path.exists(os.path.join(file_path, 'final', file_name)):
process_bar.update(1)
return
2024-04-17 15:54:00 +08:00
file_json = load_json(base_file_path)
# 确保存在基础文件而不存在特征文件的情况
feature_json = load_json(os.path.join(file_path, 'feature', file_name)) if os.path.exists(
os.path.join(file_path, 'feature', file_name)) else None
if feature_json is not None:
feature_set = {}
2024-04-20 13:20:21 +08:00
# 多线程预测bert
# with concurrent.futures.ThreadPoolExecutor(max_workers=os.cpu_count()) as executor:
# try:
# future_args = {
# executor.submit(bb2vec, item): item for item in feature_json
# }
# for future in concurrent.futures.as_completed(future_args):
# result = future.result()
# feature_set[result[0]] = result[1]
# except Exception as e:
# logger.error(f"bert 解析出错 {file_name}{e}")
with multiprocessing.Pool(processes=os.cpu_count()) as pool:
try:
results = pool.imap_unordered(bb2vec, [item for item in feature_json])
for result in results:
feature_set[result[0]] = result[1]
except Exception as e:
logger.error(f"bert 解析出错 {file_name}{e}")
try:
for item in file_json['acfg_list']:
bb_feature_addr_list = item['block_features']
item['block_features'] = [feature_set[key] for key in bb_feature_addr_list]
except Exception as e:
logger.error(f"地址对应出错{file_name}, {e}")
2024-04-17 15:54:00 +08:00
save_json(os.path.join(file_path, 'final', file_name), file_json)
2024-04-20 13:20:21 +08:00
2024-04-17 15:54:00 +08:00
else:
logger.error(f'文件{file_name}不存在特征文件')
2024-04-20 13:20:21 +08:00
process_bar.update(1)
2024-04-17 15:54:00 +08:00
return
if __name__ == '__main__':
logger = setup_logger('feature2json', './log/feature2json.log')
2024-04-20 13:20:21 +08:00
sample_type = 'benign'
2024-04-18 16:44:31 +08:00
# json_path = os.path.join(f'./out/json/{sample_type}')
2024-04-17 15:54:00 +08:00
json_path = os.path.join(f'./out/json/{sample_type}')
json_files = os.listdir(json_path)
2024-04-18 16:44:31 +08:00
now = datetime.now()
formatted_now = now.strftime("%Y-%m-%d %H:%M:%S")
print("start time:", formatted_now)
# with multiprocessing.Pool(processes=os.cpu_count()) as pool:
# result = list(tqdm(pool.imap_unordered(addr2vec, [os.path.join(json_path, file) for file in json_files[:1] if os.path.isfile(os.path.join(json_path, file))]),
# total=len(json_files)))
2024-04-20 13:20:21 +08:00
# multi_thread_order(addr2vec, [os.path.join(json_path, file) for file in json_files if
# os.path.isfile(os.path.join(json_path, file))], thread_num=THREAD_FULL)
process_bar = tqdm(total=len(json_files))
for json_file in json_files:
if os.path.isfile(os.path.join(json_path, json_file)):
addr2vec(os.path.join(json_path, json_file))