-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathSpawnProc.py
More file actions
87 lines (75 loc) · 2.44 KB
/
Copy pathSpawnProc.py
File metadata and controls
87 lines (75 loc) · 2.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
import ctypes
from ctypes.wintypes import DWORD, LPWSTR, WORD, LPBYTE, HANDLE
# Grab a handle to kernel32.dll
k_handle = ctypes.WinDLL("Kernel32.dll")
# Structure for Startup Info
class STARTUPINFO(ctypes.Structure):
_fields_ = [
("cb", DWORD),
("lpReserved", LPWSTR),
("lpDesktop", LPWSTR),
("lpTitle", LPWSTR),
("dwX", DWORD),
("dxY", 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),
]
# Structure for Process Info
class PROCESS_INFORMATION(ctypes.Structure):
_fields_ = [
("hProcess", HANDLE),
("hThread", HANDLE),
("dwProcessId", DWORD),
("dwThreadId", DWORD),
]
# Setup the Paramaters for the Win API Calls
lpApplicationName = "C:\\Windows\\System32\\cmd.exe"
lpCommandLine = None
lpProcessAttributes = None
lpThreadAttributes = None
lpEnvironment = None
lpCurrentDirectory = None
# Setup Creation Flags
# CREATE_NEW_CONSOLE Option
dwCreatedFlags = 0x00000010
# Setup to Inherit Handle
# We set this to false as we dont want to inherit the handle into our current process
bInheritHandle = False
# Create StartupInfo Structure
lpStartupInfo = STARTUPINFO()
# Set the Window to Show
lpStartupInfo.wShowWindow = 0x1
# Setup the flags
# 0x1 = STARTF_USESHOWWINDOW - Tells Windows to check the wShowWindow Flag in the Startup Info
lpStartupInfo.dwFlags = 0x1
# Grab the size of the structure after settings are set
lpStartupInfo.cb = ctypes.sizeof(lpStartupInfo)
# Create empty copy of PROCESS_INFORMATION so the data can be saved to items
lpProcessInformation = PROCESS_INFORMATION()
# Run the API Call
response = k_handle.CreateProcessW(
lpApplicationName,
lpCommandLine,
lpProcessAttributes,
lpThreadAttributes,
bInheritHandle,
dwCreatedFlags,
lpEnvironment,
lpCurrentDirectory,
ctypes.byref(lpStartupInfo),
ctypes.byref(lpProcessInformation))
# Check for Errors
if response > 0:
print("[INFO] Process Created & Running...")
else:
print("[ERROR] Could not Create Process! Error Code: {0}".format(k_handle.GetLastError()))