-
Notifications
You must be signed in to change notification settings - Fork 77
/
WinMain.cpp
403 lines (342 loc) · 14.2 KB
/
WinMain.cpp
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
/*****************************************************************************/
/* WinMain.cpp Copyright (c) Ladislav Zezula 2003 */
/*---------------------------------------------------------------------------*/
/* A file that simulates access on a file */
/*---------------------------------------------------------------------------*/
/* Date Ver Who Comment */
/* -------- ---- --- ------- */
/* 14.07.03 1.00 Lad The first version of WinMain.cpp */
/*****************************************************************************/
#include "FileTest.h"
#include "resource.h"
#pragma comment(lib, "Comctl32.lib")
//-----------------------------------------------------------------------------
// Global variables
TContextMenu g_ContextMenus[MAX_CONTEXT_MENUS];
TToolTip g_Tooltip;
DWORD g_dwWinVer;
DWORD g_dwWinBuild;
TCHAR g_szInitialDirectory[MAX_PATH];
DWORD g_dwMenuCount = 0;
//-----------------------------------------------------------------------------
// Local functions
static LPCTSTR IsCommandSwitch(LPCTSTR szArg, LPCTSTR szSwitch)
{
size_t nLength;
// It must be valid
if(szArg && szArg[0] && szSwitch && szSwitch[0])
{
// It has to start with '/' or '-'
if(szArg[0] == _T('/') || szArg[0] == _T('-'))
{
// Get length and the inner switch
nLength = _tcslen(szSwitch);
szArg++;
if(!_tcsnicmp(szArg, szSwitch, nLength))
{
return szArg + nLength;
}
}
}
return NULL;
}
static bool CheckForCommandSwitch(LPCTSTR szArg, LPCTSTR szSwitch, LPDWORD PtrValue, bool bIsSingleSwitch = false)
{
LPCTSTR szArgValue;
LPCTSTR szIntValue;
LPTSTR szEndValue;
int nRadix;
if((szArgValue = IsCommandSwitch(szArg, szSwitch)) != NULL)
{
// Pre-fill the value with zero
PtrValue[0] = 0;
// Variant #1: /Argument:IntValue
if(szArgValue[0] == _T(':') && bIsSingleSwitch == false)
{
szArgValue = szArgValue + 1;
szIntValue = SkipHexaPrefix(szArgValue);
nRadix = (szIntValue > szArgValue) ? 16 : 10;
PtrValue[0] = StrToInt(szIntValue, &szEndValue, nRadix);
return (szEndValue[0] == 0);
}
// Variant #2: /Argument
if(szArgValue[0] == 0 && bIsSingleSwitch)
{
PtrValue[0] = TRUE;
return true;
}
}
return false;
}
static void SetTokenObjectIntegrityLevel(DWORD dwIntegrityLevel)
{
SID_IDENTIFIER_AUTHORITY Sia = SECURITY_MANDATORY_LABEL_AUTHORITY;
SECURITY_DESCRIPTOR sd;
HANDLE hToken;
DWORD dwLength;
PACL pAcl;
PSID pSid;
// Do nothing on OSes where mandatory ACEs are not supported
if(pfnAddMandatoryAce == NULL)
return;
// Initialize blank security descriptor
if(!InitializeSecurityDescriptor(&sd, SECURITY_DESCRIPTOR_REVISION))
return;
// Allocate mandatory label SID
if(!AllocateAndInitializeSid(&Sia, 1, dwIntegrityLevel, 0, 0, 0, 0, 0, 0, 0, &pSid))
return;
// Open current token
if(!OpenThreadToken(GetCurrentThread(), WRITE_OWNER, TRUE, &hToken))
{
if(GetLastError() == ERROR_NO_TOKEN)
OpenProcessToken(GetCurrentProcess(), WRITE_OWNER, &hToken);
}
// If succeeded, set the integrity level
if(hToken != NULL)
{
// Create ACL
dwLength = sizeof(ACL) + sizeof(SYSTEM_MANDATORY_LABEL_ACE) - sizeof(DWORD) + GetLengthSid(pSid);
pAcl = (PACL)HeapAlloc(g_hHeap, 0, dwLength);
if(pAcl != NULL)
{
if(InitializeAcl(pAcl, dwLength, ACL_REVISION))
{
if(pfnAddMandatoryAce(pAcl, ACL_REVISION, 0, SYSTEM_MANDATORY_LABEL_NO_WRITE_UP, pSid))
{
NtSetSecurityObject(hToken, LABEL_SECURITY_INFORMATION, &sd);
}
}
HeapFree(g_hHeap, 0, pAcl);
}
}
FreeSid(pSid);
}
BOOL CALLBACK EnumMenusProc(HMODULE hModule, LPCTSTR lpszType, LPTSTR lpszName, LONG_PTR /* lParam */)
{
// Only take menus
if(lpszType == RT_MENU)
{
// Debug code check
assert(g_dwMenuCount < MAX_CONTEXT_MENUS);
// Check if the number of context menus is in range
if(g_dwMenuCount < MAX_CONTEXT_MENUS)
{
// Insert the menu entry
g_ContextMenus[g_dwMenuCount].szMenuName = lpszName;
g_ContextMenus[g_dwMenuCount].hMenu = LoadMenu(hModule, lpszName);
// Increment the menu count
g_dwMenuCount++;
}
}
// Keep enumerating
return TRUE;
}
static HANDLE InitialOpenFile(TFileTestData * pData)
{
OBJECT_ATTRIBUTES ObjAttr;
IO_STATUS_BLOCK IoStatus = {0};
UNICODE_STRING FileName;
HANDLE hFile = NULL;
if(pData->szFileName1 && pData->szFileName1[0])
{
if(IsNativeName(pData->szFileName1))
{
InitializeObjectAttributes(&ObjAttr, &FileName, OBJ_CASE_INSENSITIVE, NULL, NULL);
RtlInitUnicodeString(&FileName, pData->szFileName1);
NtCreateFile(&hFile,
pData->OpenFile.dwDesiredAccess,
&ObjAttr,
&IoStatus,
&pData->OpenFile.AllocationSize,
pData->OpenFile.dwFlagsAndAttributes,
pData->OpenFile.dwShareAccess,
pData->OpenFile.dwCreateDisposition2,
pData->OpenFile.dwCreateOptions,
pData->OpenFile.pvFileEa,
pData->OpenFile.cbFileEa);
}
else
{
hFile = CreateFile(pData->szFileName1,
pData->OpenFile.dwDesiredAccess,
pData->OpenFile.dwShareAccess,
NULL,
pData->OpenFile.dwCreateDisposition1,
pData->OpenFile.dwFlagsAndAttributes,
NULL);
}
}
return hFile;
}
//-----------------------------------------------------------------------------
// WinMain
int WINAPI _tWinMain(HINSTANCE hInstance, HINSTANCE, LPTSTR, int)
{
TFileTestData * pData;
DWORD bAsynchronousOpen = 0;
DWORD bOpenFile = 0;
DWORD bShowHelp = 0;
int nFileNameIndex = 0;
// Initialize the instance
InitInstance(hInstance);
InitCommonControls();
// Get the Windows version
g_dwWinVer = GetWindowsVersion();
// Allocate and fill our working structure with command line parameters
if((pData = new TFileTestData) != NULL)
{
// Initialize the TFileTestData structure
memset(pData, 0, sizeof(TFileTestData));
pData->MagicHeader = FILETEST_DATA_MAGIC;
pData->szDirName = pData->szBuffer1;
pData->szFileName1 = pData->szBuffer2;
pData->szFileName2 = pData->szBuffer3;
pData->szTemplate = pData->szBuffer4;
pData->szSectionName = pData->szBuffer5;
pData->RelaFile.szId = "RelativeFile";
pData->OpenFile.szId = "MainFile";
pData->pOP = &pData->OpenFile;
// Set default values for CreateFile and NtCreateFile
pData->OpenFile.dwOA_Attributes = OBJ_CASE_INSENSITIVE;
pData->OpenFile.dwCreateDisposition1 = OPEN_ALWAYS;
pData->OpenFile.dwCreateDisposition2 = FILE_OPEN_IF;
pData->OpenFile.dwDesiredAccess = GENERIC_READ;
pData->OpenFile.dwFlagsAndAttributes = FILE_ATTRIBUTE_NORMAL;
pData->OpenFile.dwShareAccess = FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE;
pData->OpenFile.dwCreateOptions = 0;
pData->dwOplockLevel = OPLOCK_LEVEL_CACHE_READ | OPLOCK_LEVEL_CACHE_WRITE;
pData->dwCopyFileFlags = 0;
pData->dwMoveFileFlags = 0;
// Set default values for opening relative file by NtCreateFile
pData->RelaFile.dwOA_Attributes = OBJ_CASE_INSENSITIVE;
pData->RelaFile.dwCreateDisposition1 = OPEN_EXISTING;
pData->RelaFile.dwCreateDisposition2 = FILE_OPEN;
pData->RelaFile.dwDesiredAccess = FILE_READ_DATA;
pData->RelaFile.dwFlagsAndAttributes = FILE_ATTRIBUTE_NORMAL;
pData->RelaFile.dwShareAccess = FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE;
// Set default values for NtCreateSection/NtOpenSection
pData->dwSectDesiredAccess = SECTION_ALL_ACCESS;
pData->dwSectPageProtection = PAGE_READONLY;
pData->dwSectAllocAttributes = SEC_COMMIT;
pData->dwSectWin32Protect = PAGE_READONLY;
// Set the default values for NtQuerySecurityObject
pData->SecurityInformation = OWNER_SECURITY_INFORMATION | GROUP_SECURITY_INFORMATION | DACL_SECURITY_INFORMATION;
pData->InitialPage = INVALID_PAGE_INDEX;
// Parse command line arguments
for(int i = 1; i < __argc; i++)
{
LPCTSTR szPrivilegeName;
if(CheckForCommandSwitch(__targv[i], _T("DesiredAccess"), &pData->OpenFile.dwDesiredAccess))
continue;
if(CheckForCommandSwitch(__targv[i], _T("ShareAccess"), &pData->OpenFile.dwShareAccess))
continue;
if(CheckForCommandSwitch(__targv[i], _T("CreateOptions"), &pData->OpenFile.dwCreateOptions))
continue;
if(CheckForCommandSwitch(__targv[i], _T("CopyFileFlags"), &pData->dwCopyFileFlags))
continue;
if(CheckForCommandSwitch(__targv[i], _T("MoveFileFlags"), &pData->dwMoveFileFlags))
continue;
if(CheckForCommandSwitch(__targv[i], _T("SecurityInformation"), &pData->SecurityInformation))
continue;
if(CheckForCommandSwitch(__targv[i], _T("InitialPage"), &pData->InitialPage))
continue;
if(CheckForCommandSwitch(__targv[i], _T("AsyncOpen"), &bAsynchronousOpen, true))
continue;
if(CheckForCommandSwitch(__targv[i], _T("OpenFile"), &bOpenFile, true))
continue;
if(CheckForCommandSwitch(__targv[i], _T("Help"), &bShowHelp, true))
continue;
if(CheckForCommandSwitch(__targv[i], _T("?"), &bShowHelp, true))
continue;
// Check for privileges to enable
if((szPrivilegeName = IsCommandSwitch(__targv[i], _T("EnablePrivilege"))) != NULL)
{
EnablePrivilege(szPrivilegeName + 1);
continue;
}
// We assume that the argument is a file name
switch(nFileNameIndex)
{
case 0: // The first file name argument
ExpandEnvironmentStrings(__targv[i], pData->szFileName1, MAX_NT_PATH);
nFileNameIndex++;
break;
case 1: // The second file name argument
ExpandEnvironmentStrings(__targv[i], pData->szFileName2, MAX_NT_PATH);
nFileNameIndex++;
break;
case 2: // The directory file name argument
ExpandEnvironmentStrings(__targv[i], pData->szDirName, MAX_NT_PATH);
nFileNameIndex++;
break;
}
}
// Set default file name
if(pData->szFileName1[0] == 0)
{
StringCchCopy(pData->szFileName1, MAX_NT_PATH, _T("C:\\TestFile.bin"));
pData->IsDefaultFileName1 = TRUE;
}
//
// DEVELOPMENT CODE: Build the NT status table from the NTSTATUS.h
//
// BuildNtStatusTableFromNTSTATUS_H();
// VerifyNtStatusTable();
//
// Resolve the dynamic loaded APIs
//
ResolveDynamicLoadedAPIs();
//
// On Vista or newer, set the required integrity level of our token object
// to lowest possible value. This will allow us to open our token even if the user
// lowers the integrity level.
//
SetTokenObjectIntegrityLevel(SECURITY_MANDATORY_UNTRUSTED_RID);
//
// Save the application initial directory
//
GetCurrentDirectory(_countof(g_szInitialDirectory), g_szInitialDirectory);
//
// Register the data editor window
//
RegisterDataEditor(hInstance);
//
// To make handles obtained by NtCreateFile usable for calling ReadFile and WriteFile,
// we have to set the FILE_SYNCHRONOUS_IO_NONALERT into CreateOptions
// and SYNCHRONIZE into DesiredAccess.
//
// Pre-load menus so they don't generate any FS requests when loaded
memset(g_ContextMenus, 0, sizeof(g_ContextMenus));
EnumResourceNames(g_hInst, RT_MENU, EnumMenusProc, NULL);
// Modify for synchronous open, if required
if(bAsynchronousOpen == false)
{
pData->OpenFile.dwCreateOptions |= FILE_SYNCHRONOUS_IO_NONALERT;
pData->RelaFile.dwCreateOptions |= FILE_SYNCHRONOUS_IO_NONALERT;
pData->OpenFile.dwDesiredAccess |= SYNCHRONIZE;
pData->RelaFile.dwDesiredAccess |= SYNCHRONIZE;
}
#ifdef __TEST_MODE__
//DebugCode_TEST();
DebugCode_SecurityDescriptor(pData->szFileName1);
#endif
// Show the main dialog
if(bShowHelp == FALSE)
{
// Shall we open the file?
if(bOpenFile)
pData->hFile = InitialOpenFile(pData);
FileTestDialog(NULL, pData);
}
else
HelpCommandLineDialog(NULL);
// Free the data blobs
pData->NtInfoData.Free();
pData->RdWrData.Free();
pData->OutData.Free();
pData->InData.Free();
delete pData;
}
UnloadDynamicLoadedAPIs();
return 0;
}