-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSvcTerminate.pas
157 lines (135 loc) · 4.36 KB
/
SvcTerminate.pas
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
unit SvcTerminate;
interface
uses
Windows, WinSvc;
const
SC_STATUS_PROCESS_INFO = 0;
type
_SERVICE_STATUS_PROCESS = packed record
dwServiceType: DWORD;
dwCurrentState: DWORD;
dwControlsAccepted: DWORD;
dwWin32ExitCode: DWORD;
dwServiceSpecificExitCode: DWORD;
dwCheckPoint: DWORD;
dwWaitHint: DWORD;
dwProcessId: DWORD;
dwServiceFlags: DWORD;
end;
SERVICE_STATUS_PROCESS = _SERVICE_STATUS_PROCESS;
TServiceStatusProcess = SERVICE_STATUS_PROCESS;
LPSERVICE_STATUS_PROCESS = ^SERVICE_STATUS_PROCESS;
PServiceStatusProcess = ^TServiceStatusProcess;
function QueryServiceStatusEx(hService: SC_HANDLE; InfoLevel: Integer; lpBuffer: Pointer; cbBufSize: DWORD; pcbBytesNeeded: PDWORD): BOOL; stdcall; external 'advapi32';
procedure ModifySecurity(Enable: Boolean);
function ServiceGetProcessID(MachineName, ServiceName: String): DWORD;
function KillProcessByPID(ProcessID: DWORD): Boolean;
implementation
function KillProcessByPID(ProcessID: DWORD): Boolean;
var hProc: THandle;
begin
// Set default result
result:=False;
// Enable permissions
ModifySecurity(True);
// Resource protection
try
// Open process
hProc:=OpenProcess(PROCESS_ALL_ACCESS, False, ProcessID);
// Check handle
if not(hProc = 0) then
begin
// Resource protection
try
// Terminate
result:=TerminateProcess(hProc, 0);
finally
// Close process handle
CloseHandle(hProc);
end;
end;
finally
// Disable the permissions
ModifySecurity(False);
end;
end;
function ServiceGetProcessID(MachineName, ServiceName: String): DWORD;
var lpStatus: TServiceStatusProcess;
hSCM: SC_HANDLE;
hSC: SC_HANDLE;
dwNeeded: DWORD;
begin
// Default result
result:=0;
// Connect to the service control manager
hSCM:=OpenSCManager(PChar(MachineName), nil, SC_MANAGER_CONNECT);
// Check handle
if not(hSCM = 0) then
begin
// Resource protection
try
// Open service
hSC:=OpenService(hSCM, PChar(ServiceName), SERVICE_QUERY_STATUS);
// Check handle
if not(hSC = 0) then
begin
// Resource protection
try
// Query service
if QueryServiceStatusEx(hSC, SC_STATUS_PROCESS_INFO, @lpStatus, SizeOf(TServiceStatusProcess), @dwNeeded) then
begin
// Update the result
result:=lpStatus.dwProcessId;
end;
finally
// Close handle
CloseServiceHandle(hSC);
end;
end;
finally
// Close handle
CloseServiceHandle(hSCM);
end;
end;
end;
procedure ModifySecurity(Enable: Boolean);
var hToken: THandle;
lpszSecName: Array [0..2] of PChar;
tp: TOKEN_PRIVILEGES;
tpPrevious: TOKEN_PRIVILEGES;
luid: TLargeInteger;
cbUnused: DWORD;
cbPrevious: DWORD;
dwCount: Integer;
begin
// Set security names
lpszSecName[0]:='SeSecurityPrivilege';
lpszSecName[1]:='SeTcbPrivilege';
lpszSecName[2]:='SeDebugPrivilege';
// Enable our process to super-level rights
if OpenProcessToken(GetCurrentProcess, TOKEN_ADJUST_PRIVILEGES or TOKEN_QUERY, hToken) then
begin
// Iterate the security names to elevate
for dwCount:=Low(lpszSecName) to High(lpszSecName) do
begin
cbPrevious:=SizeOf(TOKEN_PRIVILEGES);
if LookupPrivilegeValue(nil, lpszSecName[dwCount], luid) then
begin
tp.PrivilegeCount:=1;
tp.Privileges[0].Luid:=luid;
tp.Privileges[0].Attributes:=0;
if AdjustTokenPrivileges(hToken, False, tp, SizeOf(TOKEN_PRIVILEGES), @tpPrevious, cbPrevious) then
begin
tpPrevious.PrivilegeCount:=1;
tpPrevious.Privileges[0].Luid:=luid;
if Enable then
tpPrevious.Privileges[0].Attributes:=SE_PRIVILEGE_ENABLED
else
tpPrevious.Privileges[0].Attributes:=0;
if not(AdjustTokenPrivileges(hToken, False, tpPrevious, cbPrevious, nil, cbUnused)) then break;
end;
end;
end;
end;
end;
end.