red_smb 0.0.2

Library to play with SMB
Documentation
use crate::smb2::Smb2Header;
use crate::smb2::SMB2_HEADER_SIZE;
use crate::Result;
use nom::bytes::complete::{tag, take};
use nom::number::complete::le_u16;

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

impl Smb2SessionSetupResp {
    pub fn parse(raw: &[u8]) -> Result<Self> {
        let (raw, header) = Smb2Header::parse(raw)?;
        let (_, body) = Smb2SessionSetupRespBody::parse(raw)?;

        return Ok(Self { header, body });
    }
}

#[derive(Clone, Debug, Default, PartialEq)]
pub struct Smb2SessionSetupRespBody {
    pub flags: u16,
    pub buffer: Vec<u8>,
}

impl Smb2SessionSetupRespBody {
    pub fn parse(raw: &[u8]) -> Result<(&[u8], Self)> {
        let (raw, _) = tag(&[0x9, 0x0][..])(raw)?;
        let (raw, flags) = le_u16(raw)?;
        let (raw, offset) = le_u16(raw)?;
        let (raw, length) = le_u16(raw)?;

        let offset = offset - (SMB2_HEADER_SIZE + 8);

        let (raw, _) = take(offset)(raw)?;
        let (raw, buffer) = take(length)(raw)?;

        return Ok((
            raw,
            Self {
                flags,
                buffer: buffer.to_vec(),
            },
        ));
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    const RAW_SETUP_RESP_BODY: &'static [u8] = &[
        0x09, 0x00, 0x00, 0x00, 0x48, 0x00, 0x04, 0x01, 0xa1, 0x82, 0x01, 0x00,
        0x30, 0x81, 0xfd, 0xa0, 0x03, 0x0a, 0x01, 0x01, 0xa1, 0x0c, 0x06, 0x0a,
        0x2b, 0x06, 0x01, 0x04, 0x01, 0x82, 0x37, 0x02, 0x02, 0x0a, 0xa2, 0x81,
        0xe7, 0x04, 0x81, 0xe4, 0x4e, 0x54, 0x4c, 0x4d, 0x53, 0x53, 0x50, 0x00,
        0x02, 0x00, 0x00, 0x00, 0x0e, 0x00, 0x0e, 0x00, 0x38, 0x00, 0x00, 0x00,
        0x05, 0x02, 0x89, 0xa2, 0x0d, 0xc1, 0xf0, 0x1e, 0xa7, 0x6a, 0xe2, 0x10,
        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x9e, 0x00, 0x9e, 0x00,
        0x46, 0x00, 0x00, 0x00, 0x0a, 0x00, 0x61, 0x4a, 0x00, 0x00, 0x00, 0x0f,
        0x43, 0x00, 0x4f, 0x00, 0x4e, 0x00, 0x54, 0x00, 0x4f, 0x00, 0x53, 0x00,
        0x4f, 0x00, 0x02, 0x00, 0x0e, 0x00, 0x43, 0x00, 0x4f, 0x00, 0x4e, 0x00,
        0x54, 0x00, 0x4f, 0x00, 0x53, 0x00, 0x4f, 0x00, 0x01, 0x00, 0x0e, 0x00,
        0x57, 0x00, 0x53, 0x00, 0x30, 0x00, 0x31, 0x00, 0x2d, 0x00, 0x31, 0x00,
        0x30, 0x00, 0x04, 0x00, 0x1a, 0x00, 0x63, 0x00, 0x6f, 0x00, 0x6e, 0x00,
        0x74, 0x00, 0x6f, 0x00, 0x73, 0x00, 0x6f, 0x00, 0x2e, 0x00, 0x6c, 0x00,
        0x6f, 0x00, 0x63, 0x00, 0x61, 0x00, 0x6c, 0x00, 0x03, 0x00, 0x2a, 0x00,
        0x77, 0x00, 0x73, 0x00, 0x30, 0x00, 0x31, 0x00, 0x2d, 0x00, 0x31, 0x00,
        0x30, 0x00, 0x2e, 0x00, 0x63, 0x00, 0x6f, 0x00, 0x6e, 0x00, 0x74, 0x00,
        0x6f, 0x00, 0x73, 0x00, 0x6f, 0x00, 0x2e, 0x00, 0x6c, 0x00, 0x6f, 0x00,
        0x63, 0x00, 0x61, 0x00, 0x6c, 0x00, 0x05, 0x00, 0x1a, 0x00, 0x63, 0x00,
        0x6f, 0x00, 0x6e, 0x00, 0x74, 0x00, 0x6f, 0x00, 0x73, 0x00, 0x6f, 0x00,
        0x2e, 0x00, 0x6c, 0x00, 0x6f, 0x00, 0x63, 0x00, 0x61, 0x00, 0x6c, 0x00,
        0x07, 0x00, 0x08, 0x00, 0x87, 0x49, 0xad, 0x20, 0x31, 0xf0, 0xd6, 0x01,
        0x00, 0x00, 0x00, 0x00,
    ];

