kTrans: Knowledge-Aware Transformer for Binary Code Embedding

Wenyu Zhu, Hao Wang, Yuchen Zhou, Jiaming Wang, Zihan Sha, Zeyu Gao, Chao ZhangarXiv 2023

Abstract

Binary Code Embedding (BCE) has important applications in various reverse engineering tasks such as binary code similarity detection, type recovery, control-flow recovery and data-flow analysis. Recent studies have shown that the Transformer model can comprehend the semantics of binary code to support downstream tasks. However, existing models overlooked the prior knowledge of assembly language. In this paper, we propose a novel Transformer-based approach, namely kTrans, to generate knowledge-aware binary code embedding. By feeding explicit knowledge as additional inputs to the Transformer, and fusing implicit knowledge with a novel pre-training task, kTrans provides a new perspective to incorporating domain knowledge into a Transformer framework. We inspect the generated embeddings with outlier detection and visualization, and also apply kTrans to 3 downstream tasks: Binary Code Similarity Detection (BCSD), Function Type Recovery (FTR) and Indirect Call Recognition (ICR). Evaluation results show that kTrans can generate high-quality binary code embeddings, and outperforms state-of-the-art (SOTA) approaches on downstream tasks by 5.2%, 6.8%, and 12.6% respectively.

1. Introduction

Binary Code Embedding (BCE) is a fundamental technique in automated reverse engineering, mapping unstructured machine code into a mathematical vector space for tasks like malware analysis and vulnerability detection. While recent advancements have applied Transformer models to this domain, they often treat assembly code as a generic natural language, ignoring the unique structural and semantic properties of Instruction Set Architecture (ISA).

Current models suffer from several "blind spots":

  • Register Hierarchy: They fail to recognize that registers like rax, eax, and al are related views of the same hardware component.
  • Instruction Boundaries: They struggle to delineate where one instruction ends and the next begins.
  • Implicit Dependencies: They overlook "hidden" connections, such as the setting of CPU flags (EFLAGS), which influence subsequent program flow.

kTrans (Knowledge-Aware Transformer) addresses these limitations by explicitly injecting ISA knowledge and introducing a novel instruction-level pre-training task.

2. Methodology

The kTrans architecture is designed to move beyond simple token-level analysis by incorporating domain-specific insights at every stage of the pipeline.

Overview of the kTrans architecture, highlighting the multi-modal embedding layer and dual pre-training objectives (MLM and KI).

2.1 Explicit Knowledge Injection

Instead of processing raw assembly text, kTrans utilizes a multi-modal embedding layer. For each token, the model receives four additional pieces of ISA-derived "explicit knowledge":

  1. Instruction Type: Categorizes the operation (e.g., data movement, arithmetic, or control flow).
  2. Operand Type: Identifies the nature of operands (e.g., register, memory address, or immediate constant).
  3. Operand Read/Write Status: Tracks whether a value is being retrieved from or written to an operand.
  4. FLAGS Status: Indicates which CPU flags are modified by the instruction.
Detailed view of the multi-modal embedding layer, showing how ISA-specific knowledge is fused into the input tokens.

This multi-modal approach allows the model to accurately track data flow and register relationships that are otherwise ambiguous in raw text.

2.2 Implicit Knowledge via the Knowledge Integration (KI) Task

While standard Transformers use Masked Language Modeling (MLM) to predict individual missing tokens, kTrans introduces the Knowledge Integration (KI) task. In this task, entire instructions are masked. This forces the model to learn the logical "shape" of instructions and their contextual dependencies, effectively teaching it the underlying semantics of program execution.

3. Evaluation

The model was pre-trained on the BinaryCorp dataset, which includes over 26 million functions from 10,000 projects, spanning multiple compilers (GCC, Clang) and optimization levels (O0-O3, Os).

3.1 Key Results

kTrans demonstrated superior performance across several critical benchmarks compared to state-of-the-art models like word2vec, PalmTree, and jTrans:

  • Binary Code Similarity Detection (BCSD): Achieved a 5.2% improvement in MRR@10000.
  • Function Type Recovery (FTR): Increased accuracy by 6.8%, reaching a total of 87.6%.
  • Indirect Call Recognition (ICR): Showed the most significant gain, outperforming previous SOTA models by 12.6%.

The model also achieved a remarkably low perplexity ($1 + 3.30e^{-4}$), indicating a profound "understanding" of assembly language structures.

4. Conclusion

kTrans proves that incorporating domain-specific ISA knowledge significantly enhances the performance of Transformer models in binary code analysis. By combining explicit knowledge injection with instruction-level pre-training, kTrans bridges the gap between generic language modeling and specialized binary semantics. Future work will explore scaling the model to larger parameter counts and integrating it with general-purpose Large Language Models (LLMs) to further advance automated reverse engineering capabilities.

Summary updated

Abstract

Binary Code Embedding (BCE) has important applications in various reverse engineering tasks such as binary code similarity detection, type recovery, control-flow recovery and data-flow analysis. Recent studies have shown that the Transformer model can comprehend the semantics of binary code to support downstream tasks. However, existing models overlooked the prior knowledge of assembly language. In this paper, we propose a novel Transformer-based approach, namely kTrans, to generate knowledge-aware binary code embedding. By feeding explicit knowledge as additional inputs to the Transformer, and fusing implicit knowledge with a novel pre-training task, kTrans provides a new perspective to incorporating domain knowledge into a Transformer framework. We inspect the generated embeddings with outlier detection and visualization, and also apply kTrans to 3 downstream tasks: Binary Code Similarity Detection (BCSD), Function Type Recovery (FTR) and Indirect Call Recognition (ICR). Evaluation results show that kTrans can generate high-quality binary code embeddings, and outperforms state-of-the-art (SOTA) approaches on downstream tasks by 5.2%, 6.8%, and 12.6% respectively. kTrans is publicly available at: https://github.com/Learner0x5a/kTrans-release
BibTeX
@misc{zhu2023ktrans,
  author = {Wenyu Zhu and Hao Wang and Yuchen Zhou and Jiaming Wang and Zihan Sha and Zeyu Gao and Chao Zhang},
  title = {{kTrans: Knowledge-Aware Transformer for Binary Code Embedding}},
  year = {2023},
  eprint = {2308.12659},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/2308.12659},
}
Figure