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
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
use byteorder::{ByteOrder, LittleEndian, BigEndian, ReadBytesExt};
use std::io::Read;
use crate::errors::PcapError;
use std::borrow::Cow;
use byteorder::WriteBytesExt;
use crate::pcapng::blocks::{SectionHeaderBlock, InterfaceDescriptionBlock, EnhancedPacketBlock, SimplePacketBlock, NameResolutionBlock, InterfaceStatisticsBlock, SystemdJournalExportBlock};
use crate::pcapng::PacketBlock;
use crate::Endianness;
use derive_into_owned::IntoOwned;


//   0               1               2               3
//   0 1 2 3 4 5 6 7 0 1 2 3 4 5 6 7 0 1 2 3 4 5 6 7 0 1 2 3 4 5 6 7
//  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
//  |                          Block Type                           |
//  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
//  |                      Block Total Length                       |
//  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
//  /                          Block Body                           /
//  /          /* variable length, aligned to 32 bits */            /
//  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
//  |                      Block Total Length                       |
//  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
/// PcapNg Block
#[derive(Clone, Debug)]
pub struct Block<'a> {
    pub type_: BlockType,
    pub initial_len: u32,
    pub body: Cow<'a, [u8]>,
    pub trailer_len: u32,
    endianness: Endianness
}

impl<'a> Block<'a> {

    /// Create an "owned" `Block` from a reader
    pub(crate) fn from_reader<R:Read, B: ByteOrder>(reader: &mut R) -> Result<Block<'static>, PcapError> {

        let type_ = reader.read_u32::<B>()?.into();

        //Special case for the section header because we don't know the endianness yet
        if type_ == BlockType::SectionHeader {
            let mut initial_len = reader.read_u32::<BigEndian>()?;
            let magic = reader.read_u32::<BigEndian>()?;

            let endianness = match magic {
                0x1A2B3C4D => Endianness::Big,
                0x4D3C2B1A => Endianness::Little,
                _ => return Err(PcapError::InvalidField("SectionHeaderBlock: invalid magic number"))
            };

            if endianness == Endianness::Little {
                initial_len = initial_len.swap_bytes();
            }

            if (initial_len % 4) != 0 {
                return Err(PcapError::InvalidField("Block: (initial_len % 4) != 0"));
            }

            if initial_len < 12 {
                return Err(PcapError::InvalidField("Block: initial_len < 12"))
            }

            let body_len = initial_len - 12;
            let mut body = vec![0_u8; body_len as usize];
            // Rewrite the magic in the body
            (&mut body[..]).write_u32::<BigEndian>(magic)?;
            reader.read_exact(&mut body[4..])?;

            let trailer_len = match endianness {
                Endianness::Big => reader.read_u32::<BigEndian>()?,
                Endianness::Little => reader.read_u32::<LittleEndian>()?
            };

            if initial_len != trailer_len {
                return Err(PcapError::InvalidField("Block initial_length != trailer_length"))
            }

            Ok(
                Block {
                    type_,
                    initial_len,
                    body: Cow::Owned(body),
                    trailer_len,
                    endianness
                }
            )
        }
        else {

            //Common case
            let initial_len = reader.read_u32::<B>()?;
            if (initial_len % 4) != 0 {
                return Err(PcapError::InvalidField("Block: (initial_len % 4) != 0"));
            }

            if initial_len < 12 {
                return Err(PcapError::InvalidField("Block: initial_len < 12"))
            }

            let body_len = initial_len - 12;
            let mut body = vec![0_u8; body_len as usize];
            reader.read_exact(&mut body[..])?;

            let trailer_len = reader.read_u32::<B>()?;
            if initial_len != trailer_len {
                return Err(PcapError::InvalidField("Block initial_length != trailer_length"))
            }

            Ok(
                Block {
                    type_,
                    initial_len,
                    body: Cow::Owned(body),
                    trailer_len,
                    endianness: Endianness::new::<B>()
                }
            )
        }
    }

    /// Create an "borrowed" `Block` from a slice
    pub(crate) fn from_slice<B: ByteOrder>(mut slice: &'a[u8]) -> Result<(&[u8], Self), PcapError> {

        if slice.len() < 12 {
            return Err(PcapError::IncompleteBuffer(12 - slice.len()));
        }

        let type_ = slice.read_u32::<B>()?.into();

        //Special case for the section header because we don't know the endianness yet
        if type_ == BlockType::SectionHeader {
            let mut initial_len = slice.read_u32::<BigEndian>()?;

            let mut tmp_slice = slice;

            let magic = tmp_slice.read_u32::<BigEndian>()?;

            let endianness = match magic {
                0x1A2B3C4D => Endianness::Big,
                0x4D3C2B1A => Endianness::Little,
                _ => return Err(PcapError::InvalidField("SectionHeaderBlock: invalid magic number"))
            };

            if endianness == Endianness::Little {
                initial_len = initial_len.swap_bytes();
            }

            if (initial_len % 4) != 0 {
                return Err(PcapError::InvalidField("Block: (initial_len % 4) != 0"));
            }

            if initial_len < 12 {
                return Err(PcapError::InvalidField("Block: initial_len < 12"))
            }

            //Check if there is enough data for the body and the trailer_len
            if slice.len() < initial_len as usize - 8 {
                return Err(PcapError::IncompleteBuffer(initial_len as usize - 8 - slice.len()));
            }

            let body_len = initial_len - 12;
            let body = &slice[..body_len as usize];

            let mut rem = &slice[body_len as usize ..];

            let trailer_len = match endianness {
                Endianness::Big => rem.read_u32::<BigEndian>()?,
                Endianness::Little => rem.read_u32::<LittleEndian>()?
            };

            if initial_len != trailer_len {
                return Err(PcapError::InvalidField("Block initial_length != trailer_length"))
            }


            let block = Block {
                type_,
                initial_len,
                body: Cow::Borrowed(body),
                trailer_len,
                endianness
            };

            return Ok((rem, block))
        }
        else {

            //Common case
            let initial_len = slice.read_u32::<B>()?;

            if (initial_len % 4) != 0 {
                return Err(PcapError::InvalidField("Block: (initial_len % 4) != 0"));
            }

            if initial_len < 12 {
                return Err(PcapError::InvalidField("Block: initial_len < 12"))
            }

            //Check if there is enough data for the body and the trailer_len
            if slice.len() < initial_len as usize - 8 {
                return Err(PcapError::IncompleteBuffer(initial_len as usize - 8 - slice.len()));
            }

            let body_len = initial_len - 12;
            let body = &slice[..body_len as usize];

            let mut rem = &slice[body_len as usize ..];

            let trailer_len = rem.read_u32::<B>()?;

            if initial_len != trailer_len {
                return Err(PcapError::InvalidField("Block initial_length != trailer_length"))
            }

            let block = Block {
                type_,
                initial_len,
                body: Cow::Borrowed(body),
                trailer_len,
                endianness: Endianness::new::<B>()
            };

            Ok((rem, block))
        }
    }

    pub fn parsed(&self) -> Result<ParsedBlock, PcapError> {

        match self.endianness {
            Endianness::Big => ParsedBlock::from_slice::<BigEndian>(self.type_, &self.body).map(|r| r.1),
            Endianness::Little => ParsedBlock::from_slice::<LittleEndian>(self.type_, &self.body).map(|r| r.1)
        }
    }
}

