-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfw1rules.pl
4287 lines (3933 loc) · 177 KB
/
fw1rules.pl
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
#!/usr/bin/perl
#############################################################################
# fw1rules.pl: Analyze and print a report of Firewall-1 rules and objects
# Note: unsupported by Checkpoint or representatives.
#
# Copyright (C) 2000-2004 Volker Tanger
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# For bug reports and suggestions or if you just want to talk to me please
# contact me at [email protected]
#
# Updates will be available at http://www.wyae.de/software/
# please check there for updates prior to submitting patches!
#
# For list of changes please refer to the HISTORY file. Thanks.
#############################################################################
#
# Used modules
#
use Getopt::Long;
use File::Basename;
$VERSION = '7.3.42'; ###versioninformation###
$SCR_NAME = 'FW1rules';
$HTMLtitle = 'FireWall-1 Rule Base and Object Definitions';
$HTMLcssfile = 'fw1rules.css';
$FW1rules = 'Standard.W';
$FW1objects = 'objects.C';
$FW1user = 'fwauth.NDB';
$IconPathName = 'icons';
$Match_Logic = '(src && dst && svc && com)';
$LogFile = 'FW1Rules.log';
$DebugFile = 'FW1Rules.debug.log';
$TemplateINCLUDE = '';
$TemplateITEMSEP = ' ';
%colors =
( # NG colors included, now 36 colors
'aquamarine1' => '#7BFFD6',
'black' => '#000000',
'blue' => '#0000FF',
'blue1' => '#4241EF',
'burlywood4' => '#8C7152',
'cyan' => '#00FFFF',
'dark green' => '#848200',
'dark khaki' => '#BDB66B',
'dark orchid' => '#840084',
'darkorange3' => '#CE6500',
'darkseagreen3' => '#9CCF9C',
'deep pink' => '#FF1494',
'deepskyblue1' => '#00BEFF',
'dodgerblue3' => '#1875CE',
'firebrick' => '#B52021',
'foreground' => '#000000',
'forest green' => '#218A21',
'gold' => '#FFD700',
'gold3' => '#CEAE00',
'gray83' => '#D6D7D6',
'gray90' => '#ADB2C6',
'green' => '#00FF00',
'lemonchiffon' => '#FFFBCE',
'light coral' => '#F78284',
'lightseagreen' => '#21B2AD',
'lightskyblue4' => '#63798C',
'magenta' => '#FF00FF',
'medium orchid' => '#BD55D6',
'medium slate blue' => '#7B69EF',
'medium violet red' => '#C61484',
'navy blue' => '#000084',
'olive drab' => '#6B8E21',
'orange' => '#FFA600',
'red' => '#FF0000',
'sienna' => '#A55129',
'yellow' => '#FFFF00'
);
%netmasktranslation = (
'255.255.255.255' => '32',
'255.255.255.254' => '31',
'255.255.255.252' => '30',
'255.255.255.248' => '29',
'255.255.255.240' => '28',
'255.255.255.224' => '27',
'255.255.255.192' => '26',
'255.255.255.128' => '25',
'255.255.255.0' => '24',
'255.255.254.0' => '23',
'255.255.252.0' => '22',
'255.255.248.0' => '21',
'255.255.240.0' => '20',
'255.255.224.0' => '19',
'255.255.192.0' => '18',
'255.255.128.0' => '17',
'255.255.0.0' => '16',
'255.254.0.0' => '15',
'255.252.0.0' => '14',
'255.248.0.0' => '13',
'255.240.0.0' => '12',
'255.224.0.0' => '11',
'255.192.0.0' => '10',
'255.128.0.0' => '9',
'255.0.0.0' => '8',
'254.0.0.0' => '7',
'252.0.0.0' => '6',
'248.0.0.0' => '5',
'240.0.0.0' => '4',
'224.0.0.0' => '3',
'192.0.0.0' => '2',
'128.0.0.0' => '1',
'0.0.0.0' => '0' );
%NATtranslation = (
0 => 'hide',
1 => 'static' );
$SynDefender[0] = 'None';
$SynDefender[1] = 'SYN Relay';
$SynDefender[2] = 'SYN Gateway';
$SynDefender[3] = 'Passive SYN Gateway';
%ICMPtranslate = (
'icmp_type=ICMP_ECHOREPLY' => 0,
'icmp_type=ICMP_UNREACH', => 3,
'icmp_type=ICMP_SOURCEQUENCH', => 4,
'icmp_type=ICMP_REDIRECT', => 5,
'icmp_type=ICMP_ECHO', => 8,
'icmp_type=ICMP_TIMXCEED' => 11,
'icmp_type=ICMP_PARAMPROB', => 12,
'icmp_type=ICMP_TSTAMP', => 13,
'icmp_type=ICMP_TSTAMPREPLY', => 14,
'icmp_type=ICMP_IREQ', => 15,
'icmp_type=ICMP_IREQREPLY', => 16,
'icmp_type=ICMP_MASKREQ', => 17,
'icmp_type=ICMP_MASKREPLY', => 18,
);
##########################################################################
# print out Usage
sub Usage{
print STDERR "
-----------------------------------------------------------------------
FW1Rules USAGE
-----------------------------------------------------------------------
fw1rules.pl
[--objects=<objects file>]
[--rules=<rules file>]
[--merge_SP3=<FWS rules file>] or [--merge_AI=<FWS rules file>]
[--all_objects] [--all_services]
[--no_rules] [--no_objects] [--no_services]
[--no_servers]
[--with_implicit_rules] [--with_ip]
[--with_interfaces] [--with_antispoofing]
[--show_members]
[--sort_by_type]
[--match_comment=<string>] [--match_installon=<string>]
[--match_source=<string>] [--match_destination=<string>]
[--match_service=<string>] [--match_case]
[--match_logic=<string>]
[--verbose] [--debug] [--version]
[--dump_unused_objects=<text file>]
[--dump_unused_objects_tsv=<text file>]
[--use-css=<css file>]
[--with_colors] [--icon_path=<path>]
[--link_to=<directory>]
[--title=<document title>]
[--output_html=<html file>]
[--template=<template file>]
[--template_include=<template include filename>]
[--unused_objects]
[--hosts_only]
[--gateways_only]
[--networks_only]
[--groups_only]
[--expand_object_groups]
[--expand_service_groups]
[--output=<filled template file>]
Optional Parameters:
--------------------
--rules=<rule file>: Location of FireWall-1 rule file.
Default is 'Standard.W'
--merge_SP3=<FWS rule file> or
--merge_AI=<FWS rule file>: Location of FireWall-1 SP3 rulebases file
Merges <rule file> with comments of <FWS rule file>
eg. 'rulebases_5_0.fws'
--objects=<objects file>: Location of FireWall-1 objects file.
Default is 'objects.C' which is good for V4.1,
please use 'objects_5_0.C' if you are using CKP-NG
--all_objects / --all_services :
it will list ALL objects/services. By default only those
in use will be listed.
--no_rules / --no_objects / --no_services / --no_servers:
it will NOT read nor list rules/objects/services/servers.
Default disabled.
--no_rules implies --all_objs and --all_services
--with_implicit_rules: include the implicit rules into the tables
--with_ip: prints IP addresses along with the object names into the
access rule & objects dump for HTML.
(Note: this does not affect templates or dumps - see below)
--with_interfaces: prints interfaces per network object for HTML.
(Note: this does not affect templates or dumps - see below)
--with_antispoofing: prints antispoofing imnformation in the comment
fields of the network object for HTML.
(Note: this does not affect templates or dumps - see below)
--show_members: lists groups with their members in access/NAT rules.
(Note: this does not affect templates or dumps - see below)
--sort_by_type: use object and service type as primary sorting key
to group them. Secondary key is alphabetically then. Default
sorting is just plain alphabetically (case ignore).
--match_case: search case sensitive for all --match_options below
--match_comment=<string> / --match_installon=<string> /
--match_source=<string> / --match_destination=<string> /
--match_service=<string>:
Only those NAT and access rules will be printed that match the
literal string (NOT RegEx!) given as parameter. The objects
and services used will be limited accordingly unless the
corresponding --all... switches are given.
--match_logic=<string>: Match Pattern Logic (src/dst/svc/com)
src = Source, dst = Destination, svc = Serivce, com = Comment
Pattern Operator: && = AND, || = OR, ! = NOT
eg.: --match_logic='com && ( !dst )\'
or : --match_logic='com && ( src || dst )'
Default: --match_logic='( src && dst && svc && com )'
You will still have to give all the --match_... parameters!
--with_colors: Add html color tags to network and service objects
to reflect the colors used in the FW-1 GUI. By default, this
is OFF to enhance readability (e.g. on printouts).
--icon_path=<path>: Location of the icon graphic files.
Defaults is './icons'
--use-css=<css file>: include reference to the CSS file given (for
nicer output). Default is './fw1rules.css'
--title=<title>: Here you can set a custom document title.
Defaults to the ruleset filename (as given or Standard.W).
--link_to=<directory>: references put in [squarebrackets] into the
comment field will be HTML-linked to the file
<directory>/squarebrackets.html
or <directory>/squarebrackets.pdf
for easier rule/project/background documentation
--verbose: prints debugging information to STDERR and FWrules.log
--version: prints version and exists
--expand_object_groups: checking a rulebase with groups or
groups within groups can be a horrible job; this option
expands object groups and removes duplicates
--expand_service_groups: checking a rulebase with groups or
groups within groups can be a horrible job; this option
expands object groups and removes duplicates
To give an overall documentation of the ruleset (and used objects) you
can print the output into different file formats. Other formats will
be supported as soon as someone supplies according documentation or
patch. You can call any number of these options (okay, only one per
file type per program run):
--output_html=<html file>
For cleaning up firewall rulesets a list of unused objects comes in
very handy, so these functions sort out the culprits for you:
--dump_unused_objects=<text file>
--dump_unused_objects_tsv=<text files>
To fill a custom template with the current firewall configuration you
have to give a template file and the name of the resulting file. You
only can give one template/output pair per program run.
A number of predefined templates is supplied with the tarball in the
subdirectory templates/
A special case is the option --template_include where one can set the
template variable <<<include>>> to be replaced with the command line
parameter. This comes in handy when defining files to be included e.g.
for producing complete documentations - see the template called
templates/fullconfig.txt
--template=<template file>
--template_include=<template include filename>
--output=<filled template file>
--unused_objects
--hosts_only
--gateways_only
--networks_only
--groups_only
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!!! WARNING !!!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
The following functions to dump objects, service definitions or rules
into TabSeparated or TXT files will be removed from future versions of
FW1Rules. Please use the proper template files instead.
--dump_nat_tsv=<tab separated NAT rules file>
--dump_nat_txt=<NAT rules text file>
--dump_objects_tsv=<tab separated objects file>
--dump_objects_txt=<objects text file>
--dump_rules_tsv=<tab separated rules file>
--dump_rules_txt=<rules text file>
--dump_services_tsv=<tab separated services file>
--dump_services_txt=<services text file>
--dump_unused_objects=<text file>
--dump_unused_objects_tsv=<text files>
--dump_properties=<properties text file>
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!!! WARNING !!!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
\n";
}
##########################################################################
# correct Micro$oft stuff (line end, spaces)
sub fromdos {
$line = $_[0];
$line =~ s/\n//g;
$line =~ s/\r//g;
$line =~ s/ /\t/g;
return $line;
}
##########################################################################
# print out comments / errors
sub PrintLog{
my ($msg) = $_[0];
if ($FLAG_verbose){
print STDERR "$msg";
print LOGFILE "$msg";
}
}
##########################################################################
# print out comments / errors
sub DebugLog{
my ($msg) = $_[0];
if ($FLAG_debug){
print DEBUGFILE "$msg\n";
}
}
##########################################################################
# print only if second parameter not empty
sub PrintNonempty{
my ($FILE) = $_[0];
my ($first) = $_[1];
my ($second) = $_[2];
if ( "$second" ne '' ) {
printf $FILE ("$first","$second");
}
}
##########################################################################
# read all network entities/objects defined
#
# Object variables where obj_name equals the hash for each of these:
#
# $obj_number = number of objects
# @obj_name = names of all objects
# %obj_type = host, network, gateway, group
# %obj_location = 0=internal, 1=external
# %obj_is_fw1 = has FW1 installed? 0=false, 1=true
# %obj_ipaddr = IP Address
# %obj_netmask = netmask
# %obj_NATadr = NAT address for implicit NAT
# %obj_NATtype = 0=hide, 1=static
# %obj_members = members, if a group
# %obj_comment = comment for the object
# %obj_colour = colour the object is to be displayed with
# %obj_used = count objects usage in the rulebase
# (set later when evaluating the ruleset)
# %obj_if_number = Number of interfaces added to an object
#
# Object variables where NICinterfacenumber.obj_name equals the hash
# %obj_if_name = Name of the interface added
# %obj_if_ipaddr = IP Address of the interface added
# %obj_if_netmask = Netmask of the interface added
sub ReadNetworkObjects{
my ($dummy) = '';
my ($name) = '';
my ($lineparam) = '';
my ($amember) = '';
my ($members) = '';
$obj_number = 0;
$mode_cluster_members = 0;
while ( ($line = <INFILE> ) && ( fromdos("$line") ne "\t)" ) ) {
$line = &fromdos($line);
&DebugLog("Obj.READ1: $line");
while ( $line !~ /\t\t\: \(/ ) {
$line = <INFILE>;
$line = &fromdos($line);
&DebugLog("Obj.READ2: $line");
}
($dummy,$name) = split(/\(/,$line,2) ;
$obj_if_number{$name} = 0;
$amember = '';
$members = '';
while ( ($line = <INFILE>) && ( fromdos("$line") ne "\t\t)" ) ) {
$line = &fromdos($line);
&DebugLog("Obj.READ3: $line");
($dummy,$lineparam) = split(/\(/,$line,2) ;
$lineparam =~ s/\)$//;
if ( $line =~ /^\t\t\t:type \(/ ){
$obj_type{$name} = lc($lineparam);
} elsif ( $line =~ /^\t\t\t:location \(/ ){
$obj_location{$name} = ("$lineparam" eq 'external') * 1;
} elsif ( $line =~ /^\t\t\t:firewall \(/ ){
$obj_is_fw1{$name} = ("$lineparam" eq 'installed') * 1;
} elsif ( $line =~ /^\t\t\t:ipaddr \(/ ){
if($obj_if_number{$name} == 0){
$obj_if_number{$name} = -1;
}
$obj_ipaddr{$name} = "$lineparam";
} elsif ( $line =~ /^\t\t\t:ipaddr_first \(/ ){
$obj_netmask{$name} = "$lineparam";
} elsif ( $line =~ /^\t\t\t:ipaddr_last \(/ ){
$obj_netmask{$name} = "$obj_netmask{$name} - $lineparam";
} elsif ( $line =~ /^\t\t\t:netmask \(/ ){
$obj_netmask{$name} = $lineparam;
} elsif ( $line =~ /^\t\t\t:valid_ipaddr \(/ ){
$obj_NATadr{$name} = $lineparam;
} elsif ( $line =~ /^\t\t\t:netobj_adtr_method \(/ ){
$obj_NATtype{$name} = ("$lineparam" eq 'adtr_static') * 1;
} elsif ( $line =~ /^\t\t\t:comments \(/ ){
$obj_comment{$name} = $lineparam;
$obj_comment{$name} =~ s/^\"|\"$//g; #--- remove " at beginning and end
$obj_comment{$name} =~ s/;/ /g;
} elsif ( $line =~ /^\t\t\t:color \(/ ){
$obj_colour{$name} = lc($lineparam);
$obj_colour{$name} =~ s/^\"|\"$//g; #--- remove " at beginning and end
} elsif ( $line =~ /^\t\t\t: \(ReferenceObject/ ){
while ( ($line = <INFILE>) && ( fromdos("$line") ne "\t\t\t)" ) ) {
$line = &fromdos($line);
&DebugLog("Obj.READ4: $line");
if ( $line =~ /^\t\t\t\t:Name \(/) {
($dummy,$lineparam) = split(/\(/,$line,2) ;
$lineparam =~ s/\)$//;
$members = "$members§$lineparam";
}
}
# The 'if' clause adds the member to $members only if the current mode is 'cluster_members'.
# This prevents the 'cluster masters' from being added to $members.
}elsif($line =~ /^\t\t\t\t:\S+ \(ReferenceObject/){
while(($line = <INFILE>) && (fromdos("$line") ne "\t\t\t\t)")){
$line = fromdos($line);
if($line =~ /^\t\t\t\t\t:Name \(/){
if($mode_cluster_members){
($dummy,$lineparam) = split(/\(/,$line,2);
$lineparam =~ s/\)$//;
$members = "$members§$lineparam";
}
}
}
# process members of 'group_with_exclusion' objects.
# First the base members :
}elsif($line =~ /^\t\t\t:base \(ReferenceObject/){
while(($line = <INFILE>) && (fromdos("$line") ne "\t\t\t)")){
$line = fromdos($line);
if($line =~ /^\t\t\t\t:Name \(/){
($dummy,$lineparam) = split(/\(/,$line,2);
$lineparam =~ s/\)$//;
$obj_members_base{$name} = $lineparam;
&SetObjUsed("$lineparam");
last;
}
}
# Now the excluded members:
}elsif($line =~ /^\t\t\t:exception \(ReferenceObject/){
while(($line = <INFILE>) && (fromdos("$line") ne "\t\t\t)")){
$line = fromdos($line);
if($line =~ /^\t\t\t\t:Name \(/){
($dummy,$lineparam) = split(/\(/,$line,2);
$lineparam =~ s/\)$//;
$obj_members_exception{$name} = $lineparam;
&SetObjUsed("$lineparam");
last;
}
}
} elsif ( $line =~ /^\t\t\t: / ){
($dummy,$amember) = split(/: /,$line,2) ;
$members = "$members§$amember";
} elsif ( ($line =~ /^\t\t\t:if-(.|..) \(/ ) && ($FLAG_withinterface) ){
$obj_if_number{$name} = $1 + 1;
while ( ($line = <INFILE>) && ( fromdos("$line") ne "\t\t\t)" ) ) {
$line = &fromdos($line);
($dummy,$lineparam) = split(/\(/,$line,2) ;
$lineparam =~ s/\)$//;
if ( ($line =~ /^\t\t\t\t:iffullname \(/) || ($line =~ /^\t\t\t\t:officialname \(/) ){
$obj_if_name{"NIC$obj_if_number{$name}\.$name"} = $lineparam;
} elsif ( $line =~ /^\t\t\t\t:ipaddr \(/ ){
$obj_if_ipaddr{"NIC$obj_if_number{$name}\.$name"} = $lineparam;
} elsif ( $line =~ /^\t\t\t\t:netmask \(/ ){
$obj_if_netmask{"NIC$obj_if_number{$name}\.$name"} = $lineparam;
# process anti-spoofing settings for 4.1.
}elsif($line =~ /^\t\t\t\t:netaccess \(Others/){
$obj_if_spoof{"NIC$obj_if_number{$name}\.$name"} = "Others";
}elsif($line =~ /^\t\t\t\t:netaccess \(\" \+ (.*)\"/){
$accessobj = "$1";
$obj_if_spoof{"NIC$obj_if_number{$name}\.$name"} = "Others + " . $accessobj;
&SetObjUsed("$accessobj");
}elsif($line =~ /^\t\t\t\t:netaccess \(\"This Net\"/){
$obj_if_spoof{"NIC$obj_if_number{$name}\.$name"} = "ThisNet";
}elsif($line =~ /^\t\t\t\t\t:refname \(\"\#_(.*)\"\)/){
$accessobj = "$1";
$obj_if_spoof{"NIC$obj_if_number{$name}\.$name"} = $accessobj;
&SetObjUsed("$accessobj");
}
}
} elsif(($line =~ /^\t\t\t\t:([0-9]|[0-9][0-9]) \(/) && ($FLAG_withinterface)){
$obj_if_number{$name} = $1 + 1;
while(($line = <INFILE>) && (fromdos("$line") ne "\t\t\t\t)")){
$line = fromdos($line);
($dummy,$lineparam) = split(/\(/,$line,2);
$lineparam =~ s/\)$//;
if(($line =~ /^\t\t\t\t\t:iffullname \(/) || ($line =~ /^\t\t\t\t\t:officialname \(/)){
$obj_if_name{"NIC$obj_if_number{$name}\.$name"} = $lineparam;
}elsif($line =~ /^\t\t\t\t\t:ipaddr \(/){
$obj_if_ipaddr{"NIC$obj_if_number{$name}\.$name"} = $lineparam;
}elsif($line =~ /^\t\t\t\t\t:netmask \(/){
$obj_if_netmask{"NIC$obj_if_number{$name}\.$name"} = $lineparam;
# process anti-spoofing settings for NG.
}elsif($line =~ /^\t\t\t\t\t\t:access \(this/){
$obj_if_spoof{"NIC$obj_if_number{$name}\.$name"} = "ThisNet";
}elsif($line =~ /^\t\t\t\t\t\t\t:Name \((.*)\)/){
$obj_if_spoof{"NIC$obj_if_number{$name}\.$name"} = $1;
&SetObjUsed("$1");
}
}
}
if($line =~ /^\t\t\t:cluster_members \(/){
$mode_cluster_members = 1;
}elsif($line =~ /^\t\t\t:/){
$mode_cluster_members = 0;
}
}
if ( ("$obj_type{$name}" eq 'group') || ("$obj_type{$name}" eq 'gateway_cluster') ) {
($dummy,$members) = split (/§/, $members, 2);
$obj_members{$name} = $members;
}
$obj_name[$obj_number] = $name;
$obj_number += 1;
&PrintLog('.');
}
$obj_type{'Any'} = 'any';
if ($FLAG_sortbytype) {
@obj_name = sort { $obj_type{"$a"} cmp $obj_type{"$b"} or lc($a) cmp lc($b) } @obj_name;
} else {
@obj_name = sort { lc($a) cmp lc($b) } @obj_name;
}
}
#=====================================================================
sub ReadNetobjadtr{
my ($dummy) = '';
my ($name) = '';
my ($lineparam) = '';
my ($amember) = '';
my ($members) = '';
my ($eof_flag) = 0;
while ( ($line = <INFILE> ) && ( fromdos("$line") ne "\t)" ) ) {
$line = &fromdos($line);
&DebugLog("NetObj.READ1: $line");
$eof_flag = 0;
while ( ($line !~ /\t\t\: \(/ ) && ( ! $eof_flag ) ) {
$eof_flag = ($line = <INFILE>);
$line = &fromdos($line);
&DebugLog("NetObj.READ2: $line");
}
($dummy,$name) = split(/\(/,$line,2) ;
$amember = '';
$members = '';
while ( ($line = <INFILE>) && ( fromdos("$line") ne "\t\t)" ) ) {
$line = &fromdos($line);
&DebugLog("NetObj.READ3: $line");
($dummy,$lineparam) = split(/\(/,$line,2) ;
$lineparam =~ s/\)$//;
if ( $line =~ /^\t\t\t:type \(/ ){
$obj_type{$name} = lc($lineparam);
} elsif ( $line =~ /^\t\t\t:ipaddr_first \(/ ){
$obj_netmask{$name} = "$lineparam";
} elsif ( $line =~ /^\t\t\t:ipaddr_last \(/ ){
$obj_netmask{$name} = "$obj_netmask{$name} - $lineparam";
} elsif ( $line =~ /^\t\t\t:valid_ipaddr \(/ ){
$obj_NATadr{$name} = $lineparam;
} elsif ( $line =~ /^\t\t\t:netobj_adtr_method \(/ ){
$obj_NATtype{$name} = ("$lineparam" eq 'adtr_static') * 1;
} elsif ( $line =~ /^\t\t\t:comments \(/ ){
$obj_comment{$name} = $lineparam;
$obj_comment{$name} =~ s/^\"|\"$//g; #--- remove " at beginning and end
$obj_comment{$name} =~ s/;/ /g;
} elsif ( $line =~ /^\t\t\t:color \(/ ){
$obj_colour{$name} = lc($lineparam);
$obj_colour{$name} =~ s/^\"|\"$//g; #--- remove " at beginning and end
} elsif ( $line =~ /^\t\t\t: \(ReferenceObject/ ){
while ( ($line = <INFILE>) && ( fromdos("$line") ne "\t\t\t)" ) ) {
$line = &fromdos($line);
&DebugLog("NetObj.READ4: $line");
if ( $line =~ /^\t\t\t\t:Name \(/) {
($dummy,$lineparam) = split(/\(/,$line,2) ;
$lineparam =~ s/\)$//;
$members = "$members§$lineparam";
}
}
} elsif ( $line =~ /^\t\t\t: / ){
($dummy,$amember) = split(/: /,$line,2) ;
$members = "$members§$amember";
}
}
$obj_name[$obj_number] = $name;
$obj_number += 1;
&PrintLog('.');
}
$obj_type{'Any'} = 'any';
if ($FLAG_sortbytype) {
@obj_name = sort { $obj_type{"$a"} cmp $obj_type{"$b"} or lc($a) cmp lc($b) } @obj_name;
} else {
@obj_name = sort { lc($a) cmp lc($b) } @obj_name;
}
}
##########################################################################
# read all network services defined
#
# service variables where svc_name equals the hash for each of these:
#
# $svc_number = number of services read
# @svc_name = names of all services
# %svc_type = tcp, udp, icmp, rpc, group
# %svc_dst_port = destination port
# %svc_src_low = range source port from
# %svc_src_high = range source port to
# %svc_match = if MATCH defines (for RPCs)
# %svc_prolog = RPC prolog
# %svc_members = members, if a group
# %svc_comment = comment for the service
# %svc_colour = colour of the service
# %svc_used = count service usage in the rulebase
# (set later when evaluating the ruleset)
sub ReadServices{
my ($dummy) = '';
my ($name) = '';
my ($amember) = '';
my ($members) = '';
while ( ($line = <INFILE>) && ( fromdos("$line") ne "\t)" ) ) {
$line = &fromdos($line);
&DebugLog("Svc.READ1: $line");
while ( $line !~ /\t\t\: \(/ ) {
$line = <INFILE>;
$line = &fromdos($line);
&DebugLog("Svc.READ2: $line");
}
($dummy,$name) = split(/\(/,$line,2) ;
$amember = '';
$members = '';
while ( ($line = <INFILE>) && ( fromdos("$line") ne "\t\t)" ) ) {
$line = &fromdos($line);
&DebugLog("Svc.READ3: $line");
($dummy,$lineparam) = split(/\(/,$line,2) ;
$lineparam =~ s/\)$//;
if ( "$lineparam" =~ /"\>(.*)\"/ ){ # this stands for ports bigger than...
$lineparam = $1;
$lineparam++;
$lineparam = "$lineparam\:65535";
$svc_dst_port{$name} = $lineparam;
} elsif ( "$lineparam" =~ /"\<(.*)\"/ ){ # this stands for ports smaller than...
$lineparam = $1;
$lineparam--;
$lineparam = "0\:$lineparam";
$svc_dst_port{$name} = $lineparam;
} elsif ( $line =~ /^\t\t\t:type \(/ ){
$svc_type{$name} = lc($lineparam);
} elsif ( $line =~ /^\t\t\t:exp \(/ ){ # ICMP extensions
$lineparam =~ s/\"//g;
if ($svc_type{$name} =~ /^other$/i) {
$lineparam =~ s/\"//g;
$svc_dst_port{$name} = $lineparam;
} else {
$lineparam =~ s/\"//g;
$svc_dst_port{$name} = $ICMPtranslate{$lineparam};
}
$svc_dst_port{$name} = $ICMPtranslate{$lineparam};
} elsif ( $line =~ /^\t\t\t:port \(/ ){ # TCP/UDP destination port
$lineparam =~ tr/-/:/;
$svc_dst_port{$name} = $lineparam;
} elsif ( $line =~ /^\t\t\t:src_port \(/ ){
$svc_src_low{$name} = $lineparam;
$svc_src_high{$name} = $lineparam;
} elsif ( $line =~ /^\t\t\t:src_port_from \(/ ){
$svc_src_low{$name} = $lineparam;
} elsif ( $line =~ /^\t\t\t:src_port_to \(/ ){
$svc_src_high{$name} = $lineparam;
} elsif ( $line =~ /^\t\t\t:prematch \(/ ){
$svc_match{$name} = $lineparam;
} elsif ( $line =~ /^\t\t\t:prolog \(/ ){
$svc_prolog{$name} = $lineparam;
} elsif ( $line =~ /^\t\t\t:comments \(/ ){
$svc_comment{$name} = $lineparam;
$svc_comment{$name} =~ s/^\"|\"$//g; #--- remove " at beginning and end
$svc_comment{$name} =~ s/;/ /g;
} elsif ( $line =~ /^\t\t\t:color \(/ ){
$svc_colour{$name} = lc($lineparam);
$svc_colour{$name} =~ s/^\"|\"$//g; #--- remove " at beginning and end
} elsif ( $line =~ /^\t\t\t: \(ReferenceObject/ ){
while ( ($line = <INFILE>) && ( fromdos("$line") ne "\t\t\t)" ) ) {
$line = &fromdos($line);
&DebugLog("Svc.READ4: $line");
if ( $line =~ /^\t\t\t\t:Name \(/) {
($dummy,$lineparam) = split(/\(/,$line,2) ;
$lineparam =~ s/\)$//;
$members = "$members§$lineparam";
}
}
} elsif ( $line =~ /^\t\t\t: / ){
($dummy,$amember) = split(/:\x20/,$line,2) ;
$members = "$members§$amember";
}
}
$svc_name[$svc_number] = $name;
if ( "$svc_type{$name}" eq 'group' ) {
($dummy,$members) = split (/§/, $members, 2);
$svc_members{$name} = $members;
}
&PrintLog('.');
$svc_number += 1;
}
&PrintLog ("\n");
$svc_type{'Any'} = 'any';
if ($FLAG_sortbytype) {
@svc_name = sort { $svc_type{"$a"} cmp $svc_type{"$b"} or lc($a) cmp lc($b) } @svc_name;
} else {
@svc_name = sort { lc($a) cmp lc($b) } @svc_name;
}
}
##########################################################################
# read all network servers defined
#
# Object variables where srv_name equals the hash for each of these:
#
# $srv_number = number of servers
# @srv_name = names of all servers
# %srv_type = radius, tacacs, ufp, cvp, group
# %srv_members = members, if a group
# %srv_priority = priority of the server
# %srv_reference = reference of the server
# %srv_comment = comment for the server
# %srv_colour = colour the server is to be displayed with
# %srv_version = version of the server
sub ReadServers{
my ($dummy) = '';
my ($name) = '';
my ($lineparam) = '';
my ($amember) = '';
my ($members) = '';
$srv_number = 0;
while ( ($line = <INFILE> ) && ( fromdos("$line") ne "\t)" )) {
$line = &fromdos($line);
&DebugLog("Srv.READ1: $line");
while ( $line !~ /\t\t\: \(/ ) {
$line = <INFILE>;
$line = &fromdos($line);
&DebugLog("Srv.READ2: $line");
}
($dummy,$name) = split(/\(/,$line,2) ;
$amember = '';
$members = '';
$srv_reference{$name} = '-';
$priority{$name} = '';
while ( ($line = <INFILE>) && ( fromdos("$line") ne "\t\t)" )) {
$line = &fromdos($line);
&DebugLog("Srv.READ2: $line");
($dummy,$lineparam) = split(/\(/,$line,2) ;
$lineparam =~ s/\)$//;
if ( $line =~ /^\t\t\t:type \(/ ){
$srv_type{$name} = lc($lineparam);
} elsif ( $line =~ /^\t\t\t:color \(/ ){
$srv_colour{$name} = lc($lineparam);
$srv_colour{$name} =~ s/^\"|\"$//g; #--- remove " at beginning and end
} elsif ( $line =~ /^\t\t\t:comments \(/ ){
$srv_comment{$name} = $lineparam;
$srv_comment{$name} =~ s/^\"|\"$//g; #--- remove " at beginning and end
$srv_comment{$name} =~ s/;/ /g;
} elsif ( $line =~ /^\t\t\t:priority \(/ ){
$srv_priority{$name} = lc($lineparam);
} elsif ( $line =~ /^\t\t\t:version \(/ ){
$srv_version{$name} = $lineparam;
$srv_version{$name} =~ s/^\"|\"$//g; #--- remove " at beginning and end
$srv_version{$name} =~ s/;/ /g;
} elsif ( $line =~ /^\t\t\t:server \(/ ){
$line = &fromdos($line);
while ( $line !~ /\t\t\t\t\:(refname|Name) \(/ ) { # V4.1 | NG
$line = <INFILE>;
$line = &fromdos($line);
&DebugLog("Srv.READ3: $line");
}
($dummy,$lineparam) = split(/\(/,$line,2) ;
$srv_reference{$name} = $lineparam;
$srv_reference{$name} =~ s/^\"|\"\)$|\)$//g; #--- remove " at beginning and end
$srv_reference{$name} =~ s/;/ /g;
} elsif ( $line =~ /^\t\t\t: \(ReferenceObject/ ){
while ( ($line = <INFILE>) && ( fromdos("$line") ne "\t\t\t)" ) ) {
$line = &fromdos($line);
&DebugLog("Srv.READ4: $line");
if ( $line =~ /^\t\t\t\t:Name \(/) {
($dummy,$lineparam) = split(/\(/,$line,2) ;
$lineparam =~ s/\)$//;
$members = "$members§$lineparam";
}
}
} elsif ( $line =~ /^\t\t\t: / ){
($dummy,$amember) = split(/: /,$line,2) ;
$members = "$members§$amember";
}
}
if ( "$srv_type{$name}" eq 'group' ) {
($dummy,$members) = split (/§/, $members, 2);
$srv_members{$name} = $members;
}
$srv_name[$srv_number] = $name;
$srv_number += 1;
&PrintLog('.');
}
$srv_type{'Any'} = 'any';
if ($FLAG_sortbytype) {
@srv_name = sort { $srv_type{"$a"} cmp $srv_type{"$b"} or lc($a) cmp lc($b) } @srv_name;
} else {
@srv_name = sort { lc($a) cmp lc($b) } @srv_name;
}
}
##########################################################################
# read all resources defined
#
# resource variables where rsc_name equals the hash for each of these:
#
# $rsc_number = number of ressources read
# @rsc_name = names of all ressources
# %rsc_maxsize = maximum size
# %rsc_allowedchar= allowed characterset
# %rsc_av_setting = AntiVirus server handling
# %rsc_av_server = ...and it's server
# %rsc_type = smtp, http
# %rsc_comment = comment for the resource
#
#
sub ReadResources{
my ($dummy) = '';
my ($name) = '';
my ($amember) = '';
my ($members) = '';
while ( ($line = <INFILE>) && ( fromdos("$line") ne "\t)" ) ) {
$line = &fromdos($line);
&DebugLog("Res.READ1: $line");
while ( ($line = <INFILE>) && ( fromdos("$line") ne "\t\t)" ) ) {
$line = &fromdos($line);
&DebugLog("Res.READ2: $line");
}
&PrintLog('.');
$rsc_number += 1;
}
&PrintLog ("\n");
}
##########################################################################
# read properties
#
# %prop_setting{'XXX'} = setting for XXX
#
# of interest with respect to implicit rules:
# rip, domain_udp, domain_tcp, established,
# icmpenable, fw1enable == true / false
# rip_p, domain_udp_p, domain_tcp_p, established_p,
# icmpenable_p, fw1enable_p == first / "before last" / last
#
sub ReadProperties{
my($line) = '';
my($par) = '';
my($set) = '';
my($rest) = '';
while ( ($line = <INFILE>) && ( fromdos("$line") ne "\t)" ) ) {
$line = &fromdos($line);
&DebugLog("Prop.READ1: $line");
&PrintLog('.');
if ( "$line" =~ m/\t\t:.* \(.*\)$/ ){
($par,$set) = split(/ \(/, $line, 2);
$par =~ s/^\s+://; #--- remove " :" at the beginning
$set =~ s/\)$//; #--- remove ) at the end
$set =~ s/^\"|\"$//g; #--- remove " at beginning and end
$prop_setting{"$par"} = "$set";
}
}
&PrintLog("\n");
}
##########################################################################
# read users from exported file
#
# user variables where user_name equals the hash for each of these:
#
# $user_number = number of users read
# @user_name = names of all users
# %user_type = usrgroup, template, user
# %user_members = members, if a group
# %user_comment = comment for the user
# %user_colour = colour of the user
# %user_used = wether the user is used in the rulebase
# (set later when evaluating the ruleset)
sub ReadDBExportUser {
my ($filename) = $_[0];
# open input and seek to start of data
open (INFILE,"$filename")
or die "Cannot open the userDBexport file $filename!\n\n";
close (INFILE);
}
##########################################################################
# recursively set usage on objects
sub SetObjUsed{
my ($index) = $_[0];
my ($single) = '';
my (@members);
$obj_used{"$index"} += 1;
if ( "$obj_type{$index}" eq 'group'){
@members = split (/§/,$obj_members{$index});
foreach $single (@members) {
&SetObjUsed("$single");
}
}
}
##########################################################################
# recursively set usage on services
sub SetSvcUsed{
my ($index) = $_[0];
my ($single) = '';