Code Monkey home page Code Monkey logo

pcap-analyzer's Introduction

License: MIT Apache License 2.0 Github CI

PAL (Pcap Analysis Library)

PAL (Pcap Analysis Library) is a framework and a set of tools for Pcap file analysis, written in Rust.

The main objectives are:

  • provide a robust and efficient framework for analyzing pcap files
  • provide tools to manipulate pcap files with a unifier abstraction
  • reconstruct network data progressively for network layers (Layer 2, 3, etc.) correctly, dealing with common problems safely (fragmentation, missing data, encapsulation, etc.)
  • allow developing plugins easily, focusing only on the interesting content
  • allow plugins to interact with data at different network level (Layer 2, 3, application, etc.)
  • use Rust features like thread safety (to exploit parallelism), memory safety, zero-copy, etc.

Architecture

PAL is split into several components:

  • libpcap-tools: a library providing support functions to manipulate pcap files
  • libpcap-analyzer: the main library, providing network data reconstruction, dispatch, and plugin management. It also provides some plugins.
  • pcap-analyzer: the main executable to run plugins on pcap files
  • pcap-rewrite: a tool to rewrite a pcap file format and link type to another
  • test-analyzer: a similar tool to pcap-analyzer, with more debug plugins and verbosity (for ex. for debugging plugins)
  • explugin-example: an example of plugin developed in a separate crate

Building pcap-analyzer

Use cargo to build pcap-analyzer:

# release mode
cargo build --release
# debug mode
cargo build

Running pcap-analyzer

Just run pcap-analyzer with the names of pcap files as arguments:

pcap-analyzer file.pcap
pcap-analyzer -c config.toml file.pcap

The -p option can be used to restrict the list of plugins to load.

Concurrency level is set using the -j argument. Default is to 1 (no multithreading). Threading is useful when having many flows, so if the input file is small, or if it does not contain many flows, it is best to leave it to 1. Use the value 0 to set the number of threads to the number of virtual CPUs.

Logging is done using the log cargo crate, and will to the log file defined in configuration (pcap-analyzer.log by default). Note that in release mode, only messages with a severity of warn or more are displayed.

To get more debug info, use the test-analyzer tool. It provides the exact same features, but will be more verbose, and will output logs to stderr. The PCAP_ANALYZER_LOG environment variable can be used to set the log level (and set concurrency to 1):

PCAP_ANALYZER_LOG=debug test-analyzer cargo run -p test-analyzer -- -j 1 -c conf/pcap-analyzer.conf file.pcap

Plugins

Plugins are modules that are selected during build, and can be activated during execution. They are embedded into the resulting library.

Not all plugins are built by default, those that are not yet stable or have many dependencies are conditioned by a build feature. To build all plugins, activate the all feature, or select features individually:

cargo build --all --all-features

You can also edit libpcap-analyzer/Cargo.toml to edit the default feature.

Note that due to limitations in the handling of features in workspaces by cargo, there seems to be no easy way to enable one feature only when building the package. Also note that, due to the same limitations, cargo run will not use the features.

For the moment, the only "stable" method is to edit libpcap-analyzer/Cargo.toml.

Plugins can declare functions that will be called either when receiving data for a network layer, or for some events:

  • layer 2: raw data (only if the pcap contains L2 data)
  • layer 3: raw data + ethernet type
  • layer 4: flow + l4 data + l4 payload (if l4 type is known/supported) + l3 data + ethertype + raw packet
  • creating of a flow
  • destruction of a flow

Flows are created for every L4 communication. Flows use five-tuples (IP source and destination, L4 protocol, source and destination ports). If the protocol does not contain ports, they are set to 0.

Note that functions can be called several times for a single packet. For example, in case of encapsulated data (like IP in IP), functions will be called in order (first, the outer data, then the inner data).

Parallelism

To use parallelism, network packets have to be dispatched to worker threads. To ensure consistency, all packets from a single connexion have to be sent to the same worker, or this would create problems like handling packets out-of-order.

The current implementation dispatches packets starting from the layer 3 (layer 2 is handled by the main thread). The dispatch function is based on a symmetric hash function on IP parameters. After this dispatch, each worker thread handles its packets (in received order) and will reconstruct the layer 2 (if present), calling plugins, then layer 3, call plugins, etc.

To ensure consistency, plugins are protected (using locks) before being called. The lock is done at the plugin level, so only one handling function can be called at a time.

