blib/lib/SMB/Connection.pm untested

filter: Statementstmt 0.0% 0 / 112 Branchbran 0.0% 0 / 30 Conditioncond 0.0% 0 / 30 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 29 Podpod 0.0% 0 / 21 totaltotal 0.0% 0 / 222 CC 16 SCAR 23.0
CC
16
Cov
0%
recv_nbss
30.0
recv_command
30.0
new
12.0
CRAP 10.0
1 # SMB Perl library, Copyright (C) 2014-2018 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 use strict;
19 use warnings;
20  
21 use bytes;
22  
23 use parent 'SMB';
24  
25 use SMB::Parser;
26 use SMB::Packer;
27 use SMB::v1::Commands;
28 use SMB::v2::Commands;
29  
30 sub parse_uint8 { $_[0]->parser->uint8; }
31 sub parse_uint16 { $_[0]->parser->uint16; }
32 sub parse_uint32 { $_[0]->parser->uint32; }
33 sub parse_bytes { $_[0]->parser->bytes($_[1]); }
34 sub parse_smb1 { SMB::v1::Commands->parse($_[0]->parser) }
35 sub parse_smb2 { SMB::v2::Commands->parse($_[0]->parser) }
36  
37 sub pack_uint8 { $_[0]->packer->uint8($_[1]); }
38 sub pack_uint16 { $_[0]->packer->uint16($_[1]); }
39 sub pack_uint32 { $_[0]->packer->uint32($_[1]); }
40 sub pack_bytes { $_[0]->packer->bytes($_[1]); }
41 sub pack_smb1 { SMB::v1::Commands->pack(shift()->packer, shift, @_) }
42 sub pack_smb2 { SMB::v2::Commands->pack(shift()->packer, shift, @_) }
43  
44 sub new ($$$%) {
45 my $class = shift;
46 my $socket = shift || die "No socket";
47 my $id = shift || die "No id";
48 my %options = @_;
49  
50 my $self = $class->SUPER::new(
51 %options,
52 socket => $socket,
53 id => $id,
54 parser => SMB::Parser->new,
55 packer => SMB::Packer->new,
56 );
57  
58 unless ($self->log_level == SMB::LOG_LEVEL_NONE) {
59 my $addr_with_port = $self->get_socket_addr;
60 my ($id0, $str) = $id =~ /^-(.*)/ ? ($1, 'server') : ($id, 'client');
61 $self->{id_str} = "$str #$id0 [$addr_with_port]";
62 }
63  
64 $self->msg("Connected");
65  
66 return $self;
67 }
68  
69 sub DESTROY ($) {
70 my $self = shift;
71  
72 $self->close;
73 }
74  
75 sub close ($) {
76 my $self = shift;
77  
78 my $socket = $self->socket;
79 return unless $socket && $socket->opened;
80  
81 $self->msg("Disconnected");
82  
83 $socket->close;
84 $self->socket(undef);
85 }
86  
87 sub get_socket_addr ($;$) {
88 my $this = shift;
89 my $socket = shift || ref($this) && $this->socket || return;
90  
91 my $host = $socket->peerhost();
92 my $port = $socket->peerport();
93  
94 return wantarray ? ($host, $port) : "$host:$port";
95 }
96  
97 sub recv_nbss ($) {
98 my $self = shift;
99  
100 my $socket = $self->socket;
101 my $data1; # NBSS header
102 my $data2; # SMB packet
103 my $header_label = 'NetBIOS Session Service header';
104 my $len = $socket->read($data1, 4) //
105 return $self->err("Socket read failed: $!");
106 if ($len != 4) {
107 $self->err("Can't read $header_label (got $len bytes)");
108 return;
109 }
110 my ($packet_type, $packet_flags, $packet_len) = unpack('CCn', $data1);
111 if ($packet_type != 0 || $packet_flags > 1) {
112 $self->err("Only supported $header_label with type=0 flags=0|1");
113 return;
114 }
115 $packet_len += 1 << 16 if $packet_flags;
116 $len = $socket->read($data2, $packet_len) // 0;
117 if ($len != $packet_len) {
118 $self->err("Can't read full packet (expected $packet_len, got $len bytes)");
119 return;
120 }
121  
122 $self->parser->set($data1 . $data2, 4);
123 }
124  
125 sub recv_command ($) {
126 my $self = shift;
127  
128 $self->recv_nbss
129 or return;
130  
131 my $smb_num = $self->parse_uint8;
132 my $smb_str = $self->parse_bytes(3);
133 if ($smb_str ne 'SMB' || $smb_num != 0xff && $smb_num != 0xfe) {
134 $self->err("Neither SMB1 nor SMB2 signature found, giving up");
135 $self->mem(chr($smb_num) . $smb_str, "Signature");
136 return;
137 }
138 my $is_smb1 = $smb_num == 0xff;
139 $self->mem($self->parser->data, "<- SMB Packet");
140  
141 my $command = $is_smb1
142 ? $self->parse_smb1
143 : $self->parse_smb2;
144  
145 if ($command) {
146 $self->dbg("%s", $command->to_string);
147 } else {
148 $self->err("Failed to parse SMB%d packet", $is_smb1 ? 1 : 2);
149 }
150  
151 return $command;
152 }
153  
154 sub send_nbss ($$) {
155 my $self = shift;
156 my $data = shift;
157  
158 $self->mem($data, "-> NetBIOS Packet");
159  
160 if (!$self->socket->write($data, length($data))) {
161 $self->err("Can't write full packet");
162 return;
163 }
164 }
165  
166 sub send_command ($$) {
167 my $self = shift;
168 my $command = shift;
169  
170 $self->dbg("%s", $command->to_string);
171  
172 $self->packer->reset;
173  
174 $command->is_smb1
175 ? $self->pack_smb1($command, is_response => 1)
176 : $self->pack_smb2($command, is_response => 1);
177  
178 $self->send_nbss($self->packer->data);
179 }
180  
181 sub log ($$$) {
182 my $self = shift;
183 my $level = shift;
184 my $format = shift;
185 return if $level > $self->{log_level};
186  
187 $format =~ s/(:?$)/ - $self->{id_str}$1/;
188 $self->SUPER::log($level, $format, @_);
189 }
190  
191 1;