-
Notifications
You must be signed in to change notification settings - Fork 0
/
STD.au3
4472 lines (3540 loc) · 185 KB
/
STD.au3
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
;-----------------------------------------------------------------------------
; STD - Spot The Difference
; A poor mans file integrity checker for windows
;
; By Reinhard Dittmann
;-----------------------------------------------------------------------------
; AutoIT version: 3.3.10.2
;-----------------------------------------------------------------------------
;
;Invocation:
; std.exe /help show help
; std.exe /? show help
;
;Needs:
; sqlite.dll
; and/or
; a database on an MS SQL Server (express)
;
; _SQL UDF for MS SQL form ChrisL https://www.autoitscript.com/forum/topic/51952-_sqlau3-adodbconnection/
;-----------------------------------------------------------------------------
;Set file infos
;Versioning: "Incompatible changes to DB"."new feature"."bug fix"."minor fix"
#pragma compile(ProductVersion,"5.0.0.3")
#pragma compile(FileVersion,"5.0.0.3")
#pragma compile(FileDescription,"Spot The Difference")
#pragma compile(ProductName,"Spot The Difference")
#pragma compile(InternalName,"STD")
;set compile options
#pragma compile(Console, true)
#pragma compile(UPX, False)
#pragma compile(x64, True)
;3rd party UDF from ChrisL https://www.autoitscript.com/forum/topic/51952-_sqlau3-adodbconnection/
#include "_SQL.au3"
;AutiIt UDFs
#include <array.au3>
#include <File.au3>
#include <String.au3>
#include <FileConstants.au3>
#include <SQLite.au3>
#include <SQLite.dll.au3>
#include <MsgBoxConstants.au3>
#include <StringConstants.au3>
#include <WinAPIFiles.au3>
#include <Date.au3>
#include <Constants.au3>
#include <Crypt.au3>
;Compile options
Opt("TrayIconHide", 1)
Opt("MustDeclareVars", 1)
;Constants
global const $gcVersion = FileGetVersion(@ScriptName,"ProductVersion")
global const $gcScannameLimit = 65535 ;max number of scannames resturnd from the DB
global const $gcCheckIfEveryDirExists = False ;do a FileExists() on every directory TreeClimber() visits.
;Debug
global const $gcDEBUG = False ;master switch for debug output
global $gcDEBUGOnlyShowScanBuffer = True ;show only "searching" and buffersize during scan !
global $gcDEBUGShowVisitedDirectories = False ;show visited directories during scan !
global $gcDEBUGDoNotStartSecondProcess = False ;run only the list process and do not start the scan process
global $gcDEBUGRunWithoutCompilation = False ;force the program to run, without beeing compiled
global $gcDEBUGShowEmptyScanBuffer = False ;show "*** searching ***" if the scan process is waiting for the list process
global $gcDEBUGShowMSSQLDeleteSQLCode = False ;show SQL statement for MSSQL version of /delete
global $gcDEBUGShowMSSQLInsertBufferFlushes = False ;show when BufferedInsertIntoFiledataTable() flushes the buffer
;Profiler
global $gcDEBUGTimeGetFileInfo = True
global $gcDEBUGTimeGetFileInfo_GetFileInformationByHandle = True
global $gcDEBUGTimeGetFileInfo_FileGetAttrib = True
global $gcDEBUGTimeGetFileInfo_FileGetTime = True
global $gcDEBUGTimeGetFileInfo_FileGetVersion = True
global $gcDEBUGTimeGetFileInfo_FileGetShortName = True
global $gcDEBUGTimeGetFileInfo_CalcHashes = True
global $gcDEBUGTimeTreeClimber_MakeValidLastChar = True
global $gcDEBUGTimeGetRuleFromRuleSet = True
global $gcDEBUGTimeIsExecutable = True
global $gcDEBUGTimeIsIncludedByRule = True
global $gcDEBUGTimeIsClimbTargetByRule = True
global $gcDEBUGTimeBufferedInsertIntoFiledataTable = True
global $gcDEBUGTimeGetFilenameIDFromDB = True
global $giDEBUGTimerGetFileInfo = 0
global $giDEBUGTimerGetFileInfo_GetFileInformationByHandle = 0
global $giDEBUGTimerGetFileInfo_FileGetAttrib = 0
global $giDEBUGTimerGetFileInfo_FileGetTime = 0
global $giDEBUGTimerGetFileInfo_FileGetVersion = 0
global $giDEBUGTimerGetFileInfo_FileGetShortName = 0
global $giDEBUGTimerGetFileInfo_CalcHashes = 0
global $giDEBUGTimerTreeClimber_MakeValidLastChar = 0
global $giDEBUGTimerGetRuleFromRuleSet = 0
global $giDEBUGTimerIsExecutable = 0
global $giDEBUGTimerIsIncludedByRule = 0
global $giDEBUGTimerIsClimbTargetByRule = 0
global $giDEBUGTimerBufferedInsertIntoFiledataTable = 0
global $giDEBUGTimerGetFilenameIDFromDB = 0
if $gcDEBUG = False Then
$gcDEBUGOnlyShowScanBuffer = False
$gcDEBUGShowVisitedDirectories = False
$gcDEBUGDoNotStartSecondProcess = False
$gcDEBUGRunWithoutCompilation = False
$gcDEBUGShowEmptyScanBuffer = False
$gcDEBUGShowMSSQLDeleteSQLCode = False
$gcDEBUGShowMSSQLInsertBufferFlushes = False
$gcDEBUGTimeGetFileInfo = False
$gcDEBUGTimeGetFileInfo_GetFileInformationByHandle = False
$gcDEBUGTimeGetFileInfo_FileGetAttrib = False
$gcDEBUGTimeGetFileInfo_FileGetTime = False
$gcDEBUGTimeGetFileInfo_FileGetVersion = False
$gcDEBUGTimeGetFileInfo_FileGetShortName = False
$gcDEBUGTimeGetFileInfo_CalcHashes = False
$gcDEBUGTimeTreeClimber_MakeValidLastChar = False
$gcDEBUGTimeGetRuleFromRuleSet = False
$gcDEBUGTimeIsExecutable = False
$gcDEBUGTimeIsIncludedByRule = False
$gcDEBUGTimeIsClimbTargetByRule = False
$gcDEBUGTimeBufferedInsertIntoFiledataTable = False
$gcDEBUGTimeGetFilenameIDFromDB = False
EndIf
;Variables
global $gaRuleSet[1][3] ;all rules form config db table
;$gaRuleSet
;.....................................
;command | parameter | rulenumber
;-------------------------------------
;EmailFrom: | i@y.com | 0
;EmailTo: | y@i.com | 0
;Rule: | Test | 1
;IncDir: | "c:\test" | 1
;IncExt: | txt | 1
;Rule: | Logfiles | 2
;IncDir: | "c:\tst1" | 2
;IncExt: | log | 2
;.....................................
global $gaRuleStart[1] ;Index of start of all rules in $gaRuleSet[]
Global Enum $geRD_IncExt,$geRD_IncExtLC,$geRD_ExcExt,$geRD_IncExe,$geRD_ExcExe,$geRD_IncAll,$geRD_ExcAll,$geRD_NoHashes,$geRD_NoMD5,$geRD_NoSHA1,$geRD_IncDirs,$geRD_HasExcDir,$geRDMax
#cs
$geRD_IncExt ;column index for IncExt parameters in $gaRuleData[]
$geRD_IncExtLC ;column index for last characters of IncExt in $gaRuleData[]
$geRD_ExcExt ;column index for ExcExt parameters in $gaRuleData[]
$geRD_IncExe ;column index for IncExe statement in $gaRuleData[]
$geRD_ExcExe ;column index for ExcExe statement in $gaRuleData[]
$geRD_IncAll ;column index for IncAll statement in $gaRuleData[]
$geRD_ExcAll ;column index for ExcAll statement in $gaRuleData[]
$geRD_NoHashes ;column index for NoHashes statement in $gaRuleData[]
$geRD_NoMD5 ;column index for NoMD5 statement in $gaRuleData[]
$geRD_NoSHA1 ;column index for NoSHA1 statement in $gaRuleData[]
$geRD_IncDirs ;column index for IncDirs statement in $gaRuleData[]
$geRD_HasExcDir ;column index for the existence of "ExcDir:" or "ExcDirRec:" parameters in $gaRuleData[]
#ce
global $gaRuleData[1][$geRDMax] ;all infos of extension statements of a rule
;......................................................................................................................................................................................................
;IncExt | IncExt | ExcExt | IncExe | ExcExe | IncAll | ExcAll | NoHashes | NoMD5 | NoSHA1 | IncDirs | are there "ExcDir:" or "ExcDirRec:"
; | last char of ext | | | | | | | | | | statements in the rule ?
;$geRD_IncExt | $geRD_IncExtLC | $geRD_ExcExt | $geRD_IncExe | $geRD_ExcExe | $geRD_IncAll | $geRD_ExcAll | $geRD_NoHashes | $geRD_NoMD5 | $geRD_NoSHA1 | $geRD_IncDirs | $geRD_HasExcDir
;------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
;".txt.dll.docx." | "tlx" | ".txt.dll.xlsx." | True | False | True | False | True | True | True | True | True
;......................................................................................................................................................................................................
global $gaRuleSetLineDirStringLenPlusOne[1] ;stringlen($gaRuleSet[$i][1] & "\") for every "IncDir:","ExcDir:","IncDirRec:","ExcDirRec:" line in $gaRuleSet[]
global $gaRuleSetLineBackslashCount[1] ;number of backslashes for every "IncDir:" and "ExcDir:" line in $gaRuleSet[]
global $giCurrentDirBackslashCount ;number of backslashes in the currently scanned path
global $gaFileInfo[25] ;array with informations about the file
#cs
$gaFileInfo[0] ;name
$gaFileInfo[1] ;file could not be read 1 else 0
$gaFileInfo[2] ;size
$gaFileInfo[3] ;attributes (obsolete)
$gaFileInfo[4] ;file modification timestamp
$gaFileInfo[5] ;file creation timestamp
$gaFileInfo[6] ;file accessed timestamp
$gaFileInfo[7] ;version
$gaFileInfo[8] ;8.3 short path+name
$gaFileInfo[9] ;sha1 hash
$gaFileInfo[10] ;md5 hash
$gaFileInfo[11] ;time it took to process the file
$gaFileInfo[12] ;rulename
$gaFileInfo[13] ;1 if the "R" = READONLY attribute is set
$gaFileInfo[14] ;1 if the "A" = ARCHIVE attribute is set
$gaFileInfo[15] ;1 if the "S" = SYSTEM attribute is set
$gaFileInfo[16] ;1 if the "H" = HIDDEN attribute is set
$gaFileInfo[17] ;1 if the "N" = NORMAL attribute is set
$gaFileInfo[18] ;1 if the "D" = DIRECTORY attribute is set
$gaFileInfo[19] ;1 if the "O" = OFFLINE attribute is set
$gaFileInfo[20] ;1 if the "C" = COMPRESSED (NTFS compression, not ZIP compression) attribute is set
$gaFileInfo[21] ;1 if the "T" = TEMPORARY attribute is set
$gaFileInfo[22] ;volume serial
$gaFileInfo[23] ;number of links
$gaFileInfo[24] ;file id
#ce
global $gsExtWithVersion = ".exe.com.dll.scr.ocx.sys." ;List of file extensions GetFileinfo() does a FileGetVersion() (expensive !)
global $gsScantime = "" ;date and time of the scan
global $giScanId = 0 ;scanid
global $ghDBHandle = "" ;handle of db
global $gbMSSQL = False ;True if DB is an INI-file with the connection parameter for an MSSQL-server !
global $gsMSSQLDBName = "" ;MS SQL database name
;mailer setup
Global $goMyRet[2]
Global $goMyError = ObjEvent("AutoIt.Error", "SmtpMailErrFunc")
;Global $goMyError = ObjEvent("AutoIt.Error", "_SQL_ErrFunc")
;IsExecutable() global lookup
global $giIsExecutableLastResult = False ;result of last call to IsExecutable()
global $gsIsExecutableLastFilename = "" ;filname used for last call to IsExecutable()
Local $sReportMode = "" ; what report to create (small, medium, large)
#cs
db stucture
scantime time the scan took place
name long filename incl. path
status 1 = file exists
size size of file in byte
attributes file attributes
mtime file modification time
ctime file creation time
atime file access time
version file version information (exe or dll version)
spath short filename incl. path
sha1 sha1 hash of file content
md5 md5 hash of file content
ptime time to process the file while scanning in ms
rulename name of the rule from config file, according to that the file was processed
volume serial number of the volume that contains a file|
links number of links to this file|
fileid unique identifier that is associated with a file|
#ce
;check commandline
if $CmdLine[0] < 1 then
DoShowUsage()
exit (1)
EndIf
select
Case $CmdLine[1] = "/duplicates"
if $CmdLine[0] < 3 then
DoShowUsage()
exit (1)
EndIf
;$sDBName = $CmdLine[2]
;$sScanname = $CmdLine[3]
if FileExists($CmdLine[2]) then
OpenDB($CmdLine[2])
DoDuplicates($CmdLine[3])
CloseDB()
Else
ConsoleWriteError("Error:" & @CRLF & "Database file not found !" & @CRLF & "Filename:" & $CmdLine[2])
EndIf
Case $CmdLine[1] = "/exportscan"
if $CmdLine[0] < 4 then
DoShowUsage()
exit (1)
EndIf
;$sDBName = $CmdLine[2]
;$sScanname = $CmdLine[3]
;$sCSVFilename = $CmdLine[4]
if FileExists($CmdLine[2]) then
OpenDB($CmdLine[2])
DoExportScan($CmdLine[3],$CmdLine[4])
CloseDB()
Else
ConsoleWriteError("Error:" & @CRLF & "Database file not found !" & @CRLF & "Filename:" & $CmdLine[2])
EndIf
Case $CmdLine[1] = "/importcfg"
if $CmdLine[0] < 3 then
DoShowUsage()
exit (1)
EndIf
;$sDBName = $CmdLine[2]
;$ConfigFilename = $CmdLine[3]
OpenDB($CmdLine[2])
DoImportCfg($CmdLine[3])
CloseDB()
Case $CmdLine[1] = "/exportcfg"
if $CmdLine[0] < 3 then
DoShowUsage()
exit (1)
EndIf
;$sDBName = $CmdLine[2]
;$ConfigFilename = $CmdLine[3]
if FileExists($CmdLine[2]) then
OpenDB($CmdLine[2])
DoExportCfg($CmdLine[3])
CloseDB()
Else
ConsoleWriteError("Error:" & @CRLF & "Database file not found !" & @CRLF & "Filename:" & $CmdLine[2])
EndIf
Case $CmdLine[1] = "/validate"
if $CmdLine[0] < 3 then
DoShowUsage()
exit (1)
EndIf
;$sDBName = $CmdLine[2]
;$sScanname = $CmdLine[3]
if FileExists($CmdLine[2]) then
OpenDB($CmdLine[2])
DoValidateScan($CmdLine[3])
CloseDB()
Else
ConsoleWriteError("Error:" & @CRLF & "Database file not found !" & @CRLF & "Filename:" & $CmdLine[2])
EndIf
Case $CmdLine[1] = "/invalidate"
if $CmdLine[0] < 3 then
DoShowUsage()
exit (1)
EndIf
;$sDBName = $CmdLine[2]
;$sScanname = $CmdLine[3]
if FileExists($CmdLine[2]) then
OpenDB($CmdLine[2])
DoInvalidateScan($CmdLine[3])
CloseDB()
Else
ConsoleWriteError("Error:" & @CRLF & "Database file not found !" & @CRLF & "Filename:" & $CmdLine[2])
EndIf
Case $CmdLine[1] = "/delete"
if $CmdLine[0] < 3 then
DoShowUsage()
exit (1)
EndIf
;$sDBName = $CmdLine[2]
;$sScanname = $CmdLine[3]
if FileExists($CmdLine[2]) then
OpenDB($CmdLine[2])
DoDeleteScan($CmdLine[3])
CloseDB()
Else
ConsoleWriteError("Error:" & @CRLF & "Database file not found !" & @CRLF & "Filename:" & $CmdLine[2])
EndIf
Case $CmdLine[1] = "/history"
if $CmdLine[0] < 3 then
DoShowUsage()
exit (1)
EndIf
;$sDBName = $CmdLine[2]
;SEARCHSTRING = $CmdLine[3]
if FileExists($CmdLine[2]) then
OpenDB($CmdLine[2])
DoHistory($CmdLine[3])
CloseDB()
Else
ConsoleWriteError("Error:" & @CRLF & "Database file not found !" & @CRLF & "Filename:" & $CmdLine[2])
EndIf
Case $CmdLine[1] = "/list"
if $CmdLine[0] < 2 then
DoShowUsage()
exit (1)
EndIf
;$sDBName = $CmdLine[2]
if FileExists($CmdLine[2]) then
OpenDB($CmdLine[2])
if $CmdLine[0] = 3 then
DoListScan($CmdLine[3])
Else
DoListScan()
EndIf
CloseDB()
Else
ConsoleWriteError("Error:" & @CRLF & "Database file not found !" & @CRLF & "Filename:" & $CmdLine[2])
EndIf
Case $CmdLine[1] = "/secondprocess"
if $CmdLine[0] < 2 then
DoShowUsage()
exit (1)
EndIf
;$sDBName = $CmdLine[2]
if FileExists($CmdLine[2]) then
OpenDB($CmdLine[2])
DoGetFileinfosOfRelevantFiles()
CloseDB()
Else
ConsoleWriteError("Error:" & @CRLF & "Database file not found !" & @CRLF & "Filename:" & $CmdLine[2])
EndIf
Case $CmdLine[1] = "/scan"
;scan with two processes
if $CmdLine[0] < 2 then
DoShowUsage()
exit (1)
EndIf
if @Compiled or $gcDEBUGRunWithoutCompilation Then
;$sDBName = $CmdLine[2]
if FileExists($CmdLine[2]) then
OpenDB($CmdLine[2])
DoGetListOfRelevantFiles($CmdLine[2])
CloseDB()
Else
ConsoleWriteError("Error:" & @CRLF & "Database file not found !" & @CRLF & "Filename:" & $CmdLine[2])
EndIf
Else
ConsoleWriteError("For the " & $CmdLine[1] & " command to work " & @ScriptName & " must be compiled !")
EndIf
Case $CmdLine[1] = "/report" or $CmdLine[1] = "/reports" or $CmdLine[1] = "/reportm" or $CmdLine[1] = "/reportl"
Select
Case $CmdLine[1] = "/reports"
$sReportMode = "S"
Case $CmdLine[1] = "/reportm"
$sReportMode = "M"
Case $CmdLine[1] = "/reportl"
$sReportMode = "L"
case Else
$sReportMode = "L"
EndSelect
if $CmdLine[0] = 3 then
;@ScriptName /report DB report.txt
if FileExists($CmdLine[2]) then
OpenDB($CmdLine[2])
DoReport($sReportMode,$CmdLine[3])
CloseDB()
Else
ConsoleWriteError("Error:" & @CRLF & "Database file not found !" & @CRLF & "Filename:" & $CmdLine[2])
EndIf
elseif $CmdLine[0] = 4 then
;@ScriptName /report DB SCANNAME report.txt
if FileExists($CmdLine[2]) then
OpenDB($CmdLine[2])
DoReport($sReportMode,$CmdLine[4],"none",$CmdLine[3])
CloseDB()
Else
ConsoleWriteError("Error:" & @CRLF & "Database file not found !" & @CRLF & "Filename:" & $CmdLine[2])
EndIf
elseif $CmdLine[0] = 5 then
;@ScriptName /report DB OLDSCANNAME NEWSCANNAME report.txt
if FileExists($CmdLine[2]) then
OpenDB($CmdLine[2])
DoReport($sReportMode,$CmdLine[5],$CmdLine[3],$CmdLine[4])
CloseDB()
Else
ConsoleWriteError("Error:" & @CRLF & "Database file not found !" & @CRLF & "Filename:" & $CmdLine[2])
EndIf
Else
DoShowUsage()
exit (1)
EndIf
;$sDBName = $CmdLine[2]
;$ReportFilename = $CmdLine[3]
Case $CmdLine[1] = "/help"
DoShowHelp()
Case $CmdLine[1] = "/?"
DoShowHelp()
Case $CmdLine[1] = "/v"
DoShowVersions()
case Else
DoShowUsage()
EndSelect
Exit(0)
;---------------------------------------------------
; Functions
;---------------------------------------------------
;----- core functions -----
Func DoImportCfg($ConfigFilename)
local $iCfgLineNr = 0 ;current line number in $ConfigFilename
local $sTempCfgLine = "" ;content of current line in $ConfigFilename
local $iRuleCounter = 0
local $iCounter = 0
local $aRow = 0
local $iMaxRuleID = 0 ;biggest rule id in $ConfigFilename
local $aRuleIds[1][3] ;
;.....................................................
; RuleName | RuleID | RuleID must be generated
; "Rule:" | "RuleID:" |
;-----------------------------------------------------
; "some rule" | 42 | True
;.....................................................
;read $ConfigFilename
if FileExists($ConfigFilename) then
;read rulenames and ruleids from $ConfigFilename into $aRuleIds[]
$iCfgLineNr = 1
$iRuleCounter = 0
While True
$sTempCfgLine = ""
$sTempCfgLine = FileReadLine($ConfigFilename,$iCfgLineNr)
if @error then
ExitLoop
EndIf
;strip whitespaces at begin and end of line
$sTempCfgLine = StringStripWS($sTempCfgLine,$STR_STRIPLEADING + $STR_STRIPTRAILING)
;find "Rule:" and "RuleID:" statements in $ConfigFilename
Select
case stringleft($sTempCfgLine,stringlen("Rule:")) = "Rule:"
ReDim $aRuleIds[UBound($aRuleIds,1)+1][3]
$iRuleCounter += 1
$aRuleIds[$iRuleCounter][0] = StringTrimLeft($sTempCfgLine,stringlen("Rule:"))
$aRuleIds[$iRuleCounter][1] = 0
$aRuleIds[$iRuleCounter][2] = True
case stringleft($sTempCfgLine,stringlen("RuleID:")) = "RuleID:"
;Is this a valid integer "RuleID:" ?
if StringIsInt(StringTrimLeft($sTempCfgLine,stringlen("RuleID:"))) Then
$aRuleIds[$iRuleCounter][1] = StringTrimLeft($sTempCfgLine,stringlen("RuleID:"))
$aRuleIds[$iRuleCounter][2] = False
;is this a unique "RuleID:" ?
For $iCounter = 1 to $iRuleCounter-1
if $aRuleIds[$iRuleCounter][1] = $aRuleIds[$iCounter][1] and $aRuleIds[$iCounter][1] > 0 then
;this is not a unique rule id !
$aRuleIds[$iRuleCounter][1] = 0
$aRuleIds[$iRuleCounter][2] = True
ExitLoop
EndIf
Next
;is this the biggest rule id ?
if $aRuleIds[$iRuleCounter][1] > $iMaxRuleID then $iMaxRuleID = $aRuleIds[$iRuleCounter][1]
EndIf
case Else
EndSelect
$iCfgLineNr += 1
WEnd
;rebuild table config in DB
if $gbMSSQL Then
_SQL_Execute(-1,"DROP TABLE config;")
_SQL_Execute(-1,"CREATE TABLE [config] ([linenumber] INTEGER IDENTITY(1,1) ,[line] NTEXT NULL ,CONSTRAINT [config_PRIMARY] PRIMARY KEY NONCLUSTERED ([linenumber])); ")
Else
_SQLite_Exec(-1,"DROP TABLE IF EXISTS config;")
_SQLite_Exec(-1,"CREATE TABLE IF NOT EXISTS config (linenumber INTEGER PRIMARY KEY AUTOINCREMENT, line );")
EndIf
;import $ConfigFilename into table config
$iCfgLineNr = 1
$iRuleCounter = 0
While True
$sTempCfgLine = ""
$sTempCfgLine = FileReadLine($ConfigFilename,$iCfgLineNr)
if @error then
ExitLoop
EndIf
;find "Rule:" statements in $ConfigFilename
Select
case stringleft(StringStripWS($sTempCfgLine,$STR_STRIPLEADING + $STR_STRIPTRAILING),stringlen("Rule:")) = "Rule:"
$iRuleCounter += 1
if $gbMSSQL Then
_SQL_Execute(-1,"INSERT INTO [config] ([line]) VALUES (N'" & _StringToHex($sTempCfgLine) & "');")
Else
_SQLite_Exec(-1,"INSERT INTO config(line) values ('" & _StringToHex($sTempCfgLine) & "');")
EndIf
if $aRuleIds[$iRuleCounter][2] = True then
;let's generate a new unique "RuleID:" and put it in the DB
$iMaxRuleID += 1
$aRuleIds[$iRuleCounter][1] = $iMaxRuleID
if $gbMSSQL Then
_SQL_Execute(-1,"INSERT INTO [config] ([line]) VALUES (N'" & _StringToHex("# DO NOT CHANGE THE NEXT LINE ! - GENERATED BY " & @ScriptName) & "');")
;_SQL_Execute(-1,"INSERT INTO [config] ([line]) VALUES (N'" & _StringToHex("RuleID:" & $iMaxRuleID) & "');")
_SQL_Execute(-1,"INSERT INTO [config] ([line]) VALUES (N'" & _StringToHex("RuleID:" & GetNewRuleIDFromDB($aRuleIds[$iRuleCounter][0])) & "');")
Else
_SQLite_Exec(-1,"INSERT INTO config(line) values ('" & _StringToHex("# DO NOT CHANGE THE NEXT LINE ! - GENERATED BY " & @ScriptName) & "');")
;_SQLite_Exec(-1,"INSERT INTO config(line) values ('" & _StringToHex("RuleID:" & $iMaxRuleID) & "');")
_SQLite_Exec(-1,"INSERT INTO config(line) values ('" & _StringToHex("RuleID:" & GetNewRuleIDFromDB($aRuleIds[$iRuleCounter][0])) & "');")
EndIf
EndIf
case stringleft(StringStripWS($sTempCfgLine,$STR_STRIPLEADING + $STR_STRIPTRAILING),stringlen("# DO NOT CHANGE THE NEXT LINE ! - GENERATED BY " & @ScriptName)) = "# DO NOT CHANGE THE NEXT LINE ! - GENERATED BY " & @ScriptName
if $aRuleIds[$iRuleCounter][2] = False then
if $gbMSSQL Then
_SQL_Execute(-1,"INSERT INTO [config] ([line]) VALUES (N'" & _StringToHex($sTempCfgLine) & "');")
Else
_SQLite_Exec(-1,"INSERT INTO config(line) values ('" & _StringToHex($sTempCfgLine) & "');")
EndIf
EndIf
case stringleft(StringStripWS($sTempCfgLine,$STR_STRIPLEADING + $STR_STRIPTRAILING),stringlen("RuleID:")) = "RuleID:"
if $aRuleIds[$iRuleCounter][2] = False then
if $gbMSSQL Then
;Update rulename for the given "RuleID:"
if _SQL_QuerySingleRow(-1,"SELECT rulename FROM rules where ruleid = " & $aRuleIds[$iRuleCounter][1] & ";",$aRow) = $SQL_OK and $aRow[0]<>"" Then
_SQL_Execute(-1,"UPDATE [rules] SET [rulename]='" & _StringToHex($aRuleIds[$iRuleCounter][0]) & "' WHERE [ruleid] = " & $aRuleIds[$iRuleCounter][1] & ";")
Else
_SQL_Execute(-1,"SET IDENTITY_INSERT rules ON; " & "INSERT INTO rules ([ruleid],[rulename]) VALUES(" & $aRuleIds[$iRuleCounter][1] & ",'" & _StringToHex($aRuleIds[$iRuleCounter][0]) & "');" & " SET IDENTITY_INSERT rules OFF;")
EndIf
_SQL_Execute(-1,"INSERT INTO [config] ([line]) VALUES (N'" & _StringToHex($sTempCfgLine) & "');")
Else
;Update rulename for the given "RuleID:"
;### !!! THIS IS NOT TESTED !!! ###
if _SQLite_QuerySingleRow(-1,'SELECT rulename FROM rules where ruleid = ' & $aRuleIds[$iRuleCounter][1] & ';',$aRow) = $SQLITE_OK Then
_SQLite_Exec(-1,'UPDATE rules SET rulename="' & _StringToHex($aRuleIds[$iRuleCounter][0]) & '" WHERE ruleid = ' & $aRuleIds[$iRuleCounter][1] & ';')
Else
_SQLite_Exec(-1,'INSERT INTO rules VALUES(' & $aRuleIds[$iRuleCounter][1] & ',"' & _StringToHex($aRuleIds[$iRuleCounter][0]) & '");')
EndIf
_SQLite_Exec(-1,"INSERT INTO config(line) values ('" & _StringToHex($sTempCfgLine) & "');")
EndIf
EndIf
case Else
if $gbMSSQL Then
_SQL_Execute(-1,"INSERT INTO [config] ([line]) VALUES (N'" & _StringToHex($sTempCfgLine) & "');")
Else
_SQLite_Exec(-1,"INSERT INTO config(line) values ('" & _StringToHex($sTempCfgLine) & "');")
EndIf
EndSelect
$iCfgLineNr += 1
WEnd
EndIf
EndFunc
Func DoExportCfg($ConfigFilename)
local $aQueryResult = 0 ;result of a query
local $hQuery = 0 ;handle to a query
if FileExists($ConfigFilename) then
FileDelete($ConfigFilename)
EndIf
;export table config into $ConfigFilename
$aQueryResult = 0
$hQuery = 0
if $gbMSSQL Then
$hQuery = _SQL_Execute(-1, "SELECT line FROM config ORDER BY linenumber ASC;")
While _SQL_FetchData($hQuery, $aQueryResult) = $SQL_OK
FileWriteLine($ConfigFilename,_HexToString($aQueryResult[0]))
WEnd
Else
_SQLite_Query(-1, "SELECT line FROM config ORDER BY linenumber ASC;",$hQuery)
While _SQLite_FetchData($hQuery, $aQueryResult) = $SQLITE_OK
FileWriteLine($ConfigFilename,_HexToString($aQueryResult[0]))
WEnd
_SQLite_QueryFinalize($hQuery)
EndIf
EndFunc
Func DoDuplicates($sScanname)
;list duplicate files in a scan
;based on size,sha1,md5
;------------------------------------------------
local $aQueryResult = 0 ;result of a query
local $hQuery = 0 ;handle to a query
local $sLastCrit = ""
local $sLastFilename = ""
local $sTempSQL = ""
local $iLastLinePrinted = False
$aQueryResult = 0
if GetScannamesFromDB($sScanname,$aQueryResult) Then
;for $i = 2 to $aQueryResult[0]
$sTempSQL = "SELECT "
$sTempSQL &= "scans.scantime as scantime,"
$sTempSQL &= "filenames.path as path,"
$sTempSQL &= "filedata.status as status,"
$sTempSQL &= "filedata.size as size,"
$sTempSQL &= "filedata.sha1 as sha1,"
$sTempSQL &= "filedata.md5 as md5 "
;$sTempSQL &= "count(filedata.md5) "
$sTempSQL &= "FROM scans,filedata,filenames "
$sTempSQL &= "WHERE "
$sTempSQL &= "scans.scantime = '" & $aQueryResult[2] & "' AND "
$sTempSQL &= "filedata.scanid = scans.scanid AND "
$sTempSQL &= "filedata.filenameid = filenames.filenameid AND "
$sTempSQL &= "filedata.status = '0' AND "
$sTempSQL &= "filedata.size <> '0' AND "
$sTempSQL &= "filedata.sha1 <> '0' AND "
$sTempSQL &= "filedata.md5 <> '0' "
$sTempSQL &= "GROUP BY scans.scantime,filedata.status,filedata.size,filedata.sha1,filedata.md5,filenames.path "
$sTempSQL &= "ORDER BY filedata.size,filedata.sha1,filedata.md5,filenames.path ASC;"
ConsoleWrite("scantime,size,sha1,md5,filename" & @CRLF)
$aQueryResult = 0
if $gbMSSQL then
$hQuery = _SQL_Execute(-1, $sTempSQL)
Else
_SQLite_Query(-1, $sTempSQL,$hQuery)
EndIf
While ($gbMSSQL and _SQL_FetchData($hQuery, $aQueryResult) = $SQL_OK) or (not $gbMSSQL and _SQLite_FetchData($hQuery, $aQueryResult) = $SQLITE_OK)
if $aQueryResult[0] & "," & $aQueryResult[3] & "," & $aQueryResult[4] & "," & $aQueryResult[5] = $sLastCrit then
ConsoleWrite($sLastCrit & ",""" & $sLastFilename & """" & @CRLF)
$sLastFilename = _HexToString($aQueryResult[1])
$iLastLinePrinted = True
Else
if $iLastLinePrinted = True then ConsoleWrite($sLastCrit & ",""" & $sLastFilename & """" & @CRLF)
$sLastCrit = $aQueryResult[0] & "," & $aQueryResult[3] & "," & $aQueryResult[4] & "," & $aQueryResult[5]
$sLastFilename = _HexToString($aQueryResult[1])
$iLastLinePrinted = False
EndIf
WEnd
if not $gbMSSQL then _SQLite_QueryFinalize($hQuery)
EndIf
EndFunc
Func DoHistory($sFilename)
;list the history of a filename and how it has
;changed from scan to scan
;------------------------------------------------
local $aQueryResult = 0 ;result of a query
local $hQuery = 0 ;handle to a query
local $sLastFilename = ""
local $sTempSQL = ""
$sTempSQL = "SELECT "
$sTempSQL &= "scans.scantime,"
$sTempSQL &= "filenames.path,"
$sTempSQL &= "scans.valid,"
$sTempSQL &= "filedata.status,"
$sTempSQL &= "filedata.size,"
$sTempSQL &= "filedata.attributes,"
$sTempSQL &= "filedata.mtime,"
$sTempSQL &= "filedata.ctime,"
$sTempSQL &= "filedata.atime,"
$sTempSQL &= "filedata.version,"
$sTempSQL &= "filenames.spath,"
$sTempSQL &= "filedata.sha1,"
$sTempSQL &= "filedata.md5,"
$sTempSQL &= "filedata.ptime,"
$sTempSQL &= "rules.rulename,"
$sTempSQL &= "filedata.rattrib,"
$sTempSQL &= "filedata.aattrib,"
$sTempSQL &= "filedata.sattrib,"
$sTempSQL &= "filedata.hattrib,"
$sTempSQL &= "filedata.nattrib,"
$sTempSQL &= "filedata.dattrib,"
$sTempSQL &= "filedata.oattrib,"
$sTempSQL &= "filedata.cattrib,"
$sTempSQL &= "filedata.tattrib,"
$sTempSQL &= "filedata.volume,"
$sTempSQL &= "filedata.links,"
$sTempSQL &= "filedata.fileid "
$sTempSQL &= "FROM filedata,filenames,rules,scans "
$sTempSQL &= "WHERE "
$sTempSQL &= "filedata.filenameid = filenames.filenameid AND "
$sTempSQL &= "filedata.scanid = scans.scanid AND "
$sTempSQL &= "filedata.ruleid = rules.ruleid"
;$sTempSQL &= ";"
$sTempSQL &= " AND filenames.path like '%" & _StringToHex($sFilename) & "%' "
$sTempSQL &= "ORDER BY filenames.path ASC,scans.scantime ASC;"
OutputLineOfFileHistory($aQueryResult,True)
if $gbMSSQL then
$hQuery = _SQL_Execute(-1, $sTempSQL)
Else
_SQLite_Query(-1, $sTempSQL,$hQuery)
EndIf
While ($gbMSSQL and _SQL_FetchData($hQuery, $aQueryResult) = $SQL_OK) or (not $gbMSSQL and _SQLite_FetchData($hQuery, $aQueryResult) = $SQLITE_OK)
;_ArrayDisplay($aQueryResult)
if $sLastFilename <> _HexToString($aQueryResult[1]) then
if $sLastFilename <> "" then ConsoleWrite(@CRLF)
$sLastFilename = _HexToString($aQueryResult[1])
ConsoleWrite($sLastFilename & @CRLF)
EndIf
OutputLineOfFileHistory($aQueryResult,False)
WEnd
if not $gbMSSQL then _SQLite_QueryFinalize($hQuery)
EndFunc
Func DoDeleteScan($sScanname)
local $aQueryResult = 0 ;result of a query
local $i = 0
local $sSQLStatement = ""
$aQueryResult = 0
if GetScannamesFromDB($sScanname,$aQueryResult) Then
for $i = 2 to $aQueryResult[0]
;_ArrayDisplay($aQueryResult)
if $gbMSSQL then
$sSQLStatement = "delete from filedata where scanid = '" & GetScanIDFromDB($aQueryResult[$i]) & "';"
if $gcDEBUGShowMSSQLDeleteSQLCode then ConsoleWrite( "Debug - Next SQL: " & $sSQLStatement & @CRLF)
_SQL_Execute(-1,$sSQLStatement)
$sSQLStatement = "delete from scans where scantime = '" & $aQueryResult[$i] & "';"
if $gcDEBUGShowMSSQLDeleteSQLCode then ConsoleWrite( "Debug - Next SQL: " & $sSQLStatement & @CRLF)
_SQL_Execute(-1,$sSQLStatement)
$sSQLStatement = "delete [" & $gsMSSQLDBName & "].dbo.filenames from [" & $gsMSSQLDBName & "].dbo.filenames LEFT JOIN [" & $gsMSSQLDBName & "].dbo.filedata ON filedata.filenameid = filenames.filenameid WHERE filedata.filenameid IS NULL;"
if $gcDEBUGShowMSSQLDeleteSQLCode then ConsoleWrite( "Debug - Next SQL: " & $sSQLStatement & @CRLF)
_SQL_Execute(-1,$sSQLStatement)
$sSQLStatement = "delete [" & $gsMSSQLDBName & "].dbo.rules from [" & $gsMSSQLDBName & "].dbo.rules LEFT JOIN [" & $gsMSSQLDBName & "].dbo.filedata ON filedata.ruleid = rules.ruleid WHERE filedata.ruleid IS NULL;"
if $gcDEBUGShowMSSQLDeleteSQLCode then ConsoleWrite( "Debug - Next SQL: " & $sSQLStatement & @CRLF)
_SQL_Execute(-1,$sSQLStatement)
Else
_SQLite_Exec(-1,"delete from filedata where scanid = '" & GetScanIDFromDB($aQueryResult[$i]) & "';")
_SQLite_Exec(-1,"delete from scans where scantime = '" & $aQueryResult[$i] & "';")
_SQLite_Exec(-1,"delete from filenames where (select filenames.filenameid from filenames LEFT JOIN filedata ON filedata.filenameid = filenames.filenameid WHERE filedata.filenameid IS NULL);")
_SQLite_Exec(-1,"delete from rules where (select rules.ruleid from rules LEFT JOIN filedata ON filedata.ruleid = rules.ruleid WHERE filedata.ruleid IS NULL);")
EndIf
next
EndIf
;shrink DB file
if not $gbMSSQL then _SQLite_Exec(-1,"vacuum;")
EndFunc
Func DoReport($sReportMode,$ReportFilename,$sScannameOld = "lastvalid",$sScannameNew = "last")
local $rc = 0 ;mailer return code
local $iEmailReport = False ;send report per email (smtp)
local $sEMailBody = "" ;bodytext of email
local $aEMail[5] ;array with all email infos
$aEMail[4] = 25 ;SMTP default port = 25
#cs
$aEMail[0] ;sender email address
$aEMail[1] ;recipient email address
$aEMail[2] ;email subject
$aEMail[3] ;name of smtp server (hostname or ip-address)
$aEMail[4] ;smtp port on SMTPSERVERNAME, defaults to 25
#ce
;local $sScannameOld = ""
;local $sScannameNew = ""
local $aQueryResult = 0 ;result of a query
local $hQuery = 0 ;handle to a query
local $aCfgQueryResult = 0 ;result of a query on table config
local $hCfgQuery = 0 ;handle to a query on table config
local $sTempCfgLine = ""
local $sTempText = "" ;
local $iTempCount = "" ;
local $i = 0
local $iCountMax = 0
local $iCountMin = 0
local $sTempSQL = ""
local $iRuleNumber = 0
local $iHasRuleHeader = False ;headline for each rule ist already printed
GetRuleSetFromDB()
$sTempSQL = "SELECT "
$sTempSQL &= "scans.scantime,"
$sTempSQL &= "filenames.path,"
;$sTempSQL &= "scans.valid,"
$sTempSQL &= "filedata.status,"