Repository navigation
Expand file tree
/
Copy pathXxeLessonIT.java
More file actions
220 lines (177 loc) · 7.42 KB
/
Copy pathXxeLessonIT.java
File metadata and controls
220 lines (177 loc) · 7.42 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
package servlets.module.lesson;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.fail;
import dbProcs.GetterIT;
import dbProcs.Setter;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Locale;
import java.util.Properties;
import java.util.ResourceBundle;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.mock.web.MockHttpServletRequest;
import org.springframework.mock.web.MockHttpServletResponse;
import org.springframework.mock.web.MockServletConfig;
import testUtils.TestProperties;
import utils.InstallationException;
public class XxeLessonIT {
private static final String LANGUAGE_CODE = "en_GB";
private static final Logger log = LogManager.getLogger(XxeLessonIT.class);
private static String applicationRoot = new String();
private MockHttpServletRequest request;
private MockHttpServletResponse response;
private static final String MODULE_CLASS_NAME = "XxeLesson";
private static String TEST_USERNAME = "lessonTester";
private static ResourceBundle errors;
/** Creates DB or Restores DB to Factory Defaults before running tests */
@BeforeAll
public static void before() {
Locale enLocale = Locale.forLanguageTag(LANGUAGE_CODE);
errors = ResourceBundle.getBundle("i18n.servlets.errors", enLocale);
TestProperties.setTestPropertiesFileDirectory(log);
try {
TestProperties.ensureSchemaReady(log);
TestProperties.reseedTestData();
TestProperties.createFileSystemKey(log, "xxe.lesson.file", "xxe.lesson.solution");
GetterIT.verifyTestUser(applicationRoot, TEST_USERNAME, TEST_USERNAME);
} catch (InstallationException e) {
String message = "Could not create DB: " + e.toString();
log.fatal(message);
fail(message);
} catch (Exception e) {
log.fatal("Could not initialise test");
fail(e.toString());
}
}
@BeforeEach
public void setup() {
request = new MockHttpServletRequest();
response = new MockHttpServletResponse();
// Open All modules
if (!Setter.openAllModules(applicationRoot, false)
&& !Setter.openAllModules(applicationRoot, true)) {
fail("Could not Mark All Modules As Open");
}
}
public String doMockPost(byte[] xmlEmail, String csrfToken, int expectedResponseCode)
throws Exception {
log.debug("Creating " + MODULE_CLASS_NAME + " Servlet Instance");
try {
XxeLesson servlet = new XxeLesson();
servlet.init(new MockServletConfig(MODULE_CLASS_NAME));
request.setContentType("application/xml");
log.debug("Setting Up Params and Atrributes");
request.setContent(xmlEmail); // for an XML payload use setContent instead of addParameter
// Adding Correct CSRF Token (Token Submitted)
request.addHeader("csrfToken", csrfToken);
log.debug("Running doPost");
servlet.doPost(request, response);
assertFalse(response.getStatus() != expectedResponseCode);
return (response.getContentAsString());
} catch (Exception e) {
fail(e.toString());
throw e;
}
}
@Test
public void testLevelValidAnswerXxeInjection() {
Setter.openAllModules(applicationRoot, true);
try {
// Sign in as Normal User
log.debug("Signing in as " + TEST_USERNAME + " Through LoginServlet");
TestProperties.loginDoPost(
log, request, response, TEST_USERNAME, TEST_USERNAME, null, LANGUAGE_CODE);
log.debug("Login Servlet Complete, Getting CSRF Token");
String csrfToken = response.getCookie("token").getValue();
assertFalse(csrfToken.isEmpty());
Properties prop = new Properties();
try (InputStream xxe_input =
new FileInputStream(
System.getProperty("user.dir") + "/src/main/resources/fileSystemKeys.properties")) {
prop.load(xxe_input);
} catch (IOException e) {
log.error("Could not load properties file: " + e.toString());
throw new RuntimeException(e);
}
String errorBase = "Missing property :";
String filename = prop.getProperty("xxe.lesson.file");
if (filename == null) {
throw new RuntimeException(errorBase + "xxe.lesson.file");
}
request.setCookies(response.getCookies());
String xxeString =
"<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>"
+ "<!DOCTYPE foo ["
+ "<!ELEMENT foo ANY >"
+ "<!ENTITY xxe SYSTEM \"file://"
+ System.getProperty("user.dir")
+ "/"
+ filename
+ "\""
+ " >]><foo>&xxe;</foo>";
// String xxeString = "<?xml version=\"1.0\"
// encoding=\"ISO-8859-1\"?><email>test@test.com</email>";
String servletResponse = doMockPost(xxeString.getBytes(), csrfToken, 302);
log.debug("Servlet Response: " + servletResponse);
assertTrue(servletResponse.contains("Marsellus Wallace's Key"));
assertFalse(servletResponse.contains("You must be getting funky"));
} catch (Exception e) {
log.fatal("Could not Complete: " + e.toString());
fail("Could not Complete: " + e.toString());
}
}
@Test
public void testLevelWhenUnsafeLevelsAreDisabled() {
Setter.closeAllModules(applicationRoot);
Setter.openAllModules(applicationRoot, false);
try {
// Sign in as Normal User
log.debug("Signing in as " + TEST_USERNAME + " Through LoginServlet");
TestProperties.loginDoPost(
log, request, response, TEST_USERNAME, TEST_USERNAME, null, LANGUAGE_CODE);
log.debug("Login Servlet Complete, Getting CSRF Token");
String csrfToken = response.getCookie("token").getValue();
assertFalse(csrfToken.isEmpty());
request.setCookies(response.getCookies());
Properties prop = new Properties();
try (InputStream xxe_input =
new FileInputStream(
System.getProperty("user.dir") + "/src/main/resources/fileSystemKeys.properties")) {
prop.load(xxe_input);
} catch (IOException e) {
log.error("Could not load properties file: " + e.toString());
throw new RuntimeException(e);
}
String errorBase = "Missing property :";
String filename = prop.getProperty("xxe.lesson.file");
if (filename == null) {
throw new RuntimeException(errorBase + "xxe.lesson.file");
}
String xxeString =
"<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>"
+ "<!DOCTYPE foo ["
+ "<!ELEMENT foo ANY >"
+ "<!ENTITY xxe SYSTEM \"file://"
+ System.getProperty("user.dir")
+ "/"
+ filename
+ "\""
+ " >]><foo>&xxe;</foo>";
// String xxeString = "<?xml version=\"1.0\"
// encoding=\"ISO-8859-1\"?><email>test@test.com</email>";
String servletResponse = doMockPost(xxeString.getBytes(), csrfToken, 302);
log.debug("Servlet Response: " + servletResponse);
assertFalse(servletResponse.contains("Marsellus Wallace's Key"));
assertTrue(servletResponse.contains(errors.getString("error.notOpen")));
} catch (Exception e) {
log.fatal("Could not Complete: " + e.toString());
fail("Could not Complete: " + e.toString());
}
}
}