net_trace/
parser.rs

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
use std::process::Command;
use std::path::Path;
use std::io::{self};
use serde_json::{Value};
use chrono::DateTime;
use crate::packet::*;
use chrono::Utc;

/// Error types for PCAP parsing
#[derive(Debug)]
pub enum PcapError {
    IoError(io::Error),
    TsharkNotFound,
    ParseError(String),
    JsonError(serde_json::Error),
}

impl From<io::Error> for PcapError {
    fn from(error: io::Error) -> Self {
        PcapError::IoError(error)
    }
}

impl From<serde_json::Error> for PcapError {
    fn from(error: serde_json::Error) -> Self {
        PcapError::JsonError(error)
    }
}

/// Parse a PCAP file using tshark and return a vector of NetworkPackets
pub fn parse_pcap<P: AsRef<Path>>(pcap_path: P) -> Result<Vec<NetworkPacket>, PcapError> {
    // Check if tshark is available
    if !is_tshark_installed() {
        return Err(PcapError::TsharkNotFound);
    }

    // Run tshark command to convert pcap to JSON
    let output = Command::new("tshark")
        .args([
            "-r", pcap_path.as_ref().to_str().unwrap(),
            "-T", "json",
            "-x",  // Include hex dump
            // Fields we want to capture
            "-e", "frame.time_epoch",
            "-e", "eth.src",
            "-e", "eth.dst",
            "-e", "eth.type",
            "-e", "ip.src",
            "-e", "ip.dst",
            "-e", "ip.proto",
            "-e", "tcp.srcport",
            "-e", "tcp.dstport",
            "-e", "tcp.seq",
            "-e", "tcp.ack",
            "-e", "tcp.flags",
            "-e", "tcp.window_size",
            "-e", "tcp.options",
            "-J", "tcp",  // Only TCP packets
        ])
        .output()?;

    if !output.status.success() {
        println!("Error running tshark: {}", String::from_utf8_lossy(&output.stderr));
        return Err(PcapError::ParseError(
            String::from_utf8_lossy(&output.stderr).to_string()
        ));
    }

    let json_str = String::from_utf8_lossy(&output.stdout);
    let packets: Vec<Value> = serde_json::from_str(&json_str)?;

    // Parse JSON into NetworkPacket structs
    let network_packets = packets.into_iter()
        .filter_map(|packet| parse_packet_json(packet).ok())
        .collect();

    Ok(network_packets)
}

/// Parse a single packet from JSON to NetworkPacket struct
fn parse_packet_json(json: Value) -> Result<NetworkPacket, PcapError> {
    let layers = json.get("_source")
        .and_then(|src| src.get("layers"))
        .ok_or_else(|| PcapError::ParseError("Invalid JSON structure".to_string()))?;

    // Parse timestamp - get first element from array
    let timestamp = layers.get("frame.time_epoch")
        .and_then(|t| t.as_array())
        .and_then(|arr| arr.first())
        .and_then(|t| t.as_str())
        .and_then(|t| t.parse::<f64>().ok())
        .map(|t| {
            let secs = t.trunc() as i64;
            let nsecs = (t.fract() * 1_000_000_000.0) as u32;
            DateTime::<Utc>::from_timestamp(secs, nsecs)
                .unwrap_or_default()
        })
        .ok_or_else(|| PcapError::ParseError("Invalid timestamp".to_string()))?;

    // Parse other layers
    let ethernet_layer = parse_ethernet_layer(layers)?;
    let ip_layer = parse_ip_layer(layers)?;
    let tcp_layer = parse_tcp_layer(layers)?;
    let application_layer = parse_application_layer(layers)?;

    Ok(NetworkPacket {
        timestamp,
        ethernet_layer,
        ip_layer,
        tcp_layer,
        application_layer,
    })
}

fn parse_ethernet_layer(layers: &Value) -> Result<EthernetFrame, PcapError> {
    Ok(EthernetFrame {
        source_mac: parse_mac_address(layers.get("eth.src")
            .and_then(|mac| mac.as_array())
            .and_then(|arr| arr.first())
            .and_then(|mac| mac.as_str())
            .ok_or_else(|| PcapError::ParseError("Invalid source MAC".to_string()))?),
        destination_mac: parse_mac_address(layers.get("eth.dst")
            .and_then(|mac| mac.as_array())
            .and_then(|arr| arr.first())
            .and_then(|mac| mac.as_str())
            .ok_or_else(|| PcapError::ParseError("Invalid destination MAC".to_string()))?),
        ethertype: layers.get("eth.type")
            .and_then(|t| t.as_array())
            .and_then(|arr| arr.first())
            .and_then(|t| t.as_str())
            .and_then(|t| u16::from_str_radix(t, 16).ok())
            .unwrap_or(0x0800),
        frame_check_sequence: 0,
    })
}

