Abstract
HAMPI is a powerful string solver designed to handle complex constraints, including regular expressions and context-free grammars, by bounding string lengths. By translating these constraints into bit-vector logic, it allows developers to automatically detect vulnerabilities like SQL injection and significantly improve the code coverage of automated testing tools.
Introduction
In modern software engineering, many testing and verification tasks are reduced to "constraint satisfaction" problems. While we have excellent solvers for bit-vectors and linear arithmetic, string-manipulating programs—like Web applications—have historically been much harder to analyze.
The core challenge lies in the complexity of string operations. Many interesting properties of strings are defined by Context-Free Grammars (CFGs). However, general operations on CFGs (like determining the intersection of two grammars) are often undecidable. Consequently, researchers often resorted to building "ad-hoc" solvers for specific projects. The authors identified a clear need for a standardized, expressive, and efficient string solver that could be easily integrated into existing security and analysis pipelines.
Input Language and Constraints
HAMPI provides a rich input language for specifying string constraints. It supports a single string variable with a bounded length, regular language membership, context-free language membership, substring relations, and word equations. This expressivity allows it to model complex input requirements for a variety of software systems.
Methods
HAMPI’s "secret sauce" is its approach to bounding. By requiring a fixed length (or a small range of lengths) for the target string variable, HAMPI turns undecidable problems into tractable ones. The solver operates in four distinct steps:
- Normalization: It converts high-level constraints (membership in a CFG, substring relations, etc.) into a "core form" consisting of regular expression membership tests.
- CFG to RegEx Expansion: This is a key technical contribution. For a given length $N$, HAMPI expands a CFG into a regular expression that represents all possible strings of exactly that length derivable from the grammar.
- Bit-Vector Encoding: The core constraints are translated into a quantifier-free logic of bit-vectors. Each character is represented by an 8-bit ASCII code.
- Solving and Decoding: HAMPI leverages the STP bit-vector solver to find a satisfying assignment. If STP finds a solution, HAMPI decodes the bit-vectors back into a human-readable ASCII string.
Evaluation
The authors evaluated HAMPI across three distinct real-world scenarios:
- Static Analysis: 1,367 constraints generated from 6 PHP web applications (totaling ~340k lines of code), including the
clarolinelearning management system. - Dynamic Analysis: 216 constraints generated by the
Ardillatool while analyzing 5 PHP applications for SQL injection vulnerabilities. - Systematic Testing: Three open-source C programs—
cueconvert,logictree, andbc—used to test integration with theKleesymbolic execution engine.
Key Insights and Ideas
- Bounding is a Feature, Not a Handicap: While limiting string length might seem restrictive, the authors found that most real-world vulnerabilities (like SQL injections) can be triggered by very short strings (often $\leq 4$ characters). Bounding makes the solver much faster without sacrificing practical utility.
- Analogy to Bounded Model Checking: The authors frame HAMPI as the string-solver equivalent of Bounded Model Checking (BMC). Just as BMC finds bugs within a certain execution depth, HAMPI finds string-based bugs within a certain character length.
- Solving the "Parser Wall": Automated testing tools like Klee often get stuck at the "parsing stage" because they generate random, malformed strings that the program immediately rejects. By using HAMPI to generate strings that are guaranteed to be syntactically valid according to a grammar, the tester can bypass the parser and explore the core logic of the program.
Key Results
- Efficiency: In the static analysis dataset, HAMPI solved 99.7% of constraints in less than one second per constraint.
- Vulnerability Detection: In the dynamic analysis test, HAMPI successfully generated all 23 known SQL injection attacks, proving it could replace manual, pattern-based attack generators.
- Improved Testing Coverage: When integrated with Klee, HAMPI-generated constraints improved total line coverage by up to 2x and parser-specific code coverage by up to 5x.
- Bug Finding: HAMPI helped Klee discover 3 previously unknown errors in the
logictreeprogram, where specific structured inputs caused the program to enter an infinite loop.
Limitations and Future Work
- Single Variable Focus: The core HAMPI solver is optimized for constraints involving a single string variable. While multiple variables can be simulated by partitioning a single long string using offsets, this can become complex for the user.
- Length Dependency: If a vulnerability only exists for a string of length 100, and the user only searches up to length 50, HAMPI will report the constraints as unsatisfiable.
- Semantic vs. Syntactic Validity: While HAMPI ensures strings are syntactically valid (i.e., they pass the parser), it does not guarantee semantic validity (e.g., a generated SQL query might be grammatically correct but reference a table that doesn't exist).
Summary updated