Giter Site home page Giter Site logo

caizhengxin / libpcap-rs Goto Github PK

View Code? Open in Web Editor NEW
0.0 1.0 0.0 96 KB

Encapsulate the libpcap library using Rust

License: Apache License 2.0

Rust 99.90% C 0.10%
libpcap libpcap-driver libpcap-wrapper pcap pcap-analyzer pcap-files pcap-parser pcapng

libpcap-rs's Introduction

libpcap-rs

Crates.io Crates.io License

Encapsulate the libpcap library using Rust.

Features

  • Read pcap file
  • Write pcap file
  • Merge pcap file
  • Get first iface (active)
  • Get iface list (active)
  • Send raw packet
  • Capture packet

Usage

Install

$ sudo apt install libpcap-dev -yq

Command

$ cargo build --release

# Combine multiple files into one file
$ ./target/release/libpcap-merge -i ~/pcap/ -o new.pcap port 80

# Writes to the pcap file
$ ./target/release/libpcap-write -o test.pcap -p d4ad200073c5a8a1596edbd10800450000341b01400080065e5fc0a8000cc0a80007c4aa001a5c5c8864000000008002faf049360000020405b40103030801010402

Cargo.toml

[dependencies]
libpcap-rs = "0.2.1"

Read pcap file

use libpcap_rs::LibPcap;


fn main() {
    match LibPcap::open("~/tests/pcap/http_1.pcap", "r") {
        Ok(f) => {
            for pkt in f.read() {
                println!("{:?}", pkt);
            }        
        }
        Err(e) => {
            println!("[ERROR]: {e:?}");
        }
    }
}

Write pcap file

use libpcap_rs::LibPcap;


fn main() {
    let input = b"\x00\x0c\x29\xaf\x7f\xfe\x10\x9a\xdd\x4e\x06\x0d\x08\x00\x45\x00\
    \x00\x40\xb5\xf2\x00\x00\x40\x06\xa9\x7c\x0a\x01\x01\xea\x0a\x0a\
    \x05\x55\xc8\xd3\x01\xf6\xe0\x76\x90\x16\xc4\x44\x9b\x5a\x80\x18\
    \xff\xff\x6c\x1c\x00\x00\x01\x01\x08\x0a\x37\xc4\x50\xe2\x00\xba\
    \x7c\x1c\x4d\x6e\x00\x00\x00\x06\xff\x03\x01\xf4\x00\x64";

    let libpcap = LibPcap::open("test.pcap", "w");

    match libpcap {
        Ok(f) => f.write(input),
        Err(e) => println!("[ERROR]: {e:?}"),
    }
}

Capture packet

use libpcap_rs::{Sniff, PResult};


fn main() -> PResult<()> {
    let sniff = Sniff::open("lo")?;

    for pkt in sniff.capture(-1) {
        println!("{pkt:?}");
    }

    Ok(())
}
use libpcap_rs::{Sniff, PResult};


fn main() -> PResult<()> {
    let sniff = Sniff::open("lo")?
        .with_filter("port 80")?
        .with_snaplen(65535)
        .with_immediate_mode(1)
        .with_timeout(0);

    for pkt in sniff.capture(-1) {
        println!("{pkt:?}");
    }

    Ok(())
}

Obtain the first active network port

use libpcap_rs::get_first_iface;


fn main() {
    println!("{:?}", get_first_iface);
}

Obtain active network port list

use libpcap_rs::get_iface_list;


fn main() {
    println!("{:?}", get_iface_list());
}

Using network port send raw packet

use libpcap_rs::send_packet;


fn main() {
    let input = b"\x00\x0c\x29\xaf\x7f\xfe\x10\x9a\xdd\x4e\x06\x0d\x08\x00\x45\x00\
                \x00\x40\xb5\xf2\x00\x00\x40\x06\xa9\x7c\x0a\x01\x01\xea\x0a\x0a\
                \x05\x55\xc8\xd3\x01\xf6\xe0\x76\x90\x16\xc4\x44\x9b\x5a\x80\x18\
                \xff\xff\x6c\x1c\x00\x00\x01\x01\x08\x0a\x37\xc4\x50\xe2\x00\xba\
                \x7c\x1c\x4d\x6e\x00\x00\x00\x06\xff\x03\x01\xf4\x00\x64";
    send_packet("lo", input);
}

libpcap-rs's People

Contributors

caizhengxin avatar

Watchers

 avatar

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.