-
Notifications
You must be signed in to change notification settings - Fork 190
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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()); | ||
final String rootUri = buildWebApplicationRootUrl(networkService); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
@tooryx I tried the above command, but I received Also, could you please tag this PR to the main PR, please? There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hi @RaulDoyensec thank you for helping me on this PR There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hi @RaulDoyensec |
||
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; | ||
} | ||
} |
There was a problem hiding this comment.
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.There was a problem hiding this comment.
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?