mrphrazer / reverser_ai

Provides automated reverse engineering assistance through the use of local large language models (LLMs) on consumer hardware.
GNU General Public License v2.0
838 stars 51 forks source link

ReverserAI (v1.1)

Author: Tim Blazytko

Provides automated reverse engineering assistance through the use of local large language models (LLMs) on consumer hardware.

Description:

ReverserAI is a research project designed to automate and enhance reverse engineering tasks through the use of locally-hosted large language models (LLMs). Operating entirely offline, this initial release features the automatic suggestion of high-level, semantically meaningful function names derived from decompiler output. ReverserAI is provided as a Binary Ninja plugin; however, its architecture is designed to be extended to other reverse engineering platforms such as IDA and Ghidra.

While local LLMs do not match the performance and capabilities of their cloud-based counterparts like ChatGPT4 and require substantial computing resources, they represent a significant step forward in balancing performance with confidentiality requirements.

ReverserAI serves as an initial exploration into the potential of local LLMs as aids in reverse engineering on consumer-grade hardware. It showcases what is currently achievable and plans to be a playground for future developments in the realm of AI-assisted reverse engineering. Additionally, the project explores the benefits of combining static analysis techniques with modern AI capabilities to improve the accuracy of AI-assisted reverse engineering.

Some example use cases can be found in examples.

[!NOTE] Disclaimer: My expertise in machine learning and LLMs is limited. There may exist more efficient models or methods to achieve similar tasks with greater performance. This project represents a culmination of research into viable configurations, offering a stable foundation with acceptable performance. Feedback and contributions to improve ReverserAI are highly encouraged.

Core Features

Installation

ReverserAI can be easily integrated via Binary Ninja's plugin manager. Alternatively, for those preferring command line installation, execute in Binary Ninja's plugins folder:

git clone https://github.com/mrphrazer/reverser_ai.git
cd reverser_ai

# install requirements
pip3 install -r requirements.txt

# install ReverserAI
pip3 install .

Upon initial launch, the tool will automatically download the (default mistral-7b-instruct-v0.2.Q4_K_M.gguf large language model file (~5GB). The download time varies based on internet connection speed. To manually initiate the download or download other models, execute the model_download.py script.

Hardware Requirements

For optimal LLM performance on consumer-grade hardware, a setup with multiple CPU threads or a powerful GPU is advised. ReverserAI runs efficiently on systems with at least 16 GB of RAM and 12 CPU threads, with queries taking about 20 to 30 seconds. GPU optimizations, especially on Apple silicon devices, can reduce this to 2 to 5 seconds per query.

Usage

ReverserAI is accessible through Binary Ninja's user interface and via command line.

User Interface

To invoke the plugin within Binary Ninja, navigate to Plugins -> ReverserAI and, for example, run "Rename All Functions":

Plugin Menu

Depending on the total number of functions in the binary, this may take a while. The AI-assisted function name suggestions will appear in the Log window:

Binary Ninja Log

Configuration

Configuring ReverserAI to match your hardware setup optimizes its performance. Key configuration parameters include CPU and GPU utilization preferences: For powerful GPUs, configure ReverserAI to primarily use GPU, reducing CPU threads to minimize overhead. Without a strong GPU, increase CPU thread usage to maximize processing power. For systems with balanced resources, allocate tasks between CPU and GPU for efficient operation. Another dimension is the selection of the underlying model which greatly influences ReverserAI's functionality, performance, and resource consumption. Below, key configuration parameters are outlined along with guidance on model selection based on your computational resources and requirements.

The default configuration is designed to strike a balance between performance and resource usage, with a preference for GPU acceleration where feasible. Adjust these settings based on your specific hardware capabilities and the requirements of your reverse engineering tasks to achieve optimal performance.

Binary Ninja

To adjust settings in Binary Ninja, open Settings and search for reverser_ai. Changes require Binary Ninja to be restarted.

Plugin Settings

Each change requires a restart of Binary Ninja.

Parameter Tuning

For detailed parameter adjustment, utilize the gpt_function_namer.py script with a configuration file, starting with the provided example_config.toml:

$ time python3 scripts/gpt_function_namer.py example_config.toml
Suggested name: xor_two_numbers

real    0m1.550s
user    0m0.268s
sys 0m0.223s

Static Analysis to Improve AI-based Analysis

ReverserAI explores how to enrich AI-related reverse engineering tasks by providing more context through static analysis. Initially, Context-Aware Function Renaming is the only implemented feature.

Context-Aware Function Renaming

Experiments have shown that function renaming is especially effective for functions with contextual information, such as external API functions or strings that provide context for the AI. The goal is to narrow the analysis scope and focus on functions where context from strings, symbols, and other static analysis data can be leveraged.

Code Organization

ReverserAI's codebase maintains a clear separation between generic LLM functionalities and tool-specific integration, ensuring modularity and ease of extension. Below is an overview of the primary components:

Limitations and Future Work

ReverserAI serves as a proof of concept that demonstrates the potential of leveraging local LLMs for reverse engineering tasks on consumer-grade hardware. Currently, its primary functionality is to offer function name suggestions, but there exists significant scope for enhancement and expansion. Future directions could include:

This project welcomes further contributions, suggestions, and enhancements, including pull requests.

Contact

For more information, contact @mr_phrazer.