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.  
  9. HOST = '0.0.0.0'
  10. PORT = 7010
  11.  
  12. challenge_file = open('flag20.txt', 'r') # open the file to read
  13. challenge_data = challenge_file.readlines() # stores a list of lines from the file
  14.  
  15. # create an HTTP handler based on the existing BaseHTTPRequestHandler
  16. class HTTPHandler(BaseHTTPRequestHandler):
  17.  
  18. def do_GET(self): # generate the status code for the GET request
  19. self.respond({'status': 200})
  20.  
  21. def handle_http(self, status_code, path): # handle the request
  22. self.send_response(status_code)
  23. self.send_header('Content-type', 'text/html')
  24. self.end_headers()
  25.  
  26. output = ''
  27.  
  28. # this will define how many lines of data the user can read from the file
  29. # read about GET parameters here: https://e...content-available-to-author-only...e.com/wiki/GET_Parameter
  30. get_params = urlparse.urlparse(path) # read the GET parameters from the URL that user requested
  31. # if the username and language are passed in GET parameters, then save that to a file
  32. if 'username' in urlparse.parse_qs(get_params.query) and 'language' in urlparse.parse_qs(get_params.query):
  33. username = urlparse.parse_qs(get_params.query)['username'][0]
  34. language = urlparse.parse_qs(get_params.query)['language'][0]
  35.  
  36. print(username + ' ' + language)
  37.  
  38. # run the native Linux command, given user's input of the username and language
  39. # basically saves the language request in a file with the username
  40. cmd = 'echo {} > ./languages/{}.txt'.format(language, username)
  41.  
  42. subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
  43.  
  44. output = 'The data has been saved to the file and you can rest assured that your language shall be spoken!'
  45. elif path == '/LICENSE.md':
  46. license_file = open('LICENSE.md', 'r')
  47. license_data = license_file.readlines()
  48. license_file.close()
  49. for line in license_data:
  50. output += line.replace('\n', '<br>')
  51. # update the license file just in case
  52. license_file = open('LICENSE.md', 'w')
  53. license_file.write('''# License Agreement
  54.  
  55. If this software causes you to go bananas, accidentally destroys all your data on your laptop, changes all your messages to memes, encrypts your personal information and publishes it online, makes you wake up at 5 am on Sunday morning and exercise, calls all people on your contact list every hour, memefies your life (if it's even a word) calls Microsoft Helpdesk for no valid reason, makes fun of you in your sleep, shocks you if you don't pay attention in class, and frantically starts doing a filibuster speech -- don't blame Russians, blame their subconscious mind.
  56. ''')
  57. license_file.close()
  58. else:
  59. output = 'Freedom to changing your language without barriers!'
  60.  
  61. # generate the output to show to the user
  62. current_output = '''<p>{}</p>'''.format(output)
  63.  
  64. # make the HTML page to show the user and insert current_output there
  65. content = ''.join(open('index.html', 'r')).replace('CONTENT_PLACEMENT', current_output)
  66.  
  67. return bytes(content, 'UTF-8')
  68.  
  69. def respond(self, opts):
  70. response = self.handle_http(opts['status'], self.path)
  71. self.wfile.write(response)
  72.  
  73. if __name__ == '__main__':
  74. server = HTTPServer
  75. httpd = server((HOST, PORT), HTTPHandler)
  76. print(time.asctime(), 'Server Starts - %s:%s' % (HOST, PORT))
  77. try:
  78. httpd.serve_forever()
  79. except KeyboardInterrupt:
  80. pass
  81. httpd.server_close()
  82. print(time.asctime(), 'Server Stops - %s:%s' % (HOST, PORT))
Runtime error #stdin #stdout #stderr 0.2s 24788KB
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: 'flag20.txt'