#[derive(Copy, Clone, Debug, Eq, PartialEq)]
pub enum BlockType {
    SectionHeader,
    InterfaceDescription,
    Packet,
    SimplePacket,
    NameResolution,
    InterfaceStatistics,
    EnhancedPacket,
    SystemdJournalExport,
    Unknown(u32)
}

impl From<u32> for BlockType {
    fn from(src: u32) -> Self {
        match src {
            0x0A0D0D0A => BlockType::SectionHeader,
            0x00000001 => BlockType::InterfaceDescription,
            0x00000002 => BlockType::Packet,
            0x00000003 => BlockType::SimplePacket,
            0x00000004 => BlockType::NameResolution,
            0x00000005 => BlockType::InterfaceStatistics,
            0x00000006 => BlockType::EnhancedPacket,
            0x00000009 => BlockType::SystemdJournalExport,
            _ => BlockType::Unknown(src),
        }
    }
}

/// PcapNg parsed blocks
#[derive(Clone, Debug, IntoOwned)]
pub enum ParsedBlock<'a> {
    SectionHeader(SectionHeaderBlock<'a>),
    InterfaceDescription(InterfaceDescriptionBlock<'a>),
    Packet(PacketBlock<'a>),
    SimplePacket(SimplePacketBlock<'a>),
    NameResolution(NameResolutionBlock<'a>),
    InterfaceStatistics(InterfaceStatisticsBlock<'a>),
    EnhancedPacket(EnhancedPacketBlock<'a>),
    SystemdJournalExport(SystemdJournalExportBlock<'a>),
    Unknown(UnknownBlock<'a>)
}

impl<'a> ParsedBlock<'a> {

    /// Create a `ParsedBlock` from a slice
    pub fn from_slice<B: ByteOrder>(type_: BlockType, slice: &'a[u8]) -> Result<(&'a [u8], Self), PcapError> {

        match type_ {

            BlockType::SectionHeader => {
                let (rem, block) = SectionHeaderBlock::from_slice(slice)?;
                Ok((rem, ParsedBlock::SectionHeader(block)))
            },
            BlockType::InterfaceDescription => {
                let (rem, block) = InterfaceDescriptionBlock::from_slice::<B>(slice)?;
                Ok((rem, ParsedBlock::InterfaceDescription(block)))
            },
            BlockType::Packet => {
                let (rem, block) = PacketBlock::from_slice::<B>(slice)?;
                Ok((rem, ParsedBlock::Packet(block)))
            },
            BlockType::SimplePacket => {
                let (rem, block) = SimplePacketBlock::from_slice::<B>(slice)?;
                Ok((rem, ParsedBlock::SimplePacket(block)))
            },
            BlockType::NameResolution => {
                let (rem, block) = NameResolutionBlock::from_slice::<B>(slice)?;
                Ok((rem, ParsedBlock::NameResolution(block)))
            },
            BlockType::InterfaceStatistics => {
                let (rem, block) = InterfaceStatisticsBlock::from_slice::<B>(slice)?;
                Ok((rem, ParsedBlock::InterfaceStatistics(block)))
            },
            BlockType::EnhancedPacket => {
                let (rem, block) = EnhancedPacketBlock::from_slice::<B>(slice)?;
                Ok((rem, ParsedBlock::EnhancedPacket(block)))
            },
            BlockType::SystemdJournalExport => {
                let (rem, block) = SystemdJournalExportBlock::from_slice::<B>(slice)?;
                Ok((rem, ParsedBlock::SystemdJournalExport(block)))
            }
            _ => Ok((slice, ParsedBlock::Unknown(UnknownBlock::new(type_, slice.len() as u32, slice))))
        }
    }

    pub fn into_section_header(self) -> Option<SectionHeaderBlock<'a>> {
        match self {
            ParsedBlock::SectionHeader(section) => Some(section),
            _ => None
        }
    }

    pub fn into_interface_description(self) -> Option<InterfaceDescriptionBlock<'a>> {
        match self {
            ParsedBlock::InterfaceDescription(block) => Some(block),
            _ => None
        }
    }
}

#[derive(Clone, Debug, IntoOwned)]
pub struct UnknownBlock<'a> {
    pub type_: BlockType,
    pub length: u32,
    pub value: Cow<'a, [u8]>
}

impl<'a> UnknownBlock<'a> {
    pub fn new(type_: BlockType, length: u32, value: &'a [u8]) -> Self {
        UnknownBlock {
            type_,
            length,
            value: Cow::Borrowed(value)
        }
    }
}

/// Parse all options in a block
pub(crate) fn opts_from_slice<'a, B, F, O>(mut slice: &'a [u8], func: F) -> Result<(&'a [u8], Vec<O>), PcapError>
    where B: ByteOrder,
          F: Fn(&'a [u8], u16, u16) -> Result<O, PcapError>

{
    let mut options = vec![];

    // If there is nothing left in the slice, it means that there is no option
    if slice.is_empty() {
        return Ok((slice, options))
    }

    loop {

        if slice.len() < 4 {
            return Err(PcapError::InvalidField("Option: slice.len() < 4"));
        }

        let code = slice.read_u16::<B>()?;
        let length = slice.read_u16::<B>()? as usize;
        let pad_len = (4 - (length % 4)) % 4;

        if code == 0 {
            return Ok((slice, options));
        }

        if slice.len() < length + pad_len {
            return Err(PcapError::InvalidField("Option: length + pad.len() > slice.len()"));
        }

        let mut tmp_slice = &slice[..length];
        let opt = func(&mut tmp_slice, code, length as u16)?;

        // Jump over the padding
        slice = &slice[length+pad_len..];

        options.push(opt);
    }
}

#[derive(Clone, Debug, IntoOwned)]
pub struct UnknownOption<'a> {
    code: u16,
    length: u16,
    value: Cow<'a, [u8]>
}
impl<'a> UnknownOption<'a> {
    pub fn new(code: u16, length: u16, value: &'a[u8]) -> Self {
        UnknownOption {
            code,
            length,
            value: Cow::Borrowed(value)
        }
    }
}

#[derive(Clone, Debug, IntoOwned)]
pub struct CustomBinaryOption<'a> {
    code: u16,
    pen: u32,
    value: Cow<'a, [u8]>
}
impl<'a> CustomBinaryOption<'a> {
    pub fn from_slice<B: ByteOrder>(code: u16, mut src: &'a [u8]) -> Result<Self, PcapError> {
        let pen = src.read_u32::<B>()?;

        let opt = CustomBinaryOption {
            code,
            pen,
            value: Cow::Borrowed(src)
        };

        Ok(opt)
    }
}

#[derive(Clone, Debug, IntoOwned)]
pub struct CustomUtf8Option<'a> {
    code: u16,
    pen: u32,
    value: Cow<'a, str>
}
impl<'a> CustomUtf8Option<'a> {
    pub fn from_slice<B: ByteOrder>(code: u16, mut src: &'a [u8]) -> Result<Self, PcapError> {
        let pen = src.read_u32::<B>()?;

        let opt = CustomUtf8Option {
            code,
            pen,
            value: Cow::Borrowed(std::str::from_utf8(src)?)
        };

        Ok(opt)
    }
}