GeezerVison is showing github.com — feat: implement security misconfiguration lesson · WebGoat/WebGoat@d55c55b
File treeExpand file treeCollapse file tree Open diff view settings it/java/org/owasp/webgoat/playwright/webgoat java/org/owasp/webgoat/lessons/securitymisconfiguration resources/lessons/securitymisconfiguration
test/java/org/owasp/webgoat/lessons/securitymisconfiguration
Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: src/it/java/org/owasp/webgoat/playwright/webgoat/PlaywrightTest.java -1Lines changed: 0 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -23,7 +23,6 @@ public Options getOptions() { | 23 | 23 | .setHeadless(true) | 24 | 24 | .setContextOptions(getContextOptions()); | 25 | 25 | } | 26 | | - | 27 | 26 | } | 28 | 27 |
| 29 | 28 | protected static Browser.NewContextOptions getContextOptions() { | |
Collapse file Copy file name to clipboardExpand all lines: src/it/java/org/owasp/webgoat/playwright/webgoat/lessons/OpenRedirectLessonUITest.java -1Lines changed: 0 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
| 7 | 7 | import static com.microsoft.playwright.assertions.PlaywrightAssertions.assertThat; | 8 | 8 |
| 9 | 9 | import com.microsoft.playwright.Browser; | 10 | | -import org.assertj.core.api.Assertions; | 11 | 10 | import org.junit.jupiter.api.BeforeEach; | 12 | 11 | import org.junit.jupiter.api.DisplayName; | 13 | 12 | import org.junit.jupiter.api.Test; | |
Collapse file Copy file name to clipboard +63Lines changed: 63 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +/* | | 2 | + * SPDX-FileCopyrightText: Copyright © 2025 WebGoat authors | | 3 | + * SPDX-License-Identifier: GPL-2.0-or-later | | 4 | + */ | | 5 | +package org.owasp.webgoat.playwright.webgoat.lessons; | | 6 | + | | 7 | +import static com.microsoft.playwright.assertions.PlaywrightAssertions.assertThat; | | 8 | + | | 9 | +import com.microsoft.playwright.Browser; | | 10 | +import org.assertj.core.api.Assertions; | | 11 | +import org.owasp.webgoat.playwright.webgoat.pages.lessons.SecurityMisconfigurationLessonPage; | | 12 | +import org.junit.jupiter.api.BeforeEach; | | 13 | +import org.junit.jupiter.api.DisplayName; | | 14 | +import org.junit.jupiter.api.Test; | | 15 | +import org.owasp.webgoat.container.lessons.LessonName; | | 16 | +import org.owasp.webgoat.playwright.webgoat.PlaywrightTest; | | 17 | +import org.owasp.webgoat.playwright.webgoat.helpers.Authentication; | | 18 | + | | 19 | +public class SecurityMisconfigurationLessonUITest extends PlaywrightTest { | | 20 | + | | 21 | + private SecurityMisconfigurationLessonPage lessonPage; | | 22 | + | | 23 | + @BeforeEach | | 24 | + void navigateToLesson(Browser browser) { | | 25 | + var lessonName = new LessonName("SecurityMisconfiguration"); | | 26 | + var page = Authentication.sylvester(browser); | | 27 | + | | 28 | + this.lessonPage = new SecurityMisconfigurationLessonPage(page); | | 29 | + lessonPage.resetLesson(lessonName); | | 30 | + lessonPage.open(lessonName); | | 31 | + } | | 32 | + | | 33 | + @Test | | 34 | + @DisplayName("Walk through Security Misconfiguration lesson") | | 35 | + void shouldCompleteSecurityMisconfigurationTasks() { | | 36 | + // Task 1 - default credentials | | 37 | + lessonPage.navigateTo(2); | | 38 | + lessonPage.fillDefaultCredentials("admin", "admin"); | | 39 | + lessonPage.submitTask1(); | | 40 | + assertThat(lessonPage.task1Output()).containsText("Default admin account compromised"); | | 41 | + | | 42 | + // Task 2 - verbose error leak | | 43 | + lessonPage.navigateTo(3); | | 44 | + | | 45 | + lessonPage.triggerDebugLeak(); | | 46 | + Assertions.assertThat(lessonPage.debugOutput()).contains("SYSTEM_API_TOKEN"); | | 47 | + lessonPage.submitTask2(lessonPage.extractTokenFromDebug()); | | 48 | + assertThat(lessonPage.getAssignmentOutput()).containsText("Debug token recovered"); | | 49 | + | | 50 | + // Task 3 - actuator exposure | | 51 | + lessonPage.navigateTo(4); | | 52 | + | | 53 | + var envJson = lessonPage.requestActuatorEnv(); | | 54 | + Assertions.assertThat(envJson).contains("systemApiKey"); | | 55 | + lessonPage.submitTask3(lessonPage.extractApiKey(envJson)); | | 56 | + assertThat(lessonPage.getAssignmentOutput()).containsText("Actuator endpoints secured"); | | 57 | + | | 58 | + // Task 4 - configuration hardening | | 59 | + lessonPage.navigateTo(5); | | 60 | + lessonPage.applyHardeningConfig(); | | 61 | + assertThat(lessonPage.getAssignmentOutput()).containsText("Configuration hardened"); | | 62 | + } | | 63 | +} |
Collapse file Copy file name to clipboard +105Lines changed: 105 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +/* | | 2 | + * SPDX-FileCopyrightText: Copyright © 2025 WebGoat authors | | 3 | + * SPDX-License-Identifier: GPL-2.0-or-later | | 4 | + */ | | 5 | +package org.owasp.webgoat.playwright.webgoat.pages.lessons; | | 6 | + | | 7 | +import static org.owasp.webgoat.playwright.webgoat.PlaywrightTest.webGoatUrl; | | 8 | + | | 9 | +import com.microsoft.playwright.Locator; | | 10 | +import com.microsoft.playwright.Page; | | 11 | +import com.microsoft.playwright.Page.GetByRoleOptions; | | 12 | +import com.microsoft.playwright.options.AriaRole; | | 13 | +import java.util.regex.Matcher; | | 14 | +import java.util.regex.Pattern; | | 15 | +import org.owasp.webgoat.container.lessons.LessonName; | | 16 | + | | 17 | +public class SecurityMisconfigurationLessonPage extends LessonPage { | | 18 | + | | 19 | + private static final Pattern TOKEN_PATTERN = Pattern.compile("SYSTEM_API_TOKEN=([\\w-]+)"); | | 20 | + private static final Pattern API_KEY_PATTERN = | | 21 | + Pattern.compile("\"systemApiKey\"\s*:\s*\"([^\"]+)\""); | | 22 | + | | 23 | + public SecurityMisconfigurationLessonPage(Page page) { | | 24 | + super(page); | | 25 | + } | | 26 | + | | 27 | + public void open(LessonName lessonName) { | | 28 | + getPage().navigate(webGoatUrl("start.mvc#lesson/%s".formatted(lessonName.lessonName()))); | | 29 | + } | | 30 | + | | 31 | + public void fillDefaultCredentials(String username, String password) { | | 32 | + getPage().locator("[name='username']").fill(username); | | 33 | + getPage().locator("[name='password']").fill(password); | | 34 | + } | | 35 | + | | 36 | + public void submitTask1() { | | 37 | + getPage().getByRole(AriaRole.BUTTON, new GetByRoleOptions().setName("Attempt login")).click(); | | 38 | + } | | 39 | + | | 40 | + public Locator task1Output() { | | 41 | + return getAssignmentOutput(); | | 42 | + } | | 43 | + | | 44 | + public void triggerDebugLeak() { | | 45 | + Page page = getPage(); | | 46 | + page.getByRole(AriaRole.BUTTON, new GetByRoleOptions().setName("Trigger debug error")).click(); | | 47 | + page.waitForTimeout(500); // allow fetch to complete | | 48 | + } | | 49 | + | | 50 | + public String debugOutput() { | | 51 | + return getPage().locator("#debug-output").textContent(); | | 52 | + } | | 53 | + | | 54 | + public String extractTokenFromDebug() { | | 55 | + Matcher matcher = TOKEN_PATTERN.matcher(debugOutput()); | | 56 | + if (matcher.find()) { | | 57 | + return matcher.group(1); | | 58 | + } | | 59 | + return ""; | | 60 | + } | | 61 | + | | 62 | + public void submitTask2(String token) { | | 63 | + Page page = getPage(); | | 64 | + page.getByLabel("Leaked token").fill(token); | | 65 | + page.getByRole(AriaRole.BUTTON, new GetByRoleOptions().setName("Submit token")).click(); | | 66 | + } | | 67 | + | | 68 | + public String requestActuatorEnv() { | | 69 | + Page page = getPage(); | | 70 | + page.getByRole(AriaRole.BUTTON, new GetByRoleOptions().setName("GET /actuator/env")).click(); | | 71 | + page.waitForTimeout(300); | | 72 | + return page.locator("#actuator-output").textContent(); | | 73 | + } | | 74 | + | | 75 | + public String extractApiKey(String json) { | | 76 | + Matcher matcher = API_KEY_PATTERN.matcher(json); | | 77 | + if (matcher.find()) { | | 78 | + return matcher.group(1); | | 79 | + } | | 80 | + return ""; | | 81 | + } | | 82 | + | | 83 | + public void submitTask3(String apiKey) { | | 84 | + Page page = getPage(); | | 85 | + page.getByLabel("System API key").fill(apiKey); | | 86 | + page.getByRole(AriaRole.BUTTON, new GetByRoleOptions().setName("Submit key")).click(); | | 87 | + } | | 88 | + | | 89 | + public Locator task3Output() { | | 90 | + return getPage().locator(".lesson-page-wrapper").nth(3).locator(".attack-output"); | | 91 | + } | | 92 | + | | 93 | + public void applyHardeningConfig() { | | 94 | + Page page = getPage(); | | 95 | + page.getByLabel("management.endpoint.env.enabled").selectOption("false"); | | 96 | + page.getByLabel("management.endpoint.health.show-details").selectOption("never"); | | 97 | + page.getByLabel("spring.security.user.name").fill(""); | | 98 | + page.getByLabel("spring.security.user.password").fill(""); | | 99 | + page.getByRole(AriaRole.BUTTON, new GetByRoleOptions().setName("Apply configuration")).click(); | | 100 | + } | | 101 | + | | 102 | + public Locator task4Output() { | | 103 | + return getPage().locator(".lesson-page-wrapper").locator(".attack-output").last(); | | 104 | + } | | 105 | +} |
Collapse file Copy file name to clipboard +72Lines changed: 72 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +/* | | 2 | + * SPDX-FileCopyrightText: Copyright © 2025 WebGoat authors | | 3 | + * SPDX-License-Identifier: GPL-2.0-or-later | | 4 | + */ | | 5 | +package org.owasp.webgoat.lessons.securitymisconfiguration; | | 6 | + | | 7 | +import static org.owasp.webgoat.container.assignments.AttackResultBuilder.failed; | | 8 | +import static org.owasp.webgoat.container.assignments.AttackResultBuilder.success; | | 9 | + | | 10 | +import java.util.Map; | | 11 | +import org.owasp.webgoat.container.assignments.AssignmentEndpoint; | | 12 | +import org.owasp.webgoat.container.assignments.AssignmentHints; | | 13 | +import org.owasp.webgoat.container.assignments.AttackResult; | | 14 | +import org.springframework.http.MediaType; | | 15 | +import org.springframework.web.bind.annotation.GetMapping; | | 16 | +import org.springframework.web.bind.annotation.PostMapping; | | 17 | +import org.springframework.web.bind.annotation.RequestParam; | | 18 | +import org.springframework.web.bind.annotation.RestController; | | 19 | + | | 20 | +/** Task showing exposed actuator/admin endpoints leaking secrets. */ | | 21 | +@RestController | | 22 | +@AssignmentHints({ | | 23 | + "securitymisconfiguration.task3.hint1", | | 24 | + "securitymisconfiguration.task3.hint2" | | 25 | +}) | | 26 | +public class ActuatorExposureTask implements AssignmentEndpoint { | | 27 | + | | 28 | + static final String LEAKED_API_KEY = "INTERNAL-API-KEY-987"; | | 29 | + | | 30 | + @GetMapping( | | 31 | + value = "/SecurityMisconfiguration/task3/actuator/env", | | 32 | + produces = MediaType.APPLICATION_JSON_VALUE) | | 33 | + public Map<String, Object> actuatorEnv() { | | 34 | + return Map.of( | | 35 | + "name", "webgoat-staging", | | 36 | + "profiles", new String[] {"staging", "debug"}, | | 37 | + "systemApiKey", LEAKED_API_KEY, | | 38 | + "features", Map.of("betaUi", true, "payments", false)); | | 39 | + } | | 40 | + | | 41 | + @GetMapping( | | 42 | + value = "/SecurityMisconfiguration/task3/actuator/health", | | 43 | + produces = MediaType.APPLICATION_JSON_VALUE) | | 44 | + public Map<String, Object> actuatorHealth() { | | 45 | + return Map.of( | | 46 | + "status", "UP", | | 47 | + "checks", | | 48 | + Map.of( | | 49 | + "database", Map.of("status", "UP", "responseTimeMs", 12), | | 50 | + "cache", Map.of("status", "UP", "hitRatio", 0.91))); | | 51 | + } | | 52 | + | | 53 | + @PostMapping( | | 54 | + value = "/SecurityMisconfiguration/task3", | | 55 | + consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE) | | 56 | + public AttackResult submitApiKey(@RequestParam("apiKey") String apiKey) { | | 57 | + if (LEAKED_API_KEY.equals(apiKey)) { | | 58 | + return success(this) | | 59 | + .feedback("securitymisconfiguration.task3.success") | | 60 | + .output("Actuator endpoints now require authentication and are limited to ops network.") | | 61 | + .build(); | | 62 | + } | | 63 | + if (apiKey == null || apiKey.isBlank()) { | | 64 | + return failed(this) | | 65 | + .feedback("securitymisconfiguration.task3.failure.blank") | | 66 | + .build(); | | 67 | + } | | 68 | + return failed(this) | | 69 | + .feedback("securitymisconfiguration.task3.failure.invalid") | | 70 | + .build(); | | 71 | + } | | 72 | +} |
Collapse file Copy file name to clipboard +64Lines changed: 64 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +/* | | 2 | + * SPDX-FileCopyrightText: Copyright © 2025 WebGoat authors | | 3 | + * SPDX-License-Identifier: GPL-2.0-or-later | | 4 | + */ | | 5 | +package org.owasp.webgoat.lessons.securitymisconfiguration; | | 6 | + | | 7 | +import static org.owasp.webgoat.container.assignments.AttackResultBuilder.failed; | | 8 | +import static org.owasp.webgoat.container.assignments.AttackResultBuilder.success; | | 9 | + | | 10 | +import jakarta.validation.constraints.NotBlank; | | 11 | +import java.util.HashMap; | | 12 | +import java.util.Map; | | 13 | +import org.owasp.webgoat.container.assignments.AssignmentEndpoint; | | 14 | +import org.owasp.webgoat.container.assignments.AssignmentHints; | | 15 | +import org.owasp.webgoat.container.assignments.AttackResult; | | 16 | +import org.springframework.http.MediaType; | | 17 | +import org.springframework.web.bind.annotation.PostMapping; | | 18 | +import org.springframework.web.bind.annotation.RequestParam; | | 19 | +import org.springframework.web.bind.annotation.RestController; | | 20 | + | | 21 | +/** Task where learners harden a configuration by disabling insecure settings. */ | | 22 | +@RestController | | 23 | +@AssignmentHints({ | | 24 | + "securitymisconfiguration.task4.hint1", | | 25 | + "securitymisconfiguration.task4.hint2" | | 26 | +}) | | 27 | +public class ConfigHardeningTask implements AssignmentEndpoint { | | 28 | + | | 29 | + private static final Map<String, String> EXPECTED = | | 30 | + Map.of( | | 31 | + "management.endpoint.env.enabled", "false", | | 32 | + "management.endpoint.health.show-details", "never", | | 33 | + "spring.security.user.name", "", | | 34 | + "spring.security.user.password", ""); | | 35 | + | | 36 | + @PostMapping( | | 37 | + value = "/SecurityMisconfiguration/task4", | | 38 | + consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE) | | 39 | + public AttackResult submitConfig( | | 40 | + @RequestParam("envEnabled") @NotBlank String envEnabled, | | 41 | + @RequestParam("healthDetails") @NotBlank String healthDetails, | | 42 | + @RequestParam(value = "defaultUser", required = false) String defaultUser, | | 43 | + @RequestParam(value = "defaultPassword", required = false) String defaultPassword) { | | 44 | + | | 45 | + Map<String, String> current = new HashMap<>(); | | 46 | + current.put("management.endpoint.env.enabled", envEnabled.trim()); | | 47 | + current.put("management.endpoint.health.show-details", healthDetails.trim()); | | 48 | + current.put("spring.security.user.name", defaultUser == null ? "" : defaultUser.trim()); | | 49 | + current.put( | | 50 | + "spring.security.user.password", defaultPassword == null ? "" : defaultPassword.trim()); | | 51 | + | | 52 | + if (current.equals(EXPECTED)) { | | 53 | + return success(this) | | 54 | + .feedback("securitymisconfiguration.task4.success") | | 55 | + .output("Configuration hardened: debug endpoints disabled, default user removed.") | | 56 | + .build(); | | 57 | + } | | 58 | + | | 59 | + return failed(this) | | 60 | + .feedback("securitymisconfiguration.task4.failure.invalid") | | 61 | + .output("Check that env endpoint is disabled, health details hidden, and default user removed.") | | 62 | + .build(); | | 63 | + } | | 64 | +} |
Collapse file Copy file name to clipboard +56Lines changed: 56 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +/* | | 2 | + * SPDX-FileCopyrightText: Copyright © 2025 WebGoat authors | | 3 | + * SPDX-License-Identifier: GPL-2.0-or-later | | 4 | + */ | | 5 | +package org.owasp.webgoat.lessons.securitymisconfiguration; | | 6 | + | | 7 | +import static org.owasp.webgoat.container.assignments.AttackResultBuilder.failed; | | 8 | +import static org.owasp.webgoat.container.assignments.AttackResultBuilder.success; | | 9 | + | | 10 | +import org.apache.commons.lang3.StringUtils; | | 11 | +import org.owasp.webgoat.container.assignments.AssignmentEndpoint; | | 12 | +import org.owasp.webgoat.container.assignments.AssignmentHints; | | 13 | +import org.owasp.webgoat.container.assignments.AttackResult; | | 14 | +import org.springframework.http.MediaType; | | 15 | +import org.springframework.web.bind.annotation.PostMapping; | | 16 | +import org.springframework.web.bind.annotation.RequestParam; | | 17 | +import org.springframework.web.bind.annotation.ResponseBody; | | 18 | +import org.springframework.web.bind.annotation.RestController; | | 19 | + | | 20 | +/** Task demonstrating exploitation of default credentials. */ | | 21 | +@RestController | | 22 | +@AssignmentHints({ | | 23 | + "securitymisconfiguration.task1.hint1", | | 24 | + "securitymisconfiguration.task1.hint2" | | 25 | +}) | | 26 | +public class DefaultCredentialsTask implements AssignmentEndpoint { | | 27 | + | | 28 | + private static final String DEFAULT_USERNAME = "admin"; | | 29 | + private static final String DEFAULT_PASSWORD = "admin"; | | 30 | + | | 31 | + @PostMapping( | | 32 | + value = "/SecurityMisconfiguration/task1", | | 33 | + consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE) | | 34 | + @ResponseBody | | 35 | + public AttackResult login( | | 36 | + @RequestParam(value = "username", required = false) String username, | | 37 | + @RequestParam(value = "password", required = false) String password) { | | 38 | + | | 39 | + if (StringUtils.isBlank(username) || StringUtils.isBlank(password)) { | | 40 | + return failed(this) | | 41 | + .feedback("securitymisconfiguration.task1.failure.blank") | | 42 | + .build(); | | 43 | + } | | 44 | + | | 45 | + if (DEFAULT_USERNAME.equals(username.trim()) && DEFAULT_PASSWORD.equals(password)) { | | 46 | + return success(this) | | 47 | + .feedback("securitymisconfiguration.task1.success") | | 48 | + .output("User profile: staging admin (no MFA)") | | 49 | + .build(); | | 50 | + } | | 51 | + | | 52 | + return failed(this) | | 53 | + .feedback("securitymisconfiguration.task1.failure.invalid") | | 54 | + .build(); | | 55 | + } | | 56 | +} |
|