【问题标题】:Check if local account has a blank password using Python使用 Python 检查本地帐户是否有空密码
【发布时间】:2017-02-27 15:01:21
【问题描述】:

我正在尝试构建一个脚本来检查当前登录用户的本地帐户上的密码是否具有在 Windows 中不为空的密码。我需要将其作为安全合规性背景检查的一部分运行;它将向 Nagios 服务器报告。我需要在 Python 中完成这项工作,但如果 Python 不这样做,我对 PowerShell 持开放态度。

所以,脚本需要检测:

  • 当前登录用户的用户名。
  • 上述用户的密码是否为空。
  • 如果密码不为空,则返回错误代码 0,如果是,则返回错误代码 2。

我被困在允许我检查当前用户的密码是否为“”的任何代码上。我有一个没有太多装饰的布局,看起来像这样:

import os
import Tkinter
import tkMessageBox
from Tkinter import Tk, Toplevel

MyGui.update_idletasks()
MyGui.attributes('-topmost', True)
MyGui.geometry('{}x{}'.format(300, 150))
MyGui.resizable(width=False, height=False)
MyGui.withdraw()

ThisUser = os.getlogin()
ThisPassword = ...  # line of code necessary to test for blank password; this is the part where I'm stuck

if ThisPassword = "":
    tkMessageBox.showerror("Error For User Here", parent=MyGui)
    print "No password set!"
    sys.exit(2)
else:
    print "Password exists."
    sys.exit(0)

我发现 this article,其中使用了 WinAPI 推荐 LogonUser,但我不熟悉 C#。 Python 更适合我的舒适区,我只是不知道如何检查密码集是否为空白。我不想自己收集密码。

【问题讨论】:

    标签: python windows powershell


    【解决方案1】:

    如果用户的密码不为空,则尝试使用空密码登录将失败,错误代码为ERROR_LOGON_FAILURE。如果为空白,则登录将成功,或者,如果系统策略禁止使用空白密码,则登录失败,错误代码为 ERROR_ACCOUNT_RESTRICTION。例如:

    import winerror
    import win32security
    
    def is_password_blank(username):
        try:
            token = win32security.LogonUser(username, None, '',
                        win32security.LOGON32_LOGON_INTERACTIVE,
                        win32security.LOGON32_PROVIDER_DEFAULT)
        except win32security.error as e:
            if e.winerror == winerror.ERROR_ACCOUNT_RESTRICTION:
                return True
            elif e.winerror == winerror.ERROR_LOGON_FAILURE:
                return False
            raise
        else:
            token.Close()
            return True
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-11-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-04-28
      • 2011-02-25
      • 2017-11-02
      • 1970-01-01
      相关资源
      最近更新 更多