fork(2) 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 Challenge17 {
  18.  
  19. static private String data = "";
  20. static private String adminPassword = "";
  21. static private String adminUsername = "";
  22.  
  23. public static void main(String[] args) throws Exception {
  24. // reading the password from the file
  25. File filePass = new File("passwordANDusername.txt");
  26. BufferedReader br = new BufferedReader(new FileReader(filePass));
  27. adminPassword = br.readLine();
  28. adminUsername = br.readLine();
  29. br.close();
  30.  
  31. // read the challenge file and store it in the data
  32. File file = new File("flag17.txt");
  33. br = new BufferedReader(new FileReader(file));
  34. String line = "";
  35. while ((line = br.readLine()) != null) data += line + "<br>";
  36. br.close();
  37.  
  38. // run the HTTP server to listen for connections
  39. int port = 7007;
  40. HttpServer server = HttpServer.create(new InetSocketAddress(port), 0);
  41. server.createContext("/auth.log", new LogHandler());
  42. server.createContext("/login/", new LoginHandler());
  43. server.createContext("/", new IndexHandler());
  44. server.setExecutor(null);
  45. server.start();
  46. System.out.println("Started the server on port " + port + "...");
  47. }
  48.  
  49. // this class takes care of the action when the user clicks on Login
  50. static class LoginHandler implements HttpHandler {
  51. @Override
  52. public void handle(HttpExchange t) throws IOException {
  53. String output = "";
  54. Map<String, String> GETRequestParams = queryToMap(t.getRequestURI().getQuery());
  55. if (GETRequestParams.containsKey("username") && GETRequestParams.containsKey("password")) {
  56. String username = GETRequestParams.get("username");
  57. String password = GETRequestParams.get("password");
  58.  
  59. if (username.equals(adminUsername) && password.equals(adminPassword)) {
  60. output += data;
  61. }
  62. else {
  63. // all errors will be recorded in auth.log file
  64. output += "Username/password are not correct! Good luck next time.";
  65. }
  66. }
  67. else {
  68. output += "GET parameters are wrong, don't mess with us ;)";
  69. }
  70.  
  71. generateResponse(t, output);
  72. }
  73. }
  74.  
  75. // this class takes care of the action when the user navigates to this challenge
  76. static class IndexHandler implements HttpHandler {
  77. @Override
  78. public void handle(HttpExchange t) throws IOException {
  79. generateResponse(t, "Millions of red roses, or red roses for millions");
  80. }
  81. }
  82.  
  83. // this class takes care of the action when the user navigates to this challenge
  84. static class LogHandler implements HttpHandler {
  85. @Override
  86. public void handle(HttpExchange t) throws IOException {
  87. // read the challenge file and store it in the data
  88. File file = new File("auth.log");
  89. String line = "";
  90. String out = "";
  91. while ((line = br.readLine()) != null) out += line + "<br>";
  92. br.close();
  93. generateResponse(t, out);
  94. }
  95. }
  96.  
  97. // a method for a generic response to the user
  98. static void generateResponse(HttpExchange t, String output) throws IOException {
  99. String line = "", response = "";
  100. try {
  101. File indexFile = new File("index.html").getCanonicalFile();
  102. BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(new FileInputStream(indexFile)));
  103. while ((line = bufferedReader.readLine()) != null) {
  104. response += line;
  105. }
  106. response = response.replaceAll("CONTENT_PLACEMENT", output);
  107. bufferedReader.close();
  108. } catch (IOException e) {
  109. e.printStackTrace();
  110. }
  111. t.getResponseHeaders().add("Content-Type", "text/html");
  112. t.sendResponseHeaders(200, response.length());
  113. OutputStream os = t.getResponseBody();
  114. os.write(response.getBytes());
  115. os.close();
  116. }
  117.  
  118. static public Map<String, String> queryToMap(String query) {
  119. Map<String, String> result = new HashMap<>();
  120. for (String param : query.split("&")) {
  121. String[] entry = param.split("=");
  122. if (entry.length > 1) { // proceed only when there is a pair of parameters
  123. result.put(entry[0], entry[1]);
  124. }
  125. }
  126. return result;
  127. }
  128.  
  129. }
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
Main.java:17: error: class Challenge17 is public, should be declared in a file named Challenge17.java
public class Challenge17 {
       ^
1 error
stdout
Standard output is empty