lib/SMB/Connection.pm

filter: Statementstmt 21.9% 25 / 114 Branchbran 0.0% 0 / 36 Conditioncond 0.0% 0 / 32 MC/DCmcdc 0.0% 0 / 16 Subroutinesub 27.5% 8 / 29 Podpod 10.0% 2 / 20 totaltotal 14.1% 35 / 247 SCAR 65.4
CC
32
Cov
14%
recv_command
90.0
recv_nbss
72.0
new
56.0
CRAP 689.3
« lib/SMB/v2/Commands.pm ↑ Summary blib/lib/SMB/v2/Commands.pm »
1 # SMB Perl library, Copyright (C) 2014 Mikhael Goikhman, migo@cpan.org
2 #
3 # This program is free software: you can redistribute it and/or modify
4 # it under the terms of the GNU General Public License as published by
5 # the Free Software Foundation, either version 3 of the License, or
6 # (at your option) any later version.
7 #
8 # This program is distributed in the hope that it will be useful,
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 # GNU General Public License for more details.
12 #
13 # You should have received a copy of the GNU General Public License
14 # along with this program. If not, see <http://www.gnu.org/licenses/>.
15  
16 package SMB::Connection;
17  
18 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21 1 ▶use bytes;
Subroutine BEGIN: called CC=1 SCAR=0.0
22  
23 1 ▶use parent 'SMB';
Subroutine BEGIN: called CC=1 SCAR=0.0
24  
25 1 ▶use SMB::Parser;
Subroutine BEGIN: called CC=1 SCAR=0.0
26 1 ▶use SMB::Packer;
Subroutine BEGIN: called CC=1 SCAR=0.0
27 1 ▶use SMB::v1::Commands;
Subroutine BEGIN: called CC=1 SCAR=0.0
28 1 ▶use SMB::v2::Commands;
Subroutine BEGIN: called CC=1 SCAR=0.0
29  
30 0 ▶sub parse_uint8 { $_[0]->parser->uint8; }
Subroutine parse_uint8: not called CC=1 SCAR=6.9
Pod undocumented
31 0 ▶sub parse_uint16 { $_[0]->parser->uint16; }
Subroutine parse_uint16: not called CC=1 SCAR=6.9
Pod undocumented
32 0 ▶sub parse_uint32 { $_[0]->parser->uint32; }
Subroutine parse_uint32: not called CC=1 SCAR=6.9
Pod undocumented
33 0 ▶sub parse_bytes { $_[0]->parser->bytes($_[1]); }
Subroutine parse_bytes: not called CC=1 SCAR=6.9
Pod undocumented
34 0 ▶sub parse_smb1 { SMB::v1::Commands->parse($_[0]->parser) }
Subroutine parse_smb1: not called CC=1 SCAR=6.9
Pod undocumented
35 0 ▶sub parse_smb2 { SMB::v2::Commands->parse($_[0]->parser) }
Subroutine parse_smb2: not called CC=1 SCAR=6.9
Pod undocumented
36  
37 0 ▶sub pack_uint8 { $_[0]->packer->uint8($_[1]); }
Subroutine pack_uint8: not called CC=1 SCAR=6.9
Pod undocumented
38 0 ▶sub pack_uint16 { $_[0]->packer->uint16($_[1]); }
Subroutine pack_uint16: not called CC=1 SCAR=6.9
Pod undocumented
39 0 ▶sub pack_uint32 { $_[0]->packer->uint32($_[1]); }
Subroutine pack_uint32: not called CC=1 SCAR=6.9
Pod undocumented
40 0 ▶sub pack_bytes { $_[0]->packer->bytes($_[1]); }
Subroutine pack_bytes: not called CC=1 SCAR=6.9
Pod undocumented
41 0 ▶sub pack_smb1 { SMB::v1::Commands->pack(shift()->packer, shift, @_) }
Subroutine pack_smb1: not called CC=1 SCAR=6.9
Pod undocumented
42 0 ▶sub pack_smb2 { SMB::v2::Commands->pack(shift()->packer, shift, @_) }
Subroutine pack_smb2: not called CC=1 SCAR=6.9
Pod undocumented
43  
44 sub new ($$$%) {
45 0 ▶ my $class = shift;
Subroutine new: not called CC=7 SCAR=40.3
Pod documented
46 0 ▶ my $socket = shift || die "No socket";
Condition0%0/2
shift() || die('No socket')
l!l
00
MC/DC0%0/1
shift() || die('No socket')
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
47 0 ▶ my $id = shift || die "No id";
Condition0%0/2
shift() || die('No id')
l!l
00
MC/DC0%0/1
shift() || die('No id')
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
48 0 my %options = @_;
49  
50 0 ▶ my $quiet = delete $options{quiet} || 0;
Condition0%0/2
delete $options{'quiet'} || 0
l!l
00
MC/DC0%0/1
delete $options{'quiet'} || 0
delete $options{'quiet'}
Truth table0%0/2
A
A= delete $options{'quiet'}
Aresult
11
00
51 0 ▶ my $verbose = delete $options{verbose} || 0;
Condition0%0/2
delete $options{'verbose'} || 0
l!l
00
MC/DC0%0/1
delete $options{'verbose'} || 0
delete $options{'verbose'}
Truth table0%0/2
A
A= delete $options{'verbose'}
Aresult
11
00
52  
53 0 my $self = $class->SUPER::new(
54 %options,
55 quiet => $quiet,
56 verbose => $verbose,
57 socket => $socket,
58 id => $id,
59 parser => SMB::Parser->new,
60 packer => SMB::Packer->new,
61 );
62  
63 0 ▶ unless ($self->disable_log) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->disable_log) 0 0 0
64 0 my $addr_with_port = $self->get_socket_addr;
65 0 ▶ my ($id0, $str) = $id =~ /^-(.*)/ ? ($1, 'server') : ($id, 'client');
Branch: 1 branch
BranchTrueFalseTotal
$id =~ /^-(.*)/ ? : 0 0 0
66 0 $self->{id_str} = "$str #$id0 [$addr_with_port]";
67 }
68  
69 0 $self->msg("Connected");
70  
71 0 return $self;
72 }
73  
74 sub DESTROY ($) {
75 0 ▶ my $self = shift;
Subroutine DESTROY: not called CC=1 SCAR=6.9
76  
77 0 $self->close;
78 }
79  
80 sub close ($) {
81 0 ▶ my $self = shift;
Subroutine close: not called CC=3 SCAR=24.8
Pod undocumented
82  
83 0 my $socket = $self->socket;
84 0 ▶ return unless $socket && $socket->opened;
Branch: 1 branch
BranchTrueFalseTotal
unless $socket and $socket->opened 0 0 0
Condition0%0/3
$socket and $socket->opened
!ll&&!rl&&r
000
MC/DC0%0/2
$socket and $socket->opened
$socket $socket->opened
Truth table0%0/3
A and B
A= $socketB= $socket->opened
ABresult
0X0
100
111
85  
86 0 $self->msg("Disconnected");
87  
88 0 $socket->close;
89 0 $self->socket(undef);
90 }
91  
92 sub get_socket_addr ($;$) {
93 0 ▶ my $this = shift;
Subroutine get_socket_addr: not called CC=5 SCAR=34.0
Pod undocumented
94 0 ▶ my $socket = shift || ref($this) && $this->socket || return;
Condition0%0/8
shift() || ref $this && $this->socket || (return)
l!l
00
shift() or ref $this and $this->socket
l!l&&r!l&&!r
000
ref $this and $this->socket
!ll&&!rl&&r
000
MC/DC0%0/3
shift() || ref $this && $this->socket || (return)
shift() ref $this $this->socket
Truth table0%0/4
A or B and C
A= shift()B= ref $thisC= $this->socket
ABCresult
1XX1
0111
00X0
0100
95  
96 0 my $host = $socket->peerhost();
97 0 my $port = $socket->peerport();
98  
99 0 ▶ return wantarray ? ($host, $port) : "$host:$port";
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
100 }
101  
102 sub recv_nbss ($) {
103 0 ▶ my $self = shift;
Subroutine recv_nbss: not called CC=8 SCAR=42.8
Pod undocumented
104  
105 0 my $socket = $self->socket;
106 0 my $data1; # NBSS header
107 my $data2; # SMB packet
108 0 my $header_label = 'NetBIOS Session Service header';
109 0 ▶ my $len = $socket->read($data1, 4) //
Condition0%0/2
$socket->read($data1, 4) // (return $self->err("Read failed: $!"))
l!l
00
MC/DC0%0/1
$socket->read($data1, 4) // (return $self->err("Read failed: $!"))
$socket->read($data1, 4)
Truth table0%0/2
A
A= $socket->read($data1, 4)
Aresult
11
00
110 return $self->err("Read failed: $!");
111 0 ▶ if ($len != 4) {
Branch: 1 branch
BranchTrueFalseTotal
if ($len != 4) 0 0 0
112 0 $self->err("Can't read $header_label (got $len bytes)");
113 0 return;
114 }
115 0 my ($packet_type, $packet_flags, $packet_len) = unpack('CCn', $data1);
116 0 ▶ if ($packet_type != 0 || $packet_flags > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($packet_type != 0 or $packet_flags > 1) 0 0 0
Condition0%0/3
$packet_type != 0 or $packet_flags > 1
l!l&&r!l&&!r
000
MC/DC0%0/2
$packet_type != 0 or $packet_flags > 1
$packet_type != 0 $packet_flags > 1
Truth table0%0/3
A or B
A= $packet_type != 0B= $packet_flags > 1
ABresult
1X1
011
000
117 0 $self->err("Only supported $header_label with type=0 flags=0|1");
118 0 return;
119 }
120 0 ▶ $packet_len += 1 << 16 if $packet_flags;
Branch: 1 branch
BranchTrueFalseTotal
if $packet_flags 0 0 0
121 0 ▶ $len = $socket->read($data2, $packet_len) // 0;
Condition0%0/2
$socket->read($data2, $packet_len) // 0
l!l
00
MC/DC0%0/1
$socket->read($data2, $packet_len) // 0
$socket->read($data2, $packet_len)
Truth table0%0/2
A
A= $socket->read($data2, $packet_len)
Aresult
11
00
122 0 ▶ if ($len != $packet_len) {
Branch: 1 branch
BranchTrueFalseTotal
if ($len != $packet_len) 0 0 0
123 0 $self->err("Can't read full packet (expected $packet_len, got $len bytes)");
124 0 return;
125 }
126  
127 0 $self->parser->set($data1 . $data2, 4);
128 }
129  
130 sub recv_command ($) {
131 0 ▶ my $self = shift;
Subroutine recv_command: not called CC=9 SCAR=45.0
Pod undocumented
132  
133 0 ▶ $self->recv_nbss
Branch: 1 branch
BranchTrueFalseTotal
$self->recv_nbss or return 0 0 0
134 or return;
135  
136 0 my $smb_num = $self->parse_uint8;
137 0 my $smb_str = $self->parse_bytes(3);
138 0 ▶ if ($smb_str ne 'SMB' || $smb_num != 0xff && $smb_num != 0xfe) {
Branch: 1 branch
BranchTrueFalseTotal
if ($smb_str ne "SMB" or $smb_num != 255 and $smb_num != 254) 0 0 0
Condition0%0/6
$smb_str ne "SMB" or $smb_num != 255 and $smb_num != 254
l!l&&r!l&&!r
000
$smb_num != 255 and $smb_num != 254
!ll&&!rl&&r
000
MC/DC0%0/3
$smb_str ne "SMB" or $smb_num != 255 and $smb_num != 254
$smb_str ne "SMB" $smb_num != 255 $smb_num != 254
Truth table0%0/4
A or B and C
A= $smb_str ne "SMB"B= $smb_num != 255C= $smb_num != 254
ABCresult
1XX1
0111
00X0
0100
139 0 $self->err("Neither SMB1 nor SMB2 signature found, giving up");
140 0 $self->mem(chr($smb_num) . $smb_str, "Signature");
141 0 return;
142 }
143 0 my $is_smb1 = $smb_num == 0xff;
144 0 ▶ $self->mem($self->parser->data, "<- SMB Packet")
Branch: 1 branch
BranchTrueFalseTotal
if $self->verbose 0 0 0
145 if $self->verbose;
146  
147 0 ▶ my $command = $is_smb1
Branch: 1 branch
BranchTrueFalseTotal
$is_smb1 ? : 0 0 0
148 ? $self->parse_smb1
149 : $self->parse_smb2;
150  
151 0 ▶ if ($command) {
Branch: 1 branch
BranchTrueFalseTotal
if ($command) { } 0 0 0
152 0 $self->msg("%s", $command->dump);
153 } else {
154 0 ▶ $self->err("Failed to parse SMB%d packet", $is_smb1 ? 1 : 2);
Branch: 1 branch
BranchTrueFalseTotal
$is_smb1 ? : 0 0 0
155 }
156  
157 0 return $command;
158 }
159  
160 sub send_nbss ($$) {
161 0 ▶ my $self = shift;
Subroutine send_nbss: not called CC=3 SCAR=24.8
Pod undocumented
162 0 my $data = shift;
163  
164 0 ▶ $self->mem($data, "-> NetBIOS Packet")
Branch: 1 branch
BranchTrueFalseTotal
if $self->verbose 0 0 0
165 if $self->verbose;
166  
167 0 ▶ if (!$self->socket->write($data, length($data))) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $self->socket->write($data, length $data)) 0 0 0
168 0 $self->err("Can't write full packet");
169 0 return;
170 }
171 }
172  
173 sub send_command ($$) {
174 0 ▶ my $self = shift;
Subroutine send_command: not called CC=2 SCAR=17.9
Pod undocumented
175 0 my $command = shift;
176  
177 0 $self->msg("%s", $command->dump);
178  
179 0 $self->packer->reset;
180  
181 0 ▶ $command->is_smb1
Branch: 1 branch
BranchTrueFalseTotal
$command->is_smb1 ? : 0 0 0
182 ? $self->pack_smb1($command, is_response => 1)
183 : $self->pack_smb2($command, is_response => 1);
184  
185 0 $self->send_nbss($self->packer->data);
186 }
187  
188 sub log ($$$) {
189 0 ▶ my $self = shift;
Subroutine log: not called CC=2 SCAR=17.9
Pod documented
190 0 my $is_err = shift;
191 0 my $format = shift;
192 0 ▶ return if $self->{disable_log};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"disable_log"} 0 0 0
193 0 $format =~ s/(:?$)/ - $self->{id_str}$1/;
194 0 $self->SUPER::log($is_err, $format, @_);
195 }
196  
197 1 1;
« lib/SMB/v2/Commands.pm ↑ Summary blib/lib/SMB/v2/Commands.pm »