Java port of libinjection - SQL / SQLI / XSS tokenizer parser analyzer for detecting injection attacks.
This is a faithful port of the C implementation to Java, maintaining the same detection capabilities and fingerprinting system.
- SQLi Detection: Detects SQL injection attempts using fingerprinting and pattern matching
- XSS Detection: Detects Cross-Site Scripting (XSS) attacks in HTML5 contexts
- Multi-dialect Support: Handles ANSI SQL and MySQL syntax differences
- 9352+ Fingerprints: Comprehensive database of known SQLi attack patterns
- Zero Dependencies: No external runtime dependencies (only JUnit for tests)
- Java 8+: Compatible with Java 8 and above
<dependency>
<groupId>com.cloud-apim</groupId>
<artifactId>libinjection-jvm</artifactId>
<version>1.2.0</version>
</dependency>implementation 'com.cloud-apim:libinjection-jvm:1.2.0'import com.cloud.apim.libinjection.LibInjection;
public class Example {
public static void main(String[] args) {
String input = "-1' and 1=1 union/* foo */select load_file('/etc/passwd')--";
boolean isSqli = LibInjection.isSQLi(input);
if (isSqli) {
System.out.println("SQLi detected!");
}
}
}import com.cloud.apim.libinjection.LibInjection;
public class Example {
public static void main(String[] args) {
String input = "<script>alert('xss')</script>";
boolean isXss = LibInjection.isXSS(input);
if (isXss) {
System.out.println("XSS detected!");
}
}
}The library uses a multi-step approach to detect SQL injection:
- Tokenization: The input is parsed into SQL tokens (keywords, operators, strings, numbers, etc.)
- Folding: Tokens are reduced and normalized to create a simplified representation
- Fingerprinting: A fingerprint pattern is generated from the token sequence
- Pattern Matching: The fingerprint is compared against a database of known SQLi patterns
- Context Testing: The input is tested in multiple contexts (no quotes, single quotes, double quotes)
Example fingerprints:
s&1UE- String, logic operator, number, UNION, expression1oc- Number, operator, comment1&1- Number, logic operator, number
The XSS detector analyzes HTML5 contexts and identifies potentially dangerous patterns that could lead to script execution.
This Java port intentionally follows the C implementation closely rather than using idiomatic Java patterns. This design choice:
- Makes it easier to track changes from the upstream C version
- Facilitates debugging by allowing direct comparison with the C code
- Maintains the same performance characteristics and behavior
# Clone the repository
git clone https://github.com/cloud-apim/libinjection-jvm.git
cd libinjection-jvm
# Setup test data (required before running tests)
./setup.sh
# Build with Maven
mvn clean install
# Run tests
mvn test
# Generate sources and javadoc
mvn source:jar javadoc:jarNote: The
setup.shscript downloads the test data from libinjection-go which is required to run the HTML5 parser tests.
This port follows the versioning of the original libinjection C library.
- Zero allocation: The detection process minimizes object allocation
- Fast pattern matching: Uses binary search for keyword lookup
- Efficient tokenization: Single-pass parsing with minimal backtracking
- Thread-safe: All public methods are stateless and thread-safe
| Feature | C Version | Java Version |
|---|---|---|
| SQLi Detection | ✅ | ✅ |
| XSS Detection | ✅ | ✅ |
| Fingerprint Database | 9352 patterns | 9352 patterns |
| Multi-context Testing | ✅ | ✅ |
| MySQL/ANSI Support | ✅ | ✅ |
| Dependencies | None | None (runtime) |
Contributions are welcome! Please feel free to submit a Pull Request.
When contributing:
- Maintain compatibility with the C implementation
- Add tests for new features
- Follow the existing code style (C-like, not idiomatic Java)
- Update documentation as needed
This project is licensed under the Apache License 2.0 - see the LICENSE file for details.
The original libinjection C library is:
- Copyright (c) 2012-2016 Nick Galbreath
- Licensed under BSD 3-Clause License
- Original Author: Nick Galbreath - C implementation
- Java Port: Mathieu Ancelin - Cloud APIM
- Original C Library: https://github.com/client9/libinjection
- Documentation: https://libinjection.client9.com/
- Issue Tracker: https://github.com/cloud-apim/libinjection-jvm/issues
- Maven Central: https://search.maven.org/artifact/com.cloud-apim/libinjection-jvm
- libinjection (C) - Original C implementation
- libinjection-php - PHP extension
- libinjection-python - Python bindings
- libinjection-lua - Lua bindings
For questions, issues, or feature requests, please use the GitHub Issues page.