/
AChoir.cpp
13052 lines (10995 loc) · 407 KB
/
AChoir.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
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/****************************************************************/
/* "As we benefit from the inventions of others, we should be */
/* glad to share our own...freely and gladly." */
/* - Benjamin Franklin */
/* */
/* AChoir v0.01 - First Version (D0n Quix0te 05/30/15) */
/* AChoir v0.02 - Add Variables: &Dir &Fil &Acq &Win */
/* AChoir v0.03 - Add Hashing */
/* AChoir v0.04 - Add FOR:, &FOR, &NUM Looping */
/* AChoir v0.05 - Add CKY:, CKN:, RC=:, RC!:, RC>:, RC<:, END: */
/* &CHK, &RCD */
/* AChoir v0.06 - Add Logging */
/* AChoir v0.07 - Add /BLD (Build.Acq), /DRV:, &Prc, 32B:, 64B: */
/* BYE: */
/* AChoir v0.08 - Hash Program before running,Set Artifacts ROS */
/* AChoir v0.09 - Create Index.html for Artifact Browsing */
/* AChoir v0.10 - Mapping External Drives - Set to the ACQDir */
/* AChoir v0.11 - New &Map variable and INI: action */
/* INP: action and &Inp variable (Console Input) */
/* AChoir v0.13 - New &Tmp is the Window %Temp% variable */
/* New CPY: Action to copy files */
/* New &FNM variable - Each &FOR File Name */
/* AChoir v0.20 - Lets call this 2.0-Lots of Code improvements */
/* AChoir v0.21 - Fix GMT DST idiosyncracy */
/* AChoir v0.22 - New ARN: Action - */
/* Parse the Run Key and copy the Autorun EXEs */
/* AChoir v0.23 - /MNU Command Line Option Runs Menu.ACQ */
/* AChoir v0.24 - Expand the ARN: routine to recognize WOW64 */
/* and System32/sysnative wierdness */
/* AChoir v0.25 - More improvements to Run Key Extract */
/* AChoir v0.26 - Expand system variables %variable% */
/* AChoir v0.27 - More improvements in remote acquisition (Map) */
/* AChoir v0.28 - Add /MAP: /USR: and /PWD: command lines */
/* and MAP: USR: and PWD: INI file Actions */
/* to enable Mapping for Remote Acquisition */
/* AChoir v0.29 - Add ADM:Check and ADM:Force to check OR */
/* enforce that AChoir be run from an ADMIN ID */
/* - Converted to MSVC - Also replaced libCurl */
/* with MS WinHTTP APIs */
/* AChoir v0.30 - Improve CPY: - Prevent Overwriting Files */
/* AChoir v0.31 - Start and End Time Stamps and &Tim variable */
/* AChoir v0.32 - Changes to support 32 and 64 Bit versions! */
/* AChoir v0.33 - Turn On/Off USB Write Protect */
/* AChoir v0.34 - Internal Code Cleanup */
/* AChoir v0.35 - Add DRV: Action to Set &Drv */
/* AChoir v0.36 - Add Variables 0-9 (VR0: - VR9:) (&VR0 - &VR9) */
/* - Fix wierd Win7 "Application Data" Path */
/* Recursion Anomoly */
/* AChoir v0.37 - Remove DST Calculation - Add Checks to CPY: */
/* AChoir v0.38 - New DST Convergence Code */
/* AChoir v0.39 - Add LBL: and JMP: for Conditional Execution */
/* AChoir v0.40 - Add XIT: <Exit Command - Run on Exit> */
/* AChoir v0.41 - Offline Registry parse of AutoRun Keys */
/* for DeadBox analysis */
/* AChoir v0.42 - Change HTML display to only Root Folder */
/* AChoir v0.43 - Match DLL Delay Loading to &Dir Directory */
/* AChoir v0.44 - Fix root folder edge case */
/* AChoir v0.50 - Add CMD: - Like SYS: But uses a CMD.Exe shell */
/* In &Dir - Check Hash for AChoir ReactOS Shell */
/* AChoir v0.55 - Add LST: - Looping Object (&LST) that reads */
/* entries from a file. Also Add SID (file */
/* owner) copy on the CPY: command. */
/* AChoir v0.56 - Improve Privileges Message Display */
/* AChoir v0.57 - Fix Priv Bug & Add better Error Detection */
/* AChoir v0.75 - Add NTFS Raw Copy (NCP:) */
/* NCP:<Wilcard File Search> <Destination Dir> */
/* - Additional Recursion Error Checking */
/* AChoir v0.80 - NTFS Raw Reading now support Attribute List */
/* (Multiple Cluster Runs/Fragmented Files) */
/* AChoir v0.81 - More NTFS Raw Read honing */
/* AChoir v0.82 - Add MAX: - Max File Size (& Mem Usage) */
/* AChoir v0.83 - Add RawCopy to ARN: */
/* AChoir v0.85 - Can now Read POSIX file names & Hard Links */
/* AChoir v0.89 - Large File (> 1GB) Support */
/* AChoir v0.90 - ADD HKCU Parsing for ARN: */
/* AChoir v0.91 - Edge case exit Bug Fix */
/* AChoir v0.92 - Sig:<Typ=xxxx> Load File Type, Hex Signature */
/* - NCS: NTFS Copy by Signature */
/* (Used together to copy Files by Signature) */
/* AChoir v0.93 - Refactored some SQLite Code to avoid random */
/* Heap Corruption issues */
/* AChoir v0.95 - FINALLY Fix Abend Bug in Large File Support */
/* - Got rid of the other attempts to fix it */
/* - NOTE: v0.95 will be slower than previous */
/* Versions. I opted for slower and safer code */
/* with a smaller memory footprint. */
/* AChoir v0.96 - Clean Up some of the code, improve output. */
/* AChoir v0.96a- Cosmetic changes to Index.htm */
/* AChoir v0.97 - Add Colors, Minor Bug Fixes */
/* AChoir v0.98 - CPS: Copy by Signature (Standard Win32 API) */
/* (Used with SIG: to copy Files by Signature) */
/* - Not Recommended for Locked/System Files */
/* - Tighten Application Data recursion to 2 lvls */
/* - /Con or /ini:Console - Console as Input File */
/* AChoir v0.98a- Various improvements to Interactive Mode */
/* - Replace conditional statements with messages*/
/* - add INI:Console to Scripting */
/* - Improve switching between Script and */
/* Interactive Modes */
/* AChoir v1.0 - Cosmetic USB Message Changes */
/* - HTTP Get Bug Fixes, Fix &Acq dblSlash */
/* - Add Optional Case & Evidence Name/Number Input*/
/* - CSE:GET and CSE:SAY */
/* - /CSE Argument to Get Case Information */
/* - VCK:<x:\> NTFS, FAT32, CDFS, Other, None */
/* - &VCK - Contains Results of VCK: */
/* - EQU:<s1> <s2> - Are S1 and S2 Equal? */
/* - NEQ:<s1> <s2> - Are S1 and S2 NOT Equal? */
/* - Support Indenting (spaces or Tabs) */
/* - DSK:<type> Set &DSK looping variable to */
/* - Types: Removable, Fixed, Remote, CDROM */
/* - &DSK - Looping Var Contains Disk that match */
/* AChoir v1.1 - Peppered Flush STDOUT buffers for better */
/* PSExec Display (Remote Acq) */
/* - SHR:<Path> <Name> - Create a Local Share */
/* - SHD:<Name> - Delete a Local Share */
/* AChoir v1.2 - Add /USR:? and /PWD:? - Query MAP USR and PWD */
/* - Replaced getch() with getchar(). This is */
/* because PsExec does not work with getch(). */
/* PsExec also does not work with SetConsoleMode */
/* so there is no way to do hidden/masked */
/* password input. */
/* AChoir v1.3 - Implement NTP Client for Querying Time Drift */
/* - Fix minor display bug when using &Tim */
/* AChoir v1.4 - New Actions to Hide and Reconnect the Console */
/* - CON:Hide and CON:Show */
/* - SLP:<Sec> Sleep for <Sec>Seconds */
/* AChoir v1.5 - Add /VR0: -/VR9: Command Line Parameters */
/* - When BaseDir changes, change Windows CWD too */
/* - New Redaction Routine for PWD: EXE: CMD: */
/* AChoir v1.6 - Add EXA: and EXB: (Asyn & Background EXe) */
/* AChoir v1.7 - Fix DSK: &DSK bug for Remote Collections */
/* File not being properly closed causes loop. */
/* AChoir v1.8 - Recognize Compressed Files, and allow them to */
/* be copied by the OS API to DeCompress them */
/* The Flag for this behaviour is: */
/* SET:NCP=OSCOPY or SET:NCP=RAWONLY */
/* - Also Added built in Support for WOW64 file */
/* redirection of X86 binCopy of SYSTEM32 */
/* (sub) directories. This was needed for */
/* switching from rawcopy to bincopy - plus its */
/* a good general feature anyway. */
/* AChoir v1.9 - Recognize Compressed Size */
/* AChoir v1.9a - More Comressed Files Support */
/* AChoir v2.0 - Add LZNT1 Decompress Routine */
//* Flag behaviors have changed: */
/* SET:NCP=NODCMP - NoDecompression */
/* SET:NCP=DECOMP/RAWONLY - LZNT1 Decompress */
/* SET:NCP=OSCOPY - Do OS/API copy on Decomp Err*/
/* AChoir v2.1 - Add App Compat Manifest - For 8.1 and above */
/* comaptibility */
/* Add new Conditional Logic on Windows Version */
/* VER:WinXP, WinXP64, Vista, Win7, Win8, Win8.1 */
/* Win10 */
/* Win2000, Win2003, Win2008, Win2008R2, */
/* Win2012, Win2012R2, Win2016 */
/* AChoir v2.2 - Add Ver: Client, and Server checks */
/* AChoir v2.3 - LZNT1 Bug fixes by Yogesh Katri */
/* AChoir v2.4 - Update Offreg, and fix Edge Case of Short FN */
/* without a Long FN in $MFT record. */
/* AChoir v2.5 - Partial Back out of LZNT1 mod that negatively */
/* impacted $MFT Resident File extraction */
/* AChoir v2.6 - Fix Duplicate File copy due to multiple MFT */
/* Records for a file (Hard Links) */
/* AChoir v2.7 - Additional Messages for Looping */
/* AChoir v2.8 - Add ability to preserve Paths in CPY: and NCP:*/
/* Set:CopyPath=Full/Partial/None */
/* - Allow ACQ: and DIR: to create nested paths */
/* AChoir v2.9 - Fix FOR: without backslash (Current Dir only) */
/* Move Get: to its own routine to allow new */
/* /Get: option - This will function as a way */
/* to allow AChoir to load an INI file remotely */
/* AChoir v3.0 - Added &MEM (Total memory), &DSA (Disk Avail) */
/* Added N>>:, N<<:, and N==: For NUMBERS ONLY */
/* comparison. Note: All numbers are converted */
/* internally to longlong (atoll) */
/* These can be used together to see if we have */
/* enough disk space to capture memory i.e. */
/* N>>:&DSA &MEM */
/* AChoir v3.1 - Expand Available Disk Checking into File */
/* Copy/Extract */
/* AChoir v3.2 - &DSA Should Really Point to the &ACQ Drive */
/* in case we use MAP: */
/* Added Experimental Sysloog Output and new */
/* Settings: */
/* SET:SYSLOGS=<Syslog Server IP> */
/* SET:SYSLOGP=<Syslog Port> */
/* SET:SYSLOGL=None, Min, Max */
/* AChoir v3.3 - Expand syslogging */
/* AChoir v3.4 - Add Set:MapErr=Continue, Fail, Query) */
/* AChoir v3.5 - Add &CNR, &CN0-CN9, CN++, CN-- (Counters) */
/* &Acn (Acquisition Name) */
/* Add Set:Trim=<Yes> or <No> (Default is Yes) */
/* Trims &FOR and &LST since DOS File Redirects */
/* OFTEN add erroneous spaces */
/* AChoir v3.6 - Add SET:DELIMS= (Sets the Parsing Delimiters) */
/* &LS0-&LS9 (Parses the first 10 Cols in &LST) */
/* &FO0-&FO9 (Parses the first 10 Cols in &FOR) */
/* AChoir v3.7 - Add WildCard to CPY: (No Longer needs FOR: */
/* to do multiple file copy) */
/* Add SET:CopyDepth=nn - Set Maximum Directory */
/* for CPY: (Does not work win NCP:) - This */
/* will help speed up copying by preventing */
/* unnecessary depth (Default is 10 SubDirs) */
/* AChoir v3.8 - Better mkdir Processing (Error Correction) */
/* - Better Support for MAP: (Sets Target Dirs) */
/* Set:Cache=<local> or <Movable> - Speed */
/* enhancement to keep the Cache local to the */
/* target machine - Use with caution. */
/* AChoir v3.9 - Cut down on the Display Messages */
/* CON:MSGLevel=<min>, <std>, <max>, <debug> */
/* min=What it is doing */
/* std= What it is doing and results (default) */
/* max= What it is doing and expanded results */
/* debug= Same as max for now */
/* AChoir v4.0 - No changes - Releasing v4.0 in honor of the */
/* Mr. Robot Season 4 premier 10/06/2019 */
/* AChoir v4.1 - Add OPN: Opens a file for output, if a file */
/* is already OPN, it will be closed. Only one */
/* file can be OPN at a time. */
/* - Add OUT: Action - Appends a string to the */
/* OPN: File */
/* - Expand parsing to &LSA - &LSP and &FOA - &FOP */
/* - Add Experimental Unicode File Processing */
/* - Only UTF-16 (Big & Little Endian) */
/* AChoir v4.2 - Make Log File consistent (set to ACQName) */
/* AChoir v4.3 - Added &HST Variable (Host Name) */
/* AChoir v4.4 - Improve Parsing to recognize dbl-quotes */
/* AChoir v4.5 - Updates carried over from AChoirX 10.00.38 */
/* - Change conditional logic to only count a */
/* single occurance of &FOR and &LST comparisons */
/* This prevents the need for multiple END: */
/* statements - Multiple comparisons only get */
/* a single hit if ANY match is found. */
/* THIS IS IMPORTANT!! Wherever &FOR and &LST */
/* are used in CONDITIONAL LOGIC - A SINGLE HIT */
/* WILL BE TRUE. To Test for INDIVIDUAL cases */
/* use a specific check and NOT a Check Against */
/* a list (&LST, &FOR). */
/* - Expand &FOR and &LST Support to more Actions */
/* - Add HSH:<Filename> Will put the File hash in */
/* the &HSH Variable (Only supports a single */
/* File for now) */
/* - Implement END:Reset to clear any Dangling */
/* ENDs. Use Judiciously. */
/* */
/****************************************************************/
/* rc=0 - All Good */
/* rc=1 - Bad Input */
/* rc=2 - Bad Execution */
/* rc=3 - Internal Error */
/* */
/****************************************************************/
/****************************************************************/
/* IMPORTANT NOTE : I could not have implemented the NTFS */
/* Raw Copy function without the Excellent NTFS tutorial at: */
/* http ://www.installsetupconfig.com/win32programming/ */
/* windowsvolumeapis1index.html */
/* */
/* Much of the code in the Achoir NTFS Raw Copy function is */
/* directly from this tutorial.And I want to publicly thank */
/* them for making this example code available. */
/****************************************************************/
/****************************************************************/
/* IMPORTANT NOTE : I could not have implemented the NTFS */
/* LZNT1 Decode without the awesome LZNT1 code found in ReactOS */
/* hxxps://doxygen.reactos.org/d0/dd2/ */
/* sdk_2lib_2rtl_2compress_8c_source.html */
/* */
/* Much of the code in the Achoir LZNT1 decompressor function */
/* is directly from this code.And I want to publicly thank */
/* them for making this amazing code available. */
/****************************************************************/
#include "stdafx.h"
//Visual Leak Detector (In Debug)
//#include "VLD.h"
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <errno.h>
/* #ifndef NO_UNISTD
#include <unistd.h>
#endif NO_UNISTD
*/
#include <conio.h>
#include <time.h>
#include <io.h>
#include <direct.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <process.h>
#include <windows.h>
#include <winbase.h>
#include "md5.h"
#include <winhttp.h>
#include <Winnetwk.h>
#include <Offreg.h>
#include "ntfs.h"
#include "sqlite3.h"
//Make new operator act like malloc
#include <new>
// #pragma comment (lib, "offreg.lib")
// #pragma comment(lib, "cmcfg32.lib")
#pragma comment(lib, "Advapi32.lib")
#include "accctrl.h"
#include "aclapi.h"
#include <sddl.h>
// Required for Net Share creation
#pragma comment(lib, "Netapi32.lib")
#include <lm.h>
// Required for Winsock
#include <winsock.h>
// NTP Client Integer Types
#include <stdint.h>
#define NTP_TIMESTAMP_DELTA 2208988800ull
// Headers for determining OS Version
#include <VersionHelpers.h>
#define NUL '\0'
#define MaxArray 100
#define BUFSIZE 4096
char Version[10] = "v4.5\0";
char RunMode[10] = "Run\0";
int iRanMode = 0;
int iRunMode = 0;
int iHtmMode = 0;
int iChkYN = 0;
int iChkRC = 0;
int iIsAdmin = 0;
int iExec = 0;
int iIsCompressed = 0;
char cIsCompressed[15] = "\0";
int iCDepth = 0; // CopyDepth Counter
int iCacheType = 0; // Movable Cache (Default)
int iIsUnicode = 0; // Check file for UniCode BOM (0=No, 2=LittleEndian, 1=BigEndian)
int LstRC = 0; // Return Code for Reading LST: Record
char LastHash[50] = "none"; // Last Single File Hash
int NotFound = 0; // Flag for ensuring that only one Found Rec Increments RunMe
int YesFound = 0; // Flag for ensuring that only one Found Rec Increments RunMe
int setNCP = 2; // 0=NODCMP, 1=DECOMP/RAWONLY, 2=OSCOPY (Default)
int setCPath = 0; // 0=None, 1=Partial, 2=Full
int setMapErr = 0; // 0=Continue, 1=Query, 2=Fail
int setTrim = 1; // 0=NoTrim, 1=Trim (Default Trim the read records &For and &Lst)
int setCDepth = 10; // Maximum CPY: Directory Depth (0 = Unlimited) - Default is 10
int setMSGLvl = 2; // Display Message Level - Default=2 (med)
char Delims[10] = ",\0\0\0\0\0\0\0\0";
char *TokPtr;
int TokCnt;
int TokMax;
int TokIdx;
int TokCur;
int TokOnOff;
int iNative = 0; // Are we Native 64Bit on 64Bit (Native = 1, NonNative = 0)
char sNative[10] = "\0";
char ACQName[255];
char ACQDir[1024];
char BACQDir[1024];
char CachDir[1024];
char buffer[BUFSIZE];
char filename[FILENAME_MAX];
int verboseflag = 0;
int DebugFlag = 0;
int ListDir(char *DirName, char *LisType);
size_t Squish(char *SqString);
size_t unIndent(char *SqString);
size_t LRTrim(char *SqString);
size_t UA_fgets(char *UA_string, int UA_maxlen, FILE* UA_handle, int UA_AscOrUni);
long twoSplit(char *SpString);
char *stristr(const char *String, const char *Pattern);
int FileMD5(char *MD5FileName);
int MemAllocErr(char *ErrType);
int DirAllocErr(char *DirToCreat);
int binCopy(char *FrmFile, char *TooFile, int binLog);
int lznCopy(char *FrmFile, char *TooFile, ULONG TooSize);
void Time_tToFileTime(time_t InTimeT, int whichTime);
long varConvert(char *inVarRec);
long consInput(char *consString, int conLog, int conHide);
long mapsDrive(char *mapString, int mapLog);
long netLocalShare(char *netServer, char *netSharePath, char *netShareName, int shrLog);
long netShareDel(char *netShareName, int shrLog);
int PreIndex();
BOOL IsUserAdmin(VOID);
void showTime(char *showText);
void USB_Protect(DWORD USBOnOff);
void cleanUp_Exit(int exitRC);
BOOL SetPrivilege(HANDLE hToken, LPCTSTR lpszPrivilege, BOOL bEnablePrivilege);
char * convert_sid_to_string_sid(const PSID psid, char *sid_str);
void getCaseInfo(int SayOrGet);
int ntpGetTime(char* ntpServer);
size_t Redactor(char *inRedact, char *outRedact);
BOOL CompareWindowsVersion(DWORD dwMajorVersion, DWORD dwMinorVersion);
// Variables to create a share
int iGoodShr = 0;
NET_API_STATUS netShrRC;
SHARE_INFO_2 netShr;
DWORD netShrErr = 0;
// Routines For Raw NTFS Access
ULONG RunLength(PUCHAR run);
LONGLONG RunLCN(PUCHAR run);
ULONGLONG RunCount(PUCHAR run);
BOOL FindRun(PNONRESIDENT_ATTRIBUTE attr, ULONGLONG vcn, PULONGLONG lcn, PULONGLONG count);
PATTRIBUTE FindAttributeX(PFILE_RECORD_HEADER file, ATTRIBUTE_TYPE type, PWSTR name, int attrNum);
VOID FixupUpdateSequenceArray(PFILE_RECORD_HEADER file);
VOID ReadSectorX(ULONGLONG sector, PVOID buffer);
VOID ReadSectorToMem(ULONGLONG sector, ULONG count, PVOID buffer);
VOID ReadSectorToDisk(ULONGLONG sector, ULONG count, PVOID buffer);
VOID ReadLCN(ULONGLONG lcn, ULONG count, PVOID buffer);
VOID ReadExternalAttribute(PNONRESIDENT_ATTRIBUTE attr, ULONGLONG vcn, ULONG count, PVOID buffer);
ULONG AttributeLength(PATTRIBUTE attr);
ULONG AttributeLengthAllocated(PATTRIBUTE attr);
ULONG AttributeLengthDataSize(PATTRIBUTE attr);
ULONG AttributeLengthCompressed(PATTRIBUTE attr);
VOID ReadAttribute(PATTRIBUTE attr, PVOID buffer);
VOID ReadVCN(PFILE_RECORD_HEADER file, ATTRIBUTE_TYPE type, ULONGLONG vcn, ULONG count, PVOID buffer);
VOID ReadFileRecord(ULONG index, PFILE_RECORD_HEADER file);
VOID LoadMFT();
VOID UnloadMFT();
int FindActive();
int rawCopy(char *FrmFile, char *TooFile, int binLog);
int DumpDataII(ULONG index, CHAR* filename, CHAR* outdir, FILETIME ToCreTime, FILETIME ToModTime, FILETIME ToAccTime, int binLog, int Append);
int ExpandDirs(CHAR* FullDirName);
int HTTP_GetFile(char *HTTPGet_URL, char *HTTPGet_FileName);
//int spinOnChange(char* SpinFileName);
long long CheckDiskSpace(char *DiskToCheck);
long long CheckMemSpace();
int AChSyslog(char* SendLogMSG);
// Global Variables For Raw NTFS Access
ULONG BytesPerFileRecord;
HANDLE hVolume;
BOOT_BLOCK bootb;
PFILE_RECORD_HEADER MFT;
int readRetcd = 1; // Global read Return Code - When something goes real bad.
char Str_Temp[1024] = "\0";
char Str_Short[256] = "\0";
CHAR driveLetter[] = "C\0\0\0\0";
CHAR rootDrive[] = "C:\\\0\0\0";
char SidString[256];
ULONGLONG File_CreDate, File_AccDate, File_ModDate;
int gotOwner = 0;
// Static Security Descriptor Buffer
PSECURITY_DESCRIPTOR SecDesc[255];
// Dynamic Security Descriptor Buffer
//PSECURITY_DESCRIPTOR SecDesc = NULL;
//Start with Max Memory Alloc = 250MB
ULONG maxMemBytes = 262144000;
int maxMemExceed = 0;
int useDiskOrMem = 0; // 0 is Memory, 1 is Disk
// Global Variables For SQLite Databases
int SpinLock;
int MFT_Status = 0; // 0=Good, 1=NonFatal Error, 2=FatalError
char *dbMQuery; char *errmsg = 0; int dbMrc;
char *dbXQuery; int dbXrc; int dbXi;
int dbrc, dbMaxCol, dbRowCount, dbi;
char MFTDBFile[1024] = "C:\\AChoir\\Cache\\C-MFT.db\0";
sqlite3 *dbMFTHndl;
sqlite3_stmt *dbMFTStmt;
sqlite3_stmt *dbXMFTStmt;
FILE* LogHndl;
FILE* MCpHndl;
FILE* ForHndl;
FILE* LstHndl;
FILE* DskHndl;
FILE* MD5Hndl;
FILE* IniHndl;
FILE* WGetHndl;
FILE* HtmHndl;
FILE* OPNHndl;
char LogFile[1024] = "C:\\AChoir\\AChoir.exe\0";
char CpyFile[1024] = "C:\\AChoir\\AChoir.exe\0";
char ChkFile[1024] = "C:\\AChoir\\AChoir.exe\0";
char MD5File[1024] = "C:\\AChoir\\Hashes.txt\0";
char ForFile[1024] = "C:\\AChoir\\ForFiles\0";
char MCpFile[1024] = "C:\\AChoir\\MCpFiles\0";
char ForDisk[1024] = "C:\\AChoir\\ForDisks\0";
char LstFile[1024] = "C:\\AChoir\\LstFiles\0";
char IniFile[1024] = "C:\\AChoir\\AChoir.ACQ\0";
char HtmFile[1024] = "C:\\AChoir\\Index.html\0";
char CmdExe[1024] = "C:\\AChoir\\cmd.exe\0";
char CmdHash[35] = "d05c529f0eebb6aaf10cbdecde14d310\0";
char TempDir[1024] = "C:\\AChoir\0";
char BaseDir[1024] = "C:\\AChoir\0";
char CurrDir[1024] = "\0";
char CurrWorkDir[1024] = "\0";
char CurrFil[255] = "AChoir.dat\0";
char DiskDrive[10] = "C:\0\0\0";
char MapDrive[10] = "C:\0\0\0";
char *WinRoot = "C:\\Windows";
char *Procesr = "AMD64";
char *TempVar = "C:\\Windows\\Temp";
char *ProgVar = "C:\\Program Files";
char CrLf[3] = {0x0D, 0x0A, 0x00};
int iLogOpen = 0 ;
int iOPNisOpen = 0;
HANDLE SecTokn;
int PrivSet = 0;
int PrivOwn = 0;
int PrivSec = 0;
int PrivBac = 0;
int PrivRes = 0;
int WGetIni, WGetIsGood, WGotIsGood;
size_t lastChar;
char *iWGetFIL;
char WGetFile[1024] = "C:\\AChoir\\Achoir.dat\0";
char WGetURL[1024] = "http://127.0.0.1/AChoir/AChoir.dat\0";
char WGetDOM[1024] = "http://127.0.0.1";
char WGetFIL[1024] = "/AChoir/AChoir.dat\0";
wchar_t w_WGetURL[2028];
wchar_t w_WGetFIL[2028];
LPWSTR lpWGetURL = w_WGetURL;
LPWSTR lpWGetFIL = w_WGetFIL;
char FileRoot[1024] = "C:\\InetPub\\wwwroot\0";
char FullRoot[1024] = "C:\\InetPub\\wwwroot\0";
char LastRec[2048];
char ThisRec[2048];
char cpyChar;
int iVar;
char VarArray[2560]; // Ten 256 Byte Variables (&Var0 - &Var9)
int iCnt;
int CntArray[10]; // Ten Counter Variables (&Cn0 - &Cn9)
int iMaxCnt = 0; // Maximum Record Count (Set by FOR:, LST: &FOR, &LST)
int iMonth, iDay, iYear, iHour, iMin, iSec, iYYYY;
time_t timeval;
struct tm *lclTime;
char FullDateTime[25] = "01/01/0001 - 01:01:01\0";
char OldDate[30] = "\0";
struct tm *Old_CTime;
struct tm *Old_MTime;
struct tm *Old_ATime;
char MD5In1[256] = "\0";
char MD5In2[256] = "\0";
char MD5Out[256] = "\0";
char MD5Tmp[256] = "\0";
int iNeedSize;
int iLeftSize;
char *recvData;
char *recvTemp;
int recvSize = 25000;
int TermRC = 0;
int LastRC = 0;
int ChkRC = 0;
char *ExePtr, *ParmPtr, *CopyPtr;
char volType[10] = " \0";
char RootDir[FILENAME_MAX] = " \0";
char FullFName[FILENAME_MAX];
char ForFName[FILENAME_MAX];
char MCpFName[FILENAME_MAX];
DWORD netRC = NO_ERROR;
NETRESOURCE netRes = { 0 };
TCHAR szConnection[MAX_PATH];
DWORD ConnectSize = MAX_PATH, ConnectResult, Flags = (CONNECT_INTERACTIVE | CONNECT_REDIRECT);
size_t iPrm1, iPrm2, iPrm3;
char *iPtr1, *iPtr2, *iPtr3;
struct _stat Frmstat;
struct _stat Toostat;
FILETIME TmpTime;
FILETIME ToCTime;
FILETIME ToMTime;
FILETIME ToATime;
LPFILETIME OutFileTime;
LPCTSTR lpSubKey = TEXT("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run\0");
DWORD rcDword;
DWORD nSubkeys;
DWORD nValues;
PCWSTR ORlpSubKey = L"Microsoft\\Windows\\CurrentVersion\\Run\0";
PCWSTR ORlp6432 = L"Wow6432Node\\Microsoft\\Windows\\CurrentVersion\\Run\0";
char WDLLPath[256];
DWORD ulOptions = 0;
REGSAM samWOW64 = KEY_READ | KEY_WOW64_64KEY;
REGSAM samWOW32 = KEY_READ | KEY_WOW64_32KEY;
REGSAM samDesired = KEY_READ;
long OpenK;
long OpenRC;
long ReadK;
long MakeK;
HKEY hKey = HKEY_LOCAL_MACHINE;
HKEY hKCU = HKEY_CURRENT_USER;
ORHKEY ORhKey = HKEY_LOCAL_MACHINE;
HKEY phkResult;
ORHKEY ORphkResult;
size_t sizeofChars = 0;
size_t convertedChars = 0;
wchar_t w_ORFName[2028];
LPWSTR lpORFName = w_ORFName;
DWORD dwIndex = 0;
TCHAR lpValueName[2048];
DWORD lpcchValueName = 2048;
LPTSTR lpData[2048];
DWORD lpcbData = 2048;
wchar_t w_lpValueName[2048];
PWSTR ORlpValueName = w_lpValueName;
int samLoop = 0;
char o32VarRec[4096];
char o64VarRec[4096];
int i64x32;
char Inrec[4096];
char Inprec[255];
char Conrec[255];
char inUser[255];
char inPass[255];
char inMapp[255];
char inFnam[255];
char JmpLbl[255];
int iGoodMap = 0;
int iArgsMap = 0;
int getKey;
int iSleep;
int iXitCmd = 0;
char XitCmd[4096];
//Track Current File Information across Routines
int fileIsFrag;
ULONG totbytes, totdata;
ULONG maxFileSize, leftFileSize;
ULONG maxDataSize, leftDataSize;
int LCNType = 0; // 0 for Attributes, 1 for Files (used for tracking leftFileSize)
int iDepth = 0; // Sanity Check for Recursion Loops
//File Signature Copy Table & Vars
int iSigCount = 0;
int iSigTMax = 100;
int iSigSize = 33; // One Extra byte for null terminator
int iTypSize = 11; // One Extra byte for null terminator
char *SigTabl;
char *TypTabl;
int *SizTabl;
char * equDelim ;
char tmpSig[255];
int tmpSize;
int iNCS = 0;
int iNCSFound = 0; // 0==Found, 1==Not
int iCPS = 0;
int iCPSFound = 0; // 0==Found, 1==Not
PUCHAR ClustZero; // First Cluster buffer
// Console Hande for Hide, Show
HWND conHndl;
int iConMode = 1 ;
// Console Input instead of File
int consOrFile = 0;
// Console Coloring
void consPrefix(char *consText, int consColor);
HANDLE hConsole;
char consTemp[10] ;
int consBlu = 11 ;
int consGre = 10 ;
int consRed = 12 ;
int consYel = 14 ;
int consWhi = 15 ;
// Case Information (ONLY ONCE!)
// 0 = Not Entered, 1 = Entered, 2 = /CSE Arg
int iCase = 0 ;
char caseNumbr[255] ;
char evidNumbr[255] ;
char caseDescr[255] ;
char caseExmnr[255] ;
// DSK Variables
int dskNum ;
int dskTyp ;
int DskMe ;
char dskNam[10] = "A:\\\0";
char Dskrec[10] = "A:\\\0";
char Alphabet[30] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ\0";
// NTP Variables
char ntpFQDN[255] = "\0";
char ntpDateTime[25] = "01/01/0001 - 01:01:01\0";
// Template for padding
template <class T1, class T2> inline T1* Padd(T1* p, T2 n)
{
return (T1*)((char *)p + n);
}
//Start Testing Variables
ULONG lst_cur, lst_end, tot_byt_src, tot_byt_dst;
//End Testing Varaibles
//Global Variables for lznCopy
ULONG last_rawdLen ;
char last_Fname[2048] = "\0";
//#define STATUS_ACCESS_VIOLATION 0xC0000005
#define STATUS_BAD_COMPRESSION_BUFFER 0xC0000242
#define STATUS_SUCCESS 0x00000000
//lznCopy - Routines Definitions
static PUCHAR lznt1_decompress_chunk (UCHAR * dst, ULONG dst_size, UCHAR * src, ULONG src_size);
static NTSTATUS lznt1_decompress ( UCHAR * dst, ULONG dst_size, UCHAR * src, ULONG src_size, ULONG offset, ULONG * final_size, UCHAR * workspace);
//Windows Version
char descrWinVer[50] = "Unknown\0";
char shortWinVer[15] = "Win\0";
int iIsServer = 0;
//Offline Registry
DWORD ORRetcd ;
//Global Disk And Memory Variables
long long TotalMem, AvailDisk, longParm1, longParm2;
int iOutOfDiskSpace = 0;
//Global Syslog Variables
char Syslogd[255] = "127.0.0.1\0";
char Syslogp[10] = "514\0";
char SyslogTMSG[2048] = "AChoir Syslog Started.\0";
int iSyslogp, iSyslogLvl;
int main(int argc, char *argv[])
{
int i;
int iPtr;
size_t oPtr, ArnLen, ArnPtr;
int RunMe, ForMe, LstMe, Looper, LoopNum ;
char Tmprec[2048];
char Filrec[2048];
char Lstrec[2048];
char Tokrec[2048];
char MCprcI[2048];
char MCprcO[2048];
char Cpyrec[4096];
char Exerec[4096];
char Cmprec[4096];
char Arnrec[2048];
char Shrrec[1024];
char Redrec[1024];
DWORD dwSize = 0;
DWORD dwDownloaded = 0;
//LPSTR pszOutBuffer;
BOOL bResults = FALSE;
HINTERNET hSession = NULL, hConnect = NULL, hRequest = NULL;
char *ForSlash;
char *RootSlash;
char cName[MAX_COMPUTERNAME_LENGTH + 1];
DWORD len = 55;
char * pointEnd;
time(&timeval);
lclTime = localtime(&timeval);
iMonth = lclTime->tm_mon + 1;
iDay = lclTime->tm_mday;
iYYYY = lclTime->tm_year + 1900;
iYear = iYYYY - 2000;
iHour = lclTime->tm_hour;
iMin = lclTime->tm_min;
iSec = lclTime->tm_sec;
/****************************************************************/
/* Get the Consolewindow Handle - Since we have Focus */
/****************************************************************/
conHndl = GetConsoleWindow();
/****************************************************************/
/* Volume Information Variables */
/****************************************************************/
TCHAR volumeName[MAX_PATH + 1] = { 0 };
TCHAR fileSystemName[MAX_PATH + 1] = { 0 };
DWORD serialNumber = 0;
DWORD maxComponentLen = 0;
DWORD fileSystemFlags = 0;
int isNTFS = 0;
PUCHAR bufT;
//Set Console Handle (For Pretty Colors)
hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
/****************************************************************/
/* Set Defaults */
/****************************************************************/
iIsAdmin = 0;
iXitCmd = 0;
iLogOpen = 0;
iOutOfDiskSpace = 0;
iSyslogLvl = 0;
setMapErr = 0;
iCacheType = 0;
setMSGLvl = 2;
iOPNisOpen = 0;
memset(CurrDir, 0, 1024);
memset(CurrWorkDir, 0, 1024);
memset(TempDir, 0, 1024);
memset(BaseDir, 0, 1024);
memset(BACQDir, 0, 1024);
memset(CachDir, 0, 1024);
memset(Inprec, 0, 255);
memset(Conrec, 0, 255);
memset(inFnam, 0, 255);
memset(inMapp, 0, 255);
memset(inUser, 0, 255);
memset(inPass, 0, 255);
memset(ntpFQDN, 0, 255);
memset(VarArray, 0, 2560);
memset(cIsCompressed,0, 15);
strncpy(inFnam, "AChoir.ACQ\0", 11);
/****************************************************************/
/* What Directory are we in? */
/****************************************************************/
getcwd(BaseDir, 1000); // Just The Drive
getcwd(CurrWorkDir, 1000); // Working Directory
/****************************************************************/
/* Remove any Trailing Slashes. This happens if CWD is a */
/* mapped network drive (since it is at the root directory) */
/****************************************************************/
lastChar = strlen(BaseDir);
if ((BaseDir[lastChar-1] == '\\') && (lastChar > 2))
BaseDir[lastChar-1] = '\0';
/****************************************************************/
/* Get Envars */
/****************************************************************/
WinRoot = getenv("systemroot");
Procesr = getenv("processor_architecture");
TempVar = getenv("temp");
ProgVar = getenv("programfiles");
/****************************************************************/
/* Setup The initial 64Bit or 32Bit DLL Loading Directory */
/****************************************************************/
memset(WDLLPath, 0, 256);
if (strnicmp(Procesr, "AMD64", 5) == 0)
sprintf(WDLLPath, "%s\\64Bit\0", BaseDir);
else
sprintf(WDLLPath, "%s\\32Bit\0", BaseDir);
SetDllDirectory((LPCSTR)WDLLPath);
/****************************************************************/
/* Allocate File Signature and Type Tables. */
/****************************************************************/
iSigCount = 0;
SigTabl = (char *) malloc(iSigTMax * iSigSize) ;
if(SigTabl == NULL)
MemAllocErr("File Signature Table") ;
TypTabl = (char *) malloc(iSigTMax * iTypSize) ;
if(TypTabl == NULL)
MemAllocErr("File Type Table") ;
SizTabl = (int *) malloc(iSigTMax * sizeof(int)) ;
if(SizTabl == NULL)
MemAllocErr("File Signature Size Table") ;
/****************************************************************/
/* Build the &ACQ Incident Number */
/****************************************************************/
memset(cName, 0, MAX_COMPUTERNAME_LENGTH);
if (GetComputerName(cName, &len) == 0)
strcpy(cName, "localhost");
sprintf(ACQName, "ACQ-IR-%s-%04d%02d%02d-%02d%02d\0", cName, iYYYY, iMonth, iDay, iHour, iMin);
/****************************************************************/
/* Default Case Settings */
/****************************************************************/
memset(caseNumbr, 0, 255) ;
memset(evidNumbr, 0, 255) ;
memset(caseDescr, 0, 255) ;
memset(caseExmnr, 0, 255) ;
strncpy(caseNumbr, ACQName, 255) ;
strncpy(evidNumbr, "001", 3) ;
sprintf(caseDescr, "AChoir Live Acquisition: %s\0", ACQName) ;
strncpy(caseExmnr, "Unknown", 7) ;
/****************************************************************/
/* Get the Runmode: (Default == 1) */
/* BLD = Go Get the Utilities via cURL */
/* MNU = Run the Menu.ACQ - a VERY simple menu script */
/* RUN = Run the Live Acquisition Utility Script */
/* */
/****************************************************************/
iRunMode = 1;
iArgsMap = 0;
for (i = 1; i<argc; i++)
{
if ((strnicmp(argv[i], "/Help", 5) == 0) && (strlen(argv[i]) < 255))
{
printf("\nAChoir ver: %s, Argument/Options:\n", Version);
SetConsoleTextAttribute(hConsole, consGre);
consPrefix(" /Help ", consGre);
printf("- This Description\n");
consPrefix(" /BLD ", consGre);
printf("- Run the Build.ACQ Script (Build the AChoir Toolkit)\n");
consPrefix(" /MNU ", consGre);
printf("- Run the Menu.ACQ Script (A Simple AChoir Menu)\n");
consPrefix(" /RUN ", consGre);
printf("- Run the AChoir.ACQ Script to do a Live Acquisition\n");
consPrefix(" /DRV:<x:> ", consGre);
printf("- Set the &DRV parameter\n");
consPrefix(" /USR:<UserID> ", consGre);
printf("- User to Map to Remote Server\n");
consPrefix(" /PWD:<Password> ", consGre);
printf("- Password to Map to Remote Server\n");