This repository has been archived by the owner on Jan 3, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
_wbox.pyx
361 lines (285 loc) · 10.1 KB
/
_wbox.pyx
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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
from ctypes import WinError
from msvcrt import open_osfhandle
from os import fdopen, O_RDONLY, O_WRONLY
ctypedef const Py_UNICODE *LPCWSTR
ctypedef Py_UNICODE *LPWSTR
ctypedef const char *LPCSTR
ctypedef char *LPSTR
ctypedef void *HANDLE
ctypedef unsigned long DWORD
cdef extern from 'windows.h' nogil:
DWORD WaitForSingleObject(HANDLE, DWORD milliseconds)
cdef DWORD INFINITE, WAIT_TIMEOUT
bint GetExitCodeProcess(HANDLE, DWORD*)
cdef extern from 'helpers.h' nogil:
cdef cppclass AutoHandle:
AutoHandle()
AutoHandle(HANDLE h)
HANDLE get()
HANDLE detach()
void close()
void set(HANDLE h)
cdef extern from 'user.h' nogil:
void WBoxGenerateUserName(LPWSTR buffer, size_t)
void WBoxGeneratePassword(LPWSTR buffer, size_t)
void WBoxGeneratePassword(LPWSTR buffer, size_t, LPCWSTR alphabet)
cdef cppclass CUserManager 'UserManager':
CUserManager() except +
CUserManager(LPCWSTR username) except +
CUserManager(LPCWSTR username, LPCWSTR password) except +
LPCWSTR username()
LPCWSTR password()
cdef extern from 'wbox.h' nogil:
cdef cppclass JobbedProcessManager:
JobbedProcessManager() except +
bint spawn() except +
bint terminate(unsigned code) except +
JobbedProcessManager &time(double seconds)
JobbedProcessManager &memory(size_t bytes)
JobbedProcessManager &processes(int count)
JobbedProcessManager &withLogin(LPCWSTR username, LPCWSTR password);
JobbedProcessManager &command(LPCWSTR cmdline);
JobbedProcessManager &executable(LPCWSTR executable)
JobbedProcessManager &directory(LPCWSTR directory)
JobbedProcessManager &injectX86(LPCWSTR szExecutable)
JobbedProcessManager &injectX64(LPCWSTR szExecutable)
JobbedProcessManager &injectFunction(LPCSTR szFunction)
JobbedProcessManager &environment(LPCWSTR env, size_t cb)
unsigned long long memory()
double executionTime()
bint tle()
bint mle()
DWORD return_code()
bint wait()
bint wait(DWORD time)
AutoHandle &process()
AutoHandle &job()
AutoHandle &stdIn()
AutoHandle &stdOut()
AutoHandle &stdErr()
@staticmethod
void updateAsmX86(LPCWSTR szExecutable)
@staticmethod
void updateAsmX64(LPCWSTR szExecutable)
cdef extern from 'firewall.h' nogil:
cdef cppclass CNetworkManager 'NetworkManager':
CNetworkManager(LPCWSTR name, LPCWSTR executable) except +
cdef class UserManager:
cdef CUserManager *thisptr
def __cinit__(self, username=None, password=None):
if username is not None and password is not None:
self.thisptr = new CUserManager(username, password)
elif username is not None:
self.thisptr = new CUserManager(username)
else:
self.thisptr = new CUserManager()
def __dealloc__(self):
if self.thisptr:
del self.thisptr
def dispose(self):
if self.thisptr:
del self.thisptr
self.thisptr = NULL
def __enter__(self):
pass
def __exit__(self, exc_type, exc_val, exc_tb):
self.dispose()
property username:
def __get__(self):
if not self.thisptr:
raise ValueError('already destroyed')
return self.thisptr.username()
property password:
def __get__(self):
if not self.thisptr:
raise ValueError('already destroyed')
return self.thisptr.password()
cdef class NetworkManager:
cdef CNetworkManager *thisptr
cdef unicode _name, _executable
def __cinit__(self, name, executable):
if not isinstance(name, unicode):
name = name.decode('mbcs')
if not isinstance(executable, unicode):
executable = executable.decode('mbcs')
self._name = name
self._executable = executable
self.thisptr = new CNetworkManager(name, executable)
def __dealloc__(self):
if self.thisptr:
del self.thisptr
def dispose(self):
if self.thisptr:
del self.thisptr
self.thisptr = NULL
def __enter__(self):
pass
def __exit__(self, exc_type, exc_val, exc_tb):
self.dispose()
property name:
def __get__(self):
if not self.thisptr:
raise ValueError('already destroyed')
return self._name
property executable:
def __get__(self):
if not self.thisptr:
raise ValueError('already destroyed')
return self._executable
cdef class ProcessManager:
cdef JobbedProcessManager *thisptr
cdef double _time_limit
cdef size_t _memory_limit
cdef int _process_limit
cdef unicode _username
cdef unicode _password
cdef unicode _executable
cdef unicode _command
cdef unicode _dir
cdef unicode _inject32
cdef unicode _inject64
cdef str _inject_func
cdef object _stdin, _stdout, _stderr
def __cinit__(self, username, password):
self.thisptr = new JobbedProcessManager()
self._time_limit = 0
self._memory_limit = 0
self._process_limit = 0
self._username = username
self._password = password
self._executable = self._dir = u''
self._inject32 = self._inject64 = None
self._inject_func = None
self._stdin = self._stdout = self._stderr = None
self.thisptr.withLogin(username, password)
def __dealloc__(self):
del self.thisptr
def spawn(self):
with nogil:
if not self.thisptr.spawn():
with gil:
return False
cdef HANDLE stdin, stdout, stderr
stdin = self.thisptr.stdIn().detach()
stdout = self.thisptr.stdOut().detach()
stderr = self.thisptr.stdErr().detach()
self._stdin = fdopen(open_osfhandle(<unsigned long long> stdin, O_WRONLY), 'w')
self._stdout = fdopen(open_osfhandle(<unsigned long long> stdout, O_RDONLY), 'r')
self._stderr = fdopen(open_osfhandle(<unsigned long long> stderr, O_RDONLY), 'r')
def terminate(self, code):
return self.thisptr.terminate(code)
def wait(self, timeout=None):
cdef DWORD time, result
if timeout is not None:
time = timeout
else:
time = INFINITE
with nogil:
result = WaitForSingleObject(self.thisptr.process().get(), time)
return result
def get_exit_code(self):
cdef DWORD code
if WaitForSingleObject(self.thisptr.process().get(), 0) == WAIT_TIMEOUT:
return None
else:
if not GetExitCodeProcess(self.thisptr.process().get(), &code):
raise WinError()
return code
def set_environment(self, unicode memory):
self.thisptr.environment(memory, len(memory) * sizeof(Py_UNICODE))
def inherit_environment(self):
self.thisptr.environment(NULL, 0)
property stdin:
def __get__(self):
return self._stdin
property stdout:
def __get__(self):
return self._stdout
property stderr:
def __get__(self):
return self._stderr
property time_limit:
def __get__(self):
return self._time_limit
def __set__(self, value):
self._time_limit = value
self.thisptr.time(value)
property memory_limit:
def __get__(self):
return self._memory_limit
def __set__(self, value):
self._memory_limit = value
self.thisptr.memory(value)
property process_limit:
def __get__(self):
return self._process_limit
def __set__(self, value):
self._process_limit = value
self.thisptr.processes(value)
property executable:
def __get__(self):
return self._executable
def __set__(self, value):
self._executable = value
self.thisptr.executable(value)
property command:
def __get__(self):
return self._command
def __set__(self, value):
self._command = value
self.thisptr.command(value)
property dir:
def __get__(self):
return self._dir
def __set__(self, value):
self._dir = value
self.thisptr.directory(value)
property inject32:
def __get__(self):
return self._inject32
def __set__(self, value):
self._inject32 = value
self.thisptr.injectX86(value)
property inject64:
def __get__(self):
return self._inject64
def __set__(self, value):
self._inject64 = value
self.thisptr.injectX64(value)
property inject_func:
def __get__(self):
return self._inject_func
def __set__(self, value):
self._inject_func = value
self.thisptr.injectFunction(value)
property username:
def __get__(self):
return self._username
def __set__(self, value):
self._username = value
self.thisptr.withLogin(self._username, self._password)
property password:
def __get__(self):
return self._password
def __set__(self, value):
self._password = value
self.thisptr.withLogin(self._username, self._password)
property memory:
def __get__(self):
return self.thisptr.memory()
property mle:
def __get__(self):
return self.thisptr.mle()
property execution_time:
def __get__(self):
return self.thisptr.executionTime()
property tle:
def __get__(self):
return self.thisptr.tle()
property _handle:
def __get__(self):
return <unsigned long long> self.thisptr.process().get()
cpdef update_address_x86(unicode executable):
JobbedProcessManager.updateAsmX86(executable)
cpdef update_address_x64(unicode executable):
JobbedProcessManager.updateAsmX64(executable)