|
| 1 | +/* |
| 2 | + * Copyright 2025 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.detectors.exposedui.flowise; |
| 17 | + |
| 18 | +import static com.google.common.base.Preconditions.checkNotNull; |
| 19 | +import static com.google.common.collect.ImmutableList.toImmutableList; |
| 20 | + |
| 21 | +import com.google.common.annotations.VisibleForTesting; |
| 22 | +import com.google.common.collect.ImmutableList; |
| 23 | +import com.google.common.flogger.GoogleLogger; |
| 24 | +import com.google.protobuf.util.Timestamps; |
| 25 | +import com.google.tsunami.common.data.NetworkServiceUtils; |
| 26 | +import com.google.tsunami.common.net.http.HttpClient; |
| 27 | +import com.google.tsunami.common.net.http.HttpHeaders; |
| 28 | +import com.google.tsunami.common.net.http.HttpRequest; |
| 29 | +import com.google.tsunami.common.net.http.HttpResponse; |
| 30 | +import com.google.tsunami.common.time.UtcClock; |
| 31 | +import com.google.tsunami.plugin.PluginType; |
| 32 | +import com.google.tsunami.plugin.VulnDetector; |
| 33 | +import com.google.tsunami.plugin.annotations.PluginInfo; |
| 34 | +import com.google.tsunami.proto.AdditionalDetail; |
| 35 | +import com.google.tsunami.proto.DetectionReport; |
| 36 | +import com.google.tsunami.proto.DetectionReportList; |
| 37 | +import com.google.tsunami.proto.DetectionStatus; |
| 38 | +import com.google.tsunami.proto.NetworkService; |
| 39 | +import com.google.tsunami.proto.Severity; |
| 40 | +import com.google.tsunami.proto.TargetInfo; |
| 41 | +import com.google.tsunami.proto.TextData; |
| 42 | +import com.google.tsunami.proto.Vulnerability; |
| 43 | +import com.google.tsunami.proto.VulnerabilityId; |
| 44 | +import java.io.IOException; |
| 45 | +import java.time.Clock; |
| 46 | +import java.time.Instant; |
| 47 | +import javax.inject.Inject; |
| 48 | + |
| 49 | +/** A {@link VulnDetector} that detects an exposed Flowise UI installation. */ |
| 50 | +@PluginInfo( |
| 51 | + type = PluginType.VULN_DETECTION, |
| 52 | + name = "FlowiseExposedUiDetector", |
| 53 | + version = "0.1", |
| 54 | + description = |
| 55 | + "This detector checks whether a Flowise UI installation is exposed without proper" |
| 56 | + + " authentication.", |
| 57 | + author = "yuradoc (yuradoc.research@gmail.com)", |
| 58 | + bootstrapModule = FlowiseExposedUiDetectorBootstrapModule.class) |
| 59 | +public final class FlowiseExposedUiDetector implements VulnDetector { |
| 60 | + private static final GoogleLogger logger = GoogleLogger.forEnclosingClass(); |
| 61 | + |
| 62 | + private final Clock utcClock; |
| 63 | + private final HttpClient httpClient; |
| 64 | + |
| 65 | + @VisibleForTesting |
| 66 | + static final String RECOMMENDATION = |
| 67 | + "Please disable public access to your Flowise instance. You can enable authentication" |
| 68 | + + " for your instance by following the instructions here:" |
| 69 | + + " https://docs.flowiseai.com/configuration/authorization/app-level"; |
| 70 | + |
| 71 | + @Inject |
| 72 | + FlowiseExposedUiDetector(@UtcClock Clock utcClock, HttpClient httpClient) { |
| 73 | + this.utcClock = checkNotNull(utcClock); |
| 74 | + this.httpClient = checkNotNull(httpClient).modify().setFollowRedirects(false).build(); |
| 75 | + } |
| 76 | + |
| 77 | + @Override |
| 78 | + public ImmutableList<Vulnerability> getAdvisories() { |
| 79 | + return ImmutableList.of( |
| 80 | + Vulnerability.newBuilder() |
| 81 | + .setMainId( |
| 82 | + VulnerabilityId.newBuilder() |
| 83 | + .setPublisher("TSUNAMI_COMMUNITY") |
| 84 | + .setValue("FLOWISE_UI_EXPOSED")) |
| 85 | + .setSeverity(Severity.HIGH) |
| 86 | + .setTitle("Flowise UI Exposed") |
| 87 | + .setDescription("Flowise UI instance is exposed without proper authentication.") |
| 88 | + .setRecommendation(RECOMMENDATION) |
| 89 | + .build()); |
| 90 | + } |
| 91 | + |
| 92 | + @Override |
| 93 | + public DetectionReportList detect( |
| 94 | + TargetInfo targetInfo, ImmutableList<NetworkService> matchedServices) { |
| 95 | + logger.atInfo().log("FlowiseExposedUiDetector starts detecting."); |
| 96 | + |
| 97 | + return DetectionReportList.newBuilder() |
| 98 | + .addAllDetectionReports( |
| 99 | + matchedServices.stream() |
| 100 | + .filter(NetworkServiceUtils::isWebService) |
| 101 | + .filter(this::isServiceVulnerable) |
| 102 | + .map(networkService -> buildDetectionReport(targetInfo, networkService)) |
| 103 | + .collect(toImmutableList())) |
| 104 | + .build(); |
| 105 | + } |
| 106 | + |
| 107 | + private boolean isServiceVulnerable(NetworkService networkService) { |
| 108 | + String targetUri = NetworkServiceUtils.buildWebApplicationRootUrl(networkService); |
| 109 | + String targetApiUri = targetUri + "api/v1/apikey"; |
| 110 | + |
| 111 | + HttpResponse response; |
| 112 | + try { |
| 113 | + // plain GET request to check Flowise UI availability. |
| 114 | + response = |
| 115 | + httpClient.send(HttpRequest.get(targetUri).withEmptyHeaders().build(), networkService); |
| 116 | + if (!(response.bodyString().isPresent() && response.bodyString().get().contains("Flowise"))) { |
| 117 | + return false; |
| 118 | + } |
| 119 | + |
| 120 | + // Main request that performs vulnerability check. |
| 121 | + response = |
| 122 | + httpClient.send( |
| 123 | + HttpRequest.get(targetApiUri) |
| 124 | + .setHeaders(HttpHeaders.builder().addHeader("x-request-from", "internal").build()) |
| 125 | + .build(), |
| 126 | + networkService); |
| 127 | + return response.status().code() != 401; |
| 128 | + } catch (IOException e) { |
| 129 | + logger.atWarning().withCause(e).log("Unable to query Flowise."); |
| 130 | + return false; |
| 131 | + } |
| 132 | + } |
| 133 | + |
| 134 | + private DetectionReport buildDetectionReport( |
| 135 | + TargetInfo targetInfo, NetworkService vulnerableNetworkService) { |
| 136 | + return DetectionReport.newBuilder() |
| 137 | + .setTargetInfo(targetInfo) |
| 138 | + .setNetworkService(vulnerableNetworkService) |
| 139 | + .setDetectionTimestamp(Timestamps.fromMillis(Instant.now(utcClock).toEpochMilli())) |
| 140 | + .setDetectionStatus(DetectionStatus.VULNERABILITY_VERIFIED) |
| 141 | + .setVulnerability( |
| 142 | + getAdvisories().get(0).toBuilder() |
| 143 | + .addAdditionalDetails( |
| 144 | + AdditionalDetail.newBuilder() |
| 145 | + .setTextData( |
| 146 | + TextData.newBuilder() |
| 147 | + .setText( |
| 148 | + String.format( |
| 149 | + "The Flowise UI instance at %s is exposed without proper" |
| 150 | + + " authentication.", |
| 151 | + NetworkServiceUtils.buildWebApplicationRootUrl( |
| 152 | + vulnerableNetworkService)))))) |
| 153 | + .build(); |
| 154 | + } |
| 155 | +} |
0 commit comments