In particular:

  • if a plugin registers for several layers, the functions are guaranteed to be called in parsing order (from outer data to inner data)
  • even if several packets are concurrently handled by several workers, a single plugin will not be called concurrently. However, different plugins can execute concurrently.

Notes

  • pcap file parsing is completely reimplemented from scratch. This is the result of most existing libraries lacking features, and the will to provide a unified abstraction to manipulate the different subformats (pcap and pcapng, both in little and big-endian) and link types
  • pcap file read is done in a circular buffer (which size can be controlled using configuration). Before each buffer refill, a synchronization is done to wait all workers to finish their current jobs
  • the plugins are embedded into the main binary. Currently, there is no support for dynamic libraries, due to the lack of support/stability by Rust

License

Licensed under either of

at your option.

Contribution

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.

pcap-analyzer's People

Contributors

chifflier avatar jabedude avatar johanmazelanssi avatar pc-anssi avatar treymarc avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

pcap-analyzer's Issues

[Question] Pcap-analyzer for Voip and VoLTE pcaps

Hi,
Based on the answer on Can't build pcap-parser #5 " Depending on what you want to do, I can provide some hints on what is better to choose": I am looking to work on pcaps contianing protocols such as GTP, RTP, RTCP, SIP, S1AP, DIAMETER and parse specific attributes within the applicaiton layer, some attributes from FRAME and Transport layers as well but mainly on application layers.
Is this something currenlty possible? Any hint or recommendation on how to use it for Application layers?

unable to contribute

Hi,
I would like to contribute to the project, but I didn't succeed to push my branch:

kvmk8371@yd-5CD9245WBL:~/workspace/pcap-analyzer$ git push origin continue_to_parse_on_error 
Username for 'https://github.com': tigroo31
Password for 'https://[email protected]': 
remote: Permission to rusticata/pcap-analyzer.git denied to tigroo31.
fatal: unable to access 'https://github.com/rusticata/pcap-analyzer.git/': The requested URL returned error: 403
kvmk8371@yd-5CD9245WBL:~/workspace/pcap-analyzer$ 

Do I miss a key step to be a contributor?

Thanks by advance for this "github" support :).

Command line error D8021 : invalid numeric argument '/Wno-implicit-fallthrough'

Hello.
I cannot build in release configuration from master branch on Windows.

Here is some info on my environment:
rustc 1.65.0 (897e37553 2022-11-02)
This is my HEAD from git log:
commit 1d8fb0f

I think there is no Wno-implicit-fallthrough on MSVC.

Error:

error: failed to run custom build command for `fasthash-sys v0.3.2`

Caused by:
  process didn't exit successfully: `D:\Source\pcap-analyzer\target\release\build\fasthash-sys-8a36ab5968afd195\build-script-build` (exit code: 101)
  --- stdout
  TARGET = Some("x86_64-pc-windows-msvc")
  OPT_LEVEL = Some("z")
  TARGET = Some("x86_64-pc-windows-msvc")
  HOST = Some("x86_64-pc-windows-msvc")
  TARGET = Some("x86_64-pc-windows-msvc")
  TARGET = Some("x86_64-pc-windows-msvc")
  HOST = Some("x86_64-pc-windows-msvc")
  CC_x86_64-pc-windows-msvc = None
  CC_x86_64_pc_windows_msvc = None
  HOST_CC = None
  CC = None
  TARGET = Some("x86_64-pc-windows-msvc")
  HOST = Some("x86_64-pc-windows-msvc")
  CFLAGS_x86_64-pc-windows-msvc = None
  CFLAGS_x86_64_pc_windows_msvc = None
  HOST_CFLAGS = None
  CFLAGS = None
  DEBUG = Some("false")
  running: "C:\\Program Files\\Microsoft Visual Studio\\2022\\Community\\VC\\Tools\\MSVC\\14.34.31933\\bin\\HostX64\\x64\\cl.exe" "/nologo" "/MD" "/Os" "-Wno-implicit-fallthrough" "-Wno-unknown-attributes" "-msse4.2" "-maes" "-mavx" "-mavx2" "/DT1HA0_RUNTIME_SELECT=1" "/DT1HA0_AESNI_AVAILABLE=1" "/W4" "/FoD:\\Source\\pcap-analyzer\\target\\release\\build\\fasthash-sys-6546831e711a357c\\out\\src/fasthash.o" "/c" "src/fasthash.cpp"
  cargo:warning=cl : Command line error D8021 : invalid numeric argument '/Wno-implicit-fallthrough'
  exit code: 2

  --- stderr
  thread 'main' panicked at '

  Internal error occurred: Command "C:\\Program Files\\Microsoft Visual Studio\\2022\\Community\\VC\\Tools\\MSVC\\14.34.31933\\bin\\HostX64\\x64\\cl.exe" "/nologo" "/MD" "/Os" "-Wno-implicit-fallthrough" "-Wno-unknown-attributes" "-msse4.2" "-maes" "-mavx" "-mavx2" "/DT1HA0_RUNTIME_SELECT=1" "/DT1HA0_AESNI_AVAILABLE=1" "/W4" "/FoD:\\Source\\pcap-analyzer\\target\\release\\build\\fasthash-sys-6546831e711a357c\\out\\src/fasthash.o" "/c" "src/fasthash.cpp" with args "cl.exe" did not execute successfully (status code exit code: 2).

