fork download
  1. from http.server import BaseHTTPRequestHandler, HTTPServer
  2. import time
  3. import re
  4. import subprocess
  5. import random
  6. import string
  7. import urllib.parse as urlparse
  8. # git history password storage
  9. HOST = '0.0.0.0'
  10. PORT = 7009
  11.  
  12. challenge_file = open('flag19.txt', 'r') # open the file to read
  13. challenge_data = challenge_file.readlines() # stores a list of lines from the file
  14.  
  15. usernameANDpassword_file = open('usernameANDpassword.txt', 'r')
  16. creds_data = usernameANDpassword_file.readlines()
  17. creds_username = creds_data[0].strip()
  18. creds_password = creds_data[1].strip()
  19.  
  20. log_file = open('git.log', 'r')
  21. log_data = log_file.readlines()
  22.  
  23. # create an HTTP handler based on the existing BaseHTTPRequestHandler
  24. class HTTPHandler(BaseHTTPRequestHandler):
  25.  
  26. def do_GET(self): # generate the status code for the GET request
  27. self.respond({'status': 200})
  28.  
  29. def handle_http(self, status_code, path): # handle the request
  30. self.send_response(status_code)
  31. self.send_header('Content-type', 'text/html')
  32. self.end_headers()
  33.  
  34. output = ''
  35.  
  36. # this will define how many lines of data the user can read from the file
  37. # read about GET parameters here: https://e...content-available-to-author-only...e.com/wiki/GET_Parameter
  38. get_params = urlparse.urlparse(path) # read the GET parameters from the URL that user requested
  39. # if the username and password match the ones in the creds_data, then show the secret information
  40. if 'username' in urlparse.parse_qs(get_params.query) and 'password' in urlparse.parse_qs(get_params.query):
  41. username = urlparse.parse_qs(get_params.query)['username'][0]
  42. password = urlparse.parse_qs(get_params.query)['password'][0]
  43.  
  44. # do not allow the user to use the default admin/password credentials
  45. if creds_username == username and creds_password == password:
  46. # read the file and show it to the user if the credentials are correct
  47. for line in challenge_data:
  48. output += line + '<br>'
  49. else:
  50. output = 'Wrong admin credentials!'
  51. # process git commands
  52. elif 'git' in urlparse.parse_qs(get_params.query):
  53. cmd = urlparse.parse_qs(get_params.query)['git'][0]
  54. # if the user requests viewing logs, show the logs
  55. if cmd == 'log':
  56. for line in log_data:
  57. output += line + '<br>'
  58. # if the user requests to checkout a specific commit, allow to do that only if the hash is specified as well
  59. elif cmd == 'checkout' and 'hash' in urlparse.parse_qs(get_params.query):
  60. commit_hash = urlparse.parse_qs(get_params.query)['hash'][0]
  61. try:
  62. commit_file = open('commits/' + commit_hash, 'r') # open the file corresponding to that commit
  63. commit_file_data = commit_file.readlines() # stores a list of lines from the file
  64. commit_file.close()
  65. for line in commit_file_data:
  66. output += line + '<br>'
  67. except:
  68. output = 'File not found'
  69.  
  70. # generate the output to show to the user
  71. current_output = '''<p>{}</p>'''.format(output)
  72.  
  73. # make the HTML page to show the user and insert current_output there
  74. content = ''.join(open('index.html', 'r')).replace('CONTENT_PLACEMENT', current_output)
  75.  
  76. return bytes(content, 'UTF-8')
  77.  
  78. def respond(self, opts):
  79. response = self.handle_http(opts['status'], self.path)
  80. self.wfile.write(response)
  81.  
  82. if __name__ == '__main__':
  83. server = HTTPServer
  84. httpd = server((HOST, PORT), HTTPHandler)
  85. print(time.asctime(), 'Server Starts - %s:%s' % (HOST, PORT))
  86. try:
  87. httpd.serve_forever()
  88. except KeyboardInterrupt:
  89. pass
  90. httpd.server_close()
  91. print(time.asctime(), 'Server Stops - %s:%s' % (HOST, PORT))
Runtime error #stdin #stdout #stderr 0.21s 24808KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
Traceback (most recent call last):
  File "./prog.py", line 12, in <module>
FileNotFoundError: [Errno 2] No such file or directory: 'flag19.txt'