fn parse_ip_layer(layers: &Value) -> Result<IPv4Packet, PcapError> {
    Ok(IPv4Packet {
        version: 4,
        ihl: 5,
        dscp: 0,
        ecn: 0,
        total_length: 0,
        identification: 0,
        flags: IPv4Flags {
            reserved: false,
            dont_fragment: false,
            more_fragments: false,
        },
        fragment_offset: 0,
        ttl: 64,
        protocol: 6,
        header_checksum: 0,
        source_ip: parse_ip_address(layers.get("ip.src")
            .and_then(|ip| ip.as_array())
            .and_then(|arr| arr.first())
            .and_then(|ip| ip.as_str())
            .ok_or_else(|| PcapError::ParseError("Invalid source IP".to_string()))?),
        destination_ip: parse_ip_address(layers.get("ip.dst")
            .and_then(|ip| ip.as_array())
            .and_then(|arr| arr.first())
            .and_then(|ip| ip.as_str())
            .ok_or_else(|| PcapError::ParseError("Invalid destination IP".to_string()))?),
        options: Vec::new(),
    })
}

fn parse_tcp_layer(layers: &Value) -> Result<TCPSegment, PcapError> {
    let flags = parse_tcp_flags(layers.get("tcp.flags")
        .and_then(|f| f.as_array())
        .and_then(|arr| arr.first())
        .and_then(|f| f.as_str())
        .unwrap_or("0x000"));

    Ok(TCPSegment {
        source_port: layers.get("tcp.srcport")
            .and_then(|p| p.as_array())
            .and_then(|arr| arr.first())
            .and_then(|p| p.as_str())
            .and_then(|p| p.parse().ok())
            .unwrap_or(0),
        destination_port: layers.get("tcp.dstport")
            .and_then(|p| p.as_array())
            .and_then(|arr| arr.first())
            .and_then(|p| p.as_str())
            .and_then(|p| p.parse().ok())
            .unwrap_or(0),
        sequence_number: layers.get("tcp.seq")
            .and_then(|s| s.as_array())
            .and_then(|arr| arr.first())
            .and_then(|s| s.as_str())
            .and_then(|s| s.parse().ok())
            .unwrap_or(0),
        acknowledgment_number: layers.get("tcp.ack")
            .and_then(|a| a.as_array())
            .and_then(|arr| arr.first())
            .and_then(|a| a.as_str())
            .and_then(|a| a.parse().ok())
            .unwrap_or(0),
        data_offset: 5,
        flags,
        window_size: layers.get("tcp.window_size")
            .and_then(|w| w.as_array())
            .and_then(|arr| arr.first())
            .and_then(|w| w.as_str())
            .and_then(|w| w.parse().ok())
            .unwrap_or(0),
        checksum: 0,
        urgent_pointer: 0,
        options: Vec::new(),
    })
}

/// Parse application layer from JSON
fn parse_application_layer(layers: &Value) -> Result<ApplicationData, PcapError> {
    // Determine protocol based on ports
    let src_port = layers.get("tcp.srcport")
        .and_then(|p| p.as_str())
        .and_then(|p| p.parse::<u16>().ok())
        .unwrap_or(0);
    let dst_port = layers.get("tcp.dstport")
        .and_then(|p| p.as_str())
        .and_then(|p| p.parse::<u16>().ok())
        .unwrap_or(0);

    let protocol = match (src_port, dst_port) {
        (80, _) | (_, 80) => ApplicationProtocol::HTTP,
        (443, _) | (_, 443) => ApplicationProtocol::HTTPS,
        (21, _) | (_, 21) => ApplicationProtocol::FTP,
        (22, _) | (_, 22) => ApplicationProtocol::SSH,
        (25, _) | (_, 25) => ApplicationProtocol::SMTP,
        (53, _) | (_, 53) => ApplicationProtocol::DNS,
        _ => ApplicationProtocol::Custom(format!("PORT_{}", dst_port)),
    };

    Ok(ApplicationData {
        protocol,
        payload: Vec::new(), // Parse payload from hex dump if needed
    })
}
/// Helper function to parse MAC address string into bytes
fn parse_mac_address(mac_str: &str) -> [u8; 6] {
    let mut mac = [0u8; 6];
    let parts: Vec<&str> = mac_str.split(':').collect();
    for (i, part) in parts.iter().enumerate() {
        if i < 6 {
            mac[i] = u8::from_str_radix(part, 16).unwrap_or(0);
        }
    }
    mac
}

/// Helper function to parse IP address string into bytes
fn parse_ip_address(ip_str: &str) -> [u8; 4] {
    let mut ip = [0u8; 4];
    let parts: Vec<&str> = ip_str.split('.').collect();
    for (i, part) in parts.iter().enumerate() {
        if i < 4 {
            ip[i] = part.parse().unwrap_or(0);
        }
    }
    ip
}

/// Helper function to parse TCP flags
fn parse_tcp_flags(flags_str: &str) -> TCPFlags {
    let flags_value = u16::from_str_radix(&flags_str.trim_start_matches("0x"), 16).unwrap_or(0);
    
    TCPFlags {
        fin: flags_value & 0x001 != 0,
        syn: flags_value & 0x002 != 0,
        rst: flags_value & 0x004 != 0,
        psh: flags_value & 0x008 != 0,
        ack: flags_value & 0x010 != 0,
        urg: flags_value & 0x020 != 0,
        ece: flags_value & 0x040 != 0,
        cwr: flags_value & 0x080 != 0,
    }
}

/// Check if tshark is installed
fn is_tshark_installed() -> bool {
    Command::new("tshark")
        .arg("--version")
        .output()
        .is_ok()
}