Skip to main content

smb_server/proto/messages/
session_setup.rs

1//! SESSION_SETUP Request/Response (MS-SMB2 §2.2.5 / §2.2.6).
2
3use binrw::{BinRead, BinWrite, binrw};
4use std::io::Cursor;
5
6use crate::proto::error::ProtoResult;
7
8/// SMB2_SESSION_SETUP_REQUEST (MS-SMB2 §2.2.5).
9///
10/// `security_buffer` is opaque GSS-API/SPNEGO data, decoded by the SPNEGO
11/// layer. The wire offset is from the start of the SMB2 header; the buffer is
12/// read and written immediately after the fixed prefix.
13#[binrw]
14#[brw(little)]
15#[derive(Debug, Clone, PartialEq, Eq)]
16pub struct SessionSetupRequest {
17    pub structure_size: u16,
18    pub flags: u8,
19    pub security_mode: u8,
20    pub capabilities: u32,
21    pub channel: u32,
22    pub security_buffer_offset: u16,
23    pub security_buffer_length: u16,
24    pub previous_session_id: u64,
25    #[br(parse_with = super::read_buf, args(security_buffer_length as usize))]
26    pub security_buffer: Vec<u8>,
27}
28
29impl SessionSetupRequest {
30    /// Flag: SMB2_SESSION_FLAG_BINDING - bind to existing session (3.x).
31    pub const FLAG_BINDING: u8 = 0x01;
32    /// SecurityMode bit: SMB2_NEGOTIATE_SIGNING_REQUIRED.
33    pub const SECURITY_SIGNING_REQUIRED: u8 = 0x02;
34
35    pub fn parse(buf: &[u8]) -> ProtoResult<Self> {
36        Ok(Self::read(&mut Cursor::new(buf))?)
37    }
38    pub fn write_to(&self, out: &mut Vec<u8>) -> ProtoResult<()> {
39        let mut c = Cursor::new(Vec::new());
40        BinWrite::write(self, &mut c)?;
41        out.extend_from_slice(&c.into_inner());
42        Ok(())
43    }
44}
45
46/// SMB2_SESSION_SETUP_RESPONSE (MS-SMB2 §2.2.6).
47#[binrw]
48#[brw(little)]
49#[derive(Debug, Clone, PartialEq, Eq)]
50pub struct SessionSetupResponse {
51    pub structure_size: u16,
52    pub session_flags: u16,
53    pub security_buffer_offset: u16,
54    pub security_buffer_length: u16,
55    #[br(parse_with = super::read_buf, args(security_buffer_length as usize))]
56    pub security_buffer: Vec<u8>,
57}
58
59impl SessionSetupResponse {
60    /// Session flag: IS_GUEST.
61    pub const FLAG_IS_GUEST: u16 = 0x0001;
62    /// Session flag: IS_NULL (anonymous).
63    pub const FLAG_IS_NULL: u16 = 0x0002;
64    /// Session flag: ENCRYPT_DATA.
65    pub const FLAG_ENCRYPT_DATA: u16 = 0x0004;
66
67    pub fn parse(buf: &[u8]) -> ProtoResult<Self> {
68        Ok(Self::read(&mut Cursor::new(buf))?)
69    }
70    pub fn write_to(&self, out: &mut Vec<u8>) -> ProtoResult<()> {
71        let mut c = Cursor::new(Vec::new());
72        BinWrite::write(self, &mut c)?;
73        out.extend_from_slice(&c.into_inner());
74        Ok(())
75    }
76}
77
78#[cfg(test)]
79mod tests {
80    use super::*;
81
82    #[test]
83    fn request_round_trips() {
84        let r = SessionSetupRequest {
85            structure_size: 25,
86            flags: 0,
87            security_mode: 0x01,
88            capabilities: 0x01,
89            channel: 0,
90            security_buffer_offset: 0x58,
91            security_buffer_length: 6,
92            previous_session_id: 0,
93            security_buffer: vec![0xDE, 0xAD, 0xBE, 0xEF, 0x01, 0x02],
94        };
95        let mut buf = Vec::new();
96        r.write_to(&mut buf).unwrap();
97        assert_eq!(SessionSetupRequest::parse(&buf).unwrap(), r);
98    }
99
100    #[test]
101    fn response_round_trips() {
102        let r = SessionSetupResponse {
103            structure_size: 9,
104            session_flags: SessionSetupResponse::FLAG_IS_GUEST,
105            security_buffer_offset: 0x48,
106            security_buffer_length: 4,
107            security_buffer: vec![1, 2, 3, 4],
108        };
109        let mut buf = Vec::new();
110        r.write_to(&mut buf).unwrap();
111        assert_eq!(SessionSetupResponse::parse(&buf).unwrap(), r);
112    }
113}