Manuscript Offprint

arXiv:2610.31844

SEMPRE: Semantic-Aware Protocol Reverse Engineering via Consistency-Driven Joint Optimization

Yihua Xu, Weijie Huang, Xiaofeng Wang, Hang Zhang, Binghui Wang, Yue Duan

Open access. PaperVantage converted this article to text, so layout and figures may differ from the original.

Yihua Xu

Illinois Institute of Technology

yxu131@hawk.illinoistech.edu

Weijie Huang

Indiana University

wh25@iu.edu

Binghui Wang

Illinois Institute of Technology

bwang70@illinoistech.edu

Xiaofeng Wang

Nanyang Technological University

xiaofeng.wang@ntu.edu.sg

Hang Zhang

Indiana University

hz64@iu.edu

Yue Duan

Singapore Management University

yueduan@smu.edu.sg

Abstract—Protocol reverse engineering (PRE) is essential for understanding undocumented or proprietary communication formats, enabling security-critical tasks such as vulnerability discovery, malware C2 analysis, and IDS signature generation. However, existing PRE techniques rely on brittle hand-written rules, under-model contextual dependencies among neighboring fields, and often fail on messages with nested sub-protocols. We present SEMPRE, a fully automated PRE framework that replaces ad-hoc heuristics with recursive LLM reasoning grounded in fine-grained dynamic program analysis. SEMPRE uses taint-flow and loop traces to guide an LLM-driven tree-of-thought search that slices messages into fields while detecting nested sub-payloads, producing a hierarchical layout. It then infers each field's type and role and iteratively refines boundaries by freezing high-confidence segments and re-running detection under semantic constraints, yielding an EM-style procedure that monotonically improves structure and semantics until convergence. Evaluated on a broad corpus of mainstream protocols against six state-of-the-art PRE systems, SEMPRE achieves an average F1 of 0.76 for field detection (vs. <0.40 for all baselines) and 0.72 for semantic inference (vs. 0.14 for BinPRE). Ablations confirm the contribution of each component, and a case study on a prolific real-world botnet demonstrates practical utility.

I. INTRODUCTION

Protocol reverse engineering (PRE) is the process of analyzing communication flows to deduce the message structures, semantics, or state machines of network protocols, typically when official specifications are unavailable, incomplete, or proprietary [1], [2]. When only protocol messages and the program binaries that implement the protocol are available, PRE becomes particularly challenging. Analysts often have to rely on captured network traffic and the behavior of related (often closed-source) software to infer how systems exchange information. Figure 1 illustrates the expected output of PRE on a real-world Modbus message [3]. Given a 12-byte message (byte [0] to byte [11]), PRE aims to: (1) accurately identify the boundaries of all fields within the message (field detection), and (2) infer each field's type and role (semantic inference). PRE is a key enabler for security applications, including analyzing malware command-and-control (C2) communications [4], [5], discovering vulnerabilities in undocumented or proprietary protocols [6], and generating intrusion detection system (IDS) signatures for previously unknown threats [7].

Figure

Fig. 1: Protocol Reverse Engineering

Due to the importance of PRE, a substantial body of research has emerged. Existing studies generally fall into two categories: network-traffic-based approaches and binary-analysis-based approaches. Network-traffic-based methods, such as BinaryInferno [8], NEMESYS [9], DynPRE [10], and NetPlier [11], take captured network traces as input and apply statistical techniques to extract recurring format patterns. However, their effectiveness depends heavily on access to high-quality and diverse traffic that captures many protocol message instances—an assumption that is often difficult to satisfy in practice. In contrast, binary-analysis-based approaches, including Dispatcher [12], PolyGlot [13], Tupni [14], and BinPRE [2], leverage dynamic program analysis to recover detailed runtime semantics by observing how the protocol implementation parses and consumes message bytes. For example, if the binary consistently loads bytes [0–1] together, applies endian-aware integer operations (e.g., shifting/combining), and uses the resulting value in conditional branches or length calculations, this provides strong evidence that [0–1] forms a multi-byte integer field rather than unrelated bytes. As a result, binary-based approaches are often more practical in real-world settings where high-quality traffic traces are unavailable. Despite significant progress, we identify three fundamental

Network and Distributed System Security (NDSS) Symposium 2027

22–26 March 2027, Seoul, Republic of Korea

ISBN 978-1-970672-09-1

https://dx.doi.org/10.14722/ndss.2027.242825

www.ndss-symposium.org limitations of state-of-the-art (SOTA) binary-analysis-based techniques, as elaborated below.

First, SOTA approaches rely heavily on hand-crafted heuristics or rule sets that are tuned to specific byte patterns or protocol families. This rule-driven design limits generalizability and makes it difficult to keep pace with the diversity of real-world encodings. A representative example is BinPRE [2], which uses multiple rules to label a field as a filename only when the byte sequence ends with a known extension (e.g., ".txt" or ".jpg"). Consequently, filenames that are length-prefixed, UTF-16 encoded, or extensionless can be missed or mislabeled.

Second, existing techniques often model fields in isolation, without explicitly capturing semantic relationships among neighboring fields. In practice, many protocols exhibit strong contextual dependencies that are crucial for semantic inference. For instance, a tag immediately followed by a length field strongly suggests the canonical Type-Length-Value (TLV) pattern, and checksum fields typically appear after the data they protect. The Dynamic Host Configuration Protocol (DHCP) [15], for example, encodes options as a sequence of TLV triples. Reliably identifying the Tag-Length prefix is essential for delineating the corresponding value fields and inferring their types. Prior work, such as BinaryInferno [8], can leverage serialization patterns to detect certain LV/TLV-like structures, but it does not reason about why fields depend on one another or how upstream boundary errors propagate to downstream semantic inference. As a result, failing to model such dependencies can lead to inaccuracies that may cascade into subsequent analyses, such as vulnerability discovery.

