cookie_handler.py 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. from crypt import *
  2. from base64 import b64encode,b64decode
  3. from config import *
  4. from time import sleep
  5. import urllib
  6. from flask import render_template
  7. secret_msg = {
  8. 1337:"Welcome Back Administrator, Secret Key: %s" % (FLAGS['po2']),
  9. 1000:"Superb!"
  10. }
  11. def validate_cookie_po1(cookie):
  12. print("validating cookie: %s" % cookie)
  13. msg = decrypt(b64decode(cookie))
  14. if msg == False:
  15. return '{"Error":"PaddingError"}'
  16. else:
  17. print('Debug: decryped cookie: %s' % msg)
  18. username,pwhash,ar,flag = msg.split(':')
  19. ar = int(ar)
  20. for user in app_users:
  21. if user.username == username and user.pwhash == pwhash:
  22. # the cookie got a valid username and password
  23. return render_template("welcome_ch1.html", username=username, rights=ar, secret=secret_msg.get(ar, "--- no message for you ---"))
  24. else:
  25. return "<h5>An Error occured.</h5>"
  26. def validate_cookie_po2(cookie):
  27. print("validating cookie: %s" % cookie)
  28. msg = decrypt(b64decode(cookie))
  29. if msg == False:
  30. return '{"Error":"PaddingError"}'
  31. else:
  32. print('Debug: decryped cookie: %s' % msg)
  33. username,pwhash,ar,flag = msg.split(':')
  34. ar = int(ar)
  35. for user in app_users:
  36. if user.username == username and user.pwhash == pwhash:
  37. # the cookie got a valid username and password
  38. return render_template("welcome_ch2.html", username=username, rights=ar, secret=secret_msg.get(ar, "--- no message for you ---"))
  39. else:
  40. return "<h5>An Error occured.</h5>"
  41. def validate_cookie_po3(cookie):
  42. msg = decrypt(b64decode(cookie))
  43. if msg == False:
  44. # just pretend to have a longer timing
  45. sleep(1)
  46. return ''
  47. else:
  48. print('Debug: decryped cookie: %s' % msg)
  49. username,pwhash,ar,flag = msg.split(':')
  50. ar = int(ar)
  51. for user in app_users:
  52. if user.username == username and user.pwhash == pwhash:
  53. # the cookie got a valid username and password
  54. return render_template("welcome_ch3.html", username=username, rights=ar, secret=secret_msg.get(ar, "--- no message for you ---"))
  55. else:
  56. return "<h5>An Error occured.</h5>"
  57. def validate_cookie_po4(cookie):
  58. pass
  59. def create_cookie_po1(user_obj):
  60. print('Debug: user_obj: %s' %user_obj)
  61. user_obj.flag = FLAGS['po1']
  62. cookie = b64encode(encr(str(user_obj))) return cookie
  63. def create_cookie_po2(user_obj):
  64. print('Debug: user_obj: %s' %user_obj)
  65. user_obj.flag = "P{Its-not-that-easy!;)}"
  66. cookie = b64encode(encr(str(user_obj)))
  67. return cookie
  68. def create_cookie_po3(user_obj):
  69. print('Debug: user_obj: %s' %user_obj)
  70. user_obj.flag = FLAGS['po3']
  71. cookie = b64encode(encr(str(user_obj)))
  72. return cookie