|
| 1 | +/* |
| 2 | + * Copyright 2023 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.credentials.genericweakcredentialdetector.testers.tomcat; |
| 18 | + |
| 19 | +import static com.google.common.collect.ImmutableList.toImmutableList; |
| 20 | +import static java.nio.charset.StandardCharsets.UTF_8; |
| 21 | + |
| 22 | +import com.doyensec.ajp13.AjpMessage; |
| 23 | +import com.doyensec.ajp13.AjpReader; |
| 24 | +import com.doyensec.ajp13.ForwardRequestMessage; |
| 25 | +import com.doyensec.ajp13.Pair; |
| 26 | +import com.google.common.collect.ImmutableList; |
| 27 | +import com.google.common.flogger.GoogleLogger; |
| 28 | +import com.google.tsunami.common.data.NetworkEndpointUtils; |
| 29 | +import com.google.tsunami.common.data.NetworkServiceUtils; |
| 30 | +import com.google.tsunami.plugins.detectors.credentials.genericweakcredentialdetector.provider.TestCredential; |
| 31 | +import com.google.tsunami.plugins.detectors.credentials.genericweakcredentialdetector.tester.CredentialTester; |
| 32 | +import com.google.tsunami.proto.NetworkService; |
| 33 | +import java.io.DataInputStream; |
| 34 | +import java.io.DataOutputStream; |
| 35 | +import java.io.IOException; |
| 36 | +import java.net.Socket; |
| 37 | +import java.util.Base64; |
| 38 | +import java.util.LinkedList; |
| 39 | +import java.util.List; |
| 40 | +import javax.inject.Inject; |
| 41 | + |
| 42 | +/** Credential tester for Tomcat using AJP. */ |
| 43 | +public final class TomcatAjpCredentialTester extends CredentialTester { |
| 44 | + private static final GoogleLogger logger = GoogleLogger.forEnclosingClass(); |
| 45 | + |
| 46 | + private static final String AJP13_SERVICE = "ajp13"; |
| 47 | + private static final String TOMCAT_COOKIE_SET = "set-cookie: JSESSIONID"; |
| 48 | + private static final String TOMCAT_AUTH_HEADER = "Basic realm=\"Tomcat Manager Application\""; |
| 49 | + |
| 50 | + @Inject |
| 51 | + TomcatAjpCredentialTester() {} |
| 52 | + |
| 53 | + @Override |
| 54 | + public String name() { |
| 55 | + return "TomcatAjpCredentialTester"; |
| 56 | + } |
| 57 | + |
| 58 | + @Override |
| 59 | + public boolean batched() { |
| 60 | + return true; |
| 61 | + } |
| 62 | + |
| 63 | + @Override |
| 64 | + public String description() { |
| 65 | + return "Tomcat AJP credential tester."; |
| 66 | + } |
| 67 | + |
| 68 | + @Override |
| 69 | + public boolean canAccept(NetworkService networkService) { |
| 70 | + |
| 71 | + var uriAuthority = NetworkEndpointUtils.toUriAuthority(networkService.getNetworkEndpoint()); |
| 72 | + |
| 73 | + boolean canAcceptByNmapReport = |
| 74 | + NetworkServiceUtils.getWebServiceName(networkService).equals(AJP13_SERVICE); |
| 75 | + |
| 76 | + if (!canAcceptByNmapReport) { |
| 77 | + return false; |
| 78 | + } |
| 79 | + |
| 80 | + boolean canAcceptByCustomFingerprint = false; |
| 81 | + |
| 82 | + String[] uriParts = uriAuthority.split(":"); |
| 83 | + String host = uriParts[0]; |
| 84 | + int port = Integer.parseInt(uriParts[1]); |
| 85 | + |
| 86 | + // Check if the server response indicates a redirection to /manager/html. |
| 87 | + // This typically means that the Tomcat Manager is active and automatically |
| 88 | + // redirects users to the management interface when accessing the base manager URL. |
| 89 | + try { |
| 90 | + logger.atInfo().log("probing Tomcat manager - custom fingerprint phase using AJP"); |
| 91 | + |
| 92 | + List<Pair<String, String>> headers = new LinkedList<>(); |
| 93 | + List<Pair<String, String>> attributes = new LinkedList<>(); |
| 94 | + AjpMessage request = |
| 95 | + new ForwardRequestMessage( |
| 96 | + 2, "HTTP/1.1", "/manager/html", host, host, host, port, true, headers, attributes); |
| 97 | + |
| 98 | + byte[] response = sendAndReceive(host, port, request.getBytes()); |
| 99 | + AjpMessage responseMessage = AjpReader.parseMessage(response); |
| 100 | + |
| 101 | + canAcceptByCustomFingerprint = |
| 102 | + responseMessage.getDescription().toLowerCase().contains(TOMCAT_AUTH_HEADER.toLowerCase()); |
| 103 | + |
| 104 | + } catch (NullPointerException e) { |
| 105 | + logger.atWarning().log("Unable to query '%s'.", uriAuthority); |
| 106 | + return false; |
| 107 | + } catch (IOException e) { |
| 108 | + logger.atWarning().log("Unable to query '%s'.", uriAuthority); |
| 109 | + return false; |
| 110 | + } |
| 111 | + |
| 112 | + return canAcceptByCustomFingerprint; |
| 113 | + } |
| 114 | + |
| 115 | + @Override |
| 116 | + public ImmutableList<TestCredential> testValidCredentials( |
| 117 | + NetworkService networkService, List<TestCredential> credentials) { |
| 118 | + |
| 119 | + return credentials.stream() |
| 120 | + .filter(cred -> isTomcatAccessible(networkService, cred)) |
| 121 | + .collect(toImmutableList()); |
| 122 | + } |
| 123 | + |
| 124 | + private boolean isTomcatAccessible(NetworkService networkService, TestCredential credential) { |
| 125 | + var uriAuthority = NetworkEndpointUtils.toUriAuthority(networkService.getNetworkEndpoint()); |
| 126 | + String[] uriParts = uriAuthority.split(":"); |
| 127 | + String host = uriParts[0]; |
| 128 | + int port = Integer.parseInt(uriParts[1]); |
| 129 | + var url = String.format("%s/%s", uriAuthority, "manager/html"); |
| 130 | + |
| 131 | + logger.atInfo().log("uriAuthority: %s", uriAuthority); |
| 132 | + try { |
| 133 | + logger.atInfo().log( |
| 134 | + "url: %s, username: %s, password: %s", |
| 135 | + url, credential.username(), credential.password().orElse("")); |
| 136 | + |
| 137 | + String authorization = |
| 138 | + "Basic " |
| 139 | + + Base64.getEncoder() |
| 140 | + .encodeToString( |
| 141 | + (credential.username() + ":" + credential.password().orElse("")) |
| 142 | + .getBytes(UTF_8)); |
| 143 | + |
| 144 | + List<Pair<String, String>> headers = new LinkedList<>(); |
| 145 | + headers.add(Pair.make("Authorization", authorization)); |
| 146 | + List<Pair<String, String>> attributes = new LinkedList<>(); |
| 147 | + |
| 148 | + AjpMessage request = |
| 149 | + new ForwardRequestMessage( |
| 150 | + 2, "HTTP/1.1", "/manager/html", host, host, host, port, true, headers, attributes); |
| 151 | + |
| 152 | + byte[] response = sendAndReceive(host, port, request.getBytes()); |
| 153 | + AjpMessage responseMessage = AjpReader.parseMessage(response); |
| 154 | + |
| 155 | + return headersContainsSuccessfulLoginElements(responseMessage); |
| 156 | + } catch (IOException e) { |
| 157 | + logger.atWarning().withCause(e).log("Unable to query '%s'.", url); |
| 158 | + return false; |
| 159 | + } |
| 160 | + } |
| 161 | + |
| 162 | + // This methods send the AjpMessage generated via sockets and return the response from the server |
| 163 | + private byte[] sendAndReceive(String host, int port, byte[] data) throws IOException { |
| 164 | + try (Socket socket = new Socket(host, port)) { |
| 165 | + DataOutputStream os = new DataOutputStream(socket.getOutputStream()); |
| 166 | + DataInputStream is = new DataInputStream(socket.getInputStream()); |
| 167 | + |
| 168 | + os.write(data); |
| 169 | + os.flush(); |
| 170 | + |
| 171 | + byte[] buffReply = new byte[8192]; |
| 172 | + int bytesRead = is.read(buffReply); |
| 173 | + |
| 174 | + if (bytesRead > 0) { |
| 175 | + byte[] fullReply = new byte[bytesRead]; |
| 176 | + System.arraycopy(buffReply, 0, fullReply, 0, bytesRead); |
| 177 | + |
| 178 | + return fullReply; |
| 179 | + } |
| 180 | + return new byte[0]; |
| 181 | + } catch (IOException e) { |
| 182 | + logger.atSevere().withCause(e).log("Error sendind the AjpMessage"); |
| 183 | + throw e; |
| 184 | + } |
| 185 | + } |
| 186 | + |
| 187 | + // This method checks if the response headers contain elements indicative of a Tomcat manager |
| 188 | + // page. Specifically, it examines the cookies set rather than body elements to improve the |
| 189 | + // efficiency and speed of the plugin. By focusing on headers, the plugin can quickly identify |
| 190 | + // successful logins without parsing potentially large and variable body content. |
| 191 | + private static boolean headersContainsSuccessfulLoginElements(AjpMessage responseMessage) { |
| 192 | + try { |
| 193 | + String responseHeaders = responseMessage.getDescription().toLowerCase(); |
| 194 | + if (responseHeaders.contains(TOMCAT_COOKIE_SET.toLowerCase())) { |
| 195 | + logger.atInfo().log("Found Tomcat endpoint (TOMCAT_COOKIE_SET string present in the page)"); |
| 196 | + return true; |
| 197 | + } else { |
| 198 | + return false; |
| 199 | + } |
| 200 | + } catch (Exception e) { |
| 201 | + logger.atWarning().withCause(e).log( |
| 202 | + "An error occurred in headersContainsSuccessfulLoginElements"); |
| 203 | + return false; |
| 204 | + } |
| 205 | + } |
| 206 | +} |
0 commit comments