validate_password.py 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. # Demonstrates how to validate a password.
  2. # See also MSKB article Q180548
  3. #
  4. # To use with Kerberos you need to jump through the 'targetspn' hoops.
  5. import win32security
  6. import sys
  7. from sspi import ClientAuth, ServerAuth
  8. def validate(username, password, domain = ""):
  9. auth_info = username, domain, password
  10. ca = ClientAuth("NTLM", auth_info = auth_info)
  11. sa = ServerAuth("NTLM")
  12. data = err = None
  13. while err != 0:
  14. err, data = ca.authorize(data)
  15. err, data = sa.authorize(data)
  16. # If we get here without exception, we worked!
  17. if __name__=='__main__':
  18. if len(sys.argv) not in [2,3,4]:
  19. print "Usage: %s username [password [domain]]" % (__file__,)
  20. sys.exit(1)
  21. # password and domain are optional!
  22. password = None
  23. if len(sys.argv)>=3:
  24. password = sys.argv[2]
  25. domain = ""
  26. if len(sys.argv)>=4:
  27. domain = sys.argv[3]
  28. try:
  29. validate(sys.argv[1], password, domain)
  30. print "Validated OK"
  31. except win32security.error, details:
  32. hr, func, msg = details
  33. print "Validation failed: %s (%d)" % (msg, hr)