RUSTSEC-2020-0071: Potential segfault in the time crate

Potential segfault in the time crate

Details
Package time
Version 0.1.43
URL time-rs/time#293
Date 2020-11-18
Patched versions >=0.2.23
Unaffected versions =0.2.0,=0.2.1,=0.2.2,=0.2.3,=0.2.4,=0.2.5,=0.2.6

Impact

Unix-like operating systems may segfault due to dereferencing a dangling pointer in specific circumstances. This requires an environment variable to be set in a different thread than the affected functions. This may occur without the user's knowledge, notably in a third-party library.

The affected functions from time 0.2.7 through 0.2.22 are:

  • time::UtcOffset::local_offset_at
  • time::UtcOffset::try_local_offset_at
  • time::UtcOffset::current_local_offset
  • time::UtcOffset::try_current_local_offset
  • time::OffsetDateTime::now_local
  • time::OffsetDateTime::try_now_local

The affected functions in time 0.1 (all versions) are:

  • at
  • at_utc

Non-Unix targets (including Windows and wasm) are unaffected.

Patches

Pending a proper fix, the internal method that determines the local offset has been modified to always return None on the affected operating systems. This has the effect of returning an Err on the try_* methods and UTC on the non-try_* methods.

Users and library authors with time in their dependency tree should perform cargo update, which will pull in the updated, unaffected code.

Users of time 0.1 do not have a patch and should upgrade to an unaffected version: time 0.2.23 or greater or the 0.3. series.

Workarounds

No workarounds are known.

References

time-rs/time#293

See advisory page for additional details.

RUSTSEC-2020-0159: Potential segfault in `localtime_r` invocations

Potential segfault in localtime_r invocations

Details
Package chrono
Version 0.4.19
URL chronotope/chrono#499
Date 2020-11-10

Impact

Unix-like operating systems may segfault due to dereferencing a dangling pointer in specific circumstances. This requires an environment variable to be set in a different thread than the affected functions. This may occur without the user's knowledge, notably in a third-party library.

Workarounds

No workarounds are known.

References

See advisory page for additional details.

provide better examples

compiled all the tools pcap-analyzer test-analyzer etc. but not much happens when running them on a pcap file (other than displaying some simple stats about the pcap file itself).

Maybe something like this would be helpful to get started:

use anyhow::Result;
use libpcap_analyzer::{Analyzer, PluginRegistry};
use libpcap_analyzer::plugins::flows::FlowsInfo;
use libpcap_tools::{Config, PcapDataEngine, PcapEngine};
use std::fs::File;
use std::sync::{Arc, Mutex};

fn main() -> Result<()> {
    env_logger::init();
    let config = Config::default();
    let mut registry = PluginRegistry::new();
    let flows = Arc::new(Mutex::new(FlowsInfo::default()));
    registry.add_plugin(flows.clone());
    let analyzer = Analyzer::new(Arc::new(registry), &config);
    let mut engine = PcapDataEngine::new(analyzer, &config);
    let mut f = File::open("/home/dvc/ipld/quinn-noise-dissector/libp2p-quic.pcap")?;
    engine.run(&mut f)?;
    let json = flows.lock().unwrap().get_results_json();
    println!("{}", serde_json::to_string(&json)?);
    Ok(())
}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.