Remote/Tcpdump.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
« Remote/Sysmon.pm ↑ Summary Remote/Windefend.pm »
1 #
2 # $Id$
3 #
4 # remote::tcpdump Brik
5 #
6 package Metabrik::Remote::Tcpdump;
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::Ssh);
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 device => [ qw(device) ],
20 _started => [ qw(INTERNAL) ],
21 _channel => [ qw(INTERNAL) ],
22 _out => [ qw(INTERNAL) ],
23 _dump => [ qw(INTERNAL) ],
24 },
25 attributes_default => {
26 username => 'root',
27 hostname => 'localhost',
28 port => 22,
29 _started => 0,
30 _channel => undef,
31 },
32 commands => {
33 start => [ ],
34 status => [ ],
35 stop => [ ],
36 next => [ ],
37 nextall => [ ],
38 },
39 require_modules => {
40 'Net::Frame::Dump::Offline' => [],
41 },
42 };
43 }
44  
45 sub brik_use_properties {
46 my $self = shift;
47  
48 return {
49 attributes_default => {
50 device => defined($self->global) && $self->global->device || 'eth0',
51 },
52 };
53 }
54  
55 sub start {
56 my $self = shift;
57  
58 if ($self->_started) {
59 return $self->log->verbose("start: already started");
60 }
61  
62 $self->connect or return;
63 $self->log->verbose("ssh connection successful");
64  
65 my $dump = Net::Frame::Dump::Offline->new;
66 $self->_dump($dump);
67  
68 $self->log->debug("dump file[".$dump->file."]");
69  
70 open(my $out, '>', $dump->file)
71 or return $self->log->error("cannot open file: $!");
72 my $old = select($out);
73 $|++;
74 select($old);
75 $self->_out($out);
76  
77 my $device = $self->device;
78  
79 my $channel = $self->exec("tcpdump -U -i $device -w - 2> /dev/null") or return;
80  
81 $self->log->debug("tcpdump started");
82  
83 $self->_started(1);
84  
85 return $self->_channel($channel);
86 }
87  
88 sub status {
89 my $self = shift;
90  
91 return $self->_started;
92 }
93  
94 sub stop {
95 my $self = shift;
96  
97 if (! $self->_started) {
98 return $self->log->error($self->brik_help_run('start'));
99 }
100  
101 $self->nextall;
102  
103 my $r = $self->disconnect;
104 $self->_dump->stop;
105 unlink($self->_dump->file);
106 close($self->_out);
107  
108 $self->_started(0);
109 $self->_channel(undef);
110 $self->_out(undef);
111 $self->_dump(undef);
112  
113 return $r;
114 }
115  
116 sub next {
117 my $self = shift;
118  
119 if (! $self->_started) {
120 return $self->log->error($self->brik_help_run('start'));
121 }
122  
123 my $channel = $self->_channel;
124 if (! defined($channel)) {
125 return $self->log->error("next: channel not found");
126 }
127  
128 my $out = $self->_out;
129 while (my $line = <$channel>) {
130 print $out $line;
131 }
132  
133 # If reader not already open, we open it
134 my $dump = $self->_dump;
135 if (! $dump->isRunning) {
136 $dump->start or return $self->log->error("unable to start pcap reader");
137 }
138  
139 if (my $h = $dump->next) {
140 return $h;
141 }
142  
143 return;
144 }
145  
146 sub nextall {
147 my $self = shift;
148  
149 if (! $self->_started) {
150 return $self->log->error($self->brik_help_run('start'));
151 }
152  
153 my $channel = $self->_channel;
154 if (! defined($channel)) {
155 return $self->log->error("nextall: channel not found");
156 }
157  
158 my $out = $self->_out;
159 while (my $line = <$channel>) {
160 print $out $line;
161 }
162  
163 # If reader not already open, we open it
164 my $dump = $self->_dump;
165 if (! $dump->isRunning) {
166 $dump->start or return $self->log->error("nextall: unable to start pcap reader");
167 }
168  
169 my @next = ();
170 while (my $h = $dump->next) {
171 push @next, $h;
172 }
173  
174 return \@next;
175 }
176  
177 1;
178  
179 __END__
« Remote/Sysmon.pm ↑ Summary Remote/Windefend.pm »