cookie_handler.py 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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)))
  63. return cookie
  64. def create_cookie_po2(user_obj):
  65. print('Debug: user_obj: %s' %user_obj)
  66. user_obj.flag = "P{Its-not-that-easy!;)}"
  67. cookie = b64encode(encr(str(user_obj)))
  68. return cookie
  69. def create_cookie_po3(user_obj):
  70. print('Debug: user_obj: %s' %user_obj)
  71. user_obj.flag = FLAGS['po3']
  72. cookie = b64encode(encr(str(user_obj)))
  73. return cookie