From 4475d23d1da645223b271bd4113c65aca5f40bad Mon Sep 17 00:00:00 2001 From: Andrew Rioux Date: Tue, 23 Jan 2024 23:26:58 -0500 Subject: [PATCH] Finished adding a basic flake It can now build the Linux C2 beacon --- .gitignore | 1 + Cargo.lock | 8 ++++ flake.nix | 49 ++++++++++++++------ nl-sys/Cargo.toml | 3 +- nl-sys/build.rs | 10 +++- pcap-sys/Cargo.toml | 1 + pcap-sys/build.rs | 51 +++++++++++++-------- sparse-05/sparse-05-client/Cargo.toml | 4 +- sparse-05/sparse-05-server/Cargo.toml | 6 +-- sparse-05/sparse-05-server/src/interface.rs | 2 +- 10 files changed, 94 insertions(+), 41 deletions(-) diff --git a/.gitignore b/.gitignore index 5f0d161..63e45bb 100644 --- a/.gitignore +++ b/.gitignore @@ -4,3 +4,4 @@ examples/bind-shell/key-generator/privkey core **/core .direnv +result diff --git a/Cargo.lock b/Cargo.lock index 1fe13f4..caeee40 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -475,6 +475,12 @@ version = "2.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "6999dc1837253364c2ebb0704ba97994bd874e8f195d665c50b7548f6ea92764" +[[package]] +name = "fs_extra" +version = "1.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "42703706b716c37f96a77aea830392ad231f44c9e9a67872fa5548707e11b11c" + [[package]] name = "futures" version = "0.3.28" @@ -783,6 +789,7 @@ version = "0.1.0" dependencies = [ "autotools", "cc", + "fs_extra", "libc", ] @@ -901,6 +908,7 @@ version = "0.1.0" dependencies = [ "cmake", "errno 0.2.8", + "fs_extra", "futures", "libc", "packets", diff --git a/flake.nix b/flake.nix index 6efb704..c22f43b 100644 --- a/flake.nix +++ b/flake.nix @@ -44,8 +44,13 @@ pkg-config ]; - toolchain = pkgs.pkgsBuildHost.rust-bin.fromRustupToolchainFile - ./rust-toolchain.toml; + windowsBuildTools = buildTools ++ (with pkgs; [ + pkgsCross.mingwW64.stdenv.cc + pkgsCross.mingwW64.windows.pthreads + ]); + + toolchain = + pkgs.pkgsMusl.rust-bin.fromRustupToolchainFile ./rust-toolchain.toml; craneLib = (crane.mkLib pkgs).overrideToolchain toolchain; @@ -54,36 +59,49 @@ commonArgs = { inherit src; - #strictDeps = true; + strictDeps = true; - #CARGO_BUILD_RUSTFLAGS = "-C target-feature=+crt-static"; + CARGO_BUILD_RUSTFLAGS = "-C target-feature=+crt-static"; CARGO_BUILD_TARGET = "x86_64-unknown-linux-musl"; SPARSE_BUILD_LIBPCAP = "${libpcap-src}"; - #SPARSE_BUILD_LIBNL = "${libnl}"; + SPARSE_BUILD_LIBNL = "${libnl}"; + + nativeBuildInputs = buildTools; + buildInputs = buildTools; + + doCheck = false; }; commonWindowsArgs = commonArgs // { CARGO_BUILD_TARGET = "x86_64-pc-windows-gnu"; + + nativeBuildInputs = windowsBuildTools; + buildInputs = windowsBuildTools; }; artifacts = craneLib.buildDepsOnly commonArgs; windowsArtifacts = craneLib.buildDepsOnly commonWindowsArgs; + sparse-05-linux-server = craneLib.buildPackage (commonArgs // { + inherit artifacts; + + pname = "sparse-05-server"; + cargoExtraArgs = "-p sparse-05-server --locked"; + }); + + sparse-05-windows-server = craneLib.buildPackage (commonWindowsArgs // { + inherit windowsArtifacts; + + pname = "sparse-05-server"; + cargoExtraArgs = "-p sparse-05-server --locked"; + }); + sparse-c2-linux-beacon = craneLib.buildPackage (commonArgs // { inherit artifacts; pname = "sparse-c2-beacon"; cargoExtraArgs = "-p sparse-c2-beacon --locked"; - doCheck = false; - - nativeBuildInputs = buildTools; - buildInputs = buildTools; - }); - - crate = craneLib.buildPackage (commonArgs // { - inherit artifacts; - buildInputs = buildTools; }); in with pkgs; { devShells.default = craneLib.devShell rec { @@ -92,10 +110,11 @@ buildInputs = buildTools; SPARSE_BUILD_LIBPCAP = "${libpcap-src}"; - #SPARSE_BUILD_LIBNL = "${libnl}"; + SPARSE_BUILD_LIBNL = "${libnl}"; }; packages = rec { + inherit sparse-05-linux-server sparse-05-windows-server; inherit sparse-c2-linux-beacon; default = sparse-c2-linux-beacon; diff --git a/nl-sys/Cargo.toml b/nl-sys/Cargo.toml index f48357c..8fd93b4 100644 --- a/nl-sys/Cargo.toml +++ b/nl-sys/Cargo.toml @@ -10,4 +10,5 @@ libc = "0.2.142" [build-dependencies] autotools = "0.2" -cc = "1.0" \ No newline at end of file +cc = "1.0" +fs_extra = "1.3.0" diff --git a/nl-sys/build.rs b/nl-sys/build.rs index d1d5f5d..103da75 100644 --- a/nl-sys/build.rs +++ b/nl-sys/build.rs @@ -13,10 +13,18 @@ // You should have received a copy of the GNU Affero General Public License // along with this program. If not, see . +use fs_extra::dir::{copy, CopyOptions}; + fn main() -> std::io::Result<()> { cc::Build::new().file("src/bridge.c").compile("bridge"); - let dst = autotools::Config::new("libnl").reconf("-vi").build(); + let libnl_src = format!("{}/libnl_src", std::env::var("OUT_DIR").unwrap()); + + let mut options = CopyOptions::new(); + options.copy_inside = true; + copy(std::env!("SPARSE_BUILD_LIBNL"), &libnl_src, &options); + + let dst = autotools::Config::new(libnl_src).reconf("-vi").build(); println!("cargo:rustc-link-search=native={}/lib", dst.display()); println!("cargo:rustc-link-lib=static=nl-3"); diff --git a/pcap-sys/Cargo.toml b/pcap-sys/Cargo.toml index 74e98e8..6411b29 100644 --- a/pcap-sys/Cargo.toml +++ b/pcap-sys/Cargo.toml @@ -28,3 +28,4 @@ packets = { path = "../packets" } [build-dependencies] cmake = "0.1" +fs_extra = "1.3.0" diff --git a/pcap-sys/build.rs b/pcap-sys/build.rs index 649ed65..744ef76 100644 --- a/pcap-sys/build.rs +++ b/pcap-sys/build.rs @@ -13,24 +13,37 @@ // You should have received a copy of the GNU Affero General Public License // along with this program. If not, see . -fn main() { - let dst = cmake::Config::new(std::env!("SPARSE_BUILD_LIBPCAP")) - .define("BUILD_SHARED_LIBS", "OFF") - .define("BUILD_WITH_LIBNL", "OFF") - .define("DISABLE_BLUETOOTH", "ON") - .define("DISABLE_DAG", "ON") - .define("DISABLE_DBUS", "ON") - .define("DISABLE_DPDK", "ON") - .define("DISABLE_LINUX_USBMON", "ON") - .define("DISABLE_NETMAP", "ON") - .define("DISABLE_RDMA", "ON") - .define("DISABLE_SEPTEL", "ON") - .define("DISABLE_SNF", "ON") - .define("DISABLE_TC", "ON") - .define("PCAP_TYPE", "linux") - .build(); +use fs_extra::dir::{copy, CopyOptions}; - // panic!("hahahahah test {}", dst.display()); - println!("cargo:rustc-link-search=native={}/lib", dst.display()); - println!("cargo:rustc-link-lib=pcap"); +fn main() { + if std::env::var("CARGO_CFG_TARGET_OS").unwrap() == "windows" { + println!("cargo:rustc-lib-lib=pcap"); + } else { + let libpcap_src = format!("{}/pcap_src", std::env::var("OUT_DIR").unwrap()); + + let mut options = CopyOptions::new(); + options.copy_inside = true; + copy(std::env!("SPARSE_BUILD_LIBPCAP"), &libpcap_src, &options); + + let dst = cmake::Config::new(&libpcap_src) + .define("BUILD_SHARED_LIBS", "OFF") + .define("BUILD_WITH_LIBNL", "OFF") + .define("DISABLE_BLUETOOTH", "ON") + .define("DISABLE_DAG", "ON") + .define("DISABLE_DBUS", "ON") + .define("DISABLE_DPDK", "ON") + .define("DISABLE_LINUX_USBMON", "ON") + .define("DISABLE_NETMAP", "ON") + .define("DISABLE_RDMA", "ON") + .define("DISABLE_SEPTEL", "ON") + .define("DISABLE_SNF", "ON") + .define("DISABLE_TC", "ON") + .define("PCAP_TYPE", "linux") + .build(); + + // panic!("hahahahah test {}", dst.display()); + println!("cargo:rustc-link-search=native={}/lib", dst.display()); + println!("cargo:rustc-link-search=native={}/lib64", dst.display()); + println!("cargo:rustc-link-lib=static=pcap"); + } } diff --git a/sparse-05/sparse-05-client/Cargo.toml b/sparse-05/sparse-05-client/Cargo.toml index 5f33293..2338b36 100644 --- a/sparse-05/sparse-05-client/Cargo.toml +++ b/sparse-05/sparse-05-client/Cargo.toml @@ -10,10 +10,12 @@ ecies-ed25519 = { version = "0.5.1", features = ["serde"] } ed25519-dalek = "1.0.1" libc = "0.2.147" rand = "0.7" -raw_tty = "0.1.0" rmp-serde = "1.1.2" serde = { version = "1.0.188", features = ["derive"] } sparse-05-common = { version = "0.1.0", path = "../sparse-05-common" } structopt = { version = "0.3.26", features = ["paw"] } tempfile = "3.8.0" tokio = { version = "1.32.0", features = ["io-std", "net", "fs", "macros", "rt"] } + +[target.'cfg(unix)'.dependencies] +raw_tty = "0.1.0" diff --git a/sparse-05/sparse-05-server/Cargo.toml b/sparse-05/sparse-05-server/Cargo.toml index fd51161..842885c 100644 --- a/sparse-05/sparse-05-server/Cargo.toml +++ b/sparse-05/sparse-05-server/Cargo.toml @@ -16,13 +16,13 @@ catconf = "0.1.2" sparse-05-common = { version = "0.1.0", path = "../sparse-05-common" } ecies-ed25519 = { version = "0.5.1", features = ["serde"] } packets = { path = "../../packets" } - -[target.'cfg(unix)'.dependencies] -pcap-sys = { path = "../../pcap-sys" } +pcap-sys = { path = "../../pcap-sys", optional = true } [build-dependencies] cc = "1.0" [features] +default = ["pcap"] docker-breakout = [] exit = [] +pcap = ["dep:pcap-sys"] diff --git a/sparse-05/sparse-05-server/src/interface.rs b/sparse-05/sparse-05-server/src/interface.rs index e0c7f3e..100b209 100644 --- a/sparse-05/sparse-05-server/src/interface.rs +++ b/sparse-05/sparse-05-server/src/interface.rs @@ -9,7 +9,7 @@ use packets::{self, EthernetPkt}; use sparse_05_common::messages::TransportType; pub enum Interface { - #[cfg(target_os = "linux")] + #[cfg(feature = "pcap")] RawUdp(pcap_sys::Interface, u16), Udp(UdpSocket, u16), }