fork download
  1. import java.io.IOException;
  2. import java.io.OutputStream;
  3. import java.net.InetSocketAddress;
  4. import java.util.Random;
  5. import java.io.File;
  6. import java.io.FileInputStream;
  7. import java.io.FileReader;
  8. import java.io.BufferedReader;
  9. import java.io.InputStreamReader;
  10. import java.util.Map;
  11. import java.util.HashMap;
  12.  
  13. import com.sun.net.httpserver.HttpExchange;
  14. import com.sun.net.httpserver.HttpHandler;
  15. import com.sun.net.httpserver.HttpServer;
  16.  
  17. public class Challenge15 {
  18.  
  19. static private String data = "";
  20. static private String adminPassword = "";
  21.  
  22. public static void main(String[] args) throws Exception {
  23. // create a secure admin!
  24. // we will generate a random password
  25. // so that nobody will be able to log in as admin
  26. // even we do not know what the password is,
  27. // how smart is that, huh?
  28. adminPassword = generateSecurePassword();
  29.  
  30. // read the challenge file and store it in the data
  31. File file = new File("flag15.txt");
  32. String line = "";
  33. while ((line = br.readLine()) != null) data += line + "<br>";
  34. br.close();
  35.  
  36. // run the HTTP server to listen for connections
  37. int port = 7005;
  38. HttpServer server = HttpServer.create(new InetSocketAddress(port), 0);
  39. server.createContext("/login/", new LoginHandler());
  40. server.createContext("/", new IndexHandler());
  41. server.setExecutor(null);
  42. server.start();
  43. System.out.println("Started the server on port " + port + "...");
  44. }
  45.  
  46. // this class takes care of the action when the user clicks on Login
  47. static class LoginHandler implements HttpHandler {
  48. @Override
  49. public void handle(HttpExchange t) throws IOException {
  50. String output = "";
  51. Map<String, String> GETRequestParams = queryToMap(t.getRequestURI().getQuery());
  52. if (GETRequestParams.containsKey("username") && GETRequestParams.containsKey("password")) {
  53. String username = GETRequestParams.get("username");
  54. String password = GETRequestParams.get("password");
  55.  
  56. if (username.equals("admin") && password.equals(adminPassword)) {
  57. output += data;
  58. }
  59. else {
  60. output += "Username/password are not correct! Good luck next time.";
  61. }
  62. }
  63. else {
  64. output += "GET parameters are wrong, don't mess with us ;)";
  65. }
  66.  
  67. generateResponse(t, output);
  68. }
  69. }
  70.  
  71. // this class takes care of the action when the user navigates to this challenge
  72. static class IndexHandler implements HttpHandler {
  73. @Override
  74. public void handle(HttpExchange t) throws IOException {
  75. generateResponse(t, "Dear admin, hopefully, you remember your password!");
  76. }
  77. }
  78.  
  79. // a method for a generic response to the user
  80. static void generateResponse(HttpExchange t, String output) throws IOException {
  81. String line = "", response = "";
  82. try {
  83. File indexFile = new File("index.html").getCanonicalFile();
  84. BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(new FileInputStream(indexFile)));
  85. while ((line = bufferedReader.readLine()) != null) {
  86. response += line;
  87. }
  88. response = response.replaceAll("CONTENT_PLACEMENT", output);
  89. bufferedReader.close();
  90. } catch (IOException e) {
  91. e.printStackTrace();
  92. }
  93. t.getResponseHeaders().add("Content-Type", "text/html");
  94. t.sendResponseHeaders(200, response.length());
  95. OutputStream os = t.getResponseBody();
  96. os.write(response.getBytes());
  97. os.close();
  98. }
  99.  
  100. static public Map<String, String> queryToMap(String query) {
  101. Map<String, String> result = new HashMap<>();
  102. for (String param : query.split("&")) {
  103. String[] entry = param.split("=");
  104. if (entry.length > 1) { // proceed only when there is a pair of parameters
  105. result.put(entry[0], entry[1]);
  106. }
  107. }
  108. return result;
  109. }
  110.  
  111. static private String generateSecurePassword() {
  112. String password = "magicallysecure";
  113. Random rnd = new Random();
  114.  
  115. // generate a random number between 0 and 20 and based on that, create a password
  116. Integer random = rnd.nextInt(20);
  117.  
  118. // let's do some math on that number
  119. // btw, this code can be run on the website like https://w...content-available-to-author-only...e.com/online-java-compiler
  120. // just don't forget to import java.util.Random; at the top of the code editor
  121. random = random * 16578;
  122. random = random ^ 654321;
  123. for (int i = 0; i < password.length(); i++) {
  124. random += (int)password.charAt(i); // convert every character from password to an integer according to ASCII table and add that number to random
  125. }
  126.  
  127. password = random.toString();
  128.  
  129. return password;
  130. }
  131.  
  132. }
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
Main.java:17: error: class Challenge15 is public, should be declared in a file named Challenge15.java
public class Challenge15 {
       ^
1 error
stdout
Standard output is empty