Forensic/Pcap.pm

filter: Statementstmt 100% 9 / 9 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 3 / 3 Podpod n/a totaltotal 100% 12 / 12 CC 1 SCAR 0.0
CC
1
Cov
100%
BEGIN
1.0
BEGIN
1.0
BEGIN
1.0
CRAP 1.0
« Forensic/Dcfldd.pm ↑ Summary Forensic/Scalpel.pm »
1 #
2 # $Id$
3 #
4 # forensic::pcap Brik
5 #
6 package Metabrik::Forensic::Pcap;
7 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 1 ▶use base qw(Metabrik::Client::Elasticsearch::Query);
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 sub brik_properties {
13 return {
14 revision => '$Revision$',
15 tags => [ qw(unstable) ],
16 author => 'GomoR <GomoR[at]metabrik.org>',
17 license => 'http://opensource.org/licenses/BSD-3-Clause',
18 attributes => {
19 nodes => [ qw(node_list) ], # Inherited
20 index => [ qw(index) ], # Inherited
21 type => [ qw(type) ], # Inherited
22 },
23 attributes_default => {
24 index => 'forensicpcap-*',
25 type => 'pcap',
26 },
27 commands => {
28 create_client => [ ], # Inherited
29 reset_client => [ ], # Inherited
30 query => [ qw(query index|OPTIONAL type|OPTIONAL) ], # Inherited
31 from_json_file => [ qw(json_file index|OPTIONAL type|OPTIONAL) ], # Inherited
32 from_dump_file => [ qw(dump_file index|OPTIONAL type|OPTIONAL) ], # Inherited
33 pcap_to_elasticsearch => [ qw(file filter|OPTIONAL) ],
34 show_sessions => [ qw(ip_address port protocol) ],
35 },
36 require_modules => {
37 'Metabrik::File::Pcap' => [ ],
38 'Metabrik::Time::Universal' => [ ],
39 },
40 require_binaries => {
41 },
42 optional_binaries => {
43 },
44 need_packages => {
45 },
46 };
47 }
48  
49 sub brik_use_properties {
50 my $self = shift;
51  
52 return {
53 attributes_default => {
54 },
55 };
56 }
57  
58 sub brik_preinit {
59 my $self = shift;
60  
61 # Do your preinit here, return 0 on error.
62  
63 return $self->SUPER::brik_preinit;
64 }
65  
66 sub brik_init {
67 my $self = shift;
68  
69 # Do your init here, return 0 on error.
70  
71 return $self->SUPER::brik_init;
72 }
73  
74 sub pcap_to_elasticsearch {
75 my $self = shift;
76 my ($file, $filter) = @_;
77  
78 $filter ||= '';
79 $self->brik_help_run_undef_arg('pcap_to_elasticsearch', $file) or return;
80 $self->brik_help_run_file_not_found('pcap_to_elasticsearch', $file) or return;
81  
82 my $fp = Metabrik::File::Pcap->new_from_brik_init($self) or return;
83 $fp->open($file, 'read', $filter) or return;
84  
85 my $tu = Metabrik::Time::Universal->new_from_brik_init($self) or return;
86 my $today = $tu->today;
87  
88 my $index = "forensicpcap-$today";
89 my $type = "pcap";
90  
91 $self->create_client or return;
92  
93 my $count_before = 0;
94 if ($self->is_index_exists($index)) {
95 $count_before = $self->count($index, $type);
96 if (! defined($count_before)) {
97 return;
98 }
99 $self->log->info("pcap_to_elasticsearch: current index [$index] count is ".
100 "[$count_before]");
101 }
102  
103 $self->open_bulk_mode($index, $type) or return;
104  
105 $self->log->info("pcap_to_elasticsearch: importing file [$file] to index ".
106 "[$index] with type [$type]");
107  
108 my $print_re = qr/^[[:print:]]{5,}/;
109  
110 my $read = 0;
111 my $imported = 0;
112 while (1) {
113 my $h = $fp->read_next(10); # We read 10 by 10
114 if (! defined($h)) {
115 $self->log->error("pcap_to_elasticsearch: unable to read frame, skipping");
116 next;
117 }
118  
119 last if @$h == 0; # Eof
120  
121 $read += @$h;
122  
123 for my $this (@$h) {
124 my $simple = $fp->from_read($this);
125 if (! defined($simple)) {
126 $self->log->error("pcap_to_elasticsearch: unable to parse frame, skipping");
127 next;
128 }
129  
130 my $timestamp = $simple->timestamp;
131  
132 my $new = {
133 '@version' => 1,
134 '@timestamp' => $tu->timestamp_to_tz_time($timestamp),
135 };
136 my $skip_payload = 0;
137 for my $layer (reverse $simple->layers) {
138 my $this_layer = $layer->layer;
139  
140 my $class = ref($layer);
141 my %h = map { $_ => $layer->[$layer->cgGetIndice($_)] }
142 @{$class->cgGetAttributes};
143  
144 # The first we loop, we are using the last layer where we
145 # want to keep the payload. For all others, we remove it.
146 if ($skip_payload) {
147 delete $h{payload};
148 }
149 else {
150 $skip_payload = 1;
151 }
152 # We convert IPv4 and TCP options to hex
153 if ($layer->layer eq 'TCP') {
154 $h{options} = CORE::unpack('H*', $h{options});
155 # If payload does not seem printable, we encode in hex
156 if (defined($h{payload}) && $h{payload} !~ $print_re) {
157 $h{payload} = CORE::unpack('H*', $h{payload});
158 }
159 #print "payload[".$h{payload}."]\n" if defined($h{payload});
160 }
161 if ($layer->layer eq 'UDP') {
162 # If payload does not seem printable, we encode in hex
163 if (defined($h{payload}) && $h{payload} !~ $print_re) {
164 $h{payload} = CORE::unpack('H*', $h{payload});
165 }
166 #print "payload[".$h{payload}."]\n" if defined($h{payload});
167 }
168 elsif ($layer->layer eq 'IPv4') {
169 $h{options} = CORE::unpack('H*', $h{options});
170 }
171 delete $h{raw};
172 delete $h{nextLayer};
173 delete $h{noFixLen};
174  
175 $new->{$this_layer} = \%h;
176 }
177  
178 my $r = $self->index_bulk($new);
179 if (! defined($r)) {
180 $self->log->error("pcap_to_elasticsearch: bulk index failed for index ".
181 "[$index] at read [$read], skipping chunk");
182 next;
183 }
184  
185 $imported++;
186 }
187 }
188  
189 $self->bulk_flush;
190  
191 $self->refresh_index($index);
192  
193 my $count_current = $self->count($index, $type) or return;
194 $self->log->info("pcap_to_elasticsearch: after index [$index] count is ".
195 "[$count_current]");
196  
197 my $skipped = 0;
198 my $complete = (($count_current - $count_before) == $read) ? 1 : 0;
199 if ($complete) { # If complete, import has been retried, and everything is now ok.
200 $imported = $read;
201 }
202 else {
203 $skipped = $read - ($count_current - $count_before);
204 }
205  
206 if (! $complete) {
207 $self->log->warning("pcap_to_elasticsearch: import incomplete");
208 }
209 else {
210 $self->log->info("pcap_to_elasticsearch: successfully imported [$read] frames");
211 }
212  
213 return 1;
214 }
215  
216 sub show_sessions {
217 my $self = shift;
218 my ($ip_address, $port, $protocol) = @_;
219  
220 $protocol ||= 'tcp';
221 $self->brik_help_run_undef_arg('show_sessions', $ip_address) or return;
222 $self->brik_help_run_undef_arg('show_sessions', $port) or return;
223  
224 $protocol = lc($protocol);
225 if ($protocol ne 'tcp' && $protocol ne 'udp') {
226 return $self->log->error("show_sessions: protocol must be tcp or udp");
227 }
228  
229 #
230 # TCP query
231 #
232 # (TCP.flags:16 OR TCP.flags:24)
233 # AND (IPv4.src:$ip OR IPv4.dst:$ip)
234 # AND (TCP.dst:$port OR TCP.src:$port)
235 #
236 my @should1 = ();
237 my @should2 = (
238 { term => { 'IPv4.src' => $ip_address } },
239 { term => { 'IPv4.dst' => $ip_address } },
240 );
241 my @should3 = ();
242  
243 if ($protocol eq 'tcp') {
244 push @should1, { term => { 'TCP.flags' => 16 } };
245 push @should1, { term => { 'TCP.flags' => 24 } };
246 push @should3, { term => { 'TCP.dst' => $port } };
247 push @should3, { term => { 'TCP.src' => $port } };
248 }
249 else {
250 push @should3, { term => { 'UDP.dst' => $port } };
251 push @should3, { term => { 'UDP.src' => $port } };
252 }
253  
254 # IPv4.dst:37.247.10.18 OR IPv4.src:37.247.10.18
255  
256 my $q = {
257 size => 1000,
258 sort => [
259 { '_uid' => { order => "asc" } },
260 { '@timestamp' => { order => "asc" } },
261 ],
262 query => {
263 bool => {
264 must => [
265 { bool => { should => \@should1, }, },
266 { bool => { should => \@should2, }, },
267 { bool => { should => \@should3, }, },
268 ],
269 }
270 },
271 };
272  
273 my $r = $self->query($q) or return;
274 my $hits = $self->get_query_result_hits($r) or return;
275  
276 use Data::Dumper;
277  
278 for my $this (@$hits) {
279 #print Dumper($this)."\n"; last;
280 $this = $this->{_source};
281 my $timestamp = $this->{'@timestamp'};
282 my $ip_src = $this->{IPv4}{src};
283 my $ip_dst = $this->{IPv4}{dst};
284 my $src = $this->{TCP}{src} || $this->{UDP}{src};
285 my $dst = $this->{TCP}{dst} || $this->{UDP}{dst};
286 my $payload = $this->{TCP}{payload} || $this->{UDP}{payload} || '';
287  
288 print "$timestamp: [$ip_src]:$src > [$ip_dst]:$dst [$payload]\n";
289 }
290  
291 return 1;
292 }
293  
294 sub brik_fini {
295 my $self = shift;
296  
297 # Do your fini here, return 0 on error.
298  
299 return $self->SUPER::brik_fini;
300 }
301  
302 1;
303  
304 __END__
« Forensic/Dcfldd.pm ↑ Summary Forensic/Scalpel.pm »