繁体   English   中英

Python:kernel32.CreateProcessA()它在做什么?

[英]Python: kernel32.CreateProcessA() What is it doing?

我目前正在学习调试器以及它们如何停止进程。

这是我的代码:

    from ctypes import *
    WORD = c_ushort
    DWORD = c_ulong
    LPBYTE = POINTER(c_ubyte)
    LPTSTR = POINTER(c_char)
    HANDLE = c_void_p
    DEBUG_PROCESS = 0x00000001
    CREATE_NEW_CONSOLE = 0x00000010
    class STARTUPINFO(Structure):
        _fields_ = [
        ("cb", DWORD),
        ("lpReserved", LPTSTR),
        ("lpDesktop", LPTSTR),
        ("lpTitle", LPTSTR),
        ("dwX", DWORD),
        ("dwY", DWORD),
        ("dwXSize", DWORD),
        ("dwYSize", DWORD),
        ("dwXCountChars", DWORD),
        ("dwYCountChars", DWORD),
        ("dwFillAttribute",DWORD),
        ("dwFlags", DWORD),
        ("wShowWindow", WORD),
        ("cbReserved2", WORD),
        ("lpReserved2", LPBYTE),
        ("hStdInput", HANDLE),
        ("hStdOutput", HANDLE),
        ("hStdError", HANDLE),
        ]
    class PROCESS_INFORMATION(Structure):
        _fields_ = [
        ("hProcess", HANDLE),
        ("hThread", HANDLE),
        ("dwProcessId", DWORD),
        ("dwThreadId", DWORD),
        ]


    kernel32 = windll.kernel32
    class debugger():
        def __init__(self):
            pass

        def load(path_to_exe):
            creation_flags = DEBUG_PROCESS
            startupinfo = STARTUPINFO()
            processinfo = PROCESS_INFORMATION()
            startupinfo.dwFlags = 0x1
            startupinfo.wShowWindow = 0x0
            startupinfo.cb = sizeof(startupinfo)
            if kernel32.CreateProcessA(path_to_exe,None,None,None,None,creation_flags,None,None,byref(startupinfo),byref(processinfo)):
                print("[*] Process launched")
                print("[*] PID: %d" % (PROCESS_INFORMATION.dwProcessId))
            else:
                print("[*] Error: 0x%08x." % (kernel32.GetLastError()))

    debugger.load("C:\\WINDOWS\\system32\\calc.exe")

每当我运行它,它就会出错。 :(我发现它之所以会出现错误的原因是因为kernel32.CreateProcessA返回false。我实际上正在跟随Gray hat python,我正在将此代码转换为python 3它。

我的问题是,kernel32.CreateProcessA正在做什么,为什么它返回false,我怎么能阻止它返回false?

任何帮助将非常感激!

您的代码中有几个错误:

第一个错误是debugger类的load方法定义错误。 在你的情况下,它最可能是staticmethod:

# . . .

# This decorator required to make method static
@staticmethod
def load(path_to_exe):
    creation_flags = DEBUG_PROCESS
    startupinfo = STARTUPINFO()
    processinfo = PROCESS_INFORMATION()
    startupinfo.dwFlags = 0x1

# . . .

如果创建了进程,则第二个错误在于print

if kernel32.CreateProcessA(path_to_exe,None,None,None,None,
                           creation_flags,None,None,
                           byref(startupinfo),byref(processinfo)):
    print("[*] Process launched")

    # ERROR AT THE LINE BELOW
    # Your variant: print("[*] PID: %d" % (PROCESS_INFORMATION.dwProcessId))
    # But it should be the structure itself not it "type"
    print("[*] PID: %d" % (processinfo.dwProcessId))  
else:
    print("[*] Error: 0x%08x." % (kernel32.GetLastError()))

在我的情况下它是有效的(Windows XP)。 如果你的进程没有真正启动,你会得到类似的控制台消息:

[*] Error: 0x00000002

然后,如果你使用Python 3.x的,你应该使用不CreateProcessACreateProcessW因为在Python 3.x的所有字符串以Unicode(在WinAPI的所有功能与“A”接受ASCI串结束的功能,以结束“W”接受Unicode的字符串)。 更准确的答案是,如果你写了你的情况下发生了什么错误或异常。

当我在win64上运行像你这样的程序时,我有一个问题。但是当我将kernel32.CreateProcessA更改为kernel32.CreateProcessW时,程序运行成功。

切换前两个参数,以便您具有以下内容:

kernel32.CreateProcessA(c_char_p(0),c_char_p(path_to_exe),0,0,0,creation_flags,0,0,bytef(startupinfo),byref(processinfo))

您应该调用GetLastError函数来了解错误的真正含义。

ctypes.windll.kernel32.GetLastError

我发现这篇详细的帖子解释了如何调试和修复由CreateProcessA引起的错误: Python CreateProcessA返回FALSE

  1. 这一行必须是括号:debugger()。load(“C:\\ WINDOWS \\ system32 \\ calc.exe”)

  2. 这一行必须包含self:def load(self,path_to_exe)

  3. 如果static不能包含self:@staticmethod def load(path_to_exe)

  4. 这一行必须是:print(“[*] PID:%d”%processinfo.dwProcessId)
  1. 根据Python 2xx与3xx之间差异的快速总结 :Python 2分离了ASCII str()类型和unicode()类型。 Python 3 只有Unicode(utf-8)字符串类型

  2. 根据WinAPI文档,CreateProcess()有一个unicode版本 ,定义为具有相同参数的CreateProcessW()。

因此,如果您使用Python 2xx,则使用CreateProcessA() 在python 3xx的情况下,使用CreateProcessW()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM