Funktionierender Prototyp des Serious Games zur Vermittlung von Wissen zu Software-Engineering-Arbeitsmodellen.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

win32cred_demo.py 2.7KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. """
  2. Demonstrates prompting for credentials, saving, and loggging on with marshalled credential.
  3. Also shows how to load user's profile
  4. """
  5. import win32api
  6. import win32con
  7. import win32cred
  8. import win32net
  9. import win32profile
  10. import win32security
  11. ## Prompt for a username/pwd for local computer
  12. uiinfo = {
  13. "MessageText": "Enter credentials for local machine",
  14. "CaptionText": "win32cred_demo.py",
  15. }
  16. target, pwd, save = win32cred.CredUIPromptForCredentials(
  17. TargetName=win32api.GetComputerName(),
  18. AuthError=0,
  19. Flags=win32cred.CREDUI_FLAGS_DO_NOT_PERSIST
  20. | win32cred.CREDUI_FLAGS_SHOW_SAVE_CHECK_BOX,
  21. Save=False,
  22. UiInfo=uiinfo,
  23. )
  24. attrs = [
  25. {"Keyword": "attr1", "Flags": 0, "Value": "unicode data"},
  26. {"Keyword": "attr2", "Flags": 0, "Value": b"character data"},
  27. ]
  28. cred = {
  29. "Comment": "Created by win32cred_demo.py",
  30. "UserName": target,
  31. "TargetAlias": None,
  32. "TargetName": target,
  33. "CredentialBlob": pwd,
  34. "Flags": win32cred.CRED_FLAGS_USERNAME_TARGET,
  35. "Persist": win32cred.CRED_PERSIST_ENTERPRISE,
  36. "Type": win32cred.CRED_TYPE_DOMAIN_PASSWORD,
  37. "Attributes": attrs,
  38. }
  39. win32cred.CredWrite(cred)
  40. pwd = None
  41. print(win32cred.CredRead(target, win32cred.CRED_TYPE_DOMAIN_PASSWORD))
  42. ## Marshal saved credential and use it to log on
  43. mc = win32cred.CredMarshalCredential(win32cred.UsernameTargetCredential, target)
  44. # As of pywin32 301 this no longer works for markh and unclear when it stopped, or
  45. # even if it ever did! # Fails in Python 2.7 too, so not a 3.x regression.
  46. try:
  47. th = win32security.LogonUser(
  48. mc,
  49. None,
  50. "",
  51. win32con.LOGON32_LOGON_INTERACTIVE,
  52. win32con.LOGON32_PROVIDER_DEFAULT,
  53. )
  54. win32security.ImpersonateLoggedOnUser(th)
  55. print("GetUserName:", win32api.GetUserName())
  56. win32security.RevertToSelf()
  57. ## Load user's profile. (first check if user has a roaming profile)
  58. username, domain = win32cred.CredUIParseUserName(target)
  59. user_info_4 = win32net.NetUserGetInfo(None, username, 4)
  60. profilepath = user_info_4["profile"]
  61. ## LoadUserProfile apparently doesn't like an empty string
  62. if not profilepath:
  63. profilepath = None
  64. ## leave Flags in since 2.3 still chokes on some types of optional keyword args
  65. hk = win32profile.LoadUserProfile(
  66. th, {"UserName": username, "Flags": 0, "ProfilePath": profilepath}
  67. )
  68. ## Get user's environment variables in a form that can be passed to win32process.CreateProcessAsUser
  69. env = win32profile.CreateEnvironmentBlock(th, False)
  70. ## Cleanup should probably be in a finally block
  71. win32profile.UnloadUserProfile(th, hk)
  72. th.Close()
  73. except win32security.error as exc:
  74. print("Failed to login for some reason", exc)