use binrw::{BinRead, BinWrite, binrw};
use std::io::Cursor;
use crate::proto::error::{ProtoError, ProtoResult};
pub const SMB2_MAGIC: [u8; 4] = [0xFE, b'S', b'M', b'B'];
pub const SMB2_HEADER_STRUCTURE_SIZE: u16 = 64;
pub const SMB2_HEADER_LEN: usize = 64;
pub const SMB2_FLAGS_SERVER_TO_REDIR: u32 = 0x0000_0001;
pub const SMB2_FLAGS_ASYNC_COMMAND: u32 = 0x0000_0002;
pub const SMB2_FLAGS_RELATED_OPERATIONS: u32 = 0x0000_0004;
pub const SMB2_FLAGS_SIGNED: u32 = 0x0000_0008;
pub const SMB2_FLAGS_PRIORITY_MASK: u32 = 0x0000_0070;
pub const SMB2_FLAGS_DFS_OPERATIONS: u32 = 0x1000_0000;
pub const SMB2_FLAGS_REPLAY_OPERATION: u32 = 0x2000_0000;
#[binrw]
#[brw(little, repr = u16)]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum Command {
Negotiate = 0x0000,
SessionSetup = 0x0001,
Logoff = 0x0002,
TreeConnect = 0x0003,
TreeDisconnect = 0x0004,
Create = 0x0005,
Close = 0x0006,
Flush = 0x0007,
Read = 0x0008,
Write = 0x0009,
Lock = 0x000A,
Ioctl = 0x000B,
Cancel = 0x000C,
Echo = 0x000D,
QueryDirectory = 0x000E,
ChangeNotify = 0x000F,
QueryInfo = 0x0010,
SetInfo = 0x0011,
OplockBreak = 0x0012,
}
impl Command {
pub const fn as_u16(self) -> u16 {
self as u16
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum HeaderTail {
Sync { reserved: u32, tree_id: u32 },
Async { async_id: u64 },
}
impl HeaderTail {
pub const fn sync(tree_id: u32) -> Self {
HeaderTail::Sync {
reserved: 0,
tree_id,
}
}
pub const fn async_(async_id: u64) -> Self {
HeaderTail::Async { async_id }
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct Smb2Header {
pub credit_charge: u16,
pub channel_sequence_status: u32,
pub command: Command,
pub credit_request_response: u16,
pub flags: u32,
pub next_command: u32,
pub message_id: u64,
pub tail: HeaderTail,
pub session_id: u64,
pub signature: [u8; 16],
}
impl Default for Smb2Header {
fn default() -> Self {
Self {
credit_charge: 0,
channel_sequence_status: 0,
command: Command::Negotiate,
credit_request_response: 0,
flags: 0,
next_command: 0,
message_id: 0,
tail: HeaderTail::sync(0),
session_id: 0,
signature: [0u8; 16],
}
}
}
impl Smb2Header {
pub fn is_async(&self) -> bool {
self.flags & SMB2_FLAGS_ASYNC_COMMAND != 0
}
pub fn is_response(&self) -> bool {
self.flags & SMB2_FLAGS_SERVER_TO_REDIR != 0
}
pub fn tree_id(&self) -> Option<u32> {
match self.tail {
HeaderTail::Sync { tree_id, .. } => Some(tree_id),
HeaderTail::Async { .. } => None,
}
}
pub fn async_id(&self) -> Option<u64> {
match self.tail {
HeaderTail::Async { async_id } => Some(async_id),
HeaderTail::Sync { .. } => None,
}
}
pub fn parse(buf: &[u8]) -> ProtoResult<(Self, &[u8])> {
if buf.len() < SMB2_HEADER_LEN {
return Err(ProtoError::Malformed("short SMB2 header"));
}
let mut cursor = Cursor::new(&buf[..SMB2_HEADER_LEN]);
let raw = RawHeader::read(&mut cursor)?;
if raw.protocol_id != SMB2_MAGIC {
return Err(ProtoError::Malformed("bad SMB2 magic"));
}
if raw.structure_size != SMB2_HEADER_STRUCTURE_SIZE {
return Err(ProtoError::Malformed("SMB2 header structure_size != 64"));
}
let command = match Command::read_le(&mut Cursor::new(raw.command_raw.to_le_bytes())) {
Ok(c) => c,
Err(_) => {
return Err(ProtoError::Malformed("unknown SMB2 command opcode"));
}
};
let tail = if raw.flags & SMB2_FLAGS_ASYNC_COMMAND != 0 {
HeaderTail::Async {
async_id: u64::from_le_bytes(raw.tail_bytes),
}
} else {
let reserved = u32::from_le_bytes([
raw.tail_bytes[0],
raw.tail_bytes[1],
raw.tail_bytes[2],
raw.tail_bytes[3],
]);
let tree_id = u32::from_le_bytes([
raw.tail_bytes[4],
raw.tail_bytes[5],
raw.tail_bytes[6],
raw.tail_bytes[7],
]);
HeaderTail::Sync { reserved, tree_id }
};
Ok((
Smb2Header {
credit_charge: raw.credit_charge,
channel_sequence_status: raw.channel_sequence_status,
command,
credit_request_response: raw.credit_request_response,
flags: raw.flags,
next_command: raw.next_command,
message_id: raw.message_id,
tail,
session_id: raw.session_id,
signature: raw.signature,
},
&buf[SMB2_HEADER_LEN..],
))
}
pub fn write(&self, out: &mut Vec<u8>) -> ProtoResult<()> {
let tail_bytes = match self.tail {
HeaderTail::Sync { reserved, tree_id } => {
let mut b = [0u8; 8];
b[..4].copy_from_slice(&reserved.to_le_bytes());
b[4..].copy_from_slice(&tree_id.to_le_bytes());
b
}
HeaderTail::Async { async_id } => async_id.to_le_bytes(),
};
let raw = RawHeader {
protocol_id: SMB2_MAGIC,
structure_size: SMB2_HEADER_STRUCTURE_SIZE,
credit_charge: self.credit_charge,
channel_sequence_status: self.channel_sequence_status,
command_raw: self.command.as_u16(),
credit_request_response: self.credit_request_response,
flags: self.flags,
next_command: self.next_command,
message_id: self.message_id,
tail_bytes,
session_id: self.session_id,
signature: self.signature,
};
let start = out.len();
let mut cursor = Cursor::new(Vec::with_capacity(SMB2_HEADER_LEN));
raw.write(&mut cursor)?;
out.extend_from_slice(&cursor.into_inner());
debug_assert_eq!(out.len() - start, SMB2_HEADER_LEN);
Ok(())
}
}
#[binrw]
#[brw(little)]
#[derive(Debug, Clone, Copy)]
struct RawHeader {
protocol_id: [u8; 4],
structure_size: u16,
credit_charge: u16,
channel_sequence_status: u32,
command_raw: u16,
credit_request_response: u16,
flags: u32,
next_command: u32,
message_id: u64,
tail_bytes: [u8; 8],
session_id: u64,
signature: [u8; 16],
}
#[cfg(test)]
mod tests {
use super::*;
fn sample_sync() -> Smb2Header {
Smb2Header {
credit_charge: 1,
channel_sequence_status: 0,
command: Command::Negotiate,
credit_request_response: 1,
flags: 0,
next_command: 0,
message_id: 0,
tail: HeaderTail::Sync {
reserved: 0,
tree_id: 0,
},
session_id: 0,
signature: [0u8; 16],
}
}
fn sample_async() -> Smb2Header {
Smb2Header {
credit_charge: 4,
channel_sequence_status: 0,
command: Command::Read,
credit_request_response: 1,
flags: SMB2_FLAGS_ASYNC_COMMAND | SMB2_FLAGS_SERVER_TO_REDIR,
next_command: 0,
message_id: 42,
tail: HeaderTail::Async {
async_id: 0xDEAD_BEEF_CAFE_F00D,
},
session_id: 0x1122_3344_5566_7788,
signature: [0xAA; 16],
}
}
#[test]
fn sync_round_trips() {
let hdr = sample_sync();
let mut buf = Vec::new();
hdr.write(&mut buf).unwrap();
assert_eq!(buf.len(), SMB2_HEADER_LEN);
assert_eq!(&buf[..4], &SMB2_MAGIC);
assert_eq!(u16::from_le_bytes([buf[4], buf[5]]), 64);
let (decoded, rest) = Smb2Header::parse(&buf).unwrap();
assert!(rest.is_empty());
assert_eq!(decoded, hdr);
}
#[test]
fn async_round_trips() {
let hdr = sample_async();
let mut buf = Vec::new();
hdr.write(&mut buf).unwrap();
assert_eq!(buf.len(), SMB2_HEADER_LEN);
let (decoded, _rest) = Smb2Header::parse(&buf).unwrap();
assert_eq!(decoded, hdr);
assert!(decoded.is_async());
assert!(decoded.is_response());
assert_eq!(decoded.async_id(), Some(0xDEAD_BEEF_CAFE_F00D));
assert_eq!(decoded.tree_id(), None);
}
#[test]
fn rejects_bad_magic() {
let hdr = sample_sync();
let mut buf = Vec::new();
hdr.write(&mut buf).unwrap();
buf[0] = 0xFF;
let err = Smb2Header::parse(&buf).unwrap_err();
assert!(matches!(err, ProtoError::Malformed(_)));
}
#[test]
fn rejects_bad_structure_size() {
let hdr = sample_sync();
let mut buf = Vec::new();
hdr.write(&mut buf).unwrap();
buf[4] = 0; buf[5] = 0;
let err = Smb2Header::parse(&buf).unwrap_err();
assert!(matches!(err, ProtoError::Malformed(_)));
}
#[test]
fn rejects_short_buffer() {
let err = Smb2Header::parse(&[0u8; 32]).unwrap_err();
assert!(matches!(err, ProtoError::Malformed(_)));
}
#[test]
fn handcrafted_sync_negotiate_request() {
let mut buf = vec![0u8; 64];
buf[..4].copy_from_slice(&SMB2_MAGIC);
buf[4..6].copy_from_slice(&64u16.to_le_bytes());
let (hdr, _) = Smb2Header::parse(&buf).unwrap();
assert_eq!(hdr.command, Command::Negotiate);
assert!(!hdr.is_async());
assert_eq!(hdr.tree_id(), Some(0));
}
#[test]
fn command_round_trips_via_binrw() {
for cmd in [
Command::Negotiate,
Command::SessionSetup,
Command::Logoff,
Command::TreeConnect,
Command::TreeDisconnect,
Command::Create,
Command::Close,
Command::Flush,
Command::Read,
Command::Write,
Command::Lock,
Command::Ioctl,
Command::Cancel,
Command::Echo,
Command::QueryDirectory,
Command::ChangeNotify,
Command::QueryInfo,
Command::SetInfo,
Command::OplockBreak,
] {
let mut hdr = sample_sync();
hdr.command = cmd;
let mut buf = Vec::new();
hdr.write(&mut buf).unwrap();
let (decoded, _) = Smb2Header::parse(&buf).unwrap();
assert_eq!(decoded.command, cmd);
}
}
}