-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprotocol.rs
More file actions
1351 lines (1182 loc) · 49.4 KB
/
protocol.rs
File metadata and controls
1351 lines (1182 loc) · 49.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//! SMB 3.1.1 wire protocol definitions (macOS 26 dialect only).
//!
//! All structures are little-endian on the wire. We define the constants,
//! header layout, and per-command request/response formats needed for
//! basic file I/O operations.
use bytes::{Buf, BufMut, Bytes, BytesMut};
// ── SMB2 magic ──────────────────────────────────────────────────────────────
pub const SMB2_MAGIC: &[u8; 4] = b"\xfeSMB";
pub const SMB2_HEADER_SIZE: usize = 64;
// ── Commands ────────────────────────────────────────────────────────────────
#[repr(u16)]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Command {
Negotiate = 0x0000,
SessionSetup = 0x0001,
TreeConnect = 0x0003,
Create = 0x0005,
Close = 0x0006,
Read = 0x0008,
Write = 0x0009,
QueryDirectory = 0x000E,
SetInfo = 0x0011,
}
// ── NT Status codes we care about ───────────────────────────────────────────
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum NtStatus {
Success,
MoreProcessingRequired,
NoSuchFile,
ObjectNameNotFound,
ObjectNameCollision,
AccessDenied,
EndOfFile,
NoMoreFiles,
ObjectPathNotFound,
Unknown(u32),
}
impl NtStatus {
pub fn from_u32(v: u32) -> Self {
match v {
0x00000000 => Self::Success,
0xC0000016 => Self::MoreProcessingRequired,
0xC000000F => Self::NoSuchFile,
0xC0000034 => Self::ObjectNameNotFound,
0xC0000035 => Self::ObjectNameCollision,
0xC0000022 => Self::AccessDenied,
0xC0000011 => Self::EndOfFile,
0x80000006 => Self::NoMoreFiles,
0xC000003A => Self::ObjectPathNotFound,
other => Self::Unknown(other),
}
}
pub fn is_error(self) -> bool {
let code = match self {
Self::Success => 0x00000000,
Self::MoreProcessingRequired => 0xC0000016,
Self::NoSuchFile => 0xC000000F,
Self::ObjectNameNotFound => 0xC0000034,
Self::ObjectNameCollision => 0xC0000035,
Self::AccessDenied => 0xC0000022,
Self::EndOfFile => 0xC0000011,
Self::NoMoreFiles => 0x80000006,
Self::ObjectPathNotFound => 0xC000003A,
Self::Unknown(v) => v,
};
code & 0xC0000000 == 0xC0000000
}
}
// ── SMB2 Header ─────────────────────────────────────────────────────────────
#[derive(Debug, Clone)]
pub struct Header {
pub command: u16,
pub credit_charge: u16,
pub status: u32,
pub credits_requested: u16,
pub flags: u32,
pub next_command: u32,
pub message_id: u64,
pub tree_id: u32,
pub session_id: u64,
}
impl Header {
pub fn new(command: Command, message_id: u64) -> Self {
Self {
command: command as u16,
credit_charge: 1,
status: 0,
credits_requested: 256,
flags: 0,
next_command: 0,
message_id,
tree_id: 0,
session_id: 0,
}
}
/// Set credit charge for operations transferring `payload_size` bytes.
/// Required for Read/Write/QueryDirectory with payloads >64KB.
pub fn with_credit_charge(mut self, payload_size: u32) -> Self {
self.credit_charge = credit_charge_for(payload_size);
self
}
/// Encode the 64-byte SMB2 header into a buffer.
pub fn encode(&self, buf: &mut BytesMut) {
buf.put_slice(SMB2_MAGIC); // 0: ProtocolId
buf.put_u16_le(64); // 4: StructureSize
buf.put_u16_le(self.credit_charge); // 6: CreditCharge
buf.put_u32_le(self.status); // 8: Status
buf.put_u16_le(self.command); // 12: Command
buf.put_u16_le(self.credits_requested); // 14: CreditRequest
buf.put_u32_le(self.flags); // 16: Flags
buf.put_u32_le(self.next_command); // 20: NextCommand
buf.put_u64_le(self.message_id); // 24: MessageID
buf.put_u32_le(0); // 32: Reserved (async: AsyncId high)
buf.put_u32_le(self.tree_id); // 36: TreeId (sync)
buf.put_u64_le(self.session_id); // 40: SessionId
buf.put_slice(&[0u8; 16]); // 48: Signature
}
/// Decode a 64-byte SMB2 header from bytes.
pub fn decode(mut buf: &[u8]) -> Option<Self> {
if buf.len() < SMB2_HEADER_SIZE {
return None;
}
let magic = &buf[..4];
if magic != SMB2_MAGIC {
return None;
}
buf = &buf[4..];
let _structure_size = (&buf[..2]).get_u16_le(); // skip past
let buf = &buf[2..];
let credit_charge = (&buf[..2]).get_u16_le();
let status = (&buf[2..6]).get_u32_le();
let command = (&buf[6..8]).get_u16_le();
let credits_requested = (&buf[8..10]).get_u16_le();
let flags = (&buf[10..14]).get_u32_le();
let next_command = (&buf[14..18]).get_u32_le();
let message_id = (&buf[18..26]).get_u64_le();
let _reserved = (&buf[26..30]).get_u32_le();
let tree_id = (&buf[30..34]).get_u32_le();
let session_id = (&buf[34..42]).get_u64_le();
// signature at 42..58 — skip for now
Some(Self {
command,
credit_charge,
status,
credits_requested,
flags,
next_command,
message_id,
tree_id,
session_id,
})
}
}
/// Compute the credit charge for a payload of the given size.
/// CreditCharge = max(1, ceil(payload_size / 65536))
pub fn credit_charge_for(payload_size: u32) -> u16 {
1.max(payload_size.div_ceil(65536) as u16)
}
// ── Negotiate ───────────────────────────────────────────────────────────────
/// SMB 3.1.x dialect family
pub const DIALECT_SMB3_1_1: u16 = 0x0311;
pub const DIALECT_SMB3_0_2: u16 = 0x0302;
pub const DIALECT_SMB3_0_0: u16 = 0x0300;
// Offered dialects in preference order (highest first)
const DIALECTS: [u16; 3] = [DIALECT_SMB3_1_1, DIALECT_SMB3_0_2, DIALECT_SMB3_0_0];
// Negotiate context types for SMB 3.1.1
const SMB2_PREAUTH_INTEGRITY_CAPABILITIES: u16 = 0x0001;
const SMB2_ENCRYPTION_CAPABILITIES: u16 = 0x0002;
// Hash algorithm: SHA-512
const SHA_512: u16 = 0x0001;
// Cipher: AES-128-GCM (preferred by macOS)
const AES_128_GCM: u16 = 0x0002;
const AES_128_CCM: u16 = 0x0001;
pub fn encode_negotiate_request(buf: &mut BytesMut, client_guid: &[u8; 16]) {
let dialect_count = DIALECTS.len() as u16;
let dialects_len = DIALECTS.len() * 2;
// Build negotiate contexts (required when offering 3.1.1)
let mut contexts = BytesMut::new();
// Preauth Integrity Capabilities context
let preauth_data_len: u16 = 2 + 2 + 2 + 32; // HashAlgCount + SaltLength + HashAlg + Salt
contexts.put_u16_le(SMB2_PREAUTH_INTEGRITY_CAPABILITIES);
contexts.put_u16_le(preauth_data_len);
contexts.put_u32_le(0); // Reserved
contexts.put_u16_le(1); // HashAlgorithmCount
contexts.put_u16_le(32); // SaltLength
contexts.put_u16_le(SHA_512);
let salt = random_bytes::<32>();
contexts.put_slice(&salt);
// Pad to 8-byte alignment
let pad = (8 - (contexts.len() % 8)) % 8;
contexts.put_slice(&vec![0u8; pad]);
// Encryption Capabilities context
let enc_data_len: u16 = 2 + 2 * 2; // CipherCount + 2 ciphers
contexts.put_u16_le(SMB2_ENCRYPTION_CAPABILITIES);
contexts.put_u16_le(enc_data_len);
contexts.put_u32_le(0); // Reserved
contexts.put_u16_le(2); // CipherCount
contexts.put_u16_le(AES_128_GCM); // Preferred
contexts.put_u16_le(AES_128_CCM); // Fallback
let body_fixed_len = 36 + dialects_len;
let ctx_padding = (8 - (body_fixed_len % 8)) % 8;
let ctx_offset = (SMB2_HEADER_SIZE + body_fixed_len + ctx_padding) as u32;
buf.put_u16_le(36); // StructureSize
buf.put_u16_le(dialect_count);
buf.put_u16_le(0x0001); // SecurityMode: signing enabled
buf.put_u16_le(0); // Reserved
buf.put_u32_le(0x00000041); // Capabilities: DFS | Leasing
buf.put_slice(client_guid);
buf.put_u32_le(ctx_offset); // NegotiateContextOffset
buf.put_u16_le(2); // NegotiateContextCount
buf.put_u16_le(0); // Reserved2
for &d in &DIALECTS {
buf.put_u16_le(d);
}
buf.put_slice(&vec![0u8; ctx_padding]);
buf.put_slice(&contexts);
}
fn random_bytes<const N: usize>() -> [u8; N] {
let mut buf = [0u8; N];
unsafe extern "C" {
fn arc4random_buf(buf: *mut u8, nbytes: usize);
}
unsafe {
arc4random_buf(buf.as_mut_ptr(), N);
}
buf
}
#[derive(Debug)]
#[allow(dead_code)]
pub struct NegotiateResponse {
pub security_mode: u16,
pub dialect_revision: u16,
pub max_transact_size: u32,
pub max_read_size: u32,
pub max_write_size: u32,
}
pub fn decode_negotiate_response(body: &[u8]) -> Option<NegotiateResponse> {
if body.len() < 40 {
return None;
}
let security_mode = (&body[2..4]).get_u16_le();
let dialect_revision = (&body[4..6]).get_u16_le();
let max_transact_size = (&body[28..32]).get_u32_le();
let max_read_size = (&body[32..36]).get_u32_le();
let max_write_size = (&body[36..40]).get_u32_le();
Some(NegotiateResponse {
security_mode,
dialect_revision,
max_transact_size,
max_read_size,
max_write_size,
})
}
// ── Session Setup ───────────────────────────────────────────────────────────
pub fn encode_session_setup_request(buf: &mut BytesMut, security_blob: &[u8]) {
let offset = (SMB2_HEADER_SIZE + 24) as u16; // header + fixed part of this request
buf.put_u16_le(25); // StructureSize
buf.put_u8(0); // Flags
buf.put_u8(0x01); // SecurityMode: signing enabled
buf.put_u32_le(0); // Capabilities
buf.put_u32_le(0); // Channel
buf.put_u16_le(offset); // SecurityBufferOffset
buf.put_u16_le(security_blob.len() as u16); // SecurityBufferLength
buf.put_u64_le(0); // PreviousSessionId
buf.put_slice(security_blob);
}
#[derive(Debug)]
pub struct SessionSetupResponse {
pub session_id: u64,
pub security_buffer: Bytes,
}
pub fn decode_session_setup_response(header: &Header, body: &[u8]) -> Option<SessionSetupResponse> {
if body.len() < 9 {
return None;
}
let security_buffer_offset = (&body[4..6]).get_u16_le() as usize;
let security_buffer_length = (&body[6..8]).get_u16_le() as usize;
let sec_start = security_buffer_offset.saturating_sub(SMB2_HEADER_SIZE);
let sec_end = sec_start + security_buffer_length;
let security_buffer = if sec_end <= body.len() {
Bytes::copy_from_slice(&body[sec_start..sec_end])
} else {
Bytes::new()
};
Some(SessionSetupResponse {
session_id: header.session_id,
security_buffer,
})
}
// ── Tree Connect ────────────────────────────────────────────────────────────
pub fn encode_tree_connect_request(buf: &mut BytesMut, path: &str) {
let path_bytes: Vec<u8> = path.encode_utf16().flat_map(|c| c.to_le_bytes()).collect();
let offset = (SMB2_HEADER_SIZE + 8) as u16;
buf.put_u16_le(9); // StructureSize
buf.put_u16_le(0); // Reserved / Flags
buf.put_u16_le(offset); // PathOffset
buf.put_u16_le(path_bytes.len() as u16); // PathLength
buf.put_slice(&path_bytes);
}
// ── Create (Open) ───────────────────────────────────────────────────────────
#[repr(u32)]
#[derive(Debug, Clone, Copy)]
pub enum DesiredAccess {
GenericRead = 0x80000000,
GenericWrite = 0x40000000,
Delete = 0x00010000,
ReadAttributes = 0x00000080,
}
#[repr(u32)]
#[derive(Debug, Clone, Copy)]
pub enum ShareAccess {
Read = 0x00000001,
Delete = 0x00000004,
All = 0x00000007,
}
#[repr(u32)]
#[derive(Debug, Clone, Copy)]
pub enum CreateDisposition {
Open = 0x00000001,
OpenIf = 0x00000003,
OverwriteIf = 0x00000005,
}
#[repr(u32)]
#[derive(Debug, Clone, Copy)]
pub enum CreateOptions {
DirectoryFile = 0x00000001,
NonDirectoryFile = 0x00000040,
}
pub fn encode_create_request(
buf: &mut BytesMut,
path: &str,
desired_access: u32,
share_access: u32,
create_disposition: u32,
create_options: u32,
) {
let name_bytes: Vec<u8> = path.encode_utf16().flat_map(|c| c.to_le_bytes()).collect();
let name_offset = (SMB2_HEADER_SIZE + 56) as u16; // header + create request fixed part (57 - 1 buffer byte)
// StructureSize for Create request is 57
buf.put_u16_le(57); // StructureSize
buf.put_u8(0); // SecurityFlags
buf.put_u8(0x00); // RequestedOplockLevel: SMB2_OPLOCK_LEVEL_NONE
buf.put_u32_le(0x00000002); // ImpersonationLevel: Impersonation
buf.put_u64_le(0); // SmbCreateFlags
buf.put_u64_le(0); // Reserved
buf.put_u32_le(desired_access); // DesiredAccess
buf.put_u32_le(0x00000080); // FileAttributes: NORMAL
buf.put_u32_le(share_access); // ShareAccess
buf.put_u32_le(create_disposition); // CreateDisposition
buf.put_u32_le(create_options); // CreateOptions
buf.put_u16_le(name_offset); // NameOffset
buf.put_u16_le(name_bytes.len() as u16); // NameLength
buf.put_u32_le(0); // CreateContextsOffset
buf.put_u32_le(0); // CreateContextsLength
buf.put_slice(&name_bytes);
}
#[derive(Debug, Clone)]
pub struct CreateResponse {
pub file_id: [u8; 16],
pub last_write_time: u64,
pub file_size: u64,
}
pub fn decode_create_response(body: &[u8]) -> Option<CreateResponse> {
// Minimum: 88 bytes (StructureSize says 89, but the last byte is variable-length CreateContexts)
if body.len() < 88 {
return None;
}
let last_write_time = (&body[24..32]).get_u64_le();
// AllocationSize at 40..48
let file_size = (&body[48..56]).get_u64_le();
// Reserved2 at 60..64
let mut file_id = [0u8; 16];
file_id.copy_from_slice(&body[64..80]);
Some(CreateResponse {
file_id,
last_write_time,
file_size,
})
}
// ── Close ───────────────────────────────────────────────────────────────────
pub fn encode_close_request(buf: &mut BytesMut, file_id: &[u8; 16]) {
buf.put_u16_le(24); // StructureSize
buf.put_u16_le(0); // Flags
buf.put_u32_le(0); // Reserved
buf.put_slice(file_id); // FileId
}
// ── Read ────────────────────────────────────────────────────────────────────
pub fn encode_read_request(buf: &mut BytesMut, file_id: &[u8; 16], offset: u64, length: u32) {
buf.put_u16_le(49); // StructureSize
buf.put_u8(0); // Padding
buf.put_u8(0); // Flags
buf.put_u32_le(length); // Length
buf.put_u64_le(offset); // Offset
buf.put_slice(file_id); // FileId
buf.put_u32_le(1); // MinimumCount
buf.put_u32_le(0); // Channel
buf.put_u32_le(0); // RemainingBytes
buf.put_u16_le(0); // ReadChannelInfoOffset
buf.put_u16_le(0); // ReadChannelInfoLength
buf.put_u8(0); // Buffer (padding byte)
}
pub fn decode_read_response(body: &[u8]) -> Option<Bytes> {
if body.len() < 17 {
return None;
}
let data_offset = u16::from_le_bytes(body[2..4].try_into().unwrap()) as usize;
let data_length = (&body[4..8]).get_u32_le() as usize;
let start = data_offset.saturating_sub(SMB2_HEADER_SIZE);
let end = start + data_length;
if end > body.len() {
return None;
}
Some(Bytes::copy_from_slice(&body[start..end]))
}
/// Zero-copy variant of `decode_read_response` — takes ownership of the
/// response body Vec and slices into it without copying the data.
pub fn decode_read_response_owned(body: Vec<u8>) -> Option<Bytes> {
if body.len() < 17 {
return None;
}
let data_offset = u16::from_le_bytes(body[2..4].try_into().unwrap()) as usize;
let data_length = u32::from_le_bytes(body[4..8].try_into().unwrap()) as usize;
let start = data_offset.saturating_sub(SMB2_HEADER_SIZE);
let end = start + data_length;
if end > body.len() {
return None;
}
let mut bytes = Bytes::from(body);
bytes = bytes.slice(start..end);
Some(bytes)
}
/// Zero-copy decode that takes ownership of the full SMB2 message (header +
/// body) and returns a `Bytes` slice referencing the response payload. Avoids
/// the extra body-copy that `decode_read_response_owned` would require if the
/// caller had to split body off first.
///
/// Treats `data_offset` as an absolute offset from the start of the SMB2
/// message and rejects offsets that fall inside the 64-byte header *or*
/// inside the 16 bytes of fixed read-response fields that precede the data
/// buffer. A malformed server response with an offset short of
/// `SMB2_HEADER_SIZE + 16` would otherwise let us return header bytes — or
/// the response's own StructureSize/DataOffset/DataLength/etc — as the file
/// payload.
pub fn decode_read_response_from_msg(msg: Vec<u8>) -> Option<Bytes> {
if msg.len() < SMB2_HEADER_SIZE + READ_RESPONSE_FIXED_PART {
return None;
}
let body = &msg[SMB2_HEADER_SIZE..];
let data_offset = u16::from_le_bytes(body[2..4].try_into().unwrap()) as usize;
let data_length = u32::from_le_bytes(body[4..8].try_into().unwrap()) as usize;
// `data_offset` is from the start of the SMB2 message. The minimum
// legitimate value points at the first byte of the read response's
// Buffer field — anything before that overlaps the SMB2 header or the
// read response's fixed fields (StructureSize, DataOffset, Reserved,
// DataLength, DataRemaining, Flags = 16 bytes).
let min_offset = SMB2_HEADER_SIZE + READ_RESPONSE_FIXED_PART;
if data_offset < min_offset {
return None;
}
let start = data_offset;
let end = start.checked_add(data_length)?;
if end > msg.len() {
return None;
}
let bytes = Bytes::from(msg);
Some(bytes.slice(start..end))
}
/// Size of the read response's fixed fields (preceding the Buffer):
/// StructureSize(2) + DataOffset(1) + Reserved(1) + DataLength(4)
/// + DataRemaining(4) + Flags(4) = 16 bytes.
pub const READ_RESPONSE_FIXED_PART: usize = 16;
// ── Write ───────────────────────────────────────────────────────────────────
pub fn encode_write_request(buf: &mut BytesMut, file_id: &[u8; 16], offset: u64, data: &[u8]) {
let data_offset = (SMB2_HEADER_SIZE + 48) as u16;
buf.put_u16_le(49); // StructureSize
buf.put_u16_le(data_offset); // DataOffset
buf.put_u32_le(data.len() as u32); // Length
buf.put_u64_le(offset); // Offset
buf.put_slice(file_id); // FileId
buf.put_u32_le(0); // Channel
buf.put_u32_le(0); // RemainingBytes
buf.put_u16_le(0); // WriteChannelInfoOffset
buf.put_u16_le(0); // WriteChannelInfoLength
buf.put_u32_le(0); // Flags
buf.put_slice(data);
}
pub fn decode_write_response(body: &[u8]) -> Option<u32> {
if body.len() < 16 {
return None;
}
Some((&body[4..8]).get_u32_le()) // Count (bytes written)
}
// ── Set Info (rename) ──────────────────────────────────────────────────────
/// SMB2 SET_INFO InfoType for file information.
const SMB2_0_INFO_FILE: u8 = 0x01;
/// FileRenameInformation class (MS-FSCC 2.4.34.2).
const FILE_RENAME_INFORMATION: u8 = 0x0A;
/// Encode a SET_INFO request for FileRenameInformation (rename/move a file).
///
/// `new_name` is the destination path relative to the share root, using
/// backslash separators (SMB convention). `replace_if_exists` controls
/// whether an existing file at the destination is overwritten.
pub fn encode_set_info_rename(
buf: &mut BytesMut,
file_id: &[u8; 16],
new_name: &str,
replace_if_exists: bool,
) {
let name_bytes: Vec<u8> = new_name
.encode_utf16()
.flat_map(|c| c.to_le_bytes())
.collect();
// FileRenameInformation buffer:
// ReplaceIfExists (1) + Reserved (7) + RootDirectory (8) +
// FileNameLength (4) + FileName (variable)
let info_len = 1 + 7 + 8 + 4 + name_bytes.len();
// SET_INFO request StructureSize = 33 (fixed part = 32 + 1 buffer byte)
let buffer_offset = (SMB2_HEADER_SIZE + 32) as u16;
buf.put_u16_le(33); // StructureSize
buf.put_u8(SMB2_0_INFO_FILE); // InfoType: SMB2_0_INFO_FILE
buf.put_u8(FILE_RENAME_INFORMATION); // FileInfoClass
buf.put_u32_le(info_len as u32); // BufferLength
buf.put_u16_le(buffer_offset); // BufferOffset
buf.put_u16_le(0); // Reserved
buf.put_u32_le(0); // AdditionalInformation
buf.put_slice(file_id); // FileId (16 bytes)
// FileRenameInformation structure
buf.put_u8(u8::from(replace_if_exists)); // ReplaceIfExists
buf.put_slice(&[0u8; 7]); // Reserved
buf.put_u64_le(0); // RootDirectory (0 = relative to share root)
buf.put_u32_le(name_bytes.len() as u32); // FileNameLength
buf.put_slice(&name_bytes); // FileName (UTF-16LE)
}
// ── Query Directory ─────────────────────────────────────────────────────────
pub const FILE_ID_BOTH_DIRECTORY_INFORMATION: u8 = 0x25;
pub fn encode_query_directory_request(
buf: &mut BytesMut,
file_id: &[u8; 16],
pattern: &str,
info_class: u8,
restart: bool,
) {
let pattern_bytes: Vec<u8> = pattern
.encode_utf16()
.flat_map(|c| c.to_le_bytes())
.collect();
let name_offset = (SMB2_HEADER_SIZE + 32) as u16;
let mut flags: u8 = 0;
if restart {
flags |= 0x01; // SMB2_RESTART_SCANS
}
buf.put_u16_le(33); // StructureSize
buf.put_u8(info_class); // FileInformationClass
buf.put_u8(flags); // Flags
buf.put_u32_le(0); // FileIndex
buf.put_slice(file_id); // FileId
buf.put_u16_le(name_offset); // FileNameOffset
buf.put_u16_le(pattern_bytes.len() as u16); // FileNameLength
buf.put_u32_le(65536); // OutputBufferLength
buf.put_slice(&pattern_bytes);
}
/// A directory entry from FileIdBothDirectoryInformation
#[derive(Debug, Clone)]
pub struct DirectoryEntry {
pub file_name: String,
pub file_size: u64,
pub file_attributes: u32,
pub last_write_time: u64,
}
impl DirectoryEntry {
pub fn is_directory(&self) -> bool {
self.file_attributes & 0x10 != 0
}
}
/// Parse FILE_ID_BOTH_DIRECTORY_INFORMATION entries from a query directory response.
pub fn parse_directory_entries(data: &[u8]) -> Vec<DirectoryEntry> {
let mut entries = Vec::new();
let mut offset = 0usize;
loop {
if offset + 104 > data.len() {
break;
}
let entry = &data[offset..];
let next_entry_offset = (&entry[0..4]).get_u32_le() as usize;
let _file_index = (&entry[4..8]).get_u32_le();
let _creation_time = (&entry[8..16]).get_u64_le();
let _last_access_time = (&entry[16..24]).get_u64_le();
let last_write_time = (&entry[24..32]).get_u64_le();
let _change_time = (&entry[32..40]).get_u64_le();
let file_size = (&entry[40..48]).get_u64_le(); // EndOfFile
let _allocation_size = (&entry[48..56]).get_u64_le();
let file_attributes = (&entry[56..60]).get_u32_le();
let file_name_length = (&entry[60..64]).get_u32_le() as usize;
// FileIdBothDirectoryInformation: filename starts at offset 104
let name_start = 104;
let name_end = name_start + file_name_length;
if name_end > entry.len() {
break;
}
let name_bytes = &entry[name_start..name_end];
let file_name = String::from_utf16_lossy(
&name_bytes
.chunks_exact(2)
.map(|c| u16::from_le_bytes([c[0], c[1]]))
.collect::<Vec<_>>(),
);
// Skip . and ..
if file_name != "." && file_name != ".." {
entries.push(DirectoryEntry {
file_name,
file_size,
file_attributes,
last_write_time,
});
}
if next_entry_offset == 0 {
break;
}
offset += next_entry_offset;
}
entries
}
// ── Compound request support ───────────────────────────────────────────────
/// Flag bit for related compound operations.
pub const SMB2_FLAGS_RELATED: u32 = 0x0000_0004;
/// Sentinel file ID — server substitutes the file ID from the preceding
/// Create response in a related compound chain.
pub const SENTINEL_FILE_ID: [u8; 16] = [0xFF; 16];
/// Encode a Close request with optional post-query attribute retrieval.
/// When `postquery` is true, the server returns file metadata in the response.
pub fn encode_close_request_ex(buf: &mut BytesMut, file_id: &[u8; 16], postquery: bool) {
buf.put_u16_le(24); // StructureSize
buf.put_u16_le(u16::from(postquery)); // Flags: SMB2_CLOSE_FLAG_POSTQUERY_ATTRIB
buf.put_u32_le(0); // Reserved
buf.put_slice(file_id);
}
/// Parsed Close response (meaningful when postquery was requested).
#[derive(Debug, Clone)]
#[allow(dead_code)]
pub struct CloseResponse {
pub last_write_time: u64,
pub file_size: u64,
}
pub fn decode_close_response(body: &[u8]) -> Option<CloseResponse> {
// Layout: StructureSize(2) + Flags(2) + Reserved(4) + CreationTime(8)
// + LastAccessTime(8) + LastWriteTime(8) + ChangeTime(8)
// + AllocationSize(8) + EndOfFile(8) + FileAttributes(4) = 60 bytes
if body.len() < 56 {
return None;
}
let last_write_time = u64::from_le_bytes(body[24..32].try_into().unwrap());
let file_size = u64::from_le_bytes(body[48..56].try_into().unwrap());
Some(CloseResponse {
last_write_time,
file_size,
})
}
// ── Frame helpers ───────────────────────────────────────────────────────────
/// Prepend a 4-byte NetBIOS session length prefix to the packet.
pub fn frame_packet(header: &Header, body: &[u8]) -> BytesMut {
let total = SMB2_HEADER_SIZE + body.len();
let mut buf = BytesMut::with_capacity(4 + total);
buf.put_u32((total as u32) & 0x00FF_FFFF); // NetBIOS length (big-endian, masked to 24 bits)
header.encode(&mut buf);
buf.put_slice(body);
buf
}
/// Build a complete SMB2 request packet: \[NetBIOS length]\[Header]\[Body]
pub fn build_request<F>(header: &Header, body_builder: F) -> BytesMut
where
F: FnOnce(&mut BytesMut),
{
let mut body = BytesMut::with_capacity(256);
body_builder(&mut body);
frame_packet(header, &body)
}
/// Parse an SMB2 compound response (multiple chained messages in one frame).
/// Each returned tuple is `(header, body)` where `body` is the per-message
/// payload following the 64-byte header. Returns the messages successfully
/// parsed up to the first malformed boundary (callers rely on this for partial
/// recovery).
pub fn parse_compound_response(msg: &[u8]) -> Vec<(Header, Vec<u8>)> {
let mut results = Vec::new();
let mut offset = 0;
loop {
if offset + SMB2_HEADER_SIZE > msg.len() {
break;
}
let header = match Header::decode(&msg[offset..]) {
Some(h) => h,
None => break,
};
let next = header.next_command as usize;
let body_start = offset + SMB2_HEADER_SIZE;
let body_end = if next > 0 {
let end = offset + next;
if end > msg.len() || end < body_start {
break;
}
end
} else {
msg.len()
};
if body_start > body_end || body_end > msg.len() {
break;
}
let body = msg[body_start..body_end].to_vec();
results.push((header, body));
if next == 0 {
break;
}
offset += next;
}
results
}
#[cfg(test)]
mod tests {
use super::*;
// ── Header encode/decode round-trip ──────────────────────────────
#[test]
fn header_round_trip() {
let mut hdr = Header::new(Command::Create, 42);
hdr.session_id = 0xDEAD;
hdr.tree_id = 7;
hdr.flags = 0x04;
let mut buf = BytesMut::with_capacity(64);
hdr.encode(&mut buf);
let decoded = Header::decode(&buf).unwrap();
assert_eq!(decoded.command, Command::Create as u16);
assert_eq!(decoded.message_id, 42);
assert_eq!(decoded.session_id, 0xDEAD);
assert_eq!(decoded.tree_id, 7);
assert_eq!(decoded.flags, 0x04);
}
#[test]
fn header_decode_too_short() {
assert!(Header::decode(&[0u8; 32]).is_none());
}
#[test]
fn header_decode_bad_magic() {
let mut buf = [0u8; 64];
buf[0..4].copy_from_slice(b"XXXX");
assert!(Header::decode(&buf).is_none());
}
// ── NtStatus ─────────────────────────────────────────────────────
#[test]
fn nt_status_success_not_error() {
assert!(!NtStatus::Success.is_error());
}
#[test]
fn nt_status_not_found_is_error() {
assert!(NtStatus::ObjectNameNotFound.is_error());
}
#[test]
fn nt_status_no_more_files_not_error() {
// 0x80000006 has high bit set but not both high bits
assert!(!NtStatus::NoMoreFiles.is_error());
}
#[test]
fn nt_status_known_codes() {
assert_eq!(NtStatus::from_u32(0x00000000), NtStatus::Success);
assert_eq!(NtStatus::from_u32(0xC000000F), NtStatus::NoSuchFile);
assert_eq!(NtStatus::from_u32(0x80000006), NtStatus::NoMoreFiles);
}
// ── Decode responses ─────────────────────────────────────────────
#[test]
fn decode_create_response_valid() {
let mut body = vec![0u8; 88];
// last_write_time at offset 24
body[24..32].copy_from_slice(&100u64.to_le_bytes());
// file_size at offset 48
body[48..56].copy_from_slice(&42u64.to_le_bytes());
// file_id at offset 64
body[64..80].copy_from_slice(&[1u8; 16]);
let resp = decode_create_response(&body).unwrap();
assert_eq!(resp.last_write_time, 100);
assert_eq!(resp.file_size, 42);
assert_eq!(resp.file_id, [1u8; 16]);
}
#[test]
fn decode_create_response_too_short() {
assert!(decode_create_response(&[0u8; 10]).is_none());
}
#[test]
fn decode_read_response_valid() {
// data_offset at body[2..4], data_length at body[4..8]
let mut body = vec![0u8; 32];
let data_offset = (SMB2_HEADER_SIZE + 16) as u16; // offset within full msg
body[2..4].copy_from_slice(&data_offset.to_le_bytes());
body[4..8].copy_from_slice(&5u32.to_le_bytes()); // 5 bytes of data
body[16..21].copy_from_slice(b"hello");
let data = decode_read_response(&body).unwrap();
assert_eq!(&data[..], b"hello");
}
#[test]
fn decode_read_response_too_short() {
assert!(decode_read_response(&[0u8; 5]).is_none());
}
#[test]
fn decode_read_response_from_msg_valid() {
// Build a complete SMB2 message: 64-byte header + body. data_offset
// and data_length are measured from the start of the SMB2 message,
// matching the wire format.
let mut msg = vec![0u8; SMB2_HEADER_SIZE + 32];
let body = &mut msg[SMB2_HEADER_SIZE..];
let data_offset = (SMB2_HEADER_SIZE + 16) as u16;
body[2..4].copy_from_slice(&data_offset.to_le_bytes());
body[4..8].copy_from_slice(&5u32.to_le_bytes());
body[16..21].copy_from_slice(b"hello");
let data = decode_read_response_from_msg(msg).unwrap();
assert_eq!(&data[..], b"hello");
}
#[test]
fn decode_read_response_from_msg_too_short() {
assert!(decode_read_response_from_msg(vec![0u8; SMB2_HEADER_SIZE + 5]).is_none());
assert!(decode_read_response_from_msg(vec![0u8; 10]).is_none());
}
#[test]
fn decode_read_response_from_msg_rejects_overflow_length() {
// data_length that would extend past the buffer is rejected.
let mut msg = vec![0u8; SMB2_HEADER_SIZE + 32];
let body = &mut msg[SMB2_HEADER_SIZE..];
let data_offset = (SMB2_HEADER_SIZE + 16) as u16;
body[2..4].copy_from_slice(&data_offset.to_le_bytes());
body[4..8].copy_from_slice(&1_000_000u32.to_le_bytes());
assert!(decode_read_response_from_msg(msg).is_none());
}
#[test]
fn decode_read_response_from_msg_rejects_offset_inside_header() {
// A malformed server response with data_offset < SMB2_HEADER_SIZE
// would otherwise have us slice into the SMB2 header bytes and return
// them as payload. The decoder must reject that case.
let mut msg = vec![0u8; SMB2_HEADER_SIZE + 32];
// Seed the header with a sentinel so we'd notice if it ever leaked
// back as payload.
for (i, b) in msg[..SMB2_HEADER_SIZE].iter_mut().enumerate() {
*b = 0xA0 | (i as u8 & 0x0F);
}
let body = &mut msg[SMB2_HEADER_SIZE..];
// data_offset = 16 (inside the header, well before SMB2_HEADER_SIZE).
body[2..4].copy_from_slice(&16u16.to_le_bytes());
body[4..8].copy_from_slice(&8u32.to_le_bytes());
assert!(decode_read_response_from_msg(msg).is_none());
}
#[test]
fn decode_read_response_from_msg_rejects_offset_zero() {
// data_offset = 0 is also inside the header.
let mut msg = vec![0u8; SMB2_HEADER_SIZE + 32];
let body = &mut msg[SMB2_HEADER_SIZE..];
body[2..4].copy_from_slice(&0u16.to_le_bytes());
body[4..8].copy_from_slice(&4u32.to_le_bytes());
assert!(decode_read_response_from_msg(msg).is_none());
}
#[test]
fn decode_read_response_from_msg_rejects_offset_in_response_fixed_fields() {
// data_offset that points inside the read response's fixed fields
// (StructureSize/DataOffset/Reserved/DataLength/DataRemaining/Flags
// — 16 bytes after the SMB2 header) would otherwise leak the
// response's own structural fields back as the file payload.
let mut msg = vec![0u8; SMB2_HEADER_SIZE + 32];
let body = &mut msg[SMB2_HEADER_SIZE..];
// Offset = SMB2_HEADER_SIZE + 4 — points inside DataLength.
let bad_offset = (SMB2_HEADER_SIZE + 4) as u16;
body[2..4].copy_from_slice(&bad_offset.to_le_bytes());
body[4..8].copy_from_slice(&4u32.to_le_bytes());
assert!(decode_read_response_from_msg(msg).is_none());
// Offset = SMB2_HEADER_SIZE + 15 — one byte short of the buffer.
let mut msg = vec![0u8; SMB2_HEADER_SIZE + 32];
let body = &mut msg[SMB2_HEADER_SIZE..];
let bad_offset = (SMB2_HEADER_SIZE + 15) as u16;
body[2..4].copy_from_slice(&bad_offset.to_le_bytes());
body[4..8].copy_from_slice(&4u32.to_le_bytes());
assert!(decode_read_response_from_msg(msg).is_none());
}