| 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 | use strict; | ||
| 19 | use warnings; | ||
| 20 | |||
| 21 | use SMB::v2::Header; | ||
| 22 | |||
| 23 | our $header_stamp = "\xfeSMB"; | ||
| 24 | |||
| 25 | 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 | our %command_codes = map { $command_names[$_] => $_ } 0 .. $#command_names; | ||
| 48 | |||
| 49 | our %command_aliases = ( | ||
| 50 | 'Echo' => 'KeepAlive', | ||
| 51 | 'Find' => 'QueryDirectory', | ||
| 52 | 'Notify' => 'ChangeNotify', | ||
| 53 | 'GetInfo' => 'QueryInfo', | ||
| 54 | ); | ||
| 55 | |||
| 56 | 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 | our $MIN_MESSAGE_SIZE = 64; | ||
| 79 | |||
| 80 | sub parse ($$) { | ||
| 81 | my $class = shift; | ||
| 82 | my $parser = shift || die; | ||
| 83 | |||
| 84 | if ($parser->size < $MIN_MESSAGE_SIZE) { | ||
| 85 | warn sprintf "Too short message to parse (%d, should be at least %d)\n", $parser->size, $MIN_MESSAGE_SIZE; | ||
| 86 | return; | ||
| 87 | } | ||
| 88 | |||
| 89 | # parse header following the SMB2 stamp "\xfeSMB" | ||
| 90 | $parser->uint16; # skip reserved | ||
| 91 | my $credit_charge = $parser->uint16; | ||
| 92 | my $status = $parser->uint32; | ||
| 93 | my $code = $parser->uint16; | ||
| 94 | my $credits = $parser->uint16; | ||
| 95 | my $flags = $parser->uint32; | ||
| 96 | my $offset = $parser->uint32; # offset of the next chain command or 0 | ||
| 97 | my $mid = $parser->uint64; | ||
| 98 | my $aid = 0; | ||
| 99 | my $tid = 0; | ||
| 100 | if ($flags & SMB::v2::Header::FLAGS_ASYNC_COMMAND) { | ||
| 101 | $aid = $parser->uint64; | ||
| 102 | } else { | ||
| 103 | $parser->uint32; # reserved (according to spec), not pid | ||
| 104 | $tid = $parser->uint32; | ||
| 105 | } | ||
| 106 | my $uid = $parser->uint64; | ||
| 107 | my $sign = $parser->bytes(16); | ||
| 108 | my $struct_size = $parser->uint16; | ||
| 109 | |||
| 110 | 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 | my $command_name = $command_names[$code]; | ||
| 125 | my $command; | ||
| 126 | |||
| 127 | if ($command_name) { | ||
| 128 | my $command_class = "SMB::v2::Command::$command_name"; | ||
| 129 | my $command_filename = "SMB/v2/Command/$command_name.pm"; | ||
| 130 | unless ($INC{$command_filename} || $::_INC{$command_filename}) { | ||
| 131 | # auto-load or auto-create requested sub-class | ||
| 132 | if (!eval { require $command_filename; 1; }) { | ||
| 133 | no strict 'refs'; | ||
| 134 | @{"${command_class}::ISA"} = qw(SMB::v2::Command); | ||
| 135 | $::_INC{$command_filename} = 1; | ||
| 136 | } | ||
| 137 | } | ||
| 138 | |||
| 139 | $command = $command_class->new($header); | ||
| 140 | return $command unless $command->is_success || $command->is('SessionSetup'); | ||
| 141 | |||
| 142 | $command = $command->parse($parser) | ||
| 143 | or warn sprintf "Failed to parse SMB2 command 0x%x ($command_name)\n", $code; | ||
| 144 | } else { | ||
| 145 | warn sprintf "Got unexisting SMB2 command 0x%x\n", $code; | ||
| 146 | } | ||
| 147 | |||
| 148 | return $command; | ||
| 149 | } | ||
| 150 | |||
| 151 | sub pack ($$$%) { | ||
| 152 | my $class = shift; | ||
| 153 | my $packer = shift; | ||
| 154 | my $command = shift; | ||
| 155 | my %options = @_; | ||
| 156 | |||
| 157 | my $header = $command->header; | ||
| 158 | my $status = $command->status; | ||
| 159 | |||
| 160 | my $is_response = $command->is_response; | ||
| 161 | my $struct_size = $options{struct_size} // $command_struct_sizes[$header->code][$is_response] // $header->struct_size; | ||
| 162 | my $is_chained = $options{is_chained}; | ||
| 163 | my $is_first = $options{is_first}; | ||
| 164 | my $is_last = $options{is_last}; | ||
| 165 | |||
| 166 | my $flags = $header->flags; | ||
| 167 | if ($is_response) { | ||
| 168 | $flags |= SMB::v2::Header::FLAGS_RESPONSE; | ||
| 169 | } else { | ||
| 170 | $flags &= ~SMB::v2::Header::FLAGS_RESPONSE; | ||
| 171 | } | ||
| 172 | if ($is_chained && !$is_first) { | ||
| 173 | $flags |= SMB::v2::Header::FLAGS_CHAINED; | ||
| 174 | } else { | ||
| 175 | $flags &= ~SMB::v2::Header::FLAGS_CHAINED; | ||
| 176 | } | ||
| 177 | |||
| 178 | # skip NetBIOS header (length will be filled later) | ||
| 179 | if (!$is_chained || $is_first) { | ||
| 180 | $packer->mark('netbios-header'); | ||
| 181 | $packer->skip(4); | ||
| 182 | } | ||
| 183 | |||
| 184 | # pack SMB2 header | ||
| 185 | $packer->mark('smb-header'); | ||
| 186 | $packer->bytes($header_stamp); # SMB2 magic signature | ||
| 187 | $packer->uint16(64); # header size | ||
| 188 | $packer->uint16($header->credit_charge); | ||
| 189 | $packer->mark('status'); | ||
| 190 | $packer->uint32($is_response ? $status : 0); | ||
| 191 | $packer->uint16($header->code); | ||
| 192 | $packer->uint16($header->credits || 1); | ||
| 193 | $packer->uint32($flags); | ||
| 194 | $packer->mark('next-command'); | ||
| 195 | $packer->uint32(0); | ||
| 196 | $packer->uint64($header->mid); | ||
| 197 | # aid or pid + tid | ||
| 198 | if ($flags & SMB::v2::Header::FLAGS_ASYNC_COMMAND) { | ||
| 199 | $packer->uint64($header->aid); | ||
| 200 | } else { | ||
| 201 | $packer->uint32(0); # no pid in SMB2 spec | ||
| 202 | $packer->uint32($header->tid); | ||
| 203 | } | ||
| 204 | $packer->uint64($header->uid); | ||
| 205 | $packer->bytes("\0" x 16); # no message signing for now | ||
| 206 | |||
| 207 | $packer->mark('header-end'); | ||
| 208 | $packer->uint16($command->is_success ? $struct_size : 9); | ||
| 209 | $packer->mark('command-start'); | ||
| 210 | |||
| 211 | $command->pack($packer) if $command->is_success || $command->is('SessionSetup'); | ||
| 212 | $packer->zero(6 + 1) if $command->is_error && !$command->is('SessionSetup'); | ||
| 213 | |||
| 214 | my $payload_allowed = $struct_size % 2; | ||
| 215 | $payload_allowed = 1 if $command->is('Negotiate') && !$is_response; | ||
| 216 | my $size = $packer->diff('header-end'); | ||
| 217 | my $size0 = $struct_size & ~1; | ||
| 218 | die "SMB2 command $command->{name} pack produced size $size, expected $size0\n" | ||
| 219 | if $size > $size0 && !$payload_allowed; | ||
| 220 | $packer->zero($size0 - $size) if $size0 > $size; | ||
| 221 | |||
| 222 | $packer->mark('end'); | ||
| 223 | if ($is_chained && !$is_last) { | ||
| 224 | my $command_size = $packer->diff('header'); | ||
| 225 | my $command_size_padded = ($command_size + 7) & ~7; | ||
| 226 | $packer->zero($command_size_padded - $command_size); | ||
| 227 | $packer->mark('end'); | ||
| 228 | $packer->jump('next-command'); | ||
| 229 | $packer->uint32($command_size_padded); | ||
| 230 | } | ||
| 231 | if (!$is_chained || $is_last) { | ||
| 232 | $packer->jump('netbios-header'); | ||
| 233 | $packer->uint32_be(-$packer->diff('end') - 4); | ||
| 234 | } | ||
| 235 | $packer->jump('end'); | ||
| 236 | } | ||
| 237 | |||
| 238 | 1; |