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
|
#! /usr/bin/perl
# $OpenBSD: flow.pl,v 1.5 2017/02/27 21:56:09 bluhm Exp $
# Copyright (c) 2013 Florian Obser <florian@openbsd.org>
#
# Permission to use, copy, modify, and distribute this software for any
# purpose with or without fee is hereby granted, provided that the above
# copyright notice and this permission notice appear in all copies.
#
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
use strict;
use warnings;
use 5.010;
use Config;
use lib '.';
use Data::Dumper;
use IO::Socket::INET;
use Net::Flow;
my $port = 9996;
{
my $id2name = {
1 => 'octetDeltaCount',
2 => 'packetDeltaCount',
4 => 'protocolIdentifier',
5 => 'ipClassOfService',
7 => 'sourceTransportPort',
8 => 'sourceIPv4Address',
10 => 'ingressInterface',
11 => 'destinationTransportPort',
12 => 'destinationIPv4Address',
14 => 'egressInterface',
21 => 'flowEndSysUpTime',
22 => 'flowStartSysUpTime',
27 => 'sourceIPv6Address',
28 => 'destinationIPv6Address',
150 => 'flowStartSeconds',
151 => 'flowEndSeconds',
152 => 'flowStartMilliseconds',
153 => 'flowEndMilliseconds',
};
my $name2id = {reverse %$id2name};
sub id2name { return $id2name->{$_[0]} || $_[0]; }
sub name2id { return $name2id->{$_[0]} || $_[0]; }
}
sub get_ifs
{
my (@ifs, $prog);
open($prog, 'ifconfig |') or die $!;
while(<$prog>) {
chomp;
push(@ifs, $1) if(/^(\w+):/);
}
close($prog) or die $!;
return(grep({$_ ne 'lo0'} @ifs));
}
sub gen_pf_conf
{
my @ifs = @_;
my $skip = 'set skip on {'.join(' ', @ifs).'}';
return <<END;
$skip
pass on lo0 no state
pass on lo0 proto tcp from port 12345 to port 12346 keep state (pflow)
END
}
if (scalar(@ARGV) != 2 || ($ARGV[0] != 9 && $ARGV[0]!=10)) {
print STDERR "usage: $0 [9|10] [4|6]\n";
exit(1);
}
if (scalar(@ARGV) != 2 || ($ARGV[1] != 4 && $ARGV[1]!=6)) {
print STDERR "usage: $0 [9|10] [4|6]\n";
exit(1);
}
my @v94_elem_names = qw (sourceIPv4Address
destinationIPv4Address
ingressInterface
egressInterface
packetDeltaCount
octetDeltaCount
flowStartSysUpTime
flowEndSysUpTime
sourceTransportPort
destinationTransportPort
ipClassOfService
protocolIdentifier);
my @v96_elem_names = qw (sourceIPv6Address
destinationIPv6Address
ingressInterface
egressInterface
packetDeltaCount
octetDeltaCount
flowStartSysUpTime
flowEndSysUpTime
sourceTransportPort
destinationTransportPort
ipClassOfService
protocolIdentifier);
my @v104_elem_names = qw (sourceIPv4Address
destinationIPv4Address
ingressInterface
egressInterface
packetDeltaCount
octetDeltaCount
flowStartMilliseconds
flowEndMilliseconds
sourceTransportPort
destinationTransportPort
ipClassOfService
protocolIdentifier);
my @v106_elem_names = qw (sourceIPv6Address
destinationIPv6Address
ingressInterface
egressInterface
packetDeltaCount
octetDeltaCount
flowStartMilliseconds
flowEndMilliseconds
sourceTransportPort
destinationTransportPort
ipClassOfService
protocolIdentifier);
my ($name, $sock, $packet, $header_ref, $template_ref, $flow_ref, $flows_ref,
$error_ref, @elem_names, $prog, $line);
system('ifconfig', 'lo0', 'inet', '10.11.12.13', 'alias');
system('ifconfig', 'lo0', 'inet6', '2001:db8::13');
open($prog, '|pfctl -f -') or die $!;
print $prog gen_pf_conf(get_ifs());
close($prog) or die $!;
if (`ifconfig pflow0 2>&1` ne "pflow0: no such interface\n") {
system('ifconfig', 'pflow0', 'destroy');
}
system('ifconfig', 'pflow0', 'flowsrc', '127.0.0.1', 'flowdst',
'127.0.0.1:9996', 'pflowproto', $ARGV[0]);
system('./gen_traffic '.$ARGV[1].' &');
if ($ARGV[0] == 9 && $ARGV[1] == 4) {
@elem_names = @v94_elem_names;
} elsif ($ARGV[0] == 9 && $ARGV[1] == 6) {
@elem_names = @v96_elem_names;
} elsif ($ARGV[0] == 10 && $ARGV[1] == 4) {
@elem_names = @v104_elem_names;
} elsif ($ARGV[0] == 10 && $ARGV[1] == 6) {
@elem_names = @v106_elem_names;
}
$sock = IO::Socket::INET->new(LocalPort =>$port, Proto => 'udp');
while ($sock->recv($packet,1548)) {
($header_ref, $template_ref, $flows_ref, $error_ref) =
Net::Flow::decode(\$packet, $template_ref);
if (scalar(@$flows_ref) > 0) {
say scalar(@$flows_ref),' flows';
foreach $flow_ref (@$flows_ref) {
say scalar(keys %$flow_ref) - 1, ' elements';
say 'SetId: ', $flow_ref->{'SetId'};
my ($iif, $eif, $start, $end);
my $qpack = $Config{longsize} == 8 ? 'Q>' :
$Config{byteorder} == 1234 ? 'L>xxxx' : 'xxxxL>';
foreach $name (@elem_names) {
if ($name eq 'ingressInterface') {
$iif = unpack('N',
$flow_ref->{name2id($name)});
} elsif ($name eq 'egressInterface') {
$eif = unpack('N',
$flow_ref->{name2id($name)});
} elsif ($name eq 'flowStartSysUpTime') {
$start = unpack('N',
$flow_ref->{name2id($name)})/1000;
} elsif ($name eq 'flowEndSysUpTime') {
$end = unpack('N',
$flow_ref->{name2id($name)})/1000;
} elsif ($name eq 'flowStartSeconds') {
$start = unpack('N',
$flow_ref->{name2id($name)});
} elsif ($name eq 'flowEndSeconds') {
$end = unpack('N',
$flow_ref->{name2id($name)});
} elsif ($name eq 'flowStartMilliseconds') {
$start = unpack($qpack,
$flow_ref->{name2id($name)})/1000;
} elsif ($name eq 'flowEndMilliseconds') {
$end = unpack($qpack,
$flow_ref->{name2id($name)})/1000;
} else {
say $name,': ', unpack('H*',
$flow_ref->{name2id($name)});
}
}
say 'ingressInterface == egressInterface && '.
'egressInterface > 0: ', ($iif == $eif && $eif > 0);
}
last;
}
}
END {
system('ifconfig', 'pflow0', 'destroy');
system('ifconfig', 'lo0', 'inet', '10.11.12.13', 'delete');
system('ifconfig', 'lo0', 'inet6', '2001:db8::13', 'delete');
}
|