forked from resilar/HotFinger
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.inc
417 lines (371 loc) · 12.3 KB
/
utils.inc
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
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
;-------------------------------------------------------------------------;
; Miscellaneous utilities
;-------------------------------------------------------------------------;
SERVICE_ALL_ACCESS = 0f01ffh
SERVICE_CONTROL_STOP = 1
SERVICE_STOPPED = 1
SERVICE_RUNNING = 4
struct SERVICE_STATUS
dwServiceType dd ?
dwCurrentState dd ?
dwControlsAccepted dd ?
dwWin32ExitCode dd ?
dwServiceSpecificExitCode dd ?
dwCheckPoint dd ?
dwWaitHint dd ?
ends
;--------------------------------------------------------------------------;
; GUID strings
;--------------------------------------------------------------------------;
; szOut[39]:BYTE
proc GuidToString guid:DWORD, szOut:DWORD
push esi
xor eax, eax
push 15
push 8
pop ecx
pop esi
add esi, [guid]
std
@@: lodsb
push eax
loop @b
dec esi
lodsw
push eax
lodsw
push eax
dec esi
dec esi
lodsd
lea esi, [szOut]
push eax
cld
push utils.szGuidFormat
push dword [esi]
call [wsprintfA]
add esp, 8+11*4
lodsd ; return szOut
pop esi
ret
endp
;proc ValidateGuidString szGUID:DWORD
ValidateGuidString:
push ebx
push edi
mov ebx, utils.asciiDigitValueTable
mov edi, dword [esp + 2*4 + 4] ; [szGUID]
push edi
call [lstrlenA]
cmp eax, 38
ja .invalid
cmp eax, 36
jz .no_braces
cmp eax, 38
jnz .invalid
mov al, '{'
scasb
jnz .invalid
cmp byte [edi+36-1], '}'
jnz .invalid
.no_braces:
xor ecx, ecx
push ecx
push 12
push 4
push 4
push 4
mov cl, 8
.loop: xchg esi, edi
@@: lodsb
xlatb
test al, al
js .invalid
loop @b
pop ecx
xchg esi, edi
jecxz .valid
mov al, '-'
scasb
jz .loop
.invalid: xor eax, eax
jmp .exit
.valid: mov al, 1
.exit: pop edi
pop ebx
retn 4h
; guid[16]:BYTE
;proc StringToGuid szIn:DWORD, guid:DWORD
StringToGuid:
push dword [esp+4]
call ValidateGuidString
test eax, eax
jnz @f
retn 8h
@@:
push ebp
push ebx
push esi
push edi
virtual at esp
.regs dd 4 dup (?) ; saved ebp ebx esi edi
.addr dd ? ; return address
.szIn dd ? ; 1st param
.guid dd ? ; 2nd param
end virtual
xor ecx, ecx
mov esi, [.szIn]
mov edi, [.guid]
mov ebx, utils.asciiDigitValueTable
mov ebp, .conv
push edi ; return value
; Skip brace (if any)
cmp byte [esi], '{' ; {
jnz @f
inc esi
@@:
mov cl, 8
call ebp ; %08X
inc esi ; -
stosd
mov cl, 4
call ebp ; %04X
inc esi ; -
stosw
mov cl, 4
call ebp ; %04X
inc esi ; -
stosw
mov cl, 2
call ebp ; %02X
stosb
mov cl, 2
call ebp ; %02X
inc esi ; -
stosb
push 6
@@: mov cl, 2
call ebp ; %02X%02X%02X%02X%02X%02X
stosb
dec dword [esp]
jnz @b
pop eax
pop eax
pop edi
pop esi
pop ebx
pop ebp
retn 8h
; convert ASCII hexdigits to integer value
; esi = input, ecx = count, ebx = digit-value table
.conv: xor edx, edx
@@: lodsb
xlatb
shl edx, 4
or dl, al
loop @b
mov eax, edx
retn
;endp
; convert ASCII digits to integer value
; esi = input string (\0-terminated)
atoi: push ebx
push esi
xor eax, eax
xor edx, edx
push 10
mov ebx, utils.asciiDigitValueTable
pop ecx
@@: lodsb
test al, al
jz .done
xlatb
lea edx, [edx+edx*4]
shl edx, 1
add edx, eax
loop @b
.done: mov eax, edx
pop esi
pop ebx
retn
;--------------------------------------------------------------------------;
; Subfactor text functions
;--------------------------------------------------------------------------;
proc SubfactorText dwSubfactor:DWORD
mov eax, [dwSubfactor]
cmp eax, 10
jbe @f
xor eax, eax
@@: mov eax, [utils.pszSubfactorTable + eax*4]
ret
endp
proc SubfactorFullText dwSubfactor:DWORD
mov eax, [dwSubfactor]
cmp eax, 10
jbe @f
xor eax, eax
@@: mov eax, [utils.pszSubfactorFullTable + eax*4]
ret
endp
proc SubfactorSimpleText dwSubfactor:DWORD
push [dwSubfactor]
call SubfactorText
cmp [eax+1], ' '
jnz @f
add eax, 2
@@: ret
endp
;--------------------------------------------------------------------------;
; WinBio error strings
;--------------------------------------------------------------------------;
;proc WinBioErrorText dwErrorCode:DWORD
WinBioErrorText:
pop eax
xchg [esp], eax
sub eax, WINBIO_E_MIN
cmp eax, WINBIO_E_MAX - WINBIO_E_MIN
ja .no_winbio_err
mov eax, [utils.pszErrorTextTable + eax*4]
retn
.no_winbio_err: xor eax, eax
retn
;--------------------------------------------------------------------------;
; Run command as admin with ShellExecuteW
;--------------------------------------------------------------------------;
proc RunAsAdmin wszParameters:DWORD, hwndParent:DWORD
local wszArgv0[MAX_PATH+2]:WORD
local shinfo:SHELLEXECUTEINFO
push edi
xor eax, eax
mov ecx, sizeof.SHELLEXECUTEINFO
lea edi, [shinfo]
push ecx
push edi
rep stosb
pop edi
pop [edi + SHELLEXECUTEINFO.cbSize]
mov [edi + SHELLEXECUTEINFO.fMask], SEE_MASK_NOCLOSEPROCESS
push [hwndParent]
pop [edi + SHELLEXECUTEINFO.hwnd]
mov [edi + SHELLEXECUTEINFO.lpVerb], utils.wszRunAs
push [wszParameters]
pop [edi + SHELLEXECUTEINFO.lpParameters]
mov [edi + SHELLEXECUTEINFO.nShow], SW_SHOW
push MAX_PATH+1
lea eax, [wszArgv0]
mov [edi + SHELLEXECUTEINFO.lpFile], eax
push eax
push NULL
call [GetModuleFileNameW]
test eax, eax
js .fail
push edi
call [ShellExecuteExW]
test eax, eax
jz .fail
push 5000
push [edi + SHELLEXECUTEINFO.hProcess]
call [WaitForSingleObject]
test eax, eax
jnz .close_and_fail
push eax
push esp
push [edi + SHELLEXECUTEINFO.hProcess]
call [GetExitCodeProcess]
push [edi + SHELLEXECUTEINFO.hProcess]
call [CloseHandle]
pop eax
pop edi
ret
.close_and_fail:
push [edi + SHELLEXECUTEINFO.hProcess]
call [CloseHandle]
.fail: or eax, -1
pop edi
ret
endp
;--------------------------------------------------------------------------;
; WbioSrvc restart needed, for example, after registering a new database-
;--------------------------------------------------------------------------;
WbioSrvcRestart:
virtual at ebp
.status SERVICE_STATUS ?
.counter dd ?
.allocaSize = $ - $$
end virtual
push ebp
sub esp, .allocaSize
mov ebp, esp
xor eax, eax
push eax ; [.rc]
push GENERIC_EXECUTE
push utils.szSERVICES_ACTIVE_DATABASE
push eax
call [OpenSCManagerA]
test eax, eax
jz .fail_manager
push eax ; For CloseServiceHandle
push SERVICE_ALL_ACCESS
push utils.szWbioSrvc
push eax
call [OpenServiceA]
test eax, eax
jz .fail_service
push ebx
mov ebx, eax
; Stop the service
push -1
pop [.counter]
.stop_service: push ebp
push ebx
call [QueryServiceStatus]
test eax, eax
jz .fail
cmp [ebp + SERVICE_STATUS.dwCurrentState], SERVICE_STOPPED
jz .restart
inc [.counter]
jz .request_stop
cmp [.counter], 10
jae .fail
push 100
call [Sleep]
jmp .stop_service
.request_stop: push ebp
push SERVICE_CONTROL_STOP
push ebx
call [ControlService]
test eax, eax
jz .fail
jmp .stop_service
.restart: ; Restart the service
xor eax, eax
mov [.counter], eax
push eax ; NULL
push eax ; 0
push ebx
call [StartServiceA]
test eax, eax
jz .fail
.query_restart: push ebp
push ebx
call [QueryServiceStatus]
test eax, eax
jz .fail
cmp [ebp + SERVICE_STATUS.dwCurrentState], SERVICE_RUNNING
jz .restarted
inc [.counter]
jz .request_stop
cmp [.counter], 10
jae .fail
push 100
call [Sleep]
jmp .query_restart
.restarted:
mov dword [esp+8], 1 ; mov [.rc], 1
.fail: xchg [esp], ebx
call [CloseServiceHandle]
.fail_service: call [CloseServiceHandle]
.fail_manager: pop eax ; mov eax, [.rc]
add esp, .allocaSize
pop ebp
retn