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

时间:2013-03-02 07:42:29

标签: python ctypes

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

这是我的代码:

    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?

非常感谢任何帮助!

6 个答案:

答案 0 :(得分:3)

您的代码中有几处错误:

第一个错误是load类的debugger类定义错误。在你的情况下,最有可能是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,你应该使用CreateProcessA而不是CreateProcessW函数,因为Python 3.x中的所有字符串都是unicode(在WinAPI中,所有函数都以'A'接受asci结尾-strings,以'W'结尾接受unicode-strings)。如果你在你的案例中写下了什么错误或异常,那么更准确的答案就是。

答案 1 :(得分:2)

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

答案 2 :(得分:1)

切换前两个参数,使其具有以下内容:

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

答案 3 :(得分:1)

你应该调用 GetLastError 函数来了解错误是什么。

ctypes.windll.kernel32.GetLastError

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

答案 4 :(得分:0)

  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)

答案 5 :(得分:0)

  1. 根据a quick sum-up of differences between Python 2xx vs 3xx: Python 2分离了ASCII str()类型和unicode()类型。 Python 3 只有Unicode(utf-8)字符串类型

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

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