提升的凭证HARDCODED到脚本中

时间:2014-05-14 13:13:09

标签: python elevated-privileges

为澄清而编辑:

我们正在使用应该调用Python脚本的第三方软件(我必须创建)。此脚本发送的值如sender-ip = 10.10.10.10,脚本应返回userId

此第三方软件声称,如果我们创建凭据文件,它将使用提升的凭据运行该Python脚本。

到目前为止,这个第三方软件已经IGNORED凭证文件,因此Python脚本使用常规用户凭据运行,并且不提供所需的输出。

当我使用提升的凭据使用命令提示符手动运行Python脚本时,它可以正常工作。

我面临着让第三方软件和Python脚本“玩得很好”的压力,所以我必须对脚本中提升的凭据进行硬编码,即使我被警告​​这不是一个好的编程习惯。

AND,我必须能够通过执行

来调用脚本
C:\> python myscript.py sender-ip=10.10.10.10

以下是代码

import sys, subprocess, socket, string
import wmi, win32api, win32con

for args in [item.strip('sender-ip=') for item in sys.argv[1:]]:

    userIP = args
    userloggedon = ""

    # subprocess
    ping = subprocess.Popen(
        ["ping", "-n", "1", userIP],
        stdout = subprocess.PIPE,
        stderr = subprocess.PIPE
    )

    # can we ping the user's IP address?
    out, error = ping.communicate()

    # if we cannot ping user's IP address then userID is the error message, and exit
    if out.find("Reply from") == -1:
        errorMessage = "HOST unreachable."
        print 'errorMsg={}'.format(errorMessage)
        sys.exit()

    # if we cannot access wmi of user's IP address then userID is the error message, and exit
    try:
        c = wmi.WMI(userIP)
    except: 
        errorMessage= "WMI unreachable"
        print 'errorMsg={}'.format(errorMessage)
        sys.exit()

    # perform system lookup of IP address
    user_list = []
    for us in c.Win32_LogonSession():
        try:
            for user in us.references("Win32_LoggedOnUser"):
                user_logins = user.Antecedent.Domain + "\\" + user.Antecedent.Name
                user_list.append(user_logins)

        except:
            pass

    userloggedon = user_list[0] 
    print 'userId={}'.format(userloggedon)

0 个答案:

没有答案