1
1
<?php
2
2
/**
3
- * SigTool v0.2.1 (last modified: 2018.06.28 ).
3
+ * SigTool v0.2.1 (last modified: 2018.10.20 ).
4
4
* Generates signatures for phpMussel using main.cvd and daily.cvd from ClamAV.
5
5
*
6
6
* Package location: GitHub <https://github.com/phpMussel/SigTool>.
@@ -19,7 +19,7 @@ class SigTool
19
19
public $ Ver = '0.2.1 ' ;
20
20
21
21
/** Last modified date. */
22
- public $ Modified = '2018.06.28 ' ;
22
+ public $ Modified = '2018.10.20 ' ;
23
23
24
24
/** Script user agent. */
25
25
public $ UA = 'SigTool v%s (https://github.com/phpMussel/SigTool) ' ;
@@ -368,14 +368,25 @@ public function shorthand(&$Data) {
368
368
], $ Data );
369
369
}
370
370
$ Data = preg_replace ([
371
- '~([^a-z0-9])(?:Agent|General|Generic)([.-])~i ' , /** Let's reduce our footprint. :-) */
372
- '~([^a-z0-9])Downloader([.-])~i ' , /** CVDs use both; Let's normalise it. */
373
- '~^[^\:\n]+\:[^\n]+[\[\]][^\n]*$~m ' , /** ClamAV signature format documentation is unclear about what "[]" means. */
374
- '~^.*This ClamAV version has reached End of Life.*$\n~im ' /** Don't need these in phpMussel signatures! */
371
+ /** Let's reduce our footprint. :-) */
372
+ '~([^a-z0-9])(?:Agent|General|Generic)([.-])~i ' ,
373
+
374
+ /** CVDs use both; Let's normalise it. */
375
+ '~([^a-z0-9])Downloader([.-])~i ' ,
376
+
377
+ /** ClamAV signature format documentation is unclear about what "[]" means. */
378
+ '~^[^\:\n]+\:[^\n]+[\[\]][^\n]*$~m ' ,
379
+
380
+ /** PCRE trips over capture groups at this range sometimes. Let's play it safe and ditch the affected signatures. */
381
+ '~^.*\{-?(?:\d{4,})\}.*$\n~m ' ,
382
+
383
+ /** Not needed in the final generated signature files. */
384
+ '~^.*This ClamAV version has reached End of Life.*$\n~im '
375
385
], [
376
386
'\1X\2 ' ,
377
387
'\1Dldr\2 ' ,
378
388
'' ,
389
+ '' ,
379
390
''
380
391
], $ Data );
381
392
if (md5 ($ Data ) . ': ' . strlen ($ Data ) === $ Check ) {
@@ -817,6 +828,7 @@ public function fixPath($Path) {
817
828
"\x26" => 11
818
829
];
819
830
831
+ /** Begin working through individual signatures. */
820
832
while (($ Pos = strpos ($ FileData , "\n" , $ Offset )) !== false ) {
821
833
$ Last = $ Percent ;
822
834
$ Percent = number_format (($ SigsThis / $ SigsNDB ) * 100 , 2 ) . '% ' ;
@@ -877,21 +889,17 @@ public function fixPath($Path) {
877
889
/** Assign to the appropriate signature file (regex). */
878
890
if (preg_match ('/[^a-f\d*]/i ' , $ SigHex )) {
879
891
880
- /**
881
- * Handle PCRE conversion here (ClamAV to phpMussel formats).
882
- * Note that this may need to be changed/adapted in the future upon
883
- * relevant changes in the source file occurring (currently accounts
884
- * for what we already know about the present, but not for what may
885
- * occur in or may be planned for the future).
886
- */
892
+ /** Convert from ClamAV's pattern syntax to PCRE syntax. */
887
893
$ SigHex = preg_replace ([
888
- '~\{([0-9]+)-([0-9]+)\}~ ' ,
889
- '~\{([0-9]+)-\}~ ' ,
890
- '~\{-([0-9]+)\}~ ' ,
891
- '~\{([0-9]+)\}~ ' ,
894
+ '~^.*\{-?(?:\d{4,})\}.*$~ ' ,
895
+ '~\{(\d+)-(?:\d{4,})?\}~ ' ,
896
+ '~\{(\d+)-(\d+)\}~ ' ,
897
+ '~\{-(\d+)\}~ ' ,
898
+ '~\{(\d+)\}~ ' ,
892
899
], [
893
- '(?:..){\1,\2} ' ,
900
+ '' ,
894
901
'(?:..){\1,} ' ,
902
+ '(?:..){\1,\2} ' ,
895
903
'(?:..){0,\1} ' ,
896
904
'(?:..){\1} ' ,
897
905
], str_replace ([
@@ -901,8 +909,6 @@ public function fixPath($Path) {
901
909
'{0-1} ' ,
902
910
'{0-} ' ,
903
911
'{1-} ' ,
904
- '(30|31|32|33|34|35|36|37|38|39) ' ,
905
- '(31|32|33|34|35|36|37|38|39) ' ,
906
912
'(22|27) ' ,
907
913
'(27|22) ' ,
908
914
], [
@@ -912,12 +918,61 @@ public function fixPath($Path) {
912
918
'.? ' ,
913
919
'.* ' ,
914
920
'.+ ' ,
915
- '3[0-9] ' ,
916
- '3[1-9] ' ,
917
921
'2[27] ' ,
918
922
'2[27] ' ,
919
923
], $ SigHex ));
920
924
925
+ /** Possible character range. */
926
+ $ CharRange = ['0 ' , 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 'a ' , 'b ' , 'c ' , 'd ' , 'e ' , 'f ' ];
927
+
928
+ /** Simplify all the (xx|xx|xx|xx...) stuff into something smaller and more readable. */
929
+ foreach ($ CharRange as $ Char ) {
930
+ $ InnerCharRange = $ CharRange ;
931
+ while (true ) {
932
+ $ Replacer = '( ' ;
933
+ foreach ($ InnerCharRange as $ InnerChar ) {
934
+ $ Replacer .= $ Char . $ InnerChar . '| ' ;
935
+ }
936
+ $ Replacer = substr ($ Replacer , 0 , -1 ) . ') ' ;
937
+ $ FinalLast = array_pop ($ InnerCharRange ) ?: '' ;
938
+ $ InnerCharCount = count ($ InnerCharRange );
939
+ if (!$ InnerCharCount ) {
940
+ break ;
941
+ }
942
+ if ($ InnerCharCount === 9 ) {
943
+ $ Replacement = $ Char . '\d ' ;
944
+ } elseif ($ InnerCharCount < 9 ) {
945
+ $ Replacement = $ Char . '[0- ' . $ FinalLast . '] ' ;
946
+ } else {
947
+ $ Replacement = $ InnerCharCount === 10 ? $ Char . '[\da] ' : $ Char . '[\da- ' . $ FinalLast . '] ' ;
948
+ }
949
+ $ SigHex = str_replace ($ Replacer , $ Replacement , $ SigHex );
950
+ }
951
+ }
952
+
953
+ /** Upper-lower case stuff, and further simplification. */
954
+ foreach ($ CharRange as $ Char ) {
955
+ $ SigHex = str_replace ([
956
+ '(4 ' . $ Char . '|6 ' . $ Char . ') ' ,
957
+ '(6 ' . $ Char . '|4 ' . $ Char . ') ' ,
958
+ '(5 ' . $ Char . '|7 ' . $ Char . ') ' ,
959
+ '(7 ' . $ Char . '|5 ' . $ Char . ') ' ,
960
+ '(?:..){4} ' ,
961
+ '(?:..){3} ' ,
962
+ '(?:..){2} ' ,
963
+ '(?:..){1} '
964
+ ], [
965
+ '[46] ' . $ Char ,
966
+ '[46] ' . $ Char ,
967
+ '[57] ' . $ Char ,
968
+ '[57] ' . $ Char ,
969
+ '.{8} ' ,
970
+ '.{6} ' ,
971
+ '.... ' ,
972
+ '.. '
973
+ ], $ SigHex );
974
+ }
975
+
921
976
/** Newly formatted signature line. */
922
977
$ ThisLine = $ SigName . ': ' . $ SigHex . $ StartStop . "\n" ;
923
978
0 commit comments