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
use crate::smb2::Smb2Header;
use crate::smb2::SMB2_HEADER_SIZE;
use crate::smb2::header::commands::SMB2_SESSION_SETUP;

const SMB2_SESSION_SETUP_REQUEST_SIZE: u16 = 0x0019;


#[derive(Clone, Debug, PartialEq)]
pub struct Smb2SessionSetupReq {
    pub header: Smb2Header,
    pub body: Smb2SessionSetupReqBody
}

impl Smb2SessionSetupReq {

    pub fn new() -> Self {
        return Self {
            header: Smb2Header::new(SMB2_SESSION_SETUP),
            body: Smb2SessionSetupReqBody::default(),
        }
    }

    pub fn build(&self) -> Vec<u8> {
        let mut raw = self.header.build();
        raw.extend(self.body.build());
        return raw;
    }
}


#[derive(Clone, Debug, Default, PartialEq)]
pub struct Smb2SessionSetupReqBody {
    pub flags: u8,
    pub security_mode: u8,
    pub capabilities: u32,
    pub previous_session_id: u64,
    pub buffer: Vec<u8>,
}

impl Smb2SessionSetupReqBody {
    pub fn build(&self) -> Vec<u8> {
        let mut bytes = SMB2_SESSION_SETUP_REQUEST_SIZE.to_le_bytes().to_vec();

        bytes.push(self.flags);
        bytes.push(self.security_mode);
        bytes.extend(&self.capabilities.to_le_bytes());

        // Channel MUST be set to zero
        bytes.extend(&[0; 4]);

        let offset: u16 = SMB2_HEADER_SIZE + 24;
        bytes.extend(&offset.to_le_bytes());
        bytes.extend(&(self.buffer.len() as u16).to_le_bytes());

        bytes.extend(&self.previous_session_id.to_le_bytes());

        bytes.extend(&self.buffer);

        return bytes;
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::smb2::session_setup::SMB2_NEGOTIATE_SIGNING_ENABLED;

    const RAW_SESSION_REQ_BODY: &'static [u8] = &[
        0x19, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
        0x58, 0x00, 0x42, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
        0x60, 0x40, 0x06, 0x06, 0x2b, 0x06, 0x01, 0x05, 0x05, 0x02, 0xa0, 0x36,
        0x30, 0x34, 0xa0, 0x0e, 0x30, 0x0c, 0x06, 0x0a, 0x2b, 0x06, 0x01, 0x04,
        0x01, 0x82, 0x37, 0x02, 0x02, 0x0a, 0xa2, 0x22, 0x04, 0x20, 0x4e, 0x54,
        0x4c, 0x4d, 0x53, 0x53, 0x50, 0x00, 0x01, 0x00, 0x00, 0x00, 0x05, 0x02,
        0x88, 0xa0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
        0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    ];

    #[test]
    fn test_build_session_setup_request() {
        let mut sess_req = Smb2SessionSetupReqBody::default();
        sess_req.security_mode = SMB2_NEGOTIATE_SIGNING_ENABLED;
        sess_req.buffer = vec![
            0x60, 0x40, 0x06, 0x06, 0x2b, 0x06, 0x01, 0x05, 0x05, 0x02, 0xa0,
            0x36, 0x30, 0x34, 0xa0, 0x0e, 0x30, 0x0c, 0x06, 0x0a, 0x2b, 0x06,
            0x01, 0x04, 0x01, 0x82, 0x37, 0x02, 0x02, 0x0a, 0xa2, 0x22, 0x04,
            0x20, 0x4e, 0x54, 0x4c, 0x4d, 0x53, 0x53, 0x50, 0x00, 0x01, 0x00,
            0x00, 0x00, 0x05, 0x02, 0x88, 0xa0, 0x00, 0x00, 0x00, 0x00, 0x00,
            0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
        ];

        assert_eq!(RAW_SESSION_REQ_BODY.to_vec(), sess_req.build());
    }
}