Skip to content

A WiFi security auditing software

scan: merge captures natively, dropping the mergecap dependency

Martin Olivier8bf53a653b6f
diff --git a/Dockerfile b/Dockerfile
index 67a6f94..374f9ac 100644
--- a/Dockerfile
+++ b/Dockerfile
@@ -20,9 +20,9 @@ RUN rustup component add rustfmt
WORKDIR /workspace
-ENV DEBIAN_DEPS="--depends libgtk-4-1 --depends dbus-x11 --depends wireshark-common --depends iproute2 --depends crunch"
-ENV REDHAT_DEPS="--depends gtk4-devel --depends dbus-x11 --depends wireshark-cli --depends iproute"
-ENV ARCHLINUX_DEPS="--depends gtk4 --depends dbus --depends wireshark-cli --depends iproute2"
+ENV DEBIAN_DEPS="--depends libgtk-4-1 --depends dbus-x11 --depends iproute2 --depends crunch"
+ENV REDHAT_DEPS="--depends gtk4-devel --depends dbus-x11 --depends iproute"
+ENV ARCHLINUX_DEPS="--depends gtk4 --depends dbus --depends iproute2"
# Build and package the project
CMD cargo build && \
diff --git a/crates/agent/src/backend/pcap.rs b/crates/agent/src/backend/pcap.rs
index 84cbc5e..cfdff56 100644
--- a/crates/agent/src/backend/pcap.rs
+++ b/crates/agent/src/backend/pcap.rs
@@ -4,13 +4,13 @@
//! frames, so the file is written with link-type
//! `LINKTYPE_IEEE802_11_RADIOTAP` (127) — the same format `airodump-ng`
//! produced. This keeps everything downstream (native handshake detection, the
-//! `mergecap` accumulation, and the GUI capture export) working unchanged.
+//! capture accumulation, and the GUI capture export) working unchanged.
//!
//! Only the classic libpcap format is implemented (global header + per-record
//! header + raw bytes); that is all the readers need and it avoids pulling in a
//! libpcap dependency.
-use std::fs::File;
+use std::fs::{File, OpenOptions};
use std::io::{self, BufWriter, Write};
use std::path::Path;
use std::time::{SystemTime, UNIX_EPOCH};
@@ -19,6 +19,10 @@ const LINKTYPE_IEEE802_11_RADIOTAP: u32 = 127;
const SNAPLEN: u32 = 65535;
const PCAP_MAGIC: u32 = 0xa1b2_c3d4;
+/// Size of the libpcap global header (magic, versions, thiszone, sigfigs,
+/// snaplen, network).
+const GLOBAL_HEADER_LEN: usize = 24;
+
/// A writer appending captured frames to a libpcap file.
pub struct PcapWriter {
file: BufWriter<File>,
@@ -59,3 +63,81 @@ impl PcapWriter {
self.file.flush()
}
}
+
+/// Append the packet records of `src` onto `dst`.
+///
+/// Both files are captures written by [`PcapWriter`], so they share an identical
+/// global header, merging is therefore just a matter of appending records.
+pub fn append_records(dst: &str, src: &str) -> io::Result<()> {
+ let src_data = std::fs::read(src)?;
+ if src_data.len() <= GLOBAL_HEADER_LEN {
+ return Ok(());
+ }
+
+ let mut dst_file = OpenOptions::new().append(true).open(dst)?;
+ dst_file.write_all(&src_data[GLOBAL_HEADER_LEN..])
+}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+
+ fn temp_path(name: &str) -> String {
+ let mut path = std::env::temp_dir();
+ path.push(format!("airgorah-pcap-test-{}-{name}", std::process::id()));
+ path.to_str().unwrap().to_string()
+ }
+
+ #[test]
+ fn append_records_concatenates_packets() {
+ let dst = temp_path("dst.cap");
+ let src = temp_path("src.cap");
+
+ {
+ let mut writer = PcapWriter::create(&dst).unwrap();
+ writer.write_frame(&[1, 2, 3]).unwrap();
+ }
+ {
+ let mut writer = PcapWriter::create(&src).unwrap();
+ writer.write_frame(&[4, 5]).unwrap();
+ writer.write_frame(&[6]).unwrap();
+ }
+
+ let dst_before = std::fs::read(&dst).unwrap();
+ let src_bytes = std::fs::read(&src).unwrap();
+
+ append_records(&dst, &src).unwrap();
+
+ // dst keeps its own bytes and gains src's records (everything past the header).
+ let dst_after = std::fs::read(&dst).unwrap();
+ assert_eq!(
+ dst_after.len(),
+ dst_before.len() + (src_bytes.len() - GLOBAL_HEADER_LEN)
+ );
+ assert_eq!(&dst_after[..dst_before.len()], &dst_before[..]);
+ assert_eq!(
+ &dst_after[dst_before.len()..],
+ &src_bytes[GLOBAL_HEADER_LEN..]
+ );
+
+ std::fs::remove_file(&dst).ok();
+ std::fs::remove_file(&src).ok();
+ }
+
+ #[test]
+ fn append_records_is_a_noop_for_a_header_only_source() {
+ let dst = temp_path("dst-noop.cap");
+ let src = temp_path("src-noop.cap");
+
+ PcapWriter::create(&dst).unwrap().write_frame(&[9]).unwrap();
+ PcapWriter::create(&src).unwrap(); // header only, no frames captured
+
+ let before = std::fs::read(&dst).unwrap();
+ append_records(&dst, &src).unwrap();
+ let after = std::fs::read(&dst).unwrap();
+ assert_eq!(before, after);
+
+ std::fs::remove_file(&dst).ok();
+ std::fs::remove_file(&src).ok();
+ }
+}
diff --git a/crates/agent/src/backend/scan.rs b/crates/agent/src/backend/scan.rs
index 5273583..41624c8 100644
--- a/crates/agent/src/backend/scan.rs
+++ b/crates/agent/src/backend/scan.rs
@@ -85,25 +85,11 @@ pub fn stop_scan_process() -> Result<(), ScanError> {
return Ok(());
}
- std::process::Command::new("mergecap")
- .args([
- "-a",
- "-F",
- "pcap",
- "-w",
- &(get_merge_scan_path() + get_cap_ext()),
- &(get_old_scan_path() + get_cap_ext()),
- &(get_live_scan_path() + get_cap_ext()),
- ])
- .status()?;
-
+ super::pcap::append_records(
+ &(get_old_scan_path() + get_cap_ext()),
+ &(get_live_scan_path() + get_cap_ext()),
+ )?;
std::fs::remove_file(get_live_scan_path() + get_cap_ext()).ok();
- std::fs::remove_file(get_old_scan_path() + get_cap_ext()).ok();
- std::fs::rename(
- get_merge_scan_path() + get_cap_ext(),
- get_old_scan_path() + get_cap_ext(),
- )
- .ok();
Ok(())
}
@@ -156,7 +142,3 @@ pub fn get_live_scan_path() -> String {
pub fn get_old_scan_path() -> String {
format!("{}-{}", OLD_SCAN_PATH, std::process::id())
}
-
-pub fn get_merge_scan_path() -> String {
- format!("{}-{}", MERGE_SCAN_PATH, std::process::id())
-}
diff --git a/crates/agent/src/globals.rs b/crates/agent/src/globals.rs
index cd8fff9..07372bb 100644
--- a/crates/agent/src/globals.rs
+++ b/crates/agent/src/globals.rs
@@ -19,7 +19,6 @@ pub static CAPTURE_DIR: &str = "/var/lib/airgorah";
pub static LIVE_SCAN_PATH: &str = "/var/lib/airgorah/live_scan";
pub static OLD_SCAN_PATH: &str = "/var/lib/airgorah/old_scan";
-pub static MERGE_SCAN_PATH: &str = "/var/lib/airgorah/merge_scan";
/// Handle to the running native capture thread. `stop` is raised to ask the
/// thread to exit; `handle` is joined to wait for it to finish flushing the
diff --git a/crates/common/src/deps.rs b/crates/common/src/deps.rs
index e74be03..43902fb 100644
--- a/crates/common/src/deps.rs
+++ b/crates/common/src/deps.rs
@@ -62,11 +62,6 @@ pub const TOOLS: &[Tool] = &[
requirer: Requirer::Agent,
optional: false,
},
- Tool {
- name: "mergecap",
- requirer: Requirer::Agent,
- optional: false,
- },
Tool {
name: "macchanger",
requirer: Requirer::Agent,