本软件是我们学术研究的成果。请参阅我们的arXiv论文:arxiv
如果您使用此代码,请引用我们的学术论文如下:
@inproceedings{massarelli2018safe,
title={SAFE: Self-Attentive Function Embeddings for Binary Similarity},
author={Massarelli, Luca and Di Luna, Giuseppe Antonio and Petroni, Fabio and Querzoni, Leonardo and Baldoni, Roberto},
booktitle={Proceedings of 16th Conference on Detection of Intrusions and Malware & Vulnerability Assessment (DIMVA)},
year={2019}
}
你需要在系统中安装 radare2。
要创建函数的嵌入:
git clone https://github.com/gadiluna/SAFE.git
pip install -r requirements
chmod +x download_model.sh
./download_model.sh
python safe.py -m data/safe.pb -i helloworld.o -a 100000F30
一旦你有了两个嵌入 embedding_x 和 embedding_y,你可以计算对应函数的相似度,方法如下:
from sklearn.metrics.pairwise import cosine_similarity
sim=cosine_similarity(embedding_x, embedding_y)
SAFE 需要少量信息即可工作。其中两个是必需的:一个模型告诉 SAFE 如何将汇编指令转换为向量(i2v 模型),以及一个模型告诉 SAFE 如何将二进制函数转换为向量。 这两个模型可以通过以下命令下载:
./download_model.sh
下载器会将模型下载并放置在 data 目录中。 下载后的目录树应如下所示:
safe/-- githubcode
\
\--data/-----safe.pb
\
\---i2v/
safe.pb 文件包含用于将二进制函数转换为向量的 safe 模型。 i2v 文件夹包含 i2v 模型。
此部分包含重现我们实验所需的细节,如果你是 SAFE 的用户,可以跳过。
这是针对 AMD64 架构的冻结 TensorFlow 训练模型。你可以通过以下方式将其导入到你的项目中:
import tensorflow as tf
with tf.gfile.GFile("safe.pb", "rb") as f:
graph_def = tf.GraphDef()
graph_def.ParseFromString(f.read())
with tf.Graph().as_default() as graph:
tf.import_graph_def(graph_def)
sess = tf.Session(graph=graph)
参见文件:neural_network/SAFEEmbedder.py
i2v 文件夹包含两个文件: 一个矩阵,其中每一行是一条 asm 指令的嵌入。 一个 JSON 文件,包含将 asm 指令映射到上述矩阵行号的字典。 参见文件:asm_embedding/InstructionsConverter.py
如果你想使用我们的数据集训练模型,首先需要运行:
python3 downloader.py -td
这将把数据集下载到 data 文件夹中。注意数据集是压缩的,所以你需要自行解压。这些数据将是 SQLite 数据库。 要开始训练,请使用 neural_network/train.sh。 可以通过更改 train.sh 中的参数来选择数据库。 如果想了解数据集的详细信息,请参阅我们的论文。
如果你想创建自己的数据集,可以使用 dataset creation 文件夹中的脚本 ExperimentUtil。
如果你想使用 SAFE 二进制代码搜索引擎,可以使用脚本 ExperimentUtil 创建知识库。 然后可以通过 function_search 中的脚本在其中搜索。
在我们的代码中,我们使用 godown 从 Google Drive 下载数据。感谢 godown 的创建者 circulosmeos。
感谢 Davide Italiano 的有益讨论。