tokenhub.py 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-"
  3. # vim: set expandtab tabstop=4 shiftwidth=4:
  4. """
  5. This file is part of the XSSer project, https://xsser.03c8.net
  6. Copyright (c) 2010/2020 | psy <epsylon@riseup.net>
  7. xsser is free software; you can redistribute it and/or modify it under
  8. the terms of the GNU General Public License as published by the Free
  9. Software Foundation version 3 of the License.
  10. xsser is distributed in the hope that it will be useful, but WITHOUT ANY
  11. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  12. FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
  13. details.
  14. You should have received a copy of the GNU General Public License along
  15. with xsser; if not, write to the Free Software Foundation, Inc., 51
  16. Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  17. """
  18. from threading import Thread
  19. import socket
  20. import time
  21. success_token_url = False
  22. token_arrived_hash = None
  23. class ReceiverThread(Thread):
  24. def __init__(self, client, addr, parent):
  25. Thread.__init__(self)
  26. self.daemon = True
  27. self.client = client
  28. self.parent = parent
  29. def run(self):
  30. data = self.client.recv(1024)
  31. if data:
  32. self.parent.data_arrived(data)
  33. self.client.send(b'XSSer "token-hub" service running... ;-)\n\n')
  34. self.client.send(b'### INCOMING DATA:\n\n')
  35. self.client.send(data)
  36. self.client.close()
  37. self.parent.client_finished(self)
  38. class HubThread(Thread):
  39. def __init__(self, parent):
  40. Thread.__init__(self)
  41. self.daemon = True
  42. self._clients = []
  43. self._armed = True
  44. self.ready = False
  45. self.running = False
  46. self.parent = parent
  47. self.token_arrived_flag = False
  48. self.success_arrived_flag = False
  49. def check_hash(self, hashing):
  50. if token_arrived_hash:
  51. if success_token_url:
  52. if token_arrived_hash == hashing: # [100% VULNERABLE] check!
  53. self.token_arrived_flag = True
  54. self.success_arrived_flag = False
  55. elif '/success/' in success_token_url:
  56. self.token_arrived_flag = True
  57. self.success_arrived_flag = True
  58. else:
  59. self.token_arrived_flag = False
  60. else:
  61. self.token_arrived_flag = False
  62. else:
  63. self.token_arrived_flag = False
  64. return self.token_arrived_flag, self.success_arrived_flag, token_arrived_hash
  65. def url_request(self, url):
  66. split_url = url.split(b"/")
  67. if len(split_url) > 2:
  68. if split_url[1] == b'success':
  69. global success_token_url
  70. global token_arrived_hash
  71. success_token_url = url.decode('utf-8')
  72. token_arrived_hash = split_url[2].decode('utf-8')
  73. self.parent.token_arrived(split_url[2].decode('utf-8'))
  74. def data_arrived(self, data):
  75. data.split(b"\n")[0]
  76. if data.startswith(b"GET"):
  77. split_data = data.split()
  78. if len(split_data) > 1:
  79. self.url_request(split_data[1])
  80. def client_finished(self, _thread):
  81. try:
  82. self._clients.remove(_thread)
  83. except:
  84. pass
  85. def shutdown(self):
  86. if self.ready:
  87. try:
  88. self.socket.shutdown(socket.SHUT_RDWR)
  89. self.socket.close()
  90. except OSError:
  91. pass
  92. self.running = False
  93. self._armed = False
  94. self.ready = False
  95. def run(self):
  96. while not self.running and self._armed:
  97. try:
  98. s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  99. s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) # try re-use socket
  100. s.bind(('localhost', 19084))
  101. self.running = True
  102. except socket.error as e:
  103. #print("socket busy, retry opening:", e)
  104. if e.errno == 98: # its in use wait a bit and retry
  105. time.sleep(5)
  106. if not self._armed:
  107. return
  108. self.socket = s
  109. self.ready = True
  110. s.listen(1)
  111. while self.running and self._armed:
  112. try:
  113. conn, addr = s.accept()
  114. except socket.timeout:
  115. pass
  116. except socket.error as e:
  117. if self.ready == False:
  118. return
  119. else:
  120. break
  121. else:
  122. t = ReceiverThread(conn, addr, self)
  123. t.start()
  124. self._clients.append(t)
  125. if self.ready:
  126. s.close()
  127. self.ready = False