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
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 ```rust let mut pktbuf = [0u8; 1500]; let pkt = packetbuilder!( pktbuf, ether({setsource => MacAddr(10,1,1,1,1,1)}) / ipv4({setsource => ipv4addr!("127.0.0.1"), setdestination => ipv4addr!("127.0.0.1") }) / icmpdestunreach({seticmptype => IcmpTypes::DestinationUnreachable}) / ipv4({setsource => ipv4addr!("10.8.0.1"), setdestination => ipv4addr!("127.0.0.1") }) / udp({setsource => 53, setdestination => 5353}) / payload({"hello".tostring().intobytes()}) );
let ifname = env::args().nth(1)
.expect("Usage: ./packetbuilder
Generate a TCP PSH|ACK packet with data
rust
let mut pktbuf = [0u8; 1500];
let pkt = packetbuilder!(
pktbuf,
ether({setdestination => MacAddr(1,2,3,4,5,6), setsource => MacAddr(10,1,1,1,1,1)}) /
ipv4({setsource => ipv4addr!("127.0.0.1"), setdestination => ipv4addr!("127.0.0.1") }) /
tcp({setsource => 43455, setdestination => 80, setflags => (TcpFlags::PSH | TcpFlags::ACK)}) /
payload({"hello".tostring().intobytes()})
);
Generate a TCP SYN packet with mss and wscale options specified over VLAN ID 10
rust
let mut pktbuf = [0u8; 1500];
let pkt = packetbuilder!(
pktbuf,
ether({setdestination => MacAddr(1,2,3,4,5,6), setsource => MacAddr(10,1,1,1,1,1)}) /
vlan({setvlanidentifier => 10}) /
ipv4({setsource => ipv4addr!("192.168.1.1"), setdestination => ipv4addr!("127.0.0.1") }) /
tcp({setsource => 43455, setdestination => 80, setoptions => &[TcpOption::mss(1200), TcpOption::wscale(2)]}) /
payload({[0; 0]})
);
Generate a UDP packet with data
rust
let mut pktbuf = [0u8; 1500];
let pkt = packetbuilder!(
pktbuf,
ether({setdestination => MacAddr(1,2,3,4,5,6), setsource => MacAddr(10,1,1,1,1,1)}) /
ipv4({setsource => ipv4addr!("127.0.0.1"), setdestination => ipv4addr!("127.0.0.1") }) /
udp({setsource => 12312, setdestination => 143}) /
payload({"hello".tostring().intobytes()})
);
Generate an ICMP Echo Request packet
rust
let mut pktbuf = [0u8; 1500];
let pkt = packetbuilder!(
pktbuf,
ether({setdestination => MacAddr(1,2,3,4,5,6), setsource => MacAddr(10,1,1,1,1,1)}) /
ipv4({setsource => ipv4addr!("127.0.0.1"), setdestination => ipv4addr!("127.0.0.1") }) /
icmpechoreq({seticmptype => IcmpTypes::EchoRequest}) /
payload({"hello".tostring().intobytes()})
);
```
Licensed under either of
at your option.
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.