GithubHelp home page GithubHelp logo

porlockzzz / safe Goto Github PK

View Code? Open in Web Editor NEW

This project forked from gadiluna/safe

0.0 1.0 0.0 190 KB

SAFE: Self-Attentive Function Embeddings for binary similarity

License: Other

Python 89.02% Shell 0.85% Perl 1.79% C 0.10% HTML 0.40% Ruby 7.83%

safe's Introduction

SAFE : Self Attentive Function Embedding

Paper

This software is the outcome of our accademic research. See our arXiv paper: arxiv

If you use this code, please cite our accademic paper as:

@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}
}

What you need

You need radare2 installed in your system.

Quickstart

To create the embedding of a function:

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

What to do with an embedding?

Once you have two embeddings embedding_x and embedding_y you can compute the similarity of the corresponding functions as:

from sklearn.metrics.pairwise import cosine_similarity

sim=cosine_similarity(embedding_x, embedding_y)
 

Data Needed

SAFE needs few information to work. Two are essentials, a model that tells safe how to convert assembly instructions in vectors (i2v model) and a model that tells safe how to convert an binary function into a vector. Both models can be downloaded by using the command

./download_model.sh

the downloader downloads the model and place them in the directory data. The directory tree after the download should be.

safe/-- githubcode
     \
      \--data/-----safe.pb
               \
                \---i2v/
            

The safe.pb file contains the safe-model used to convert binary function to vectors. The i2v folder contains the i2v model.

Hardcore Details

This section contains details that are needed to replicate our experiments, if you are an user of safe you can skip it.

Safe.pb

This is the freezed tensorflow trained model for AMD64 architecture. You can import it in your project using:

 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)

see file: neural_network/SAFEEmbedder.py

i2v

The i2v folder contains two files. A Matrix where each row is the embedding of an asm instruction. A json file that contains a dictonary mapping asm instructions into row numbers of the matrix above. see file: asm_embedding/InstructionsConverter.py

Train the model

If you want to train the model using our datasets you have to first use:

 python3 downloader.py -td

This will download the datasets into data folder. Note that the datasets are compressed so you have to decompress them yourself. This data will be an sqlite databases. To start the train use neural_network/train.sh. The db can be selected by changing the parameter into train.sh. If you want information on the dataset see our paper.

Create your own dataset

If you want to create your own dataset you can use the script ExperimentUtil into the folder dataset creation.

Create a functions knowledge base

If you want to use SAFE binary code search engine you can use the script ExperimentUtil to create the knowledge base. Then you can search through it using the script into function_search

Related Projects

Thanks

In our code we use godown to download data from Google drive. We thank circulosmeos, the creator of godown.

We thank Davide Italiano for the useful discussions.

safe's People

Contributors

gadiluna avatar fabiopetroni avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.