Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

kubeflow weak credential tester #552

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
import com.google.tsunami.plugins.detectors.credentials.genericweakcredentialdetector.testers.grafana.GrafanaCredentialTester;
import com.google.tsunami.plugins.detectors.credentials.genericweakcredentialdetector.testers.hydra.HydraCredentialTester;
import com.google.tsunami.plugins.detectors.credentials.genericweakcredentialdetector.testers.jenkins.JenkinsCredentialTester;
import com.google.tsunami.plugins.detectors.credentials.genericweakcredentialdetector.testers.kubeflow.KubeflowCredentialTester;
import com.google.tsunami.plugins.detectors.credentials.genericweakcredentialdetector.testers.mlflow.MlFlowCredentialTester;
import com.google.tsunami.plugins.detectors.credentials.genericweakcredentialdetector.testers.mysql.MysqlCredentialTester;
import com.google.tsunami.plugins.detectors.credentials.genericweakcredentialdetector.testers.hive.HiveCredentialTester;
Expand Down Expand Up @@ -68,6 +69,7 @@ protected void configurePlugin() {

Multibinder<CredentialTester> credentialTesterBinder =
Multibinder.newSetBinder(binder(), CredentialTester.class);
credentialTesterBinder.addBinding().to(KubeflowCredentialTester.class);
credentialTesterBinder.addBinding().to(JenkinsCredentialTester.class);
credentialTesterBinder.addBinding().to(MlFlowCredentialTester.class);
credentialTesterBinder.addBinding().to(MysqlCredentialTester.class);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,199 @@
/*
* Copyright 2024 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.google.tsunami.plugins.detectors.credentials.genericweakcredentialdetector.testers.kubeflow;

import static com.google.common.base.Preconditions.checkNotNull;
import static com.google.tsunami.common.data.NetworkServiceUtils.buildWebApplicationRootUrl;
import static com.google.tsunami.common.data.NetworkServiceUtils.isWebService;
import static com.google.tsunami.common.net.http.HttpRequest.get;
import static com.google.tsunami.common.net.http.HttpRequest.post;

import com.google.common.collect.ImmutableList;
import com.google.common.flogger.GoogleLogger;
import com.google.gson.JsonObject;
import com.google.protobuf.ByteString;
import com.google.tsunami.common.data.NetworkEndpointUtils;
import com.google.tsunami.common.data.NetworkServiceUtils;
import com.google.tsunami.common.net.http.HttpClient;
import com.google.tsunami.common.net.http.HttpHeaders;
import com.google.tsunami.common.net.http.HttpResponse;
import com.google.tsunami.plugins.detectors.credentials.genericweakcredentialdetector.provider.TestCredential;
import com.google.tsunami.plugins.detectors.credentials.genericweakcredentialdetector.tester.CredentialTester;
import com.google.tsunami.proto.NetworkService;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import java.io.IOException;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.List;
import java.util.Objects;
import javax.inject.Inject;

/** Credential tester specifically for kubeflow. */
public final class KubeflowCredentialTester extends CredentialTester {
private static final GoogleLogger logger = GoogleLogger.forEnclosingClass();

private final HttpClient httpClient;

@Inject
KubeflowCredentialTester(HttpClient httpClient) {
this.httpClient = checkNotNull(httpClient).modify().setFollowRedirects(false).build();
}

@Override
public String name() {
return "KubeflowCredentialTester";
}

@Override
public String description() {
return "Kubeflow credential tester.";
}

@Override
public boolean canAccept(NetworkService networkService) {
return NetworkServiceUtils.getWebServiceName(networkService).equals("kubeflow");
}

@Override
public boolean batched() {
return true;
}

@Override
public ImmutableList<TestCredential> testValidCredentials(
NetworkService networkService, List<TestCredential> credentials) {
// Always return 1st weak credential to gracefully handle no auth configured case, where we
// return empty credential instead of all the weak credentials
logger.atWarning().log("====== ======== ");
return credentials.stream()
.filter(cred -> isKubeflowAccessible(networkService, cred))
.findFirst()
.map(ImmutableList::of)
.orElseGet(ImmutableList::of);
}

private boolean isKubeflowAccessible(NetworkService networkService, TestCredential credential) {
// logger.atWarning().log("======================= '%s'", credential.username());
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@tooryx I tried to find out which user/pass is passed to here and only the TOP_100_PASSWORDS passed to this method for testing the login. but I need to use the default credentials only.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @grandsilva,

I would need to look more closely, but if I remember correctly, the weak credential tester will use the service name that is reported by nmap. The default credentials are defined for service "kubeflow" but I am not sure if nmap actually reports kubeflow with this name. Could you try to tweak the service name in the default credentials file and let me know if this is causing the issue?

final String rootUri = buildWebApplicationRootUrl(networkService);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

java -cp "tsunami-main-0.0.24-SNAPSHOT-cli.jar:/home/user/tsunami/Plugins/*"   -Dtsunami.config.location=/home/user/tsunami/tsunami_tcs.yaml   com.google.tsunami.main.cli.TsunamiCli   --uri-target=http://127.0.0.1:8080  --scan-results-local-output-format=JSON   --scan-results-local-output-filename=/tmp/tsunami-output.json  --http-client-trust-all-certificates

@tooryx I tried the above command, but I received WARNING: Plugin '/Tsunami Team (tsunami-dev@google.com)/VULN_DETECTION/GenericCredentialDetectorPlugin/0.1' failed: null which because of some internal error in buildWebApplicationRootUrl the plugin doesn't work too, this issue is new this time for me. I tried to manually create the rootUri and this solved the issue. but still even with the fingerprint phase and adding the "kubeflow" service name I can't receive the default credentials.

Also, could you please tag this PR to the main PR, please?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @grandsilva,

I haven't seen any testbed, you should also create one here https://github.com/google/security-testbeds to fully test your plugin. If you create it I can check the plugin and what is going wrong.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @RaulDoyensec
I uploaded an early version just now
google/security-testbeds#99

thank you for helping me on this PR

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @RaulDoyensec
I updated the testbed to a fully automatic bash script.

try {
HttpResponse rsp =
httpClient.send(
get(rootUri + "oauth2/start?rd=%2F").withEmptyHeaders().build(), networkService);
if (rsp.headers().get("set-cookie").isEmpty() || rsp.headers().get("location").isEmpty()) {
return false;
}
String oauth2ProxyKubeflowCsrf = rsp.headers().get("set-cookie").get();
HttpHeaders.Builder headers =
HttpHeaders.builder().addHeader("Cookie", oauth2ProxyKubeflowCsrf);
rsp =
httpClient.send(
get(rootUri + rsp.headers().get("location").get().substring(1))
.setHeaders(headers.build())
.build(),
networkService);
if (rsp.headers().get("location").isEmpty()) {
return false;
}
rsp =
httpClient.send(
get(rootUri + rsp.headers().get("location").get().substring(1))
.setHeaders(headers.build())
.build(),
networkService);
if (rsp.headers().get("location").isEmpty()) {
return false;
}
rsp =
httpClient.send(
get(rootUri + rsp.headers().get("location").get().substring(1))
.setHeaders(headers.build())
.build(),
networkService);
if (rsp.bodyString().isEmpty()) {
return false;
}
String action = null;
Document doc = Jsoup.parse(rsp.bodyString().get());
for (Element anchor : doc.getElementsByTag("form")) {
action = anchor.attr("action");
}
if (Objects.isNull(action)) {
return false;
}
rsp =
httpClient.send(
post(rootUri + action.substring(1))
.setHeaders(
headers
.addHeader("Content-Type", "application/x-www-form-urlencoded")
.build())
.setRequestBody(
ByteString.copyFromUtf8(
String.format(
"login=%s&password=%s",
URLEncoder.encode(credential.username(), StandardCharsets.UTF_8),
URLEncoder.encode(
credential.password().get(), StandardCharsets.UTF_8))))
.build(),
networkService);
if (rsp.headers().get("location").isEmpty()) {
return false;
}
rsp =
httpClient.send(
get(rootUri + rsp.headers().get("location").get().substring(1))
.setHeaders(headers.build())
.build(),
networkService);
if (rsp.headers().get("set-cookie").isEmpty() || rsp.headers().get("location").isEmpty()) {
return false;
}
String authCookie = "";
ImmutableList<String> setCookieHeaders = rsp.headers().getAll("set-cookie");
for (String setCookieHeader : setCookieHeaders) {
if (setCookieHeader.startsWith("oauth2_proxy_kubeflow=")) {
authCookie = setCookieHeader;
}
}
rsp =
httpClient.send(
get(rootUri + "api/dashboard-links")
.setHeaders(
HttpHeaders.builder()
.addHeader("Cookie", authCookie)
.addHeader("Accept", "application/json")
.build())
.build(),
networkService);
if (rsp.bodyJson().isEmpty() || !rsp.status().isSuccess()) {
return false;
}
JsonObject bodyJsonObj = rsp.bodyJson().get().getAsJsonObject();
if (bodyJsonObj.has("menuLinks")
|| bodyJsonObj.has("documentationItems")
|| bodyJsonObj.has("quickLinks")) {
return true;
}
} catch (RuntimeException | IOException e) {
logger.atWarning().withCause(e).log("Failed to send HTTP request to '%s'", rootUri);
return false;
}
return false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -83,3 +83,9 @@ service_default_credentials {
default_usernames: "default"
default_passwords: ""
}

service_default_credentials {
service_name: "kubeflow"
default_usernames: "user@example.com"
default_passwords: "12341234"
}
Loading