    #[test]
    fn test_parse_session_setup_response() {
        let mut sess_rep = Smb2SessionSetupRespBody::default();
        sess_rep.buffer = vec![
            0xa1, 0x82, 0x01, 0x00, 0x30, 0x81, 0xfd, 0xa0, 0x03, 0x0a, 0x01,
            0x01, 0xa1, 0x0c, 0x06, 0x0a, 0x2b, 0x06, 0x01, 0x04, 0x01, 0x82,
            0x37, 0x02, 0x02, 0x0a, 0xa2, 0x81, 0xe7, 0x04, 0x81, 0xe4, 0x4e,
            0x54, 0x4c, 0x4d, 0x53, 0x53, 0x50, 0x00, 0x02, 0x00, 0x00, 0x00,
            0x0e, 0x00, 0x0e, 0x00, 0x38, 0x00, 0x00, 0x00, 0x05, 0x02, 0x89,
            0xa2, 0x0d, 0xc1, 0xf0, 0x1e, 0xa7, 0x6a, 0xe2, 0x10, 0x00, 0x00,
            0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x9e, 0x00, 0x9e, 0x00, 0x46,
            0x00, 0x00, 0x00, 0x0a, 0x00, 0x61, 0x4a, 0x00, 0x00, 0x00, 0x0f,
            0x43, 0x00, 0x4f, 0x00, 0x4e, 0x00, 0x54, 0x00, 0x4f, 0x00, 0x53,
            0x00, 0x4f, 0x00, 0x02, 0x00, 0x0e, 0x00, 0x43, 0x00, 0x4f, 0x00,
            0x4e, 0x00, 0x54, 0x00, 0x4f, 0x00, 0x53, 0x00, 0x4f, 0x00, 0x01,
            0x00, 0x0e, 0x00, 0x57, 0x00, 0x53, 0x00, 0x30, 0x00, 0x31, 0x00,
            0x2d, 0x00, 0x31, 0x00, 0x30, 0x00, 0x04, 0x00, 0x1a, 0x00, 0x63,
            0x00, 0x6f, 0x00, 0x6e, 0x00, 0x74, 0x00, 0x6f, 0x00, 0x73, 0x00,
            0x6f, 0x00, 0x2e, 0x00, 0x6c, 0x00, 0x6f, 0x00, 0x63, 0x00, 0x61,
            0x00, 0x6c, 0x00, 0x03, 0x00, 0x2a, 0x00, 0x77, 0x00, 0x73, 0x00,
            0x30, 0x00, 0x31, 0x00, 0x2d, 0x00, 0x31, 0x00, 0x30, 0x00, 0x2e,
            0x00, 0x63, 0x00, 0x6f, 0x00, 0x6e, 0x00, 0x74, 0x00, 0x6f, 0x00,
            0x73, 0x00, 0x6f, 0x00, 0x2e, 0x00, 0x6c, 0x00, 0x6f, 0x00, 0x63,
            0x00, 0x61, 0x00, 0x6c, 0x00, 0x05, 0x00, 0x1a, 0x00, 0x63, 0x00,
            0x6f, 0x00, 0x6e, 0x00, 0x74, 0x00, 0x6f, 0x00, 0x73, 0x00, 0x6f,
            0x00, 0x2e, 0x00, 0x6c, 0x00, 0x6f, 0x00, 0x63, 0x00, 0x61, 0x00,
            0x6c, 0x00, 0x07, 0x00, 0x08, 0x00, 0x87, 0x49, 0xad, 0x20, 0x31,
            0xf0, 0xd6, 0x01, 0x00, 0x00, 0x00, 0x00,
        ];

        assert_eq!(
            sess_rep,
            Smb2SessionSetupRespBody::parse(RAW_SETUP_RESP_BODY)
                .unwrap()
                .1
        );
    }
}