lib/SMB/v2/Commands.pm

filter: Statementstmt 16.3% 20 / 122 Branchbran 0.0% 0 / 40 Conditioncond 0.0% 0 / 40 MC/DCmcdc 0.0% 0 / 25 Subroutinesub 66.6% 4 / 6 Podpod 0.0% 0 / 2 totaltotal 10.2% 24 / 235 SCAR 68.4
CC
35
Cov
10%
pack
650.0
parse
116.0
BEGIN
1.0
CRAP 931.0
« blib/lib/SMB/Server.pm ↑ Summary lib/SMB/Connection.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::v2::Commands;
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 SMB::v2::Header;
Subroutine BEGIN: called CC=1 SCAR=0.0
22  
23 1 our $header_stamp = "\xfeSMB";
24  
25 1 our @command_names = (
26 'Negotiate', # 0x00
27 'SessionSetup', # 0x01
28 'SessionLogoff', # 0x02
29 'TreeConnect', # 0x03
30 'TreeDisconnect', # 0x04
31 'Create', # 0x05
32 'Close', # 0x06
33 'Flush', # 0x07
34 'Read', # 0x08
35 'Write', # 0x09
36 'Lock', # 0x0A
37 'Ioctl', # 0x0B
38 'Cancel', # 0x0C
39 'KeepAlive', # 0x0D
40 'QueryDirectory', # 0x0E
41 'ChangeNotify', # 0x0F
42 'QueryInfo', # 0x10
43 'SetInfo', # 0x11
44 'Break', # 0x12
45 );
46  
47 1 our %command_codes = map { $command_names[$_] => $_ } 0 .. $#command_names;
48  
49 1 our %command_aliases = (
50 'Echo' => 'KeepAlive',
51 'Find' => 'QueryDirectory',
52 'Notify' => 'ChangeNotify',
53 'GetInfo' => 'QueryInfo',
54 );
55  
56 1 our @command_struct_sizes = (
57 [ 36, 65 ], # 0x00
58 [ 25, 9 ], # 0x01
59 [ 4, 4 ], # 0x02
60 [ 9, 16 ], # 0x03
61 [ 4, 4 ], # 0x04
62 [ 57, 89 ], # 0x05
63 [ 24, 60 ], # 0x06
64 [ 24, 4 ], # 0x07
65 [ 49, 17 ], # 0x08
66 [ 49, 17 ], # 0x09
67 [ 48, 4 ], # 0x0A
68 [ 57, 49 ], # 0x0B
69 [ 4, 0 ], # 0x0C
70 [ 4, 4 ], # 0x0D
71 [ 33, 9 ], # 0x0E
72 [ 32, 9 ], # 0x0F
73 [ 41, 9 ], # 0x10
74 [ 33, 2 ], # 0x11
75 [ 24, 24 ], # 0x12 # or [ 36, 44 ]
76 );
77  
78 1 our $MIN_MESSAGE_SIZE = 64;
79  
80 sub parse ($$) {
81 0 ▶ my $class = shift;
Subroutine parse: not called CC=11 SCAR=47.5
Pod undocumented
82 0 ▶ my $parser = shift || die;
Condition0%0/2
shift() || die()
l!l
00
MC/DC0%0/1
shift() || die()
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
83  
84 0 ▶ if ($parser->size < $MIN_MESSAGE_SIZE) {
Branch: 1 branch
BranchTrueFalseTotal
if ($parser->size < $SMB::v2::Commands::MIN_MESSAGE_SIZE) 0 0 0
85 0 warn sprintf "Too short message to parse (%d, should be at least %d)\n", $parser->size, $MIN_MESSAGE_SIZE;
86 0 return;
87 }
88  
89 # parse header following the SMB2 stamp "\xfeSMB"
90 0 $parser->uint16; # skip reserved
91 0 my $credit_charge = $parser->uint16;
92 0 my $status = $parser->uint32;
93 0 my $code = $parser->uint16;
94 0 my $credits = $parser->uint16;
95 0 my $flags = $parser->uint32;
96 0 my $offset = $parser->uint32; # offset of the next chain command or 0
97 0 my $mid = $parser->uint64;
98 0 my $aid = 0;
99 0 my $tid = 0;
100 0 ▶ if ($flags & SMB::v2::Header::FLAGS_ASYNC_COMMAND) {
Branch: 1 branch
BranchTrueFalseTotal
if ($flags & 2) { } 0 0 0
101 0 $aid = $parser->uint64;
102 } else {
103 0 $parser->uint32; # reserved (according to spec), not pid
104 0 $tid = $parser->uint32;
105 }
106 0 my $uid = $parser->uint64;
107 0 my $sign = $parser->bytes(16);
108 0 my $struct_size = $parser->uint16;
109  
110 0 my $header = SMB::v2::Header->new(
111 code => $code,
112 status => $status,
113 uid => $uid,
114 tid => $tid,
115 mid => $mid,
116 signature => $sign,
117 flags => $flags,
118 aid => $aid,
119 credits => $credits,
120 credit_charge => $credit_charge,
121 struct_size => $struct_size,
122 );
123  
124 0 my $command_name = $command_names[$code];
125 0 my $command;
126  
127 0 ▶ if ($command_name) {
Branch: 1 branch
BranchTrueFalseTotal
if ($command_name) { } 0 0 0
128 0 my $command_class = "SMB::v2::Command::$command_name";
129 0 my $command_filename = "SMB/v2/Command/$command_name.pm";
130 0 ▶ unless ($INC{$command_filename} || $::_INC{$command_filename}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($INC{$command_filename} or $main::_INC{$command_filename}) 0 0 0
Condition0%0/3
$INC{$command_filename} or $main::_INC{$command_filename}
l!l&&r!l&&!r
000
MC/DC0%0/2
$INC{$command_filename} or $main::_INC{$command_filename}
$INC{$command_filename} $main::_INC{$command_filename}
Truth table0%0/3
A or B
A= $INC{$command_filename}B= $main::_INC{$command_filename}
ABresult
1X1
011
000
131 # auto-load or auto-create requested sub-class
132 0 ▶ if (!eval { require $command_filename; 1; }) {
Branch: 1 branch
BranchTrueFalseTotal
unless (eval { do { require $command_filename; 1 } }) 0 0 0
133 1 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
134 0 @{"${command_class}::ISA"} = qw(SMB::v2::Command);
135 0 $::_INC{$command_filename} = 1;
136 }
137 }
138  
139 0 $command = $command_class->new($header);
140 0 ▶ return $command unless $command->is_success || $command->is('SessionSetup');
Branch: 1 branch
BranchTrueFalseTotal
unless $command->is_success or $command->is("SessionSetup") 0 0 0
Condition0%0/3
$command->is_success or $command->is("SessionSetup")
l!l&&r!l&&!r
000
MC/DC0%0/2
$command->is_success or $command->is("SessionSetup")
$command->is_success $command->is("SessionSetup")
Truth table0%0/3
A or B
A= $command->is_successB= $command->is("SessionSetup")
ABresult
1X1
011
000
141  
142 0 ▶ $command = $command->parse($parser)
Branch: 1 branch
BranchTrueFalseTotal
$command = $command->parse($parser) or warn sprintf("Failed to parse SMB2 command 0x%x ($command_name)\n", $code) 0 0 0
143 or warn sprintf "Failed to parse SMB2 command 0x%x ($command_name)\n", $code;
144 } else {
145 0 warn sprintf "Got unexisting SMB2 command 0x%x\n", $code;
146 }
147  
148 0 return $command;
149 }
150  
151 sub pack ($$$%) {
152 0 ▶ my $class = shift;
Subroutine pack: not called CC=25 SCAR=64.8
Pod undocumented
153 0 my $packer = shift;
154 0 my $command = shift;
155 0 my %options = @_;
156  
157 0 my $header = $command->header;
158 0 my $status = $command->status;
159  
160 0 my $is_response = $command->is_response;
161 0 ▶ my $struct_size = $options{struct_size} // $command_struct_sizes[$header->code][$is_response] // $header->struct_size;
Condition0%0/6
($options{"struct_size"} // $SMB::v2::Commands::command_struct_sizes[$header->code][$is_response]) // $header->struct_size
l!l&&r!l&&!r
000
$options{"struct_size"} // $SMB::v2::Commands::command_struct_sizes[$header->code][$is_response]
l!l&&r!l&&!r
000
MC/DC0%0/3
($options{"struct_size"} // $SMB::v2::Commands::command_struct_sizes[$header->code][$is_response]) // $header->struct_size
$options{"struct_size"} $SMB::v2::Commands::command_struct_sizes[$header->code][$is_response] $header->struct_size
Truth table0%0/4
A // B // C
A= $options{"struct_size"}B= $SMB::v2::Commands::command_struct_sizes[$header->code][$is_response]C= $header->struct_size
ABCresult
1XX1
01X1
0011
0000
162 0 my $is_chained = $options{is_chained};
163 0 my $is_first = $options{is_first};
164 0 my $is_last = $options{is_last};
165  
166 0 my $flags = $header->flags;
167 0 ▶ if ($is_response) {
Branch: 1 branch
BranchTrueFalseTotal
if ($is_response) { } 0 0 0
168 0 $flags |= SMB::v2::Header::FLAGS_RESPONSE;
169 } else {
170 0 $flags &= ~SMB::v2::Header::FLAGS_RESPONSE;
171 }
172 0 ▶ if ($is_chained && !$is_first) {
Branch: 1 branch
BranchTrueFalseTotal
if ($is_chained and not $is_first) { } 0 0 0
Condition0%0/3
$is_chained and not $is_first
!ll&&!rl&&r
000
MC/DC0%0/2
$is_chained and not $is_first
$is_chained not $is_first
Truth table0%0/3
A and not(B)
A= $is_chainedB= not $is_first
ABresult
0X0
100
111
173 0 $flags |= SMB::v2::Header::FLAGS_CHAINED;
174 } else {
175 0 $flags &= ~SMB::v2::Header::FLAGS_CHAINED;
176 }
177  
178 # skip NetBIOS header (length will be filled later)
179 0 ▶ if (!$is_chained || $is_first) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $is_chained or $is_first) 0 0 0
Condition0%0/3
not $is_chained or $is_first
l!l&&r!l&&!r
000
MC/DC0%0/2
not $is_chained or $is_first
not $is_chained $is_first
Truth table0%0/3
not(A) or B
A= not $is_chainedB= $is_first
ABresult
1X1
011
000
180 0 $packer->mark('netbios-header');
181 0 $packer->skip(4);
182 }
183  
184 # pack SMB2 header
185 0 $packer->mark('smb-header');
186 0 $packer->bytes($header_stamp); # SMB2 magic signature
187 0 $packer->uint16(64); # header size
188 0 $packer->uint16($header->credit_charge);
189 0 $packer->mark('status');
190 0 ▶ $packer->uint32($is_response ? $status : 0);
Branch: 1 branch
BranchTrueFalseTotal
$is_response ? : 0 0 0
191 0 $packer->uint16($header->code);
192 0 ▶ $packer->uint16($header->credits || 1);
Condition0%0/2
$header->credits || 1
l!l
00
MC/DC0%0/1
$header->credits || 1
$header->credits
Truth table0%0/2
A
A= $header->credits
Aresult
11
00
193 0 $packer->uint32($flags);
194 0 $packer->mark('next-command');
195 0 $packer->uint32(0);
196 0 $packer->uint64($header->mid);
197 # aid or pid + tid
198 0 ▶ if ($flags & SMB::v2::Header::FLAGS_ASYNC_COMMAND) {
Branch: 1 branch
BranchTrueFalseTotal
if ($flags & 2) { } 0 0 0
199 0 $packer->uint64($header->aid);
200 } else {
201 0 $packer->uint32(0); # no pid in SMB2 spec
202 0 $packer->uint32($header->tid);
203 }
204 0 $packer->uint64($header->uid);
205 0 $packer->bytes("\0" x 16); # no message signing for now
206  
207 0 $packer->mark('header-end');
208 0 ▶ $packer->uint16($command->is_success ? $struct_size : 9);
Branch: 1 branch
BranchTrueFalseTotal
$command->is_success ? : 0 0 0
209 0 $packer->mark('command-start');
210  
211 0 ▶ $command->pack($packer) if $command->is_success || $command->is('SessionSetup');
Branch: 1 branch
BranchTrueFalseTotal
if $command->is_success or $command->is("SessionSetup") 0 0 0
Condition0%0/3
$command->is_success or $command->is("SessionSetup")
l!l&&r!l&&!r
000
MC/DC0%0/2
$command->is_success or $command->is("SessionSetup")
$command->is_success $command->is("SessionSetup")
Truth table0%0/3
A or B
A= $command->is_successB= $command->is("SessionSetup")
ABresult
1X1
011
000
212 0 ▶ $packer->zero(6 + 1) if $command->is_error && !$command->is('SessionSetup');
Branch: 1 branch
BranchTrueFalseTotal
if $command->is_error and not $command->is("SessionSetup") 0 0 0
Condition0%0/3
$command->is_error and not $command->is("SessionSetup")
!ll&&!rl&&r
000
MC/DC0%0/2
$command->is_error and not $command->is("SessionSetup")
$command->is_error not $command->is("SessionSetup")
Truth table0%0/3
A and not(B)
A= $command->is_errorB= not $command->is("SessionSetup")
ABresult
0X0
100
111
213  
214 0 my $payload_allowed = $struct_size % 2;
215 0 ▶ $payload_allowed = 1 if $command->is('Negotiate') && !$is_response;
Branch: 1 branch
BranchTrueFalseTotal
if $command->is("Negotiate") and not $is_response 0 0 0
Condition0%0/3
$command->is("Negotiate") and not $is_response
!ll&&!rl&&r
000
MC/DC0%0/2
$command->is("Negotiate") and not $is_response
$command->is("Negotiate") not $is_response
Truth table0%0/3
A and not(B)
A= $command->is("Negotiate")B= not $is_response
ABresult
0X0
100
111
216 0 my $size = $packer->diff('header-end');
217 0 my $size0 = $struct_size & ~1;
218 0 ▶ die "SMB2 command $command->{name} pack produced size $size, expected $size0\n"
Branch: 1 branch
BranchTrueFalseTotal
if $size > $size0 and not $payload_allowed 0 0 0
Condition0%0/3
$size > $size0 and not $payload_allowed
!ll&&!rl&&r
000
MC/DC0%0/2
$size > $size0 and not $payload_allowed
$size > $size0 not $payload_allowed
Truth table0%0/3
A and not(B)
A= $size > $size0B= not $payload_allowed
ABresult
0X0
100
111
219 if $size > $size0 && !$payload_allowed;
220 0 ▶ $packer->zero($size0 - $size) if $size0 > $size;
Branch: 1 branch
BranchTrueFalseTotal
if $size0 > $size 0 0 0
221  
222 0 $packer->mark('end');
223 0 ▶ if ($is_chained && !$is_last) {
Branch: 1 branch
BranchTrueFalseTotal
if ($is_chained and not $is_last) 0 0 0
Condition0%0/3
$is_chained and not $is_last
!ll&&!rl&&r
000
MC/DC0%0/2
$is_chained and not $is_last
$is_chained not $is_last
Truth table0%0/3
A and not(B)
A= $is_chainedB= not $is_last
ABresult
0X0
100
111
224 0 my $command_size = $packer->diff('header');
225 0 my $command_size_padded = ($command_size + 7) & ~7;
226 0 $packer->zero($command_size_padded - $command_size);
227 0 $packer->mark('end');
228 0 $packer->jump('next-command');
229 0 $packer->uint32($command_size_padded);
230 }
231 0 ▶ if (!$is_chained || $is_last) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $is_chained or $is_last) 0 0 0
Condition0%0/3
not $is_chained or $is_last
l!l&&r!l&&!r
000
MC/DC0%0/2
not $is_chained or $is_last
not $is_chained $is_last
Truth table0%0/3
not(A) or B
A= not $is_chainedB= $is_last
ABresult
1X1
011
000
232 0 $packer->jump('netbios-header');
233 0 $packer->uint32_be(-$packer->diff('end') - 4);
234 }
235 0 $packer->jump('end');
236 }
237  
238 1 1;
« blib/lib/SMB/Server.pm ↑ Summary lib/SMB/Connection.pm »