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());
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());
}
}