Third, these PRE techniques struggle to robustly handle a special yet prevalent case – nested protocols, where a message embeds a sub-payload that follows a different protocol specification. While dynamic binary-analysis-based approaches (e.g., Polyglot [13], Tupni [14], and BinPRE [2]) can expose certain field semantics by observing runtime parsing behavior or can infer repeated or nested layouts from control-flow regularities, these methods primarily recover syntactic structure rather than an explicit hierarchical semantic representation. In practice, inferred fields are often flattened into a single sequence, obscuring protocol-layer boundaries. However, nested payloads typically induce stronger dependencies within a sub-protocol than across protocol boundaries. Existing systems do not explicitly represent or reason about this distinction, and approaches with limited semantic cues (e.g., BinaryInferno) still assign roles largely at the level of individual fields, without modeling hierarchical relationships or distinguishing intra-payload dependencies from inter-layer transitions. Consequently, nested protocols may be only partially recovered, and inferred layouts can become brittle for multi-layer designs such as EtherNet/IP (EIP) [16] encapsulating Common Industrial Protocol (CIP) [17] payloads.

Recent studies [18], [19], [20] suggest that large language models (LLMs) can outperform rigid, heuristic-based pipelines in reverse-engineering tasks such as disassembly [20] and decompilation [19], [18]. Trained on large corpora, LLMs can internalize recurring structural regularities and, importantly, capture relationships among distant components, enabling more flexible inference under uncertainty. Motivated by these advances, we first examined whether an off-the-shelf LLM could perform protocol reverse engineering (PRE) without additional structure. In a pilot study, we asked GPT-o3 (the same backbone model used in our approach) to perform field detection on 179 messages from four representative protocols (50 Modbus, 41 S7, 38 FTP, and 50 DNS). The results exposed the limitations of naive prompting: the model achieved an F1 of 0.69 on Modbus (the simplest format in our set) and remained below 0.50 on S7, FTP, and DNS. These outcomes indicate that dependable PRE requires more than a single, unconstrained pass through an LLM. A qualitative inspection suggests that naive prompts often trigger hallucinated fields, incorrect semantic assumptions, and inconsistent labeling across messages, especially for formats with optional fields, variable-length encodings, or layered structure. We attribute this behavior to the difficulty of reliably grounding generation on long, noisy inputs (e.g., dynamic traces) and maintaining global consistency when many interacting constraints must be satisfied simultaneously.

To effectively tackle the PRE problem, our key insight is that field detection and semantic inference are tightly coupled: accurate boundaries make semantics easier to infer, while semantic constraints (e.g., TLV relationships, checksums, length fields, nested payload headers) can in turn validate and refine boundaries. With this, we develop SEMPRE, a fully automated PRE framework, which integrates fine-grained dynamic program analysis with a recursive LLM-driven reasoning procedure that performs joint optimizations over field detection and semantic inference, rather than treating them as two independent stages as in prior work. SEMPRE first performs fine-grained dynamic taint analysis and loop detection while executing the protocol binary on a given input message. These traces provide low-level evidence about how individual bytes are consumed (e.g., grouped loads, comparisons, arithmetic, and repeated parsing behavior), which we encode as structured hints for downstream reasoning. Next, in the field detection step, SEMPRE uses an LLM-based tree-of-thought search to propose candidate segmentations that are consistent with the collected taint/loop evidence. To address nested protocols, SEMPRE exploits the LLM's capability to recognize sub-payload boundaries and recursively applies the same detection procedure to any inferred nested regions, yielding an initial hierarchical message layout. Given this layout, our system performs semantic inference to predict the type and role of each field. The inferred semantics are then fed back into a subsequent detection round: high-confidence boundaries are frozen, and ambiguous regions are re-segmented under stronger semantic constraints. Then, SEMPRE alternates between field detection and semantic inference, using the output of each step to constrain the other until a fixed point is reached. If the process fails to converge within a preset budget, SEMPRE restarts with alternative hypotheses to escape poor local optima. Inspired by the Expectation-Maximization (EM) algorithm [21], we formalize this procedure as an iterative optimization process and prove that each update is monotonic and that the procedure converges to a local optimum. The end result is a heuristic-free, effective PRE solution that improves both accuracy and stability compared to state-of-the-art techniques.

We implemented a prototype of SEMPRE and evaluated it against six state-of-the-art PRE tools. The results demonstrate that SEMPRE achieves the best overall performance across a diverse set of popular protocols. In field detection, SEMPRE attains the highest average F1 score of 0.76, demonstrating a 90%+ improvement with no other tools exceeding 0.4. In semantic inference, SEMPRE achieves an average F1 score of 0.72, substantially outperforming the 0.14 of BinPRE, the state-of-the-art PRE tool. Our further ablation study confirms and quantifies the contribution of each design component. In addition, we demonstrate SEMPRE's practicality for security tasks by using it to analyze the protocol of a real-world, widely distributed botnet malware sample.

Contributions. Our contributions are summarized as follows:

• We designed a fully automated PRE framework, which integrates fine-grained dynamic program analysis with a recursive LLM-driven reasoning procedure that performs joint optimizations over field detection and semantic inference.

• We formally proved that our algorithm converges to a local optimum.

• We implemented a prototype of SEMPRE and open-sourced our implementation 1^{1} to facilitate future research in this field. We further evaluated its efficacy. Our experiments demonstrate that SEMPRE significantly outperforms prior PRE mechanisms and excels in analyzing real-world malware protocols ( §\SIV).

References (50)