Git Product home page Git Product logo

packet_builder's Introduction

packet_builder

Crates.io - packet_builder Build Status License: MIT License: Apache-2.0

packet_builder is a high-level rust library for low-level networking that makes use of macros to provide a "kwargs-like" interface a-la python's dpkt/scapy.

With packet_builder you can construct and modify arbitrary packet data and attempt to send it via a NIC, which uses libpnet under the covers.

sendpacket wasn't being maintained so as an exercise in learning Rust I forked it to make packet_builder. Sane defaults are used for fields that aren't set by the caller and checksums are calculated for you. Currently the following protocols are supported:

  • Ethernet
  • ARP
  • 802.1Q
  • IPv4
  • ICMP
  • UDP
  • TCP

Examples

All the macros are essentially wrappers around the packet structures and functions in libpnet so all the set functions for the various packet types within libpnet are valid. See the examples directory for complete examples.

Generate a destination unreachable ICMP packet and send it

let mut pkt_buf = [0u8; 1500];
let pkt = packet_builder!(
     pkt_buf,
     ether({set_source => MacAddr(10,1,1,1,1,1)}) / 
     ipv4({set_source => ipv4addr!("127.0.0.1"), set_destination => ipv4addr!("127.0.0.1") }) /
     icmp_dest_unreach({set_icmp_type => IcmpTypes::DestinationUnreachable}) / 
     ipv4({set_source => ipv4addr!("10.8.0.1"), set_destination => ipv4addr!("127.0.0.1") }) /
     udp({set_source => 53, set_destination => 5353}) /
     payload({"hello".to_string().into_bytes()})
);

let if_name = env::args().nth(1)
    .expect("Usage: ./packet_builder <interface name>");
let (mut sender, _receiver) = build_channel!(if_name);
sender.send_to(pkt.packet(), None).unwrap().unwrap();

Generate a TCP PSH|ACK packet with data

let mut pkt_buf = [0u8; 1500];
let pkt = packet_builder!(
     pkt_buf,
     ether({set_destination => MacAddr(1,2,3,4,5,6), set_source => MacAddr(10,1,1,1,1,1)}) / 
     ipv4({set_source => ipv4addr!("127.0.0.1"), set_destination => ipv4addr!("127.0.0.1") }) /
     tcp({set_source => 43455, set_destination => 80, set_flags => (TcpFlags::PSH | TcpFlags::ACK)}) /
     payload({"hello".to_string().into_bytes()})
);

Generate a TCP SYN packet with mss and wscale options specified over VLAN ID 10

let mut pkt_buf = [0u8; 1500];
let pkt = packet_builder!(
   pkt_buf,
   ether({set_destination => MacAddr(1,2,3,4,5,6), set_source => MacAddr(10,1,1,1,1,1)}) /
   vlan({set_vlan_identifier => 10}) /
   ipv4({set_source => ipv4addr!("192.168.1.1"), set_destination => ipv4addr!("127.0.0.1") }) /
   tcp({set_source => 43455, set_destination => 80, set_options => &[TcpOption::mss(1200), TcpOption::wscale(2)]}) /
   payload({[0; 0]})
);

Generate a UDP packet with data

let mut pkt_buf = [0u8; 1500];
let pkt = packet_builder!(
     pkt_buf,
     ether({set_destination => MacAddr(1,2,3,4,5,6), set_source => MacAddr(10,1,1,1,1,1)}) / 
     ipv4({set_source => ipv4addr!("127.0.0.1"), set_destination => ipv4addr!("127.0.0.1") }) /
     udp({set_source => 12312, set_destination => 143}) /
     payload({"hello".to_string().into_bytes()})
);

Generate an ICMP Echo Request packet

let mut pkt_buf = [0u8; 1500];
let pkt = packet_builder!(
     pkt_buf,
     ether({set_destination => MacAddr(1,2,3,4,5,6), set_source => MacAddr(10,1,1,1,1,1)}) / 
     ipv4({set_source => ipv4addr!("127.0.0.1"), set_destination => ipv4addr!("127.0.0.1") }) /
     icmp_echo_req({set_icmp_type => IcmpTypes::EchoRequest}) / 
     payload({"hello".to_string().into_bytes()})
);

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.

packet_builder's People

Contributors

ahughes-tenable avatar blakehawkins avatar bossmc avatar hughesac avatar imsnif avatar jaimevaldemoros avatar jamesturnermetaswitch avatar jvg-metaswitch avatar jwt95 avatar shakram02 avatar ttys3 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

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.