forked from WebGoat/WebGoat
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Flags are now wired through a Spring config - Introduced Flag class - Removed Flags from the FlagController
- Loading branch information
Showing
9 changed files
with
131 additions
and
145 deletions.
There are no files selected for viewing
88 changes: 6 additions & 82 deletions
88
src/main/java/org/owasp/webgoat/lessons/challenges/Flag.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,89 +1,13 @@ | ||
/* | ||
* This file is part of WebGoat, an Open Web Application Security Project utility. For details, please see http://www.owasp.org/ | ||
* | ||
* Copyright (c) 2002 - 2019 Bruce Mayhew | ||
* | ||
* This program 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 2 of the | ||
* License, or (at your option) any later version. | ||
* | ||
* This program 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. | ||
* | ||
* You should have received a copy of the GNU General Public License along with this program; if | ||
* not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA | ||
* 02111-1307, USA. | ||
* | ||
* Getting Source ============== | ||
* | ||
* Source for this application is maintained at https://github.com/WebGoat/WebGoat, a repository for free software projects. | ||
*/ | ||
|
||
package org.owasp.webgoat.lessons.challenges; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
import java.util.UUID; | ||
import java.util.stream.IntStream; | ||
import javax.annotation.PostConstruct; | ||
import lombok.AllArgsConstructor; | ||
import lombok.Getter; | ||
import org.owasp.webgoat.container.assignments.AssignmentEndpoint; | ||
import org.owasp.webgoat.container.assignments.AttackResult; | ||
import org.owasp.webgoat.container.session.WebSession; | ||
import org.owasp.webgoat.container.users.UserTracker; | ||
import org.owasp.webgoat.container.users.UserTrackerRepository; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.http.MediaType; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.bind.annotation.RequestMethod; | ||
import org.springframework.web.bind.annotation.RequestParam; | ||
import org.springframework.web.bind.annotation.ResponseBody; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
/** | ||
* @author nbaars | ||
* @since 3/23/17. | ||
*/ | ||
@RestController | ||
public class Flag extends AssignmentEndpoint { | ||
|
||
public static final Map<Integer, String> FLAGS = new HashMap<>(); | ||
@Autowired private UserTrackerRepository userTrackerRepository; | ||
@Autowired private WebSession webSession; | ||
|
||
@AllArgsConstructor | ||
private class FlagPosted { | ||
@Getter private boolean lessonCompleted; | ||
} | ||
public record Flag(int number, String answer) { | ||
|
||
@PostConstruct | ||
public void initFlags() { | ||
IntStream.range(1, 10).forEach(i -> FLAGS.put(i, UUID.randomUUID().toString())); | ||
public boolean isCorrect(String flag) { | ||
return answer.equals(flag); | ||
} | ||
|
||
@RequestMapping( | ||
path = "/challenge/flag", | ||
method = RequestMethod.POST, | ||
produces = MediaType.APPLICATION_JSON_VALUE) | ||
@ResponseBody | ||
public AttackResult postFlag(@RequestParam String flag) { | ||
UserTracker userTracker = userTrackerRepository.findByUser(webSession.getUserName()); | ||
String currentChallenge = webSession.getCurrentLesson().getName(); | ||
int challengeNumber = | ||
Integer.valueOf( | ||
currentChallenge.substring(currentChallenge.length() - 1, currentChallenge.length())); | ||
String expectedFlag = FLAGS.get(challengeNumber); | ||
final AttackResult attackResult; | ||
if (expectedFlag.equals(flag)) { | ||
userTracker.assignmentSolved(webSession.getCurrentLesson(), "Assignment" + challengeNumber); | ||
attackResult = success(this).feedback("challenge.flag.correct").build(); | ||
} else { | ||
userTracker.assignmentFailed(webSession.getCurrentLesson()); | ||
attackResult = failed(this).feedback("challenge.flag.incorrect").build(); | ||
} | ||
userTrackerRepository.save(userTracker); | ||
return attackResult; | ||
@Override | ||
public String toString() { | ||
return answer; | ||
} | ||
} |
62 changes: 62 additions & 0 deletions
62
src/main/java/org/owasp/webgoat/lessons/challenges/FlagController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
/* | ||
* This file is part of WebGoat, an Open Web Application Security Project utility. For details, please see http://www.owasp.org/ | ||
* | ||
* Copyright (c) 2002 - 2019 Bruce Mayhew | ||
* | ||
* This program 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 2 of the | ||
* License, or (at your option) any later version. | ||
* | ||
* This program 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. | ||
* | ||
* You should have received a copy of the GNU General Public License along with this program; if | ||
* not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA | ||
* 02111-1307, USA. | ||
* | ||
* Getting Source ============== | ||
* | ||
* Source for this application is maintained at https://github.com/WebGoat/WebGoat, a repository for free software projects. | ||
*/ | ||
|
||
package org.owasp.webgoat.lessons.challenges; | ||
|
||
import lombok.AllArgsConstructor; | ||
import org.owasp.webgoat.container.assignments.AssignmentEndpoint; | ||
import org.owasp.webgoat.container.assignments.AttackResult; | ||
import org.owasp.webgoat.container.session.WebSession; | ||
import org.owasp.webgoat.container.users.UserTracker; | ||
import org.owasp.webgoat.container.users.UserTrackerRepository; | ||
import org.springframework.http.MediaType; | ||
import org.springframework.web.bind.annotation.PostMapping; | ||
import org.springframework.web.bind.annotation.RequestParam; | ||
import org.springframework.web.bind.annotation.ResponseBody; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
@RestController | ||
@AllArgsConstructor | ||
public class FlagController extends AssignmentEndpoint { | ||
|
||
private final UserTrackerRepository userTrackerRepository; | ||
private final WebSession webSession; | ||
private final Flags flags; | ||
|
||
@PostMapping(path = "/challenge/flag", produces = MediaType.APPLICATION_JSON_VALUE) | ||
@ResponseBody | ||
public AttackResult postFlag(@RequestParam String flag) { | ||
UserTracker userTracker = userTrackerRepository.findByUser(webSession.getUserName()); | ||
Flag expectedFlag = flags.getFlag(webSession.getCurrentLesson()); | ||
final AttackResult attackResult; | ||
if (expectedFlag.isCorrect(flag)) { | ||
userTracker.assignmentSolved( | ||
webSession.getCurrentLesson(), "Assignment" + expectedFlag.number()); | ||
attackResult = success(this).feedback("challenge.flag.correct").build(); | ||
} else { | ||
userTracker.assignmentFailed(webSession.getCurrentLesson()); | ||
attackResult = failed(this).feedback("challenge.flag.incorrect").build(); | ||
} | ||
userTrackerRepository.save(userTracker); | ||
return attackResult; | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
src/main/java/org/owasp/webgoat/lessons/challenges/Flags.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package org.owasp.webgoat.lessons.challenges; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
import java.util.UUID; | ||
import java.util.stream.IntStream; | ||
import org.owasp.webgoat.container.lessons.Lesson; | ||
import org.springframework.context.annotation.Configuration; | ||
|
||
@Configuration | ||
public class Flags { | ||
private final Map<Integer, Flag> FLAGS = new HashMap<>(); | ||
|
||
public Flags() { | ||
IntStream.range(1, 10).forEach(i -> FLAGS.put(i, new Flag(i, UUID.randomUUID().toString()))); | ||
} | ||
|
||
public Flag getFlag(Lesson forLesson) { | ||
String lessonName = forLesson.getName(); | ||
int challengeNumber = Integer.valueOf(lessonName.substring(lessonName.length() - 1)); | ||
return FLAGS.get(challengeNumber); | ||
} | ||
|
||
public Flag getFlag(int flagNumber) { | ||
return FLAGS.get(flagNumber); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.