fork download
  1. import java.io.IOException;
  2. import java.io.OutputStream;
  3. import java.io.UnsupportedEncodingException;
  4. import java.net.InetSocketAddress;
  5. import java.util.Random;
  6. import java.io.File;
  7. import java.io.FileInputStream;
  8. import java.io.FileReader;
  9. import java.io.BufferedReader;
  10. import java.io.InputStreamReader;
  11. import java.util.Map;
  12. import java.util.HashMap;
  13. import java.util.Arrays;
  14. import java.security.MessageDigest;
  15.  
  16. import com.sun.net.httpserver.HttpExchange;
  17. import com.sun.net.httpserver.HttpHandler;
  18. import com.sun.net.httpserver.HttpServer;
  19.  
  20. public class Challenge16 {
  21.  
  22. static private String data = "";
  23. static private String adminPasswordHash = "";
  24.  
  25. public static void main(String[] args) throws Exception {
  26. // hashing the password helps security
  27. // this hash corresponds to one of the commonly used passwords in 2018
  28. // just for testing purposes, please change when deploying in production!!!
  29. adminPasswordHash = "0571749e2ac330a7455809c6b0e7af90";
  30.  
  31. // read the challenge file and store it in the data
  32. File file = new File("flag16.txt");
  33. String line = "";
  34. while ((line = br.readLine()) != null) data += line + "<br>";
  35. br.close();
  36.  
  37. // run the HTTP server to listen for connections
  38. int port = 7006;
  39. HttpServer server = HttpServer.create(new InetSocketAddress(7006), 0);
  40. server.createContext("/login/", new LoginHandler());
  41. server.createContext("/", new IndexHandler());
  42. server.setExecutor(null);
  43. server.start();
  44. System.out.println("Started the server on port " + port + "...");
  45. }
  46.  
  47. // this class takes care of the action when the user clicks on Login
  48. static class LoginHandler implements HttpHandler {
  49. @Override
  50. public void handle(HttpExchange t) throws IOException {
  51. String output = "";
  52. Map<String, String> GETRequestParams = queryToMap(t.getRequestURI().getQuery());
  53. if (GETRequestParams.containsKey("username") && GETRequestParams.containsKey("password")) {
  54. String username = GETRequestParams.get("username");
  55. String password = GETRequestParams.get("password");
  56.  
  57. if (username.equals("admin") && md5hash(password).equals(adminPasswordHash)) {
  58. output += data;
  59. }
  60. else {
  61. output += "Username/password are not correct! Good luck next time.";
  62. }
  63. }
  64. else {
  65. output += "GET parameters are wrong, don't mess with us ;)";
  66. }
  67.  
  68. generateResponse(t, output);
  69. }
  70. }
  71.  
  72. // this class takes care of the action when the user navigates to this challenge
  73. static class IndexHandler implements HttpHandler {
  74. @Override
  75. public void handle(HttpExchange t) throws IOException {
  76. generateResponse(t, "Hash the cash, cash the hash");
  77. }
  78. }
  79.  
  80. // a method for a generic response to the user
  81. static void generateResponse(HttpExchange t, String output) throws IOException {
  82. String line = "", response = "";
  83. try {
  84. File indexFile = new File("index.html").getCanonicalFile();
  85. BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(new FileInputStream(indexFile)));
  86. while ((line = bufferedReader.readLine()) != null) {
  87. response += line;
  88. }
  89. response = response.replaceAll("CONTENT_PLACEMENT", output);
  90. bufferedReader.close();
  91. } catch (IOException e) {
  92. e.printStackTrace();
  93. }
  94. t.getResponseHeaders().add("Content-Type", "text/html");
  95. t.sendResponseHeaders(200, response.length());
  96. OutputStream os = t.getResponseBody();
  97. os.write(response.getBytes());
  98. os.close();
  99. }
  100.  
  101. // calculates MD5 hash of the input String
  102. static public String md5hash(String s) {
  103. try {
  104. byte[] bytesOfMessage = s.getBytes("UTF-8");
  105. MessageDigest md = MessageDigest.getInstance("MD5");
  106. for (byte b : md.digest(bytesOfMessage)) {
  107. sb.append(String.format("%02x", b & 0xff));
  108. }
  109. return sb.toString();
  110. }
  111. catch(Exception e) {
  112. System.out.println(e.getMessage());
  113. }
  114. return null;
  115. }
  116.  
  117. static public Map<String, String> queryToMap(String query) {
  118. Map<String, String> result = new HashMap<>();
  119. for (String param : query.split("&")) {
  120. String[] entry = param.split("=");
  121. if (entry.length > 1) { // proceed only when there is a pair of parameters
  122. result.put(entry[0], entry[1]);
  123. }
  124. }
  125. return result;
  126. }
  127.  
  128. }
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
Main.java:20: error: class Challenge16 is public, should be declared in a file named Challenge16.java
public class Challenge16 {
       ^
1 error
stdout
Standard output is empty