-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathAChCleanr.go
1720 lines (1396 loc) · 72 KB
/
AChCleanr.go
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
//
// AChCleanr = Clean Up AChoirX After Running
//
// AChCleanr v00.00.01 - Alpha 1
package main
import (
"fmt"
"time"
"os"
"strings"
"strconv"
"encoding/csv"
"regexp"
"runtime"
"path/filepath"
"bufio"
"github.com/shirou/gopsutil/cpu")
// Global Variable Settings
var Version = "v00.00.01" // Version
var RunMode = "Run" // Character Runmode Flag (Build, Run, Menu)
var iRunMode = 0 // Int Runmode Flag (0, 1, 2)
var Console_Status = 0 // Was the Console Ever Invoked
var inFnam = "AChCleanr.ACQ" // Script Name
var MyProg = "none" // My Program Name and Path (os.Args[0])
var MyHash = "none" // My Hash
var inUser = "none" // UserId
var inPass = "none" // Password
var opArchit = "AMD64" // Architecture
var opSystem = "Windows" // Which Operating System are we running on
var iopSystem = 0 // Operating System Flag (0=win, 1=lin, 2=osx, 3=?)
var OSVersion = "Windows 10.0.0" // Operating System Version: OS Major.Minor.Build
var slashDelim byte = '\\' // Directory Delimiter Win vs. Lin vs. OSX
var slashDelimS string = "\\" // Same Thing but a String
var iNative = 0 // Are we Native 64Bit on 64Bit (Native = 1, NonNative = 0)
var sNative = "" // Native String (blank or Non-)
var iIsAdmin = 0 // Are we an Admin
var sIsAdmin = "" // Are we an Admin String (blank or Non-)
var yIsAdmin = "No" // Are we an Admin (Yes | No)
var LastRC = 0 // Last Return Code From External Executable
var RunMe = 0 // Used to Track Conditional Run Routines
var Looper = 0 // Flag to Keep Looping
var ForMe = 0 // Flag to identify &For is being used
var LstMe = 0 // Flag to identify &LST is being used
var ifFor = 0 // Flag contains FOR, FO0 - FOP
var iFor = 0 // Loop Counter FOR, FO0 - FOP
var iMaxCnt = 0 // Maximum Record Count (Set by FOR:, LST: &FOR, &LST)
var iLstCnt = 0 // LST Record Counter
var iForCnt = 0 // FOR Record Counter
var iLst = 0 // Loop Counter LST, LS0 - LSP
var ifLst = 0 // Flag contains LST, LS0 - LSP
var LoopNum = 0 // Loop Counter
var NotFound = 0 // Flag for ensuring that only one Found Rec Increments RunMe
var YesFound = 0 // Flag for ensuring that only one Found Rec Increments RunMe
var ForSlash = 0 // Last Occurance of Slash to find File in Path
var ForFName = "File.txt" // Parsed File name from Path
var iFiltype = 0 // Filter Type Include(1) or Exclude(2)
var FltRecFound = 0 // Found a filter Record match
var iFiltscope = 0 // Filter Scope Full Match(1) or Partial Match(2)
var consOrFile = 0 // Console Input instead of File
var JmpLbl = "LBL:Top" // Working Jump Label Build String
var iSleep = 0 // Seconds to Sleep
var iOPNisOpen = 0 // Is the User Defined File Open?
var FullDateTime = "01/01/0001 - 01:01:01" // Date and Time
// Max CPU for Throttleing
var cpu_max float64 = 999
//Tokenize Records
var tokCount = 0 // Token Counter
var Delims = ",\\/" // Tokenizing Delimiters (Lst, For(Win), For(Lin)
var iParseQuote = 0 // CSV Quoted String Parsing (0=Strict, 1=Lazy)
// Global File Names
var IniFile = "C:\\AChoir\\AChCleanr.Acq" // AChoir Script File
var IncFile = "C:\\AChoir\\AChCleanr.Acq" // AChoir Include (INC:) Script File
var ForFile = "C:\\AChoir\\Cache\\ForFiles" // Do action for these Files
var LstFile = "C:\\AChoir\\Data.Lst" // List of Data
var FltFile = "C:\\AChoir\\Cache\\FltFiles" // Filter File for &LST and &FOR
var ChkFile = "C:\\AChoir\\Data.Chk" // Check For File Existence
var CachDir = "C:\\AChoir\\Cache" // AChoir Caching Directory
var CurrDir = "" // Current Directory
var TempDir = "" // Temp Build Directory String
var CurrFil = "Current.fil" // Current File Name
var CurrWorkDir = "C:\\AChoir" // Current Workin Directory
var OpnHndl *os.File // User Defined Output File(s)
// Global File Handles
var ForScan *bufio.Scanner // IO Reader for ForFile
var LstScan *bufio.Scanner // IO Reader for LstFile
var FltScan *bufio.Scanner // IO Reader for FltFile
var ForHndl *os.File // File Handle for the ForFile
var LstHndl *os.File // File Handle for the LstFile
var FltHndl *os.File // File Handle for the FltFile
var ini_err error // Ini File Errors
var for_err error // For File Errors
var lst_err error // Lst File Errors
var opn_err error // User Defined File Errors
var flt_err error // Flt File Errors
var cwd_err error // Current Directory Errors
var for_rcd bool // Return Code for ForFile Read
var lst_rcd bool // Return Code for LstFile Read
// Arrays
var ForsArray = []string{"&FO0", "&FO1", "&FO2", "&FO3", "&FO4", "&FO5", "&FO6", "&FO7", "&FO8",
"&FO9", "&FOA", "&FOB", "&FOC", "&FOD", "&FOE", "&FOF", "&FOG", "&FOH",
"&FOI", "&FOJ", "&FOK", "&FOL", "&FOM", "&FON", "&FOO", "&FOP"}
var LstsArray = []string{"&LS0", "&LS1", "&LS2", "&LS3", "&LS4", "&LS5", "&LS6", "&LS7", "&LS8",
"&LS9", "&LSA", "&LSB", "&LSC", "&LSD", "&LSE", "&LSF", "&LSG", "&LSH",
"&LSI", "&LSJ", "&LSK", "&LSL", "&LSM", "&LSN", "&LSO", "&LSP"}
// Host Information
var cName = "localhost" // Endpoint Host Name
var cNewName = "localhost" // From /NAM: command line option
var host_err error // HostName Errors
var MyPID = 0 // This Programs Process ID
// Windows OS Variables
var WinRoot = "NA" // Windows Root Directory
var Procesr = "NA" // Processor
var TempVar = "NA" // Windows Temp Directory
var ProgVar = "NA" // Windows Program Files
//Tokenize Records
var iDblShard = 0 // Double Glob Shard Pointer
var WalkfileWild = "Wildcard" // Wildcard Portion of the WalkFile Routines
// Input and Output Records
var Inrec = "File Input Record" // File Input Record
var Tmprec = "Formatted Console Record" // Console Formatting Record
var Filrec = "File Record" // File Record
var Lstrec = "List Record" // List Record
var Fltrec = "Filter Record" // Filter Record
var o32VarRec = "32 bit Variables" // 32 Bit Variable Expansion Record
var Inprec = "Console Input" // Console Input Record
var Conrec = "Console Record" // Console Output Record
// INC: Nested Arrays
var HndlArry [10]*os.File
var ScanArry[10]*bufio.Scanner
var iIniCount = 0
// Cleaner Variables
var iClnExe = 0
var iClnDir = 0
var iClnAcn = 0
var ClnExe = "AChoirX.exe"
var ClnDir = "C:\\AChoir"
var ClnAcn = "ACQ-IR-LocalHost-00000000-0000" // AChoir Unique Collection Name
// Main Line
func main() {
// Get Host Name
cName, host_err = os.Hostname()
if host_err != nil {
cName = "LocalHost"
}
// Get My PID
MyPID = os.Getpid()
// Get Operating System and Architecture
opArchit = runtime.GOARCH
Procesr = opArchit
opSystem = runtime.GOOS
switch opSystem {
case "windows":
iopSystem = 0
slashDelim = '\\'
WinRoot = os.Getenv("SYSTEMROOT")
//Procesr = os.Getenv("PROCESSOR_ARCHITECTURE")
TempVar = os.Getenv("TEMP")
ProgVar = os.Getenv("PROGRAMFILES")
case "linux":
iopSystem = 1
slashDelim = '/'
case "darwin":
iopSystem = 2
slashDelim = '/'
default:
iopSystem = 3
slashDelim = '/'
}
// If Windows we get Major.Minor.Build - Linux and OSX not implemented yet
OSVersion = GetOSVer()
// Initial Settings and Configuration
slashDelimS = fmt.Sprintf("%c", slashDelim)
setDirectories(cName)
inFnam = "AChCleanr.ACQ"
// Start by Parsing any Command Line Parameters
for i := 1; i < len(os.Args); i++ {
if strings.HasPrefix(strings.ToUpper(os.Args[i]), "/HELP") {
fmt.Printf("\nAChCleanr ver: %s, Argument/Options:\n", Version)
fmt.Printf(" /Help - This Description\n")
fmt.Printf(" /EXE:<EXE Name> - The AChoirX EXE File to Delete\n")
fmt.Printf(" /DIR:<Directory> - The AChoirX Root Directory to Clean\n")
fmt.Printf(" /ACN:<Acquisition> - The AChoirX Acquisition/directory\n")
fmt.Printf(" /INI:<File Name> - Run the <File Name> script instead of AChCleanr.ACQ\n\n")
fmt.Printf(" Usage:\n")
fmt.Printf(" AChCleanr Cleans up and deletes files from an AChoirX Run in order to DISOLVE the software\n\n")
fmt.Printf(" AChCleanr /EXE:<AChoirExecutable.exe> /DIR:<AchoirRoot> /INI:<Alternate Script>\n")
os.Exit(0)
} else if len(os.Args[i]) > 5 && strings.HasPrefix(strings.ToUpper(os.Args[i]), "/INI:") {
// Check if Input is Console
if strings.HasPrefix(strings.ToUpper(os.Args[i]), "/INI:CONSOLE") {
consOrFile = 1
RunMode = "Con"
Console_Status = 1
inFnam = os.Args[i][5:]
iRunMode = 1
} else if len(os.Args[i]) < 254 {
RunMode = "Ini"
inFnam = os.Args[i][5:]
// Initially Set iRunmode to 2 (in case we are running remote)
// Avoids Creating a Local BACQDIR
iRunMode = 2
} else {
fmt.Println("[!] /INI: Too Long - Greater than 254 chars")
}
} else if len(os.Args[i]) > 5 && strings.HasPrefix(strings.ToUpper(os.Args[i]), "/EXE:") {
if len(os.Args[i]) < 254 {
iClnExe = 1
ClnExe = os.Args[i][5:]
} else {
fmt.Println("[!] /EXE: Too Long - Greater than 254 chars")
}
} else if len(os.Args[i]) > 5 && strings.HasPrefix(strings.ToUpper(os.Args[i]), "/DIR:") {
if len(os.Args[i]) < 254 {
iClnDir = 1
ClnDir = os.Args[i][5:]
} else {
fmt.Println("[!] /DIR: Too Long - Greater than 254 chars")
}
} else if len(os.Args[i]) > 5 && strings.HasPrefix(strings.ToUpper(os.Args[i]), "/ACN:") {
if len(os.Args[i]) < 254 {
iClnAcn = 1
ClnAcn = os.Args[i][5:]
} else {
fmt.Println("[!] /DIR: Too Long - Greater than 254 chars")
}
}
}
//****************************************************************
//* Check If We are an Admin/Root *
//****************************************************************
if (IsUserAdmin()) {
iIsAdmin = 1
sIsAdmin = ""
yIsAdmin = "Yes"
} else {
iIsAdmin = 0
sIsAdmin = "NON-"
yIsAdmin = "No"
}
//****************************************************************
// Set Initial File Names (ClnDir needs to be set 1st) *
//****************************************************************
IniFile = fmt.Sprintf("%s%c%s", ClnDir, slashDelim, inFnam)
CachDir = fmt.Sprintf("%s%cCache", ClnDir, slashDelim)
ForFile = fmt.Sprintf("%s%cForFiles", CachDir, slashDelim)
LstFile = fmt.Sprintf("%s%cLstFiles", CachDir, slashDelim)
//****************************************************************
// Console or File Mode? *
//****************************************************************
if consOrFile == 1 {
fmt.Printf("[+] Switching to Console Input.\n")
fmt.Printf(">>>")
ScanArry[iIniCount] = bufio.NewScanner(os.Stdin)
} else {
HndlArry[iIniCount], ini_err = os.Open(IniFile)
if ini_err != nil {
fmt.Printf("[!] Error Opening Ini File: %s\n", IniFile)
}
ScanArry[iIniCount] = bufio.NewScanner(HndlArry[iIniCount])
}
// Do This Forever
for {
// Input Scan (File and Console) until Error (EOF)
if !ScanArry[iIniCount].Scan() {
//****************************************************************
// See if this was a child INI (INC:). If so, return to parent *
//****************************************************************
if iIniCount > 0 {
fmt.Printf("[+] End of INC: Input... Returning...\n")
HndlArry[iIniCount].Close()
iIniCount--
continue
} else {
fmt.Printf("[+] End of Input... Exiting...\n")
cleanUp_Exit(LastRC);
os.Exit(LastRC);
break
}
}
//Remove any preceding blanks
Tmprec = strings.TrimSpace(ScanArry[iIniCount].Text())
// Dont Process any Comments
if strings.HasPrefix(Tmprec, "*") {
continue
}
//****************************************************************
//* Conditional Execution *
//****************************************************************
if RunMe > 0 {
if strings.HasPrefix(strings.ToUpper(Tmprec), "CKY:") {
RunMe++
} else if strings.HasPrefix(strings.ToUpper(Tmprec), "CKN:") {
RunMe++
} else if strings.HasPrefix(strings.ToUpper(Tmprec), "END:") {
if RunMe > 0 {
RunMe--
}
}
} else {
Looper = 1
//****************************************************************
//* ForFiles Looper Setup *
//****************************************************************
ifFor = 0
if strings.Contains(strings.ToUpper(Tmprec), "&FOR") {
ifFor = 1
}
// Loop Through to check for &FO0 - &FOP
for iFor = 0; iFor < 26; iFor++ {
if strings.Contains(strings.ToUpper(Tmprec), ForsArray[iFor]) {
ifFor = 1
}
}
if ifFor == 1 {
ForMe = 1
ForHndl, for_err = os.Open(ForFile)
if for_err != nil {
fmt.Printf("[!] &FOR Directory has not been set with the FOR: command. Ignoring &FOR Loop...\n")
Looper = 0
}
iMaxCnt = 0
iForCnt = 0
ForScan = bufio.NewScanner(ForHndl)
} else {
ForMe = 0
}
//****************************************************************
//* LstFiles Looper Setup *
//****************************************************************
ifLst = 0
if strings.Contains(strings.ToUpper(Tmprec), "&LST") {
ifLst = 1
}
// Loop Through to check for &LS0 - &LSP
for iLst = 0; iLst < 26; iLst++ {
if strings.Contains(strings.ToUpper(Tmprec), LstsArray[iLst]) {
ifLst = 1
}
}
if ifLst == 1 {
LstMe = 1
LstHndl, lst_err = os.Open(LstFile)
if lst_err != nil {
fmt.Printf("[!] &LST File was not found (LST: not set): %s\n", LstFile)
Looper = 0
}
iMaxCnt = 0
iLstCnt = 0
LstScan = bufio.NewScanner(LstHndl)
} else {
LstMe = 0
}
//****************************************************************
//* Loop (FOR: and LST:) until Looper = 1 *
//****************************************************************
LoopNum = 0
NotFound = 0
YesFound = 0
for Looper > 0 {
if ForMe == 0 && LstMe == 0 {
Looper = 0
} else if ForMe == 1 && LstMe == 0 {
for_rcd = ForScan.Scan()
for_err = ForScan.Err()
// No Error and no EOF - So Process the Record
if for_err == nil && for_rcd == true {
Filrec = strings.TrimSpace(ForScan.Text())
Looper = 1
LoopNum++
iMaxCnt++
iForCnt++
//****************************************************************
//* Get Just the File Name *
//****************************************************************
ForSlash = strings.LastIndexByte(Filrec, slashDelim)
if (ForSlash == -1) {
ForFName = Filrec
} else if len(Filrec[ForSlash+1:]) < 2 {
ForFName = "Unknown"
} else {
ForFName = Filrec[ForSlash+1:]
}
} else {
fmt.Printf("[+] For Records Processed: %d\n", LoopNum)
break
}
} else if ForMe == 0 && LstMe == 1 {
lst_rcd = LstScan.Scan()
lst_err = LstScan.Err()
// No Error and no EOF - So Process the Record
if lst_err == nil && lst_rcd == true {
Lstrec = strings.TrimSpace(LstScan.Text())
// Simple way to ignore UTF-16 - Not Great, but it works
Lstrec = strings.Replace(Lstrec, "\x00", "", -1)
Lstrec = strings.Replace(Lstrec, "\xfe", "", -1)
Lstrec = strings.Replace(Lstrec, "\xff", "", -1)
//Lstrec = strings.ToValidUTF8(Lstrec, "") // Force it to be UTF-8
Looper = 1
LoopNum++
iMaxCnt++
iLstCnt++
} else {
fmt.Printf("[+] Lst Records Processed: %d\n", LoopNum)
break
}
} else {
Looper = 0
fmt.Printf("[!] AChoirX does not yet support Nested Looping (&LST + &FOR)\n > %s\n", Tmprec)
Tmprec = fmt.Sprintf("***: Command Bypassed")
}
//****************************************************************
//* If Filtering has been confiured, the same Filtering File *
//* will be used for both &FOR and &LST *
//****************************************************************
if iFiltype > 0 {
if LstMe == 1 || ForMe == 1 {
//****************************************************************
//* Check for &LST (LstMe) or &FOR (ForMe) filtering *
//****************************************************************
FltHndl, flt_err := os.Open(FltFile)
if flt_err != nil {
iFiltype = 0
fmt.Printf("[!] Filter file could not be opened: %s\n", FltFile)
} else {
FltScan = bufio.NewScanner(FltHndl)
FltRecFound = 0
for FltScan.Scan() {
Fltrec = strings.TrimSpace(FltScan.Text())
if LstMe == 1 {
//****************************************************************
//* &LST Look for Match - Filtscope is Full(1) or Part(2) *
//****************************************************************
//fmt.Printf("[c] -%s-%s-\n", Lstrec, Fltrec)
if iFiltscope == 1 {
if strings.ToUpper(Lstrec) == strings.ToUpper(Fltrec) {
FltRecFound = 1
}
} else {
if CaseInsensitiveContains(Lstrec, Fltrec) {
FltRecFound = 1
}
}
} else if ForMe == 1 {
//****************************************************************
//* &For Look for Match - Filtscope is Full(1) or Part(2) *
//****************************************************************
if iFiltscope == 1 {
if strings.ToUpper(Filrec) == strings.ToUpper(Fltrec) {
FltRecFound = 1
}
} else {
if CaseInsensitiveContains(Filrec, Fltrec) {
FltRecFound = 1
}
}
}
}
FltHndl.Close()
//****************************************************************
//* Include Filter Logic(1) - If not found, bypass *
//****************************************************************
if iFiltype == 1 && FltRecFound == 0 {
continue
}
//****************************************************************
//* Exclude Filter Logic(2) - if found, bypass *
//****************************************************************
if iFiltype == 2 && FltRecFound == 1 {
continue
}
}
}
}
//****************************************************************
//* Check for System Variables and Expand them *
//****************************************************************
//* This function changes in AChoirX - AChoir uses %EnVar% *
//* but native GOLang support $Var or ${Var}. AChoirS now uses *
//* the Native GoLang functions to prevent reinventing the wheel *
//****************************************************************
if strings.Contains(Tmprec, "${") {
varConvert(Tmprec)
} else {
o32VarRec = Tmprec
}
//****************************************************************
//* Now Further expand o32VarRec for Achoir unique variables *
//****************************************************************
if CaseInsensitiveContains(o32VarRec, "&Dir") {
if len(CurrDir) > 0 {
TempDir = fmt.Sprintf("%s%c%s", ClnDir, slashDelim, CurrDir)
} else {
TempDir = ClnDir
}
repl_Dir := NewCaseInsensitiveReplacer("&Dir", TempDir)
o32VarRec = repl_Dir.Replace(o32VarRec)
}
if CaseInsensitiveContains(o32VarRec, "&Exe") {
repl_Exe := NewCaseInsensitiveReplacer("&Exe", ClnExe)
o32VarRec = repl_Exe.Replace(o32VarRec)
}
if CaseInsensitiveContains(o32VarRec, "&Fil") {
repl_Fil := NewCaseInsensitiveReplacer("&Fil", CurrFil)
o32VarRec = repl_Fil.Replace(o32VarRec)
}
if CaseInsensitiveContains(o32VarRec, "&Acn") {
repl_Acn := NewCaseInsensitiveReplacer("&Acn", ClnAcn)
o32VarRec = repl_Acn.Replace(o32VarRec)
}
if CaseInsensitiveContains(o32VarRec, "&Win") {
repl_Win := NewCaseInsensitiveReplacer("&Win", WinRoot)
o32VarRec = repl_Win.Replace(o32VarRec)
}
// First process &FOR - This should prevent unnecessary parsing
if CaseInsensitiveContains(o32VarRec, "&For") {
repl_For := NewCaseInsensitiveReplacer("&For", Filrec)
o32VarRec = repl_For.Replace(o32VarRec)
}
// If any &FO_ are left, process those
if CaseInsensitiveContains(o32VarRec, "&Fo") {
// Always allow &For - Even if Parsing Fails
if CaseInsensitiveContains(o32VarRec, "&For") {
repl_For := NewCaseInsensitiveReplacer("&For", Filrec)
o32VarRec = repl_For.Replace(o32VarRec)
}
// Split string, we will likely need it split
runeDelims := []rune(Delims)
tokRdr := csv.NewReader(strings.NewReader(Filrec))
// Windows Delim Stored in Delims[1] - Linux Delim Stored in Delims[2]
// This is setable with SET:Delims= (Default is: ,\/)
if iopSystem == 0 {
tokRdr.Comma = runeDelims[1]
} else {
tokRdr.Comma = runeDelims[2]
}
tokRdr.FieldsPerRecord = -1
tokRdr.TrimLeadingSpace = true
if iParseQuote == 1 {
// Does not enforce strict matching quotes when parsing
tokRdr.LazyQuotes = true
}
tokFields, tok_err := tokRdr.Read()
if tok_err != nil {
fmt.Printf("[!] Parsing Error for Record(%d): %s\n", LoopNum, tok_err)
repl_For := NewCaseInsensitiveReplacer("&Fo", "Parse_Err_")
o32VarRec = repl_For.Replace(o32VarRec)
} else {
tokCount = len(tokFields)
if tokCount < 25 {
for i := tokCount; i < 26; i++ {
tokFields = append(tokFields, "")
}
}
// Look for Replacements &Fo0 - FoP
for iFor = 0; iFor < 26; iFor++ {
if CaseInsensitiveContains(o32VarRec, ForsArray[iFor]) {
repl_For := NewCaseInsensitiveReplacer(ForsArray[iFor], tokFields[iFor])
o32VarRec = repl_For.Replace(o32VarRec)
}
}
}
}
//Clear out the &LST - This should prevent unnecessary parsing
if CaseInsensitiveContains(o32VarRec, "&Lst") {
repl_Lst := NewCaseInsensitiveReplacer("&Lst", Lstrec)
o32VarRec = repl_Lst.Replace(o32VarRec)
}
// If any &LS_ are left, process those
if CaseInsensitiveContains(o32VarRec, "&Ls") {
// Always allow &LST - Even if Parsing Fails
if CaseInsensitiveContains(o32VarRec, "&Lst") {
repl_Lst := NewCaseInsensitiveReplacer("&Lst", Lstrec)
o32VarRec = repl_Lst.Replace(o32VarRec)
}
// Split string, we will likely need it split
runeDelims := []rune(Delims)
tokRdr := csv.NewReader(strings.NewReader(Lstrec))
tokRdr.Comma = runeDelims[0]
tokRdr.FieldsPerRecord = -1
tokRdr.TrimLeadingSpace = true
if iParseQuote == 1 {
// Does not enforce strict matching quotes when parsing
tokRdr.LazyQuotes = true
}
tokFields, tok_err := tokRdr.Read()
if tok_err != nil {
fmt.Printf("[!] Parsing Error for Record(%d): %s\n", LoopNum, tok_err)
repl_Lst := NewCaseInsensitiveReplacer("&Ls", "Parse_Err_")
o32VarRec = repl_Lst.Replace(o32VarRec)
} else {
tokCount = len(tokFields)
if tokCount < 25 {
for i := tokCount; i < 26; i++ {
tokFields = append(tokFields, "")
}
}
// Look for Replacements &Ls0 - LsP
for iLst = 0; iLst < 26; iLst++ {
if CaseInsensitiveContains(o32VarRec, LstsArray[iLst]) {
repl_Lst := NewCaseInsensitiveReplacer(LstsArray[iLst], tokFields[iLst])
o32VarRec = repl_Lst.Replace(o32VarRec)
}
}
}
}
if CaseInsensitiveContains(o32VarRec, "&Num") {
repl_Num := NewCaseInsensitiveReplacer("&Num", strconv.Itoa(LoopNum))
o32VarRec = repl_Num.Replace(o32VarRec)
}
if CaseInsensitiveContains(o32VarRec, "&Cnr") {
repl_Cnr := NewCaseInsensitiveReplacer("&Cnr", strconv.Itoa(iMaxCnt))
o32VarRec = repl_Cnr.Replace(o32VarRec)
}
if CaseInsensitiveContains(o32VarRec, "&Fnm") {
repl_Fnm := NewCaseInsensitiveReplacer("&Fnm", ForFName)
o32VarRec = repl_Fnm.Replace(o32VarRec)
}
if CaseInsensitiveContains(o32VarRec, "&Chk") {
repl_Chk := NewCaseInsensitiveReplacer("&Chk", ChkFile)
o32VarRec = repl_Chk.Replace(o32VarRec)
}
if CaseInsensitiveContains(o32VarRec, "&Ver") {
repl_Ver := NewCaseInsensitiveReplacer("&Ver", OSVersion)
o32VarRec = repl_Ver.Replace(o32VarRec)
}
if CaseInsensitiveContains(o32VarRec, "&Myp") {
repl_Myp := NewCaseInsensitiveReplacer("&Myp", MyProg)
o32VarRec = repl_Myp.Replace(o32VarRec)
}
if CaseInsensitiveContains(o32VarRec, "&Myh") {
repl_Myh := NewCaseInsensitiveReplacer("&Myh", MyHash)
o32VarRec = repl_Myh.Replace(o32VarRec)
}
if CaseInsensitiveContains(o32VarRec, "&Adm") {
repl_Adm := NewCaseInsensitiveReplacer("&Adm", yIsAdmin)
o32VarRec = repl_Adm.Replace(o32VarRec)
}
if CaseInsensitiveContains(o32VarRec, "&Usr") {
repl_Usr := NewCaseInsensitiveReplacer("&Usr", inUser)
o32VarRec = repl_Usr.Replace(o32VarRec)
}
if CaseInsensitiveContains(o32VarRec, "&Pwd") {
repl_Pwd := NewCaseInsensitiveReplacer("&Pwd", inPass)
o32VarRec = repl_Pwd.Replace(o32VarRec)
}
//****************************************************************
//* Now execute the Actions *
//****************************************************************
Inrec = o32VarRec
if len(Inrec) < 1 {
continue
} else if strings.HasPrefix(Inrec, "*") {
continue
} else if len(Inrec) < 4 {
continue
} else if strings.HasPrefix(strings.ToUpper(Inrec), "LBL:") {
continue
} else if strings.HasPrefix(strings.ToUpper(Inrec), "JMP:") && len(Inrec) > 4 {
if consOrFile == 1 {
fmt.Printf("[*] Jumping Does not make sense in Interactive Mode. Ignoring...\n")
} else {
// Rewind File and Jump to a Label (LBL:)
// Note: For This to work we have to Reset both the Handle and the Scanner!
RunMe = 0
HndlArry[iIniCount].Seek(0, 0)
ScanArry[iIniCount] = bufio.NewScanner(HndlArry[iIniCount])
JmpLbl = fmt.Sprintf("LBL:%s", Inrec[4:])
for ScanArry[iIniCount].Scan() {
Tmprec = strings.TrimSpace(ScanArry[iIniCount].Text())
if Tmprec == JmpLbl {
break
}
}
}
} else if strings.HasPrefix(strings.ToUpper(Inrec), "DIR:") {
//****************************************************************
//* Set Current Directory *
//****************************************************************
// Sanity Check - Replace Delimiters base on OS
if iopSystem == 0 {
Inrec = strings.Replace(Inrec, "/", "\\", -1)
} else {
Inrec = strings.Replace(Inrec, "\\", "/", -1)
}
// Explicit Path (Dependent upon OS!
osDir := fmt.Sprintf("DIR:%c", slashDelim)
if strings.HasPrefix(strings.ToUpper(Inrec), osDir) {
if len(Inrec) > 5 {
CurrDir = fmt.Sprintf("%s", Inrec[5:])
TempDir = fmt.Sprintf("%s%c%s", ClnDir, slashDelim, CurrDir)
} else {
CurrDir = ""
TempDir = ClnDir
}
} else {
if len(Inrec) > 4 {
//Check to see if it is an append or new &Dir
//Dont add // if it's new!
if len(CurrDir) > 0 {
CurrDir += slashDelimS
}
CurrDir += Inrec[4:]
TempDir = fmt.Sprintf("%s%c%s", ClnDir, slashDelim, CurrDir)
}
}
// Have we created this Directory already?
if _, CurrDir_err := os.Stat(TempDir); os.IsNotExist(CurrDir_err) {
fmt.Printf("[+] Creating Sub-Directory: %s\n", CurrDir)
ExpandDirs(TempDir)
}
// Reset The WorkingDirectory to the new Directory
CurrWorkDir = fmt.Sprintf("%s%c%s", ClnDir, slashDelim, CurrDir)
os.Chdir(CurrWorkDir)
} else if strings.HasPrefix(strings.ToUpper(Inrec), "FIL:") {
CurrFil = Inrec[4:]
TempDir = fmt.Sprintf("%s%c%s", ClnDir, slashDelim, CurrDir)
if _, CurrDir_err := os.Stat(TempDir); os.IsNotExist(CurrDir_err) {
fmt.Printf("[+] Creating Sub-Directory: %s\n", CurrDir)
ExpandDirs(TempDir)
}
fmt.Printf("[+] File has been Set to: %s\n", CurrFil)
} else if strings.HasPrefix(strings.ToUpper(Inrec), "INI:") {
IniFile = Inrec[4:]
if strings.ToUpper(IniFile) == "CONSOLE" {
if consOrFile == 0 {
RunMode = "Con"
inFnam = "Console"
Console_Status = 1
iRunMode = 1
consOrFile = 1
fmt.Printf("[+] Switching to Console (Interactive Mode)\n")
HndlArry[iIniCount].Close()
ScanArry[iIniCount] = bufio.NewScanner(os.Stdin)
}
} else {
if FileExists(IniFile) {
fmt.Printf("[!] Switching to INI File: %s\n", Inrec[4:])
// Only close the handle if its not Console. If it is Console Set it back to File
if consOrFile == 0 {
HndlArry[iIniCount].Close()
} else {
consOrFile = 0
}
HndlArry[iIniCount], ini_err = os.Open(IniFile)
if ini_err != nil {
fmt.Printf("[!] Error Opening Ini File: %s - Exiting.\n", IniFile)
cleanUp_Exit(3)
os.Exit(3)
}
RunMode = "Ini"
ScanArry[iIniCount] = bufio.NewScanner(HndlArry[iIniCount])
RunMe = 0 // Conditional run Script default is yes
} else {
fmt.Printf("[!] Requested INI File Not Found: %s - Ignored.\n", Inrec[4:])
}
}
} else if strings.HasPrefix(strings.ToUpper(Inrec), "INC:") {
// Include an INI file - All variables, Labels, Etc. Remain the same
// It is essentially an injected INI file that is inline
IncFile = Inrec[4:]
if iIniCount > 8 {
fmt.Printf("[!] Maximum Nested INC: Files has been exceeded. Ignoring: %s...\n", IncFile)
} else {
if FileExists(IncFile) {
fmt.Printf("[+] Including INC File: %s\n", Inrec[4:])
// Programming note: Here we DO NOT Close the parent file. But bump the array
// to have an additonal open file
iIniCount++
HndlArry[iIniCount], ini_err = os.Open(IncFile)
if ini_err != nil {
fmt.Printf("[!] Error Opening Include File: %s - Ignoring.\n", IncFile)
iIniCount--
} else {
RunMode = "Ini"
ScanArry[iIniCount] = bufio.NewScanner(HndlArry[iIniCount])
RunMe = 0 // Conditional run Script default is yes
}
} else {
fmt.Printf("[!] Requested INC File Not Found: %s - Ignored.\n", Inrec[4:])
}
}
} else if strings.HasPrefix(strings.ToUpper(Inrec), "ADM:CHECK") {
fmt.Printf("[+] Running as %sAdmin/Root\n", sIsAdmin)
} else if strings.HasPrefix(strings.ToUpper(Inrec), "ADM:FORCE") {
if iIsAdmin == 1 {
fmt.Printf("[+] Running as Admin/Root - Continuing...\n")
} else {
fmt.Printf("[+] NOT Running as Admin/Root - Exiting...\n")
cleanUp_Exit(3)
os.Exit(3)
}
} else if strings.HasPrefix(strings.ToUpper(Inrec), "CON:HIDE") {
fmt.Printf("[+] Hiding Console...\n")
winConHideShow(0)
} else if strings.HasPrefix(strings.ToUpper(Inrec), "CON:SHOW") {
fmt.Printf("[+] Showing Console...\n")
winConHideShow(1)
} else if strings.HasPrefix(strings.ToUpper(Inrec), "SLP:") {
iSleep, _ = strconv.Atoi(Inrec[4:])
time.Sleep (time.Duration(iSleep) * time.Second)
} else if strings.HasPrefix(strings.ToUpper(Inrec), "INP:") {
consInput(Inrec[4:], 1, 0)
Inprec = Conrec
} else if strings.HasPrefix(strings.ToUpper(Inrec), "USR:") {
inUser = Inrec[4:]
} else if strings.HasPrefix(strings.ToUpper(Inrec), "PWD:") {
inPass = Inrec[4:]
} else if strings.HasPrefix(strings.ToUpper(Inrec), "VER:") {
//****************************************************************
//* Check the Input String for Version. This can be Partial. *
//* For Example: Windows 10.0.18362 will get a TRUE for *
//* Ver:Win, Ver:Windows 10, Ver:Windows 10.0.183, etc... *
//****************************************************************
if consOrFile == 1 {
fmt.Printf("[*] OS Version Detected: %s\n", OSVersion)
} else {
if strings.HasPrefix(strings.ToUpper(OSVersion), strings.ToUpper(Inrec[4:])) {
// Yes on First Match Only
if YesFound == 0 {
YesFound = 1
}
} else {
// No On First Not Match Only
if NotFound == 0 {
NotFound = 1