winKernel.py
6.63 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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
#winKernel.py
#A part of NonVisual Desktop Access (NVDA)
#Copyright (C) 2006-2007 NVDA Contributors <http://www.nvda-project.org/>
#This file is covered by the GNU General Public License.
#See the file COPYING for more details.
import ctypes
import ctypes.wintypes
from ctypes import *
from ctypes.wintypes import *
kernel32=ctypes.windll.kernel32
advapi32 = windll.advapi32
#Constants
INFINITE = 0xffffffff
#Process control
PROCESS_ALL_ACCESS=0x1F0FFF
PROCESS_TERMINATE=0x1
PROCESS_VM_OPERATION=0x8
PROCESS_VM_READ=0x10
PROCESS_VM_WRITE=0X20
SYNCHRONIZE=0x100000
PROCESS_QUERY_INFORMATION=0x400
READ_CONTROL=0x20000
MEM_COMMIT=0x1000
MEM_RELEASE=0x8000
PAGE_READWRITE=0x4
MAXIMUM_ALLOWED = 0x2000000
STARTF_USESTDHANDLES = 0x00000100
#Console handles
STD_INPUT_HANDLE=-10
STD_OUTPUT_HANDLE=-11
STD_ERROR_HANDLE=-12
LOCALE_USER_DEFAULT=0x0400
DATE_LONGDATE=0x00000002
TIME_NOSECONDS=0x00000002
def GetStdHandle(handleID):
h=kernel32.GetStdHandle(handleID)
if h==0:
raise WinError()
return h
GENERIC_READ=0x80000000
GENERIC_WRITE=0x40000000
FILE_SHARE_READ=1
FILE_SHARE_WRITE=2
OPEN_EXISTING=3
def CreateFile(fileName,desiredAccess,shareMode,securityAttributes,creationDisposition,flags,templateFile):
res=kernel32.CreateFileW(fileName,desiredAccess,shareMode,securityAttributes,creationDisposition,flags,templateFile)
if res==0:
raise ctypes.WinError()
return res
def openProcess(*args):
return kernel32.OpenProcess(*args)
def closeHandle(*args):
return kernel32.CloseHandle(*args)
#added by Rui Batista to use on Say_battery_status script
#copied from platform sdk documentation (with required changes to work in python)
class SYSTEM_POWER_STATUS(ctypes.Structure):
_fields_ = [("ACLineStatus", ctypes.c_byte), ("BatteryFlag", ctypes.c_byte), ("BatteryLifePercent", ctypes.c_byte), ("Reserved1", ctypes.c_byte), ("BatteryLifeTime", ctypes.wintypes.DWORD), ("BatteryFullLiveTime", ctypes.wintypes.DWORD)]
def GetSystemPowerStatus(sps):
return kernel32.GetSystemPowerStatus(ctypes.byref(sps))
def getThreadLocale():
return kernel32.GetThreadLocale()
class SYSTEMTIME(ctypes.Structure):
_fields_ = (
("wYear", WORD),
("wMonth", WORD),
("wDayOfWeek", WORD),
("wDay", WORD),
("wHour", WORD),
("wMinute", WORD),
("wSecond", WORD),
("wMilliseconds", WORD)
)
def GetDateFormat(Locale,dwFlags,date,lpFormat):
if date is not None:
date=SYSTEMTIME(date.year,date.month,0,date.day,date.hour,date.minute,date.second,0)
lpDate=byref(date)
else:
lpDate=None
bufferLength=kernel32.GetDateFormatW(Locale, dwFlags, lpDate, lpFormat, None, 0)
buf=ctypes.create_unicode_buffer("", bufferLength)
kernel32.GetDateFormatW(Locale, dwFlags, lpDate, lpFormat, buf, bufferLength)
return buf.value
def GetTimeFormat(Locale,dwFlags,date,lpFormat):
if date is not None:
date=SYSTEMTIME(date.year,date.month,0,date.day,date.hour,date.minute,date.second,0)
lpTime=byref(date)
else:
lpTime=None
bufferLength=kernel32.GetTimeFormatW(Locale,dwFlags,lpTime,lpFormat, None, 0)
buf=ctypes.create_unicode_buffer("", bufferLength)
kernel32.GetTimeFormatW(Locale,dwFlags,lpTime,lpFormat, buf, bufferLength)
return buf.value
def openProcess(*args):
return kernel32.OpenProcess(*args)
def virtualAllocEx(*args):
res = kernel32.VirtualAllocEx(*args)
if res == 0:
raise WinError()
return res
def virtualFreeEx(*args):
return kernel32.VirtualFreeEx(*args)
def readProcessMemory(*args):
return kernel32.ReadProcessMemory(*args)
def writeProcessMemory(*args):
return kernel32.WriteProcessMemory(*args)
def waitForSingleObject(handle,timeout):
return kernel32.WaitForSingleObject(handle,timeout)
SHUTDOWN_NORETRY = 0x00000001
def SetProcessShutdownParameters(level, flags):
res = kernel32.SetProcessShutdownParameters(level, flags)
if res == 0:
raise ctypes.WinError()
def GetExitCodeProcess(process):
exitCode = ctypes.wintypes.DWORD()
if not kernel32.GetExitCodeProcess(process, ctypes.byref(exitCode)):
raise ctypes.WinError()
return exitCode.value
def TerminateProcess(process, exitCode):
if not kernel32.TerminateProcess(process, exitCode):
raise ctypes.WinError()
DRIVE_UNKNOWN = 0
DRIVE_NO_ROOT_DIR = 1
DRIVE_REMOVABLE = 2
DRIVE_FIXED = 3
DRIVE_REMOTE = 4
DRIVE_CDROM = 5
DRIVE_RAMDISK = 6
def GetDriveType(rootPathName):
return kernel32.GetDriveTypeW(rootPathName)
class SECURITY_ATTRIBUTES(Structure):
_fields_ = (
("nLength", DWORD),
("lpSecurityDescriptor", LPVOID),
("bInheritHandle", BOOL)
)
def __init__(self, **kwargs):
super(SECURITY_ATTRIBUTES, self).__init__(nLength=sizeof(self), **kwargs)
def CreatePipe(pipeAttributes, size):
read = ctypes.wintypes.HANDLE()
write = ctypes.wintypes.HANDLE()
if kernel32.CreatePipe(ctypes.byref(read), ctypes.byref(write), byref(pipeAttributes) if pipeAttributes else None, ctypes.wintypes.DWORD(size)) == 0:
raise ctypes.WinError()
return read.value, write.value
class STARTUPINFOW(Structure):
_fields_=(
('cb',DWORD),
('lpReserved',LPWSTR),
('lpDesktop',LPWSTR),
('lpTitle',LPWSTR),
('dwX',DWORD),
('dwY',DWORD),
('dwXSize',DWORD),
('dwYSize',DWORD),
('dwXCountChars',DWORD),
('dwYCountChars',DWORD),
('dwFillAttribute',DWORD),
('dwFlags',DWORD),
('wShowWindow',WORD),
('cbReserved2',WORD),
('lpReserved2',POINTER(c_byte)),
('hSTDInput',HANDLE),
('hSTDOutput',HANDLE),
('hSTDError',HANDLE),
)
def __init__(self, **kwargs):
super(STARTUPINFOW, self).__init__(cb=sizeof(self), **kwargs)
STARTUPINFO = STARTUPINFOW
class PROCESS_INFORMATION(Structure):
_fields_=(
('hProcess',HANDLE),
('hThread',HANDLE),
('dwProcessID',DWORD),
('dwThreadID',DWORD),
)
def CreateProcessAsUser(token, applicationName, commandLine, processAttributes, threadAttributes, inheritHandles, creationFlags, environment, currentDirectory, startupInfo, processInformation):
if advapi32.CreateProcessAsUserW(token, applicationName, commandLine, processAttributes, threadAttributes, inheritHandles, creationFlags, environment, currentDirectory, byref(startupInfo), byref(processInformation)) == 0:
raise WinError()
def GetCurrentProcess():
return kernel32.GetCurrentProcess()
def OpenProcessToken(ProcessHandle, DesiredAccess):
token = HANDLE()
if advapi32.OpenProcessToken(ProcessHandle, DesiredAccess, byref(token)) == 0:
raise WinError()
return token.value
DUPLICATE_SAME_ACCESS = 0x00000002
def DuplicateHandle(sourceProcessHandle, sourceHandle, targetProcessHandle, desiredAccess, inheritHandle, options):
targetHandle = HANDLE()
if kernel32.DuplicateHandle(sourceProcessHandle, sourceHandle, targetProcessHandle, byref(targetHandle), desiredAccess, inheritHandle, options) == 0:
raise WinError()
return targetHandle.value
PAPCFUNC = ctypes.WINFUNCTYPE(None, ctypes.wintypes.ULONG)
THREAD_SET_CONTEXT = 16