Some python scrips for demonstrating chap protocol
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

chap-server.py 3.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. #!/usr/bin/env python2
  2. import uuid
  3. import pickle
  4. import hashlib
  5. from xmlrpc.server import SimpleXMLRPCServer
  6. from xmlrpc.server import SimpleXMLRPCRequestHandler
  7. # Restrict to a particular path.
  8. class RequestHandler(SimpleXMLRPCRequestHandler):
  9. rpc_paths = ('/RPC2',)
  10. # Create server
  11. server = SimpleXMLRPCServer(("localhost", 8000),
  12. requestHandler=RequestHandler)
  13. server.register_introspection_functions()
  14. def save_users( users ):
  15. pickle.dump( users, open( 'users.data', 'wb' ) )
  16. # Register an instance; all the methods of the instance are
  17. # published as XML-RPC methods
  18. class CHAP:
  19. # initializes class-instance and instance variables
  20. # if no userdata exists, it creates a default user
  21. def __init__( self ):
  22. self.keys = {}
  23. self.authenticated = {}
  24. try:
  25. self.users = pickle.load( open( 'users.data', 'rb' ) )
  26. except FileNotFoundError:
  27. self.users = { 'admin': 'Pass123' }
  28. save_users( self.users )
  29. # adds a user to dictionary and saves it to a file
  30. # won't be executable if not authorized or if user already exists
  31. def create_user( self, session, username, password ):
  32. if ( self.authenticated.get( session ) == True ):
  33. if not username in self.users:
  34. self.users[ username ] = password
  35. save_users( self.users )
  36. return 'Created user successfully'
  37. else:
  38. return 'User already exists'
  39. else:
  40. return 'Sorry, please authenticate first'
  41. # tells the server to start the autentification process
  42. # and send the generated random salt
  43. def init( self ):
  44. session = str( uuid.uuid4() )
  45. key = str( uuid.uuid4() )
  46. while session in self.keys:
  47. session = str( uuid.uuid4() )
  48. self.keys[ session ] = key
  49. return [ session, key ]
  50. # checks if send hash is same as internally generated to validate if the correct
  51. # password was used
  52. def auth( self, session, username, password_hash ):
  53. if session in self.keys and username in self.users:
  54. combined = self.users.get( username ) + self.keys.get( session )
  55. passhash = hashlib.sha256( combined.encode( 'utf-8' ) ).hexdigest()
  56. self.authenticated[ session ] = passhash == password_hash
  57. return self.authenticated[ session ] == True
  58. else:
  59. return False
  60. # adds functionality for users to log them selfes off, but also need the
  61. # the password_hash to ensure, that nobody else logs you off
  62. def logout( self, session, username, password_hash ):
  63. if self.auth( session, username, password_hash ):
  64. del self.authenticated[ session ]
  65. del self.keys[ session ]
  66. return self.authenticated.get( session ) != True
  67. # a little method that refuses to say hi, if you
  68. # are not authenticated
  69. def hello( self, session ):
  70. if ( self.authenticated.get( session ) == True ):
  71. return 'Hi, you are authenticated'
  72. else:
  73. return 'Sorry, please authenticate first'
  74. server.register_instance(CHAP())
  75. # Run the server's main loop
  76. server.serve_forever()