simple_auth.py 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. # A demo of basic SSPI authentication.
  2. # There is a 'client' context and a 'server' context - typically these will
  3. # be on different machines (here they are in the same process, but the same
  4. # concepts apply)
  5. import sspi
  6. import win32security, sspicon, win32api
  7. def lookup_ret_code(err):
  8. for k,v in sspicon.__dict__.items():
  9. if k[0:6] in ('SEC_I_','SEC_E_') and v==err:
  10. return k
  11. """
  12. pkg_name='Kerberos'
  13. sspiclient=SSPIClient(pkg_name, win32api.GetUserName(), ## target spn is ourself
  14. None, None, ## use none for client name and authentication information for current context
  15. ## u'username', (u'username',u'domain.com',u'passwd'),
  16. sspicon.ISC_REQ_INTEGRITY|sspicon.ISC_REQ_SEQUENCE_DETECT|sspicon.ISC_REQ_REPLAY_DETECT| \
  17. sspicon.ISC_REQ_DELEGATE|sspicon.ISC_REQ_CONFIDENTIALITY|sspicon.ISC_REQ_USE_SESSION_KEY)
  18. sspiserver=SSPIServer(pkg_name, None,
  19. sspicon.ASC_REQ_INTEGRITY|sspicon.ASC_REQ_SEQUENCE_DETECT|sspicon.ASC_REQ_REPLAY_DETECT| \
  20. sspicon.ASC_REQ_DELEGATE|sspicon.ASC_REQ_CONFIDENTIALITY|sspicon.ASC_REQ_STREAM|sspicon.ASC_REQ_USE_SESSION_KEY)
  21. """
  22. pkg_name='NTLM'
  23. # Setup the 2 contexts.
  24. sspiclient=sspi.ClientAuth(pkg_name)
  25. sspiserver=sspi.ServerAuth(pkg_name)
  26. # Perform the authentication dance, each loop exchanging more information
  27. # on the way to completing authentication.
  28. sec_buffer=None
  29. while 1:
  30. err, sec_buffer = sspiclient.authorize(sec_buffer)
  31. err, sec_buffer = sspiserver.authorize(sec_buffer)
  32. if err==0:
  33. break
  34. # The server can now impersonate the client. In this demo the 2 users will
  35. # always be the same.
  36. sspiserver.ctxt.ImpersonateSecurityContext()
  37. print 'Impersonated user: ',win32api.GetUserNameEx(win32api.NameSamCompatible)
  38. sspiserver.ctxt.RevertSecurityContext()
  39. print 'Reverted to self: ',win32api.GetUserName()
  40. pkg_size_info=sspiclient.ctxt.QueryContextAttributes(sspicon.SECPKG_ATTR_SIZES)
  41. # Now sign some data
  42. msg='some data to be encrypted ......'
  43. sigsize=pkg_size_info['MaxSignature']
  44. sigbuf=win32security.PySecBufferDescType()
  45. sigbuf.append(win32security.PySecBufferType(len(msg), sspicon.SECBUFFER_DATA))
  46. sigbuf.append(win32security.PySecBufferType(sigsize, sspicon.SECBUFFER_TOKEN))
  47. sigbuf[0].Buffer=msg
  48. sspiclient.ctxt.MakeSignature(0,sigbuf,1)
  49. sspiserver.ctxt.VerifySignature(sigbuf,1)
  50. # And finally encrypt some.
  51. trailersize=pkg_size_info['SecurityTrailer']
  52. encbuf=win32security.PySecBufferDescType()
  53. encbuf.append(win32security.PySecBufferType(len(msg), sspicon.SECBUFFER_DATA))
  54. encbuf.append(win32security.PySecBufferType(trailersize, sspicon.SECBUFFER_TOKEN))
  55. encbuf[0].Buffer=msg
  56. sspiclient.ctxt.EncryptMessage(0,encbuf,1)
  57. print 'Encrypted data:',repr(encbuf[0].Buffer)
  58. sspiserver.ctxt.DecryptMessage(encbuf,1)
  59. print 'Unencrypted data:',encbuf[0].Buffer