Giter VIP home page Giter VIP logo

rust-packet's Introduction

Actions Status codecov

Rust Packet

A network packet parser and builder. This crate is meant to provide similar functionality to python's scapy module in terms of packet and layer manipulation.

Example

See examples for more.

Packet manipulation

use rust_packet::prelude::*;

fn main() {
    // Build a packet!
    let pkt: Packet = pkt! {
        ether! {
            dst: "de:ad:be:ef:c0:fe".parse()?
        }?,
        ipv4! {
            src: "127.0.0.1".parse()?,
            dst: "127.0.0.2".parse()?,
        }?,
        udp! {
            dport: 1337
        }?,
        raw! {
            data: b"hello world!".to_vec()
        }?,
    }.unwrap();

    // Read a packet!
    let input: Vec<u8> = pkt.to_bytes().unwrap();
    let mut pkt = Packet::from_bytes(input.as_ref()).unwrap();

    // Change the packet!
    if let Some(ipv4) = pkt.ipv4_mut() {
        ipv4.dst = "127.0.0.3".parse().unwrap()
    }

    // Update the packet! (This will update the various checksums (Ipv4, TCP, UDP))
    pkt.update().unwrap();

    // Write the packet!
    let raw_bytes = pkt.to_bytes().unwrap();
}

Network interfaces

This utilizes the libpnet crate to read and write from network interfaces.

See PcapFile example to read a pcap.

use rust_packet::prelude::*;

fn main() {
    // Read from interface
    // See also `Pcap` and `PcapFile`!
    let mut int = Interface::<Pnet>::new("lo").unwrap();

    for (i, pkt) in (&mut int).enumerate() {
        println!("Packet: {:?}", pkt);
        if i == 5 {
            break;
        }
    }

    // Write to interface
    let pkt = pkt! {
        ether! {}?,
        ipv4! {}?,
        udp! {}?,
        raw! {
            data: b"Hello world".to_vec(),
        }?
    }
    .unwrap();

    int.write(pkt).unwrap();
}

Contributing a new layer

If you'd like to contribute a new layer, the layer module is the best place to start! Search the codebase for # Layer for areas in which will need to be updated!

rust-packet's People

Contributors

sharksforarms avatar dependabot-preview[bot] avatar wcampbell0x2a avatar

Watchers

James Cloos avatar  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.