Showing posts with label LDAP. Show all posts
Showing posts with label LDAP. Show all posts

Monday, December 20, 2021

Btrace - Tracing JndiLookups - Log4jShell Exploitation Attempts

 BTrace is a safe, dynamic tracing tool for the Java platform.BTrace can be used to dynamically trace a running Java program. BTrace dynamically instruments the classes of the target application to inject tracing code ("bytecode tracing").

BTrace can be used to define trace points that the user is interested to track and when the trace point is reached the user can perform their tasks of interest - such as printing arguments, stack trace etc. 

BTrace can be used to trigger in one of the following scenarios,

  • OnMethod
  • OnTimer
  • OnError 
  • OnExit 
  • OnEvent 
  • OnLowMemory
  • OnProbe
  •  OnMethod is what we will be using. BTrace can be used to trace specific method in specific class making it an ideal tool to trace specific functions of interest instead of debugging the Java code.

    The class that we are interested to track is org.apache.logging.log4j.core.lookup.JndiLookup and the method is lookup. Using this information we can create the probe file that can be fed to BTrace tool. The probe will have the action to perform once the trace point is hit. Lets create a probe file that will print out the arguments passed to the lookup function. Looking up the definition of lookup function we can understand that argument 2 holds the information about JNDI resource name.

    public String lookup(LogEvent event, String key)

    Parameters:

    event - The current LogEvent (is ignored by this StrLookup).

    key - the JNDI resource name to be looked up, may be null

    Returns:

    The String value of the JNDI resource.


    The above code will set the function lookup as trace point and prints the arguments passed to the function. The probe can be run using BTrace like - btrace -v -o <output log> <pid> <path to probe>. The below video shows BTrace's live action. 



    Wednesday, December 15, 2021

    CVE-2021-44228 - Retrieving Payload

    CVE-2021-44228 tracks a remote code execution vulnerability in Apache Log4j. An attacker who can control the logging message has the ability to execute arbitrary code loaded from attacker controlled JDNI related endpoints such as LDAP, RMI, DNS, HTTP etc.

    LDAP Payloads

    A typical LDAP attack request can look like this,

    To download the payload defenders can resort to using curl. The payload can be downloaded like,


    Now that we have the response from the attacker's LDAP server we can understand where the javaCodeBase is located - which is actually a malicious class file that contains code to run malicious commands. Lets follow the class file. The URL to class file can be formed by combining javaCodeBase & javaFactory which becomes http://<attacker_server>:8082/Exploit.class. 

    The downloaded class file reveals the attacker's intentions clearly.


    You can now further payloads seen in the Java class above.

    RMI Payloads

    A typical RMI attack request can look like this,


    To download payload from the RMI server we can resort to python. I've written a simple python code that can be used to download payload RMI payload from attacker server.


    The script can be executed like - python rmi_client.py <attacker_ip> <port> <rmi_endpoint>. Once executed the RMI server should spill out the payload like below.


    You can now further payloads seen in the response from the attacker controlled RMI server.