使用 Python 检查本地帐户是否有空密码 [英] Check if local account has a blank password using Python

查看:24
本文介绍了使用 Python 检查本地帐户是否有空密码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm trying to build a script that checks to see whether or not the password on the currently logged in user's local account has a password that isn't blank in Windows. I need this to run as part of a background check for security compliance; it's going to report to a Nagios server. I need this done in Python, but I'm open to PowerShell if Python won't do it.

因此,脚本需要检测:

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

我被困在哪一段代码上,我可以检查当前用户的密码是否为".我有一个没有太多装饰的布局,看起来像这样:

I'm stuck on just whichever bit of code will allow me to check if the password of the current user is "". I have a layout which, without too many embellishments, looks something like this:

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)

我发现了这篇文章,其中使用了 WinAPI 推荐 LogonUser,但我不精通 C#.Python 更适合我的舒适区,我只是不知道如何检查密码集是否为空.我不想收集密码本身.

I spotted this article, where a WinAPI commend LogonUser is used, but I'm not savvy with C#. Python is more within my comfort zone, I just can't figure out how to check whether or not a password set is blank. I don't want to collect the password, itself.

推荐答案

如果用户的密码不为空,则尝试使用空密码登录将失败并显示错误代码 ERROR_LOGON_FAILURE.如果为空,则登录要么成功,要么在系统策略禁止空白密码的情况下失败,并显示错误代码 ERROR_ACCOUNT_RESTRICTION.例如:

If a user's password is not blank, then attempting a logon with a blank password will fail with the error code ERROR_LOGON_FAILURE. If it is blank, then the logon will either succeed or, if system policy forbids blank passwords, will fail with the error code ERROR_ACCOUNT_RESTRICTION. For example:

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

这篇关于使用 Python 检查本地帐户是否有空密码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