diff --git a/__network/rust-rawsock-demo/Cargo.lock b/__network/rust-rawsock-demo/Cargo.lock new file mode 100644 index 0000000..ba86728 --- /dev/null +++ b/__network/rust-rawsock-demo/Cargo.lock @@ -0,0 +1,166 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "bitflags" +version = "1.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a" + +[[package]] +name = "cc" +version = "1.0.90" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8cd6604a82acf3039f1144f54b8eb34e91ffba622051189e71b781822d5ee1f5" + +[[package]] +name = "dlopen" +version = "0.1.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "71e80ad39f814a9abe68583cd50a2d45c8a67561c3361ab8da240587dda80937" +dependencies = [ + "dlopen_derive", + "lazy_static", + "libc", + "winapi", +] + +[[package]] +name = "dlopen_derive" +version = "0.1.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f236d9e1b1fbd81cea0f9cbdc8dcc7e8ebcd80e6659cd7cb2ad5f6c05946c581" +dependencies = [ + "libc", + "quote", + "syn", +] + +[[package]] +name = "errno" +version = "0.2.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f639046355ee4f37944e44f60642c6f3a7efa3cf6b78c78a0d989a8ce6c396a1" +dependencies = [ + "errno-dragonfly", + "libc", + "winapi", +] + +[[package]] +name = "errno-dragonfly" +version = "0.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "aa68f1b12764fab894d2755d2518754e71b4fd80ecfb822714a1206c2aab39bf" +dependencies = [ + "cc", + "libc", +] + +[[package]] +name = "lazy_static" +version = "1.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646" + +[[package]] +name = "libc" +version = "0.2.153" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9c198f91728a82281a64e1f4f9eeb25d82cb32a5de251c6bd1b5154d63a8e7bd" + +[[package]] +name = "proc-macro2" +version = "0.4.30" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cf3d2011ab5c909338f7887f4fc896d35932e29146c12c8d01da6b22a80ba759" +dependencies = [ + "unicode-xid", +] + +[[package]] +name = "quote" +version = "0.6.13" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6ce23b6b870e8f94f81fb0a363d65d86675884b34a09043c81e5562f11c1f8e1" +dependencies = [ + "proc-macro2", +] + +[[package]] +name = "rawsock" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bb3edb83948587d85e23284103bfa797da3f16f686d89dad4c42366ef65a9266" +dependencies = [ + "bitflags", + "dlopen", + "dlopen_derive", + "errno", + "libc", + "time", +] + +[[package]] +name = "rust-rawsock-demo" +version = "0.1.0" +dependencies = [ + "rawsock", +] + +[[package]] +name = "syn" +version = "0.15.44" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9ca4b3b69a77cbe1ffc9e198781b7acb0c7365a883670e8f1c1bc66fba79a5c5" +dependencies = [ + "proc-macro2", + "quote", + "unicode-xid", +] + +[[package]] +name = "time" +version = "0.1.45" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1b797afad3f312d1c66a56d11d0316f916356d11bd158fbc6ca6389ff6bf805a" +dependencies = [ + "libc", + "wasi", + "winapi", +] + +[[package]] +name = "unicode-xid" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fc72304796d0818e357ead4e000d19c9c174ab23dc11093ac919054d20a6a7fc" + +[[package]] +name = "wasi" +version = "0.10.0+wasi-snapshot-preview1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1a143597ca7c7793eff794def352d41792a93c481eb1042423ff7ff72ba2c31f" + +[[package]] +name = "winapi" +version = "0.3.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5c839a674fcd7a98952e593242ea400abe93992746761e38641405d28b00f419" +dependencies = [ + "winapi-i686-pc-windows-gnu", + "winapi-x86_64-pc-windows-gnu", +] + +[[package]] +name = "winapi-i686-pc-windows-gnu" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" + +[[package]] +name = "winapi-x86_64-pc-windows-gnu" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" diff --git a/__network/rust-rawsock-demo/Cargo.toml b/__network/rust-rawsock-demo/Cargo.toml new file mode 100644 index 0000000..20ecd02 --- /dev/null +++ b/__network/rust-rawsock-demo/Cargo.toml @@ -0,0 +1,9 @@ +[package] +name = "rust-rawsock-demo" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +rawsock = "0.3.0" diff --git a/__network/rust-rawsock-demo/src/bin/list_interfaces.rs b/__network/rust-rawsock-demo/src/bin/list_interfaces.rs new file mode 100644 index 0000000..70a7835 --- /dev/null +++ b/__network/rust-rawsock-demo/src/bin/list_interfaces.rs @@ -0,0 +1,24 @@ +use rawsock::{InterfaceDescription, open_best_library}; + +fn print_interfaces(interfs: Vec) { + println!("Found interfaces:"); + for (num, interf) in interfs.iter().enumerate() { + println!("{}: {}, {}", num, interf.name, interf.description); + } +} + +// from: https://github.com/szymonwieloch/rust-rawsock/tree/master/examples +fn main() { + // this is generic version that work with the current library: + let lib = open_best_library().expect("Could not open any library"); + let interfs = lib.all_interfaces().expect("Could not obtain interface list"); + print_interfaces(interfs); + + // use rawsock::traits::Library; + // //this is a version with concrete library type + // //pcap and pfring support different subsets of interfaces and add some virtual interfaces + // //although "real" interfaces should be available in both of them + // let lib = rawsock::pcap::Library::open_default_paths().expect("Could not open pcap library"); + // let interfs = lib.all_interfaces().expect("Could not obtain interface list"); + // print_interfaces(interfs); +} \ No newline at end of file diff --git a/__network/rust-rawsock-demo/src/bin/static.rs b/__network/rust-rawsock-demo/src/bin/static.rs new file mode 100644 index 0000000..2df21ab --- /dev/null +++ b/__network/rust-rawsock-demo/src/bin/static.rs @@ -0,0 +1,70 @@ +use rawsock::{pcap, pfring, wpcap}; +use rawsock::traits::{DynamicInterface, Library}; + +pub const ICMP_PACKET: [u8; 84] = [ + 0x45, 0x00, 0x00, 0x54, 0xee, 0x96, 0x40, 0x00, 0x40, 0x01, 0x79, 0xf0, 0xc0, 0xa8, 0x01, 0x6a, + 0x08, 0x08, 0x08, 0x08, 0x08, 0x00, 0x2f, 0x08, 0x66, 0xc2, 0x00, 0x12, 0x82, 0xaa, 0xcc, 0x5c, + 0x00, 0x00, 0x00, 0x00, 0x51, 0x49, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x11, 0x12, 0x13, + 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 0x20, 0x21, 0x22, 0x23, + 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f, 0x30, 0x31, 0x32, 0x33, + 0x34, 0x35, 0x36, 0x37]; + +pub fn open_library() -> T where T: Library { + let lib = T::open_default_paths().expect("Could not open library"); + println!("Library opened, version is {}", lib.version()); + lib +} + +pub fn receive_packets<'a, T>(interf: &mut T) where T: DynamicInterface<'a> { + println!("Receiving 5 packets:"); + for i in 1..6 { + let packet = interf.receive().expect("Error while receiving packet"); + println!("Packet {} is {}", i, packet); + } +} + +pub fn send_packets<'a, T>(interf: &mut T) where T: DynamicInterface<'a> { + println!("Data link: {}", interf.data_link()); + println!("Sending 5 ICMP ping packets:"); + for i in 1..6 { + interf.send(&ICMP_PACKET).expect("Errow while sending packet"); + println!("Packet {} was sent", i); + } +} + +// from: https://github.com/szymonwieloch/rust-rawsock/tree/master/examples +fn main() { + run_pcap(); + run_pfring(); + run_wpcap(); +} + +fn run_pcap() { + let lib = open_library::(); + let ifname = lib.all_interfaces() + .expect("Could not obtain interface list").first() + .expect("There are no available interfaces").name.clone(); + let mut interf = lib.open_interface(&ifname).expect("Could not open pcap interface"); + send_packets(&mut interf); + receive_packets(&mut interf); +} + +fn run_wpcap() { + let lib = open_library::(); + let ifname = lib.all_interfaces() + .expect("Could not obtain interface list").first() + .expect("There are no available interfaces").name.clone(); + let mut interf = lib.open_interface(&ifname).expect("Could not open wpcap interface"); + send_packets(&mut interf); + receive_packets(&mut interf); +} + +fn run_pfring() { + let lib = open_library::(); + let ifname = lib.all_interfaces() + .expect("Could not obtain interface list").first() + .expect("There are no available interfaces").name.clone(); + let mut interf = lib.open_interface(&ifname).expect("Could not open pfring interface"); + send_packets(&mut interf); + receive_packets(&mut interf); +} \ No newline at end of file