-
Notifications
You must be signed in to change notification settings - Fork 176
/
stdafx.h
410 lines (373 loc) · 11.7 KB
/
stdafx.h
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
// stdafx.h : include file for standard system include files,
// or project specific include files that are used frequently, but
// are changed infrequently
//
#pragma once
#define _CRT_SECURE_NO_WARNINGS
#define _CRT_NON_CONFORMING_WCSTOK
#include "targetver.h"
#define _CRT_RAND_S
#include <stdlib.h>
#include <stdio.h>
#include <tchar.h>
#define _ATL_CSTRING_EXPLICIT_CONSTRUCTORS // some CString constructors will be explicit
#include <atlstr.h>
#include <time.h>
#ifdef _DEBUG
#define VERIFY(x) { BOOL b = x; _ASSERT(b); }
#else
#define VERIFY(x) x
#endif
#ifndef VC_EXTRALEAN
#define VC_EXTRALEAN // Exclude rarely-used stuff from Windows headers
#endif
//#include <afx.h>
//#include <afxwin.h> // MFC core and standard components
//#include <afxext.h> // MFC extensions
//#ifndef _AFX_NO_OLE_SUPPORT
//#include <afxdtctl.h> // MFC support for Internet Explorer 4 Common Controls
//#endif
//#ifndef _AFX_NO_AFXCMN_SUPPORT
//#include <afxcmn.h> // MFC support for Windows Common Controls
//#endif // _AFX_NO_AFXCMN_SUPPORT
#include <iostream>
#include <Windows.h>
#include <WinSvc.h>
#include <vector>
#include "CmdLineParser.h"
extern bool gbODS;
extern bool gbStop;
extern bool gbInService;
#define BAD_HANDLE(x) ((INVALID_HANDLE_VALUE == x) || (NULL == x))
const WORD MSGID_SETTINGS = 1; //local sending settings to remote service, content of this message scrambled
const WORD MSGID_RESP_SEND_FILES = 2; //remote indicates file needs to be sent
const WORD MSGID_SENT_FILES = 3; //local is sending file
const WORD MSGID_OK = 4; //simple OK response
const WORD MSGID_START_APP = 5;
const WORD MSGID_FAILED = 6; //request failed
class FileInfo
{
public:
FileInfo() { bCopyFile = false; fileVersionMS = 0; fileVersionLS = 0; fileLastWrite.dwHighDateTime = 0; fileLastWrite.dwLowDateTime = 0; }
FileInfo(const FileInfo& in) { *this = in; }
FileInfo& operator=(const FileInfo& in)
{
filenameOnly = in.filenameOnly;
fullFilePath = in.fullFilePath;
bCopyFile = in.bCopyFile;
fileVersionMS = in.fileVersionMS;
fileVersionLS = in.fileVersionLS;
fileLastWrite.dwHighDateTime = in.fileLastWrite.dwHighDateTime;
fileLastWrite.dwLowDateTime = in.fileLastWrite.dwLowDateTime;
return *this;
}
CString filenameOnly;
CString fullFilePath; //not serialized to remote server
FILETIME fileLastWrite;
DWORD fileVersionMS;
DWORD fileVersionLS;
bool bCopyFile; //not serialized to remote server
};
class RemMsg
{
RemMsg(const RemMsg&); //not implemented
RemMsg& operator=(const RemMsg&); //not implemented
DWORD GetUniqueID();
public:
RemMsg() { m_msgID = 0; m_pBuff = NULL; m_expectedLen = 0; m_uniqueProcessID = GetUniqueID(); }
RemMsg(WORD msgID)
{
m_msgID = msgID;
m_bResetReadItr = true;
m_pBuff = NULL;
m_expectedLen = 0;
m_uniqueProcessID = GetUniqueID();
}
virtual ~RemMsg()
{
delete [] m_pBuff;
}
const BYTE* GetDataToSend(DWORD& totalLen);
void SetFromReceivedData(BYTE* pData, DWORD dataLen); //returns whether all data received or not
RemMsg& operator<<(LPCWSTR s);
RemMsg& operator<<(bool b);
RemMsg& operator<<(DWORD d);
RemMsg& operator<<(__int64 d);
RemMsg& operator<<(FILETIME d);
RemMsg& operator>>(CString& s);
RemMsg& operator>>(bool& b);
RemMsg& operator>>(DWORD& d);
RemMsg& operator>>(__int64& d);
RemMsg& operator>>(FILETIME& d);
WORD m_msgID;
std::vector<BYTE> m_payload;
std::vector<BYTE>::iterator m_readItr;
bool m_bResetReadItr;
DWORD m_expectedLen;
DWORD m_uniqueProcessID; //helps remote PAExecs differentiate from possibly multiple clients talking to it
private:
BYTE* m_pBuff;
};
class Settings
{
Settings(const Settings&); //not implemented
Settings& operator=(const Settings&); //not implemented
public:
Settings()
{
bCopyFiles = false;
bForceCopy = false;
bCopyIfNewerOrHigherVer = false;
bDontWaitForTerminate = false;
bDontLoadProfile = false;
bRunElevated = false;
bRunLimited = false;
remoteCompConnectTimeoutSec = 0;
bUseSystemAccount = false;
bShowUIOnWinLogon = false;
priority = NORMAL_PRIORITY_CLASS;
hProcess = INVALID_HANDLE_VALUE;
hUserProfile = INVALID_HANDLE_VALUE; //call UnloadUserProfile when done
hUser = INVALID_HANDLE_VALUE;
hUserImpersonated = INVALID_HANDLE_VALUE;
bDisableFileRedirection = false;
bODS = false;
hStdOut = INVALID_HANDLE_VALUE;
hStdIn = INVALID_HANDLE_VALUE;
hStdErr = INVALID_HANDLE_VALUE;
bNeedToDetachFromAdmin = false;
bNeedToDetachFromIPC = false;
bNeedToDeleteServiceFile = false;
bNeedToDeleteService = false;
bInteractive = false;
processID = 0;
bNoDelete = false;
timeoutSeconds = 0;
bNoName = false;
sessionToInteractWith = (DWORD)-1; //not initialized
targetShare = L"ADMIN$";
targetSharePath = L"%SYSTEMROOT%";
}
void Serialize(RemMsg& msg, bool bSave)
{
#define CUR_SERIALIZE_VERSION 1
if(bSave)
{
msg << (DWORD)CUR_SERIALIZE_VERSION;
msg << (DWORD)allowedProcessors.size();
for(std::vector<WORD>::iterator itr = allowedProcessors.begin(); allowedProcessors.end() != itr; itr++)
msg << (DWORD)*itr;
msg << bCopyFiles;
msg << bForceCopy;
msg << bCopyIfNewerOrHigherVer;
msg << bDontWaitForTerminate;
msg << bDontLoadProfile;
msg << sessionToInteractWith;
msg << bInteractive;
msg << bRunElevated;
msg << bRunLimited;
msg << password;
msg << user;
msg << bUseSystemAccount;
msg << workingDir;
msg << bShowUIOnWinLogon;
msg << (DWORD)priority;
msg << app;
msg << appArgs;
msg << bDisableFileRedirection;
msg << bODS;
msg << remoteLogPath;
msg << bNoDelete;
msg << srcDir;
msg << destDir;
msg << (DWORD)srcFileInfos.size();
for(std::vector<FileInfo>::iterator fItr = srcFileInfos.begin(); srcFileInfos.end() != fItr; fItr++)
{
msg << (*fItr).filenameOnly;
msg << (*fItr).fileLastWrite;
msg << (*fItr).fileVersionLS;
msg << (*fItr).fileVersionMS;
//bCopyFiles is NOT sent
//fullFilePath is NOT sent
}
msg << (DWORD)destFileInfos.size();
for(std::vector<FileInfo>::iterator fItr = destFileInfos.begin(); destFileInfos.end() != fItr; fItr++)
{
msg << (*fItr).filenameOnly;
msg << (*fItr).fileLastWrite;
msg << (*fItr).fileVersionLS;
msg << (*fItr).fileVersionMS;
//bCopyFiles is NOT sent
//fullFilePath is NOT sent
}
msg << timeoutSeconds;
}
else
{
allowedProcessors.clear();
DWORD version = 0;
msg >> version;
DWORD num = 0;
msg >> num;
for(DWORD i = 0; i < num; i++)
{
DWORD proc = 0;
msg >> proc;
allowedProcessors.push_back((WORD)proc);
}
msg >> bCopyFiles;
msg >> bForceCopy;
msg >> bCopyIfNewerOrHigherVer;
msg >> bDontWaitForTerminate;
msg >> bDontLoadProfile;
msg >> sessionToInteractWith;
msg >> bInteractive;
msg >> bRunElevated;
msg >> bRunLimited;
msg >> password;
msg >> user;
msg >> bUseSystemAccount;
msg >> workingDir;
msg >> bShowUIOnWinLogon;
msg >> (DWORD&)priority;
msg >> app;
msg >> appArgs;
msg >> bDisableFileRedirection;
msg >> bODS;
msg >> remoteLogPath;
msg >> bNoDelete;
msg >> srcDir;
msg >> destDir;
msg >> num;
for(DWORD i = 0; i < num; i++)
{
FileInfo fi;
msg >> fi.filenameOnly;
msg >> fi.fileLastWrite;
msg >> fi.fileVersionLS;
msg >> fi.fileVersionMS;
fi.bCopyFile = false; //not known whether it will be copied yet
//bCopyFiles is NOT sent
//fullFilePath is NOT sent
srcFileInfos.push_back(fi);
}
msg >> num;
for(DWORD i = 0; i < num; i++)
{
FileInfo fi;
msg >> fi.filenameOnly;
msg >> fi.fileLastWrite;
msg >> fi.fileVersionLS;
msg >> fi.fileVersionMS;
fi.bCopyFile = false; //not known whether it will be copied yet
//bCopyFiles is NOT sent
//fullFilePath is NOT sent
destFileInfos.push_back(fi);
}
msg >> timeoutSeconds;
}
}
bool ResolveFilePaths();
std::vector<WORD> allowedProcessors; //empty means any
bool bCopyFiles;
bool bForceCopy;
bool bCopyIfNewerOrHigherVer;
bool bDontWaitForTerminate;
bool bDontLoadProfile;
DWORD sessionToInteractWith;
bool bInteractive;
bool bRunElevated;
bool bRunLimited;
CString password;
CString user;
bool bUseSystemAccount;
CString workingDir;
bool bShowUIOnWinLogon;
int priority;
CString app;
CString appArgs;
bool bDisableFileRedirection;
bool bODS;
CString remoteLogPath;
bool bNoDelete;
CString srcDir;
CString destDir;
std::vector<FileInfo> srcFileInfos; //index 0 will be source of app, but may not match app if -csrc or -clist are being used
std::vector<FileInfo> destFileInfos; //index 0 will be app
DWORD timeoutSeconds;
//NOT TRANSMITTED
DWORD remoteCompConnectTimeoutSec;
std::vector<CString> computerList; //run locally if empty
HANDLE hProcess;
DWORD processID;
HANDLE hUserProfile; //call UnloadUserProfile when done
HANDLE hUser;
HANDLE hUserImpersonated;
CString localLogPath;
HANDLE hStdOut;
HANDLE hStdIn;
HANDLE hStdErr;
bool bNeedToDetachFromAdmin;
bool bNeedToDetachFromIPC;
bool bNeedToDeleteServiceFile;
bool bNeedToDeleteService;
bool bNoName;
CString serviceName;
CString targetShare;
CString targetSharePath;
};
class ListenParam
{
public:
ListenParam() { pSettings = NULL; remoteServer = NULL; pid = GetCurrentProcessId(); hStop = CreateEvent(NULL, TRUE, FALSE, NULL); workerThreads = 0; InitializeCriticalSection(&cs); }
~ListenParam() { CloseHandle(hStop); DeleteCriticalSection(&cs); }
Settings* pSettings;
LPCWSTR remoteServer;
CString machineName;
DWORD pid;
HANDLE hStop;
long workerThreads;
CRITICAL_SECTION cs;
std::vector<std::string> inputSentToSuppressInOutput;
};
const unsigned char UTF8_BOM[] = { unsigned char(0xEF), unsigned char(0xBB), unsigned char(0xBF) };
typedef struct
{
DWORD origSessionID;
HANDLE hUser;
bool bPreped;
}CleanupInteractive;
DWORD StartLocalService(CCmdLineParser& cmdParser);
void Log(LPCWSTR str, bool bForceODS);
void Log(LPCWSTR str, DWORD lastError);
CString GetSystemErrorMessage(DWORD lastErrorVal);
CString StrFormat(LPCTSTR pFormat, ...); //returns a formatted CString. Inspired by .NET's String.Format
void PrintCopyright();
void PrintUsage();
bool GetComputerList(Settings& settings, LPCWSTR& cmdLine);
DWORD wtodw(const wchar_t* num);
bool StartProcess(Settings& settings, HANDLE remoteCmdPipe);
bool EnablePrivilege(LPCWSTR privilegeStr, HANDLE hToken = NULL);
BOOL PrepForInteractiveProcess(Settings& settings, CleanupInteractive* pCI, DWORD sessionID);
void CleanUpInteractiveProcess(CleanupInteractive* pCI);
bool EstablishConnection(Settings& settings, LPCTSTR lpszRemote, LPCTSTR lpszResource, bool bConnect);
bool CopyPAExecToRemote(Settings& settings, LPCWSTR targetComputer);
bool InstallAndStartRemoteService(LPCWSTR remoteServer, Settings& settings);
bool SendSettings(LPCWSTR remoteServer, Settings& settings, HANDLE& hPipe, bool& bNeedToSendFile);
bool SendRequest(LPCWSTR remoteServer, HANDLE& hPipe, RemMsg& msgOut, RemMsg& msgReturned, Settings& settings);
bool SendFilesToRemote(LPCWSTR remoteServer, Settings& settings, HANDLE& hPipe);
void StopAndDeleteRemoteService(LPCWSTR remoteServer, Settings& settings);
void DeletePAExecFromRemote(LPCWSTR targetComputer, Settings& settings);
void HandleMsg(RemMsg& msg, RemMsg& response, HANDLE hPipe);
bool GetTargetFileInfo(Settings& settings);
bool ParseCommandLine(Settings& settings, LPCWSTR cmdLine);
void StartRemoteApp(LPCWSTR remoteServer, Settings& settings, HANDLE& hPipe, int& appExitCode);
void DisableFileRedirection();
void RevertFileRedirection();
bool CreateIOPipesInService(Settings& settings, LPCWSTR caller, DWORD pid);
BOOL ConnectToRemotePipes(ListenParam* pLP, DWORD dwRetryCount, DWORD dwRetryTimeOut);
CString LastLog();
void Duplicate(HANDLE& h, LPCSTR file, int line);
bool ReadTextFile(LPCWSTR fileName, CString& content);
CString ExpandToFullPath(LPCWSTR path);
BOOL IsLocalSystem();