Repository navigation
Expand file tree
/
Copy pathSecurityMisconfigStealTokens.java
More file actions
229 lines (218 loc) · 9.18 KB
/
Copy pathSecurityMisconfigStealTokens.java
File metadata and controls
229 lines (218 loc) · 9.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
package servlets.module.challenge;
import dbProcs.Database;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.Cookie;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Locale;
import java.util.ResourceBundle;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import utils.Hash;
import utils.ShepherdLogManager;
import utils.Validate;
/**
* Security Misconfiguration Steal Tokens <br>
* <br>
* This file is part of the Security Shepherd Project.
*
* <p>The Security Shepherd project is free software: you can redistribute it and/or modify it under
* the terms of the GNU General Public License as published by the Free Software Foundation, either
* version 3 of the License, or (at your option) any later version.<br>
*
* <p>The Security Shepherd project is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
* PURPOSE. See the GNU General Public License for more details.<br>
*
* <p>You should have received a copy of the GNU General Public License along with the Security
* Shepherd project. If not, see <http://www.gnu.org/licenses/>.
*
* @author Mark Denihan
*/
public class SecurityMisconfigStealTokens extends HttpServlet {
// Security Misconfiguration Challenge
private static final long serialVersionUID = 1L;
private static final Logger log = LogManager.getLogger(SecurityMisconfigStealTokens.class);
private static String levelName = "Security Misconfig Cookie Flags Servlet";
public static String levelHash =
"c4285bbc6734a10897d672c1ed3dd9417e0530a4e0186c27699f54637c7fb5d4";
private static String levelResult =
"92755de2ebb012e689caf8bfec629b1e237d23438427499b6bf0d7933f1b8215"; // Base Key. User is given
// user specific key
/**
* This servlet will return the key to complete as long as the cookie submitted is valid and does
* not belong to the user making the request
*/
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// Setting IpAddress To Log and taking header for original IP if forwarded from proxy
ShepherdLogManager.setRequestIp(request.getRemoteAddr(), request.getHeader("X-Forwarded-For"));
HttpSession ses = request.getSession(true);
if (Validate.validateSession(ses)) {
// Translation Stuff
Locale locale = new Locale(Validate.validateLanguage(request.getSession()));
ResourceBundle errors = ResourceBundle.getBundle("i18n.servlets.errors", locale);
ResourceBundle bundle =
ResourceBundle.getBundle(
"i18n.servlets.challenges.securityMisconfig.stealTokens", locale);
ShepherdLogManager.setRequestIp(
request.getRemoteAddr(),
request.getHeader("X-Forwarded-For"),
ses.getAttribute("userName").toString());
log.debug(levelName + " servlet accessed by: " + ses.getAttribute("userName").toString());
PrintWriter out = response.getWriter();
out.print(getServletInfo());
String htmlOutput = new String();
try {
String applicationRoot = getServletContext().getRealPath("");
String userId = ses.getAttribute("userStamp").toString();
String userActualCookie = getUserToken(userId, applicationRoot);
// Getting Submitted Cookie
int i = 0;
Cookie[] userCookies = request.getCookies();
Cookie theToken = null;
for (i = 0; i < userCookies.length; i++) {
if (userCookies[i].getName().compareTo("securityMisconfigLesson") == 0) {
theToken = userCookies[i];
break; // End Loop, because we found the token
}
}
String cookieValue = theToken.getValue();
log.debug("User Submitted Cookie: " + cookieValue);
log.debug("Stored Cookie Value : " + userActualCookie);
if (cookieValue.compareTo(userActualCookie) == 0) {
// User is using their own Cookie: Not Complete
htmlOutput =
new String(
"<h2 class='title'>"
+ bundle.getString("securityMisconfig.servlet.stealTokens.notComplete")
+ "</h2>"
+ "<p>"
+ bundle.getString(
"securityMisconfig.servlet.stealTokens.notComplete.message")
+ "<p>");
} else {
// User submitted something different from their cookie
boolean notUsersTokenButValid = validToken(userId, cookieValue, applicationRoot);
if (notUsersTokenButValid) {
log.debug("Valid Cookie of another User Dectected");
// Get key and add it to the output
String userKey =
Hash.generateUserSolution(levelResult, (String) ses.getAttribute("userName"));
htmlOutput =
"<h2 class='title'>"
+ bundle.getString("securityMisconfig.servlet.stealTokens.complete")
+ "</h2>"
+ "<p>"
+ bundle.getString("securityMisconfig.servlet.stealTokens.youDidIt")
+ " "
+ "<a>"
+ userKey
+ "</a>"
+ "</p>";
} else {
htmlOutput =
new String(
"<h2 class='title'>"
+ bundle.getString("securityMisconfig.servlet.stealTokens.notComplete")
+ "</h2>"
+ "<p>"
+ bundle.getString(
"securityMisconfig.servlet.stealTokens.notComplete.yourToken")
+ "<p>");
}
}
} catch (Exception e) {
out.write(errors.getString("securityMisconfig.servlet.stealTokens.notComplete.yourToken"));
log.fatal(levelName + " - " + e.toString());
}
log.debug("Outputting HTML");
out.write(htmlOutput);
} else {
log.error(levelName + " servlet accessed with no session");
}
}
/**
* Method that will return a users token. If the user does not have a token, this will set one.
*
* @param userId User Identifier to search for
* @param applicationRoot Running context of application
* @return The token associated with the submitted userId
* @throws SQLException
*/
public static String getUserToken(String userId, String applicationRoot) throws SQLException {
String userToken = new String();
log.debug("Getting user token with id: " + userId);
Connection conn =
Database.getChallengeConnection(applicationRoot, "SecurityMisconfigStealToken");
try {
CallableStatement getTokenCs = conn.prepareCall("call getToken(?)");
getTokenCs.setString(1, userId);
log.debug("Executing getToken procedure...");
ResultSet tokenRs = getTokenCs.executeQuery();
if (tokenRs.next()) {
userToken = tokenRs.getString(1);
} else {
log.error("No Results From Call");
throw new SQLException("No results from getToken Call. Empty Result Set");
}
tokenRs.close();
} catch (SQLException e) {
log.error("Could not get user SecurityMisconfigStealToken token: " + e.toString());
throw e;
}
conn.close();
if (!userToken.isEmpty()) {
log.debug("Found token: " + userToken);
}
return userToken;
}
/**
* Method to validate if a token exists in the database which does not belong to the user
* submitting the request
*
* @param userId The ID of the user submitting the request
* @param token The token submitted in the request
* @param applicationRoot Running context of the application
* @return Boolean depicting if the token exists in the database and does not belong to the user
* submitting the request
* @throws SQLException
*/
public static boolean validToken(String userId, String token, String applicationRoot)
throws SQLException {
boolean validToken = false;
log.debug("Checking token:" + token);
Connection conn =
Database.getChallengeConnection(applicationRoot, "SecurityMisconfigStealToken");
try {
CallableStatement validateTokenCs = conn.prepareCall("call validToken(?, ?)");
validateTokenCs.setString(1, userId);
validateTokenCs.setString(2, token);
log.debug("Executing validToken procedure...");
ResultSet tokenRs = validateTokenCs.executeQuery();
if (tokenRs.next()) {
if (tokenRs.getInt(1) > 0) {
log.debug("Valid Token Detected");
validToken = true;
}
} else {
log.error("No Results From validToken Call");
throw new SQLException("No results from validToken Call. Empty Result Set");
}
tokenRs.close();
} catch (SQLException e) {
log.error("Could not verify token: " + e.toString());
throw e;
}
conn.close();
return validToken;
}
}