-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexkern_inst_engine.cpp
820 lines (675 loc) · 24.7 KB
/
exkern_inst_engine.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
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
// exkern_inst_engine.cpp : Defines the entry point for the console application.
//
#include "installer.h"
using namespace std;
/*
A perfect combination of C and C++ features are used in this installer engine designed for replacing system files on x64, x86 and IA-64 Windows NT 6.x.
To work with the win32 API, I use char array based strings, but combined with the convenience of vector containers.
*/
BOOL LocalBackupRestore = FALSE;
BOOL DeleteNewFiles;
vector<CHAR*> FileExistingNative;
vector<CHAR*> FileNewNative;
#ifndef _X86_
vector<CHAR*> FileExistingWOW64;
vector<CHAR*> FileNewWOW64;
#endif
CHAR curdir [MAX_PATH];
CHAR windirsys [MAX_PATH];
CHAR windircurrent [MAX_PATH];
#ifndef _X86_
CHAR windirsyswow [MAX_PATH];
#endif
void SetupSystemDirs(CHAR* windir)
{
GetWindowsDirectoryA(windirsys, MAX_PATH);
if(windir != NULL)
{
if(windirsys[0] == windir[0])
LocalBackupRestore = TRUE;
strcpy_s(windirsys, MAX_PATH, windir);
}
#ifndef _X86_
strcpy_s(windirsyswow, MAX_PATH, windirsys);
strcat_s(windirsyswow, MAX_PATH, "\\SysWOW64\0");
#endif
strcat_s(windirsys, MAX_PATH, "\\System32\0");
}
void TakeoverSystemFiles()
/*
Icacls calls wait for the takeown call to end before executing, due to the possibility of
a race condition where the icacls call finishes before the takeown call, causing the former to fail.
*/
{
CHAR TakeownBase [MAX_PATH];
CHAR IcaclsBase [MAX_PATH];
CHAR Bcd [MAX_PATH];
CHAR* IcaclsTemp;
CHAR* Username;
CHAR* TakeownTemp;
DWORD UsernameBuffer = 0;
DWORD ExitCode;
STARTUPINFOA sua;
PROCESS_INFORMATION ProcInfo;
GetStartupInfoA(&sua);
GetUserNameA(Username, &UsernameBuffer);
Username = (CHAR*)malloc(UsernameBuffer);
GetUserNameA(Username, &UsernameBuffer);
strcpy_s(TakeownBase, MAX_PATH, windirsys);
strcpy_s(IcaclsBase, MAX_PATH, windirsys);
strcat_s(TakeownBase, MAX_PATH, "\\takeown.exe /F ");
strcat_s(IcaclsBase, MAX_PATH, "\\icacls.exe \"");
for (size_t i = 0; i < FileExistingNative.size(); i++)
{
TakeownTemp = (CHAR*) malloc(sizeof(CHAR)*MAX_PATH);
strcpy_s(TakeownTemp, MAX_PATH, TakeownBase);
strcat_s(TakeownTemp, MAX_PATH, windirsys);
strcat_s(TakeownTemp, MAX_PATH, "\\");
strcat_s(TakeownTemp, MAX_PATH, FileExistingNative.at(i));
IcaclsTemp = (CHAR*) malloc(sizeof(CHAR)*MAX_PATH);
strcpy_s(IcaclsTemp, MAX_PATH, IcaclsBase);
strcat_s(IcaclsTemp, MAX_PATH, windirsys);
strcat_s(IcaclsTemp, MAX_PATH, "\\");
strcat_s(IcaclsTemp, MAX_PATH, FileExistingNative.at(i));
strcat_s(IcaclsTemp, MAX_PATH, "\" /grant \"");
strcat_s(IcaclsTemp, MAX_PATH, Username);
strcat_s(IcaclsTemp, MAX_PATH, "\":F");
if(!CreateProcessA(NULL, TakeownTemp, NULL, NULL, FALSE, CREATE_PRESERVE_CODE_AUTHZ_LEVEL | CREATE_NO_WINDOW, NULL, NULL,
&sua, &ProcInfo))
{
printf("\nFailed to create process with the following parameters: %s.\nSetup will now exit.\nPress any key to continue.", TakeownTemp);
getchar();
free(TakeownTemp);
free(IcaclsTemp);
exit(-1);
}
while(true)
{
GetExitCodeProcess(ProcInfo.hProcess, &ExitCode);
if(ExitCode != STILL_ACTIVE)
break;
}
CloseHandle(ProcInfo.hProcess);
CloseHandle(ProcInfo.hThread);
if(!CreateProcessA(NULL, IcaclsTemp, NULL, NULL, FALSE, CREATE_PRESERVE_CODE_AUTHZ_LEVEL | CREATE_NO_WINDOW, NULL, NULL,
&sua, &ProcInfo))
{
printf("\nFailed to create process with the following parameters: %s.\nSetup will now exit.\nPress any key to continue.", IcaclsTemp);
getchar();
free(TakeownTemp);
free(IcaclsTemp);
exit(-1);
}
while(true)
{
GetExitCodeProcess(ProcInfo.hProcess, &ExitCode);
if(ExitCode != STILL_ACTIVE)
break;
}
CloseHandle(ProcInfo.hProcess);
CloseHandle(ProcInfo.hThread);
free(TakeownTemp);
free(IcaclsTemp);
}
#ifndef _X86_
for (size_t i = 0; i < FileExistingWOW64.size(); i++)
{
TakeownTemp = (CHAR*) malloc(sizeof(CHAR)*MAX_PATH);
strcpy_s(TakeownTemp, MAX_PATH, TakeownBase);
strcat_s(TakeownTemp, MAX_PATH, windirsyswow);
strcat_s(TakeownTemp, MAX_PATH, "\\");
strcat_s(TakeownTemp, MAX_PATH, FileExistingWOW64.at(i));
IcaclsTemp = (CHAR*) malloc(sizeof(CHAR)*MAX_PATH);
strcpy_s(IcaclsTemp, MAX_PATH, IcaclsBase);
strcat_s(IcaclsTemp, MAX_PATH, windirsyswow);
strcat_s(IcaclsTemp, MAX_PATH, "\\");
strcat_s(IcaclsTemp, MAX_PATH, FileExistingWOW64.at(i));
strcat_s(IcaclsTemp, MAX_PATH, "\" /grant \"");
strcat_s(IcaclsTemp, MAX_PATH, Username);
strcat_s(IcaclsTemp, MAX_PATH, "\":F");
if(!CreateProcessA(NULL, TakeownTemp, NULL, NULL, FALSE, CREATE_PRESERVE_CODE_AUTHZ_LEVEL | CREATE_NO_WINDOW, NULL, NULL,
&sua, &ProcInfo))
{
printf("\nFailed to create process with the following parameters: %s.\nSetup will now exit.\nPress any key to continue.", TakeownTemp);
getchar();
free(TakeownTemp);
free(IcaclsTemp);
exit(-1);
}
while(true)
{
GetExitCodeProcess(ProcInfo.hProcess, &ExitCode);
if(ExitCode != STILL_ACTIVE)
break;
}
CloseHandle(ProcInfo.hProcess);
CloseHandle(ProcInfo.hThread);
if(!CreateProcessA(NULL, IcaclsTemp, NULL, NULL, FALSE, CREATE_PRESERVE_CODE_AUTHZ_LEVEL | CREATE_NO_WINDOW, NULL, NULL,
&sua, &ProcInfo))
{
printf("\nFailed to create process with the following parameters: %s.\nSetup will now exit.\nPress any key to continue.", IcaclsTemp);
getchar();
free(TakeownTemp);
free(IcaclsTemp);
exit(-1);
}
while(true)
{
GetExitCodeProcess(ProcInfo.hProcess, &ExitCode);
if(ExitCode != STILL_ACTIVE)
break;
}
CloseHandle(ProcInfo.hProcess);
CloseHandle(ProcInfo.hThread);
free(TakeownTemp);
free(IcaclsTemp);
}
#endif
free(Username);
strcpy_s(Bcd, MAX_PATH, windirsys);
// This is done for the benefit of the Vista extended kernel.
// Perhaps in future, a list of "jobs" can be included, with variables representing paths that may be
// part of the command line arguments.
strcat_s(Bcd, MAX_PATH, "\\bcdedit.exe /set {current} nointegritychecks yes");
if(!CreateProcessA(NULL, Bcd, NULL, NULL, FALSE, CREATE_PRESERVE_CODE_AUTHZ_LEVEL | CREATE_NO_WINDOW, NULL,
NULL, &sua, &ProcInfo))
{
printf("\nFailed to create process with the following parameters: %s.\nSetup will now exit.\nPress any key to continue.", Bcd);
getchar();
exit(-1);
}
CloseHandle(ProcInfo.hProcess);
CloseHandle(ProcInfo.hThread);
}
void MoveBackupSystemFiles(BOOL Restore)
/*
Intended usage is to move paged/in-memory files so new ones can be copied, or to restore the old versions of files
online after the installer has been run earlier in the session, or to restore backupped files offline.
That is why you can use the "restore back-up" feature even if the system does not meet the
requirements for installation.
*/
{
CHAR* BackupFile;
CHAR* BackupFileBak;
CHAR* InUseFileToBeRemoved;
BOOL Success;
for(size_t i = 0; i < FileExistingNative.size(); i++)
{
BackupFile = (CHAR*) malloc(sizeof(CHAR)*MAX_PATH);
BackupFileBak = (CHAR*) malloc(sizeof(CHAR)*MAX_PATH);
InUseFileToBeRemoved = (CHAR*) malloc(sizeof(CHAR)*MAX_PATH);
strcpy_s(BackupFile, MAX_PATH, windirsys);
strcat_s(BackupFile, MAX_PATH, "\\");
strcat_s(BackupFile, MAX_PATH, FileExistingNative.at(i));
strcpy_s(BackupFileBak, MAX_PATH, BackupFile);
strcat_s(BackupFileBak, MAX_PATH, ".bak");
strcpy_s(InUseFileToBeRemoved, MAX_PATH, windirsys);
strcat_s(InUseFileToBeRemoved, MAX_PATH, "\\");
strcat_s(InUseFileToBeRemoved, MAX_PATH, FileExistingNative.at(i));
strcat_s(InUseFileToBeRemoved, MAX_PATH, ".del");
if(Restore)
{
if(LocalBackupRestore)
{
Success = MoveFileA(BackupFile, InUseFileToBeRemoved);
if(!Success && GetLastError() != 2)
{
printf("\nERROR: Unable to move %s for safe removal", BackupFile);
}
else if(GetLastError() == 2)
{
printf("\nBackup for %s is missing, restoring", BackupFile);
MoveFileA(InUseFileToBeRemoved, BackupFile);
}
else
MoveFileExA(InUseFileToBeRemoved, NULL, MOVEFILE_DELAY_UNTIL_REBOOT);
}
Success = MoveFileExA(BackupFileBak, BackupFile, MOVEFILE_COPY_ALLOWED | MOVEFILE_REPLACE_EXISTING);
// Suppression of the error message when there are no files to back up (newly copied files)
if(!Success && GetLastError() != 2)
printf("\nERROR: Unable to restore %s to %s", BackupFileBak, BackupFile);
}
else
{
Success = MoveFileExA(BackupFile, BackupFileBak, MOVEFILE_COPY_ALLOWED | MOVEFILE_REPLACE_EXISTING);
if(!Success && GetLastError() != 2)
printf("\nERROR: Unable to backup %s to %s", BackupFile, BackupFileBak);
}
free(BackupFile);
free(BackupFileBak);
}
#ifndef _X86_
for(size_t i = 0; i < FileExistingWOW64.size(); i++)
{
BackupFile = (CHAR*) malloc(sizeof(CHAR)*MAX_PATH);
BackupFileBak = (CHAR*) malloc(sizeof(CHAR)*MAX_PATH);
InUseFileToBeRemoved = (CHAR*) malloc(sizeof(CHAR)*MAX_PATH);
strcpy_s(BackupFile, MAX_PATH, windirsyswow);
strcat_s(BackupFile, MAX_PATH, "\\");
strcat_s(BackupFile, MAX_PATH, FileExistingWOW64.at(i));
strcpy_s(BackupFileBak, MAX_PATH, BackupFile);
strcat_s(BackupFileBak, MAX_PATH, ".bak");
strcpy_s(InUseFileToBeRemoved, MAX_PATH, windirsyswow);
strcat_s(InUseFileToBeRemoved, MAX_PATH, "\\");
strcat_s(InUseFileToBeRemoved, MAX_PATH, FileExistingWOW64.at(i));
strcat_s(InUseFileToBeRemoved, MAX_PATH, ".del");
if(Restore)
{
if(LocalBackupRestore)
{
Success = MoveFileA(BackupFile, InUseFileToBeRemoved);
if(!Success && GetLastError() != 2)
{
printf("\nERROR: Unable to move %s for safe removal", BackupFile);
}
else if(GetLastError() == 2)
{
printf("\nBackup for %s is missing, restoring", BackupFile);
MoveFileA(InUseFileToBeRemoved, BackupFile);
}
else
MoveFileExA(InUseFileToBeRemoved, NULL, MOVEFILE_DELAY_UNTIL_REBOOT);
}
Success = MoveFileExA(BackupFileBak, BackupFile, MOVEFILE_COPY_ALLOWED | MOVEFILE_REPLACE_EXISTING);
if(!Success && GetLastError() != 2)
printf("\nERROR: Unable to restore %s to %s", BackupFileBak, BackupFile);
}
else
{
Success = MoveFileExA(BackupFile, BackupFileBak, MOVEFILE_COPY_ALLOWED | MOVEFILE_REPLACE_EXISTING);
if(!Success && GetLastError() != 2)
printf("\nERROR: Unable to backup %s to %s", BackupFile, BackupFileBak);
}
free(BackupFile);
free(BackupFileBak);
}
#endif
for(size_t i = 0; i < FileNewNative.size(); i++)
{
BackupFile = (CHAR*) malloc(sizeof(CHAR)*MAX_PATH);
BackupFileBak = (CHAR*) malloc(sizeof(CHAR)*MAX_PATH);
InUseFileToBeRemoved = (CHAR*) malloc(sizeof(CHAR)*MAX_PATH);
strcpy_s(BackupFile, MAX_PATH, windirsys);
strcat_s(BackupFile, MAX_PATH, "\\");
strcat_s(BackupFile, MAX_PATH, FileNewNative.at(i));
strcpy_s(BackupFileBak, MAX_PATH, BackupFile);
strcat_s(BackupFileBak, MAX_PATH, ".bak");
strcpy_s(InUseFileToBeRemoved, MAX_PATH, windirsys);
strcat_s(InUseFileToBeRemoved, MAX_PATH, "\\");
strcat_s(InUseFileToBeRemoved, MAX_PATH, FileNewNative.at(i));
strcat_s(InUseFileToBeRemoved, MAX_PATH, ".del");
if(Restore)
{
if(LocalBackupRestore)
{
Success = MoveFileA(BackupFile, InUseFileToBeRemoved);
if(!Success && GetLastError() != 2)
{
printf("\nERROR: Unable to move %s for safe removal", BackupFile);
}
else if(GetLastError() == 2 && !DeleteNewFiles)
{
printf("\nBackup for %s is missing, restoring", BackupFile);
MoveFileA(InUseFileToBeRemoved, BackupFile);
}
else
MoveFileExA(InUseFileToBeRemoved, NULL, MOVEFILE_DELAY_UNTIL_REBOOT);
}
Success = MoveFileExA(BackupFileBak, BackupFile, MOVEFILE_COPY_ALLOWED | MOVEFILE_REPLACE_EXISTING);
if(!Success && GetLastError() != 2)
printf("\nERROR: Unable to restore %s to %s", BackupFileBak, BackupFile);
}
else
{
Success = MoveFileExA(BackupFile, BackupFileBak, MOVEFILE_COPY_ALLOWED | MOVEFILE_REPLACE_EXISTING);
if(!Success && GetLastError() != 2)
printf("\nERROR: Unable to backup %s to %s", BackupFile, BackupFileBak);
}
free(BackupFile);
free(BackupFileBak);
}
#ifndef _X86_
for(size_t i = 0; i < FileNewWOW64.size(); i++)
{
BackupFile = (CHAR*) malloc(sizeof(CHAR)*MAX_PATH);
BackupFileBak = (CHAR*) malloc(sizeof(CHAR)*MAX_PATH);
InUseFileToBeRemoved = (CHAR*) malloc(sizeof(CHAR)*MAX_PATH);
strcpy_s(BackupFile, MAX_PATH, windirsyswow);
strcat_s(BackupFile, MAX_PATH, "\\");
strcat_s(BackupFile, MAX_PATH, FileNewWOW64.at(i));
strcpy_s(BackupFileBak, MAX_PATH, BackupFile);
strcat_s(BackupFileBak, MAX_PATH, ".bak");
strcpy_s(InUseFileToBeRemoved, MAX_PATH, windirsyswow);
strcat_s(InUseFileToBeRemoved, MAX_PATH, "\\");
strcat_s(InUseFileToBeRemoved, MAX_PATH, FileNewWOW64.at(i));
strcat_s(InUseFileToBeRemoved, MAX_PATH, ".del");
if(Restore)
{
if(LocalBackupRestore)
{
Success = MoveFileA(BackupFile, InUseFileToBeRemoved);
if(!Success && GetLastError() != 2)
{
printf("\nERROR: Unable to move %s for safe removal", BackupFile);
}
else if(GetLastError() == 2 && !DeleteNewFiles)
{
printf("\nBackup for %s is missing, restoring", BackupFile);
MoveFileA(InUseFileToBeRemoved, BackupFile);
}
else
MoveFileExA(InUseFileToBeRemoved, NULL, MOVEFILE_DELAY_UNTIL_REBOOT);
}
Success = MoveFileExA(BackupFileBak, BackupFile, MOVEFILE_COPY_ALLOWED | MOVEFILE_REPLACE_EXISTING);
if(!Success && GetLastError() != 2)
printf("\nERROR: Unable to restore %s to %s", BackupFileBak, BackupFile);
}
else
{
Success = MoveFileExA(BackupFile, BackupFileBak, MOVEFILE_COPY_ALLOWED | MOVEFILE_REPLACE_EXISTING);
if(!Success && GetLastError() != 2)
printf("\nERROR: Unable to backup %s to %s", BackupFile, BackupFileBak);
}
free(BackupFile);
free(BackupFileBak);
}
#endif
}
void CopyFiles()
{
CHAR* NewFilePath;
CHAR* SourcePath;
for (size_t i = 0; i < FileExistingNative.size(); i++)
{
NewFilePath = (CHAR*) malloc(sizeof(CHAR)*MAX_PATH);
SourcePath = (CHAR*) malloc(sizeof(CHAR)*MAX_PATH);
strcpy_s(NewFilePath, MAX_PATH, windirsys);
strcat_s(NewFilePath, MAX_PATH, "\\");
strcat_s(NewFilePath, MAX_PATH, FileExistingNative.at(i));
strcpy_s(SourcePath, MAX_PATH, curdir);
strcat_s(SourcePath, MAX_PATH, "\\");
strcat_s(SourcePath, MAX_PATH, FileExistingNative.at(i));
if(!CopyFileA(SourcePath, NewFilePath, FALSE))
{
free(NewFilePath);
free(SourcePath);
printf("\n File %s is missing from setup folder or could not be copied. Rolling back changes.",
FileExistingNative.at(i));
MoveBackupSystemFiles(TRUE);
return;
}
free(SourcePath);
free(NewFilePath);
}
#ifndef _X86_
for (size_t i = 0; i < FileExistingWOW64.size(); i++)
{
NewFilePath = (CHAR*) malloc(sizeof(CHAR)*MAX_PATH);
SourcePath = (CHAR*) malloc(sizeof(CHAR)*MAX_PATH);
strcpy_s(NewFilePath, MAX_PATH, windirsyswow);
strcat_s(NewFilePath, MAX_PATH, "\\");
strcat_s(NewFilePath, MAX_PATH, FileExistingWOW64.at(i));
strcpy_s(SourcePath, MAX_PATH, curdir);
strcat_s(SourcePath, MAX_PATH, "\\");
strcpy_s(SourcePath, MAX_PATH, FileExistingWOW64.at(i));
strcat_s(SourcePath, MAX_PATH, ".wow64");
if(!CopyFileA(SourcePath, NewFilePath, FALSE))
{
free(NewFilePath);
printf("\n File %s is missing from setup folder or could not be copied. Rolling back changes.",
SourcePath);
free(SourcePath);
MoveBackupSystemFiles(TRUE);
return;
}
free(NewFilePath);
free(SourcePath);
}
#endif
for (size_t i = 0; i < FileNewNative.size(); i++)
{
NewFilePath = (CHAR*) malloc(sizeof(CHAR)*MAX_PATH);
SourcePath = (CHAR*) malloc(sizeof(CHAR)*MAX_PATH);
strcpy_s(NewFilePath, MAX_PATH, windirsys);
strcat_s(NewFilePath, MAX_PATH, "\\");
strcat_s(NewFilePath, MAX_PATH, FileNewNative.at(i));
strcpy_s(SourcePath, MAX_PATH, curdir);
strcat_s(SourcePath, MAX_PATH, "\\");
strcat_s(SourcePath, MAX_PATH, FileNewNative.at(i));
if(!CopyFileA(SourcePath, NewFilePath, FALSE))
{
free(NewFilePath);
printf("\n File %s is missing from setup folder or could not be copied. Rolling back changes.",
FileNewNative.at(i));
free(SourcePath);
MoveBackupSystemFiles(TRUE);
return;
}
free(NewFilePath);
free(SourcePath);
}
#ifndef _X86_
for (size_t i = 0; i < FileNewWOW64.size(); i++)
{
NewFilePath = (CHAR*) malloc(sizeof(CHAR)*MAX_PATH);
SourcePath = (CHAR*) malloc(sizeof(CHAR)*MAX_PATH);
strcpy_s(NewFilePath, MAX_PATH, windirsyswow);
strcat_s(NewFilePath, MAX_PATH, "\\");
strcat_s(NewFilePath, MAX_PATH, FileNewWOW64.at(i));
strcpy_s(SourcePath, MAX_PATH, curdir);
strcat_s(SourcePath, MAX_PATH, "\\");
strcpy_s(SourcePath, MAX_PATH, FileNewWOW64.at(i));
strcat_s(SourcePath, MAX_PATH, ".wow64");
if(!CopyFileA(SourcePath, NewFilePath, FALSE))
{
free(NewFilePath);
printf("\n File %s is missing from setup folder or could not be copied. Rolling back changes.",
SourcePath);
free(SourcePath);
MoveBackupSystemFiles(TRUE);
return;
}
free(NewFilePath);
free(SourcePath);
}
#endif
}
void InitFileLists()
/*
Filenames are read from a configuration file named setup.conf. There are two separate categories of files:
"existing files" and "new files". Existing files are files that are part of the target version of Windows
and require file ownership to be given to the administrator. New files are novel to the version of Windows
and do not require the same file ownership and permission tasks to be performed.
The (FileExisting/FileNew)Native sections are available on every platform, while only x64 and IA64 installers
make use of the *WOW64 analogs. All files are assumed to be copied to System32 or SysWOW64, as is presently
demanded by the projects that use this installer.
*/
{
FILE* configfile;
CHAR Path [MAX_PATH];
int List = 0;
int fopenerr = fopen_s(&configfile, "setup.conf", "r");
if(fopenerr)
{
printf("\nUnable to load or locate configuration file. Setup will exit.");
getchar();
getchar();
exit(-1);
}
while(!feof(configfile))
{
fscanf_s(configfile, "%s ", Path, MAX_PATH);
if(strstr(Path, "[FileExistingNative]"))
List = 1;
else if(strstr(Path, "[FileNewNative]"))
List = 2;
#ifndef _X86_
else if(strstr(Path, "[FileExistingWOW64]"))
List = 3;
else if(strstr(Path, "[FileNewWOW64]"))
List = 4;
#endif
else
{
if(List == 1)
{
CHAR* Pos = (CHAR*)malloc(sizeof(CHAR)*MAX_PATH);
strcpy_s(Pos, MAX_PATH, Path);
strcat_s(Pos, MAX_PATH, "\0");
FileExistingNative.push_back(Pos);
}
if(List == 2)
{
CHAR* Pos = (CHAR*)malloc(sizeof(CHAR)*MAX_PATH);
strcpy_s(Pos, MAX_PATH, Path);
FileNewNative.push_back(Pos);
}
#ifndef _X86_
if(List == 3)
{
CHAR* Pos = (CHAR*)malloc(sizeof(CHAR)*MAX_PATH);
strcpy_s(Pos, MAX_PATH, Path);
FileExistingWOW64.push_back(Pos);
}
if(List == 4)
{
CHAR* Pos = (CHAR*)malloc(sizeof(CHAR)*MAX_PATH);
strcpy_s(Pos, MAX_PATH, Path);
FileNewWOW64.push_back(Pos);
}
#endif
}
}
fclose(configfile);
/* A listing of the contents in each file section can be printed if the DEBUG flag
is used at compile time.
It is possible that the same data could be printed to a file using a verbose command line switch, or
printed to the command line itself also using a verbose command line switch, if this feature is desired
by users.
*/
#ifdef DEBUG
cout << "File debug listings: " << endl;
for(int i = 0; i < FileExistingNative.size(); i++)
cout << FileExistingNative.at(i) << "\n";
cout << "File NewX64: " << endl;
for(int i = 0; i < FileNewNative.size(); i++)
cout << FileNewNative.at(i) << "\n";
#ifndef _X86_
cout << "File ExistingWOW64: " << endl;
for(int i = 0; i < FileExistingWOW64.size(); i++)
cout << FileExistingWOW64.at(i) << "\n";
cout << "File NewWOW64: " << endl;
for(int i = 0; i < FileNewWOW64.size(); i++)
cout << FileNewWOW64.at(i) << "\n";
#endif
#endif
}
int _tmain(int argc, _TCHAR* argv[])
/*
Name of the product to be installed is gathered from the configuration file, as is the required maximum/minimum
OS build numbers, if necessary. Build numbers are obtained directly from the PEB to best even the most
powerful version spoofers, that is until you load the installer via CreateProcess in a suspended state and
manually modify its PEB before resuming the starting thread.
To emphasize the installer's ability to restore failed installs in situations where the target operating system
install is unbootable, other systems that may not be able to install the product can be used to restore another
system that is capable of using the product.
*/
{
CHAR ProductName [128];
CHAR CurrentConfigPath [MAX_PATH];
CHAR windir [MAX_PATH];
char s;
DWORD BuildNumber;
DWORD MinBuildNumber;
DWORD MaxBuildNumber;
CHAR Test [MAX_PATH];
CHAR winsystemp [MAX_PATH];
HANDLE Check;
GetCurrentDirectoryA(MAX_PATH, curdir);
GetSystemDirectoryA(winsystemp, MAX_PATH);
strcpy_s(CurrentConfigPath, MAX_PATH, curdir);
strcat_s(CurrentConfigPath, MAX_PATH, "\\");
strcat_s(CurrentConfigPath, MAX_PATH, "setup.conf");
#ifndef _X86_
BuildNumber = (DWORD) NtCurrentTeb()->ProcessEnvironmentBlock->Reserved9[23];
#else
BuildNumber = (DWORD) NtCurrentTeb()->ProcessEnvironmentBlock->Reserved9[29];
#endif
BuildNumber = BuildNumber << 16;
BuildNumber = BuildNumber >> 16;
MinBuildNumber = GetPrivateProfileIntA("Config", "MinBuildNumber", 0, CurrentConfigPath);
MaxBuildNumber = GetPrivateProfileIntA("Config", "MaxBuildNumber", (1 << 30) - 1 + (1 << 30), CurrentConfigPath);
GetPrivateProfileStringA("Config", "ProductName", "Product Name Not Defined", ProductName, sizeof(ProductName),
CurrentConfigPath);
/*
Conventional methods of detecting a lack of process elevation were not working well here,
such as GetPrivilegeName() consistently providing me with an error code related to asynchronous I/O issues.
So we have to create a small file in system32 then delete it immediately. If it cannot create that file, then
the privileges are insufficient to run the installer.
*/
strcpy_s(Test, MAX_PATH, winsystemp);
strcat_s(Test, MAX_PATH, "\\");
strcat_s(Test, MAX_PATH, "test");
DeleteFileA(Test);
SetLastError(0);
Check = CreateFileA(Test, GENERIC_WRITE, 0, NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_SYSTEM, NULL);
if(GetLastError() == 5)
{
printf("\nInsufficient permissions to run %s setup. Please right-click \"run as administrator\" and try again.",
ProductName);
getchar();
getchar();
return 0;
}
else
{
CloseHandle(Check);
DeleteFileA(Test);
}
#ifdef _X86_
BOOL Wow64Process;
if(IsWow64Process(GetCurrentProcess(), &Wow64Process))
{
printf("The %s setup package is intended for an x86 version of Windows.", ProductName);
printf("\nOffline backup restoration is still possible. To restore a backup, press 'R'.\n");
scanf_s("%c", &s, 1);
if(s == 'r' || s == 'R')
goto Restore;
return 0;
}
#endif
if(BuildNumber < MinBuildNumber || BuildNumber > MaxBuildNumber)
{
printf("An OS build between %d and %d is required to run %s setup.", MinBuildNumber, MaxBuildNumber, ProductName);
printf("\nOffline backup restoration is still possible. To restore a backup, press 'R'.\n");
scanf_s("%c", &s, 1);
if(s == 'r' || s == 'R')
goto Restore;
return 0;
}
DeleteNewFiles = GetPrivateProfileIntA("Config", "DeleteNewFiles", 0, CurrentConfigPath);
printf("This is %s setup. \nPress 'R' for restore. \nPress all other keys for setup.\n",
ProductName);
scanf_s("%c", &s, 1);
InitFileLists();
if(s == 'r' || s == 'R')
{
Restore:
printf("Provide the path of the Windows directory that you want to restore.\n");
scanf_s("%s", windir, MAX_PATH);
SetupSystemDirs(windir);
MoveBackupSystemFiles(TRUE);
printf("\nRestoration of backed-up files is complete\nPress any key to continue.");
getchar();
getchar();
return 0;
}
SetupSystemDirs(NULL);
TakeoverSystemFiles();
MoveBackupSystemFiles(FALSE);
CopyFiles();
printf("\n%s setup is complete.\nPress any key to continue.", ProductName);
getchar();
getchar();
return 0;
}