Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

156 feature golem add embedded files for dropper functionality #162

Merged
Merged
Show file tree
Hide file tree
Changes from 11 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,9 @@ build/**
# Credentials
.creds/**
implants/imix/imix-test-config.json
implants/golem/assets/*


!implants/golem/assets/.gitkeep

implants/golem/embed_files_golem_prod/*
4 changes: 2 additions & 2 deletions docs/_docs/user-guide/golem.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ You can leverage the power of Eldritch with minimal exposure in the system proce
```bash
git clone [email protected]:KCarretto/realm.git
cd realm/implants/golem
cargo run
cargo run -- -i
# - or -
../target/debug/golem working_dir/tomes/hello_world.tome
../target/debug/golem ../../tests/golem_cli_test/tomes/hello_world.tome
```
1 change: 1 addition & 0 deletions implants/eldritch/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ tera = "1.17.1"
gazebo = "0.8.1"
nix = "0.26.1"
eval = "0.4.3"
rust-embed="6.6.0"

[dependencies.windows-sys]
version = "0.45.0"
Expand Down
70 changes: 70 additions & 0 deletions implants/eldritch/src/assets.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
mod copy_impl;
mod list_impl;

use derive_more::Display;

use starlark::environment::{Methods, MethodsBuilder, MethodsStatic};
use starlark::values::none::NoneType;
use starlark::values::{StarlarkValue, Value, UnpackValue, ValueLike, ProvidesStaticType};
use starlark::{starlark_type, starlark_simple_value, starlark_module};

use serde::{Serialize,Serializer};
use rust_embed::RustEmbed;

#[cfg(debug_assertions)]
#[derive(RustEmbed)]
#[folder = "../../tests/embedded_files_test"]
pub struct Asset;
hulto marked this conversation as resolved.
Show resolved Hide resolved

#[cfg(not(debug_assertions))]
#[derive(RustEmbed)]
#[folder = "../../implants/golem/embed_files_golem_prod"]
pub struct Asset;
hulto marked this conversation as resolved.
Show resolved Hide resolved
hulto marked this conversation as resolved.
Show resolved Hide resolved


#[derive(Copy, Clone, Debug, PartialEq, Display, ProvidesStaticType)]
#[display(fmt = "AssetsLibrary")]
pub struct AssetsLibrary();
starlark_simple_value!(AssetsLibrary);

impl<'v> StarlarkValue<'v> for AssetsLibrary {
starlark_type!("assets_library");

fn get_methods() -> Option<&'static Methods> {
static RES: MethodsStatic = MethodsStatic::new();
RES.methods(methods)
}
}

impl Serialize for AssetsLibrary {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
serializer.serialize_none()
}
}

impl<'v> UnpackValue<'v> for AssetsLibrary {
fn expected() -> String {
AssetsLibrary::get_type_value_static().as_str().to_owned()
}

fn unpack_value(value: Value<'v>) -> Option<Self> {
Some(*value.downcast_ref::<AssetsLibrary>().unwrap())
hulto marked this conversation as resolved.
Show resolved Hide resolved
}
}

// This is where all of the "assets.X" impl methods are bound
#[starlark_module]
fn methods(builder: &mut MethodsBuilder) {
fn copy(this: AssetsLibrary, src: String, dest: String) -> anyhow::Result<NoneType> {
if false { println!("Ignore unused this var. _this isn't allowed by starlark. {:?}", this); }
copy_impl::copy(src, dest)?;
Ok(NoneType{})
}
fn list(this: AssetsLibrary) -> anyhow::Result<Vec<String>> {
if false { println!("Ignore unused this var. _this isn't allowed by starlark. {:?}", this); }
list_impl::list()
}
}
41 changes: 41 additions & 0 deletions implants/eldritch/src/assets/copy_impl.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
use std::fs;
use anyhow::Result;

pub fn copy(src: String, dst: String) -> Result<()> {
let src_file = match super::Asset::get(src.as_str()) {
Some(local_src_file) => local_src_file.data,
None => return Err(anyhow::anyhow!("Embedded file {src} not found.")),
};

match fs::write(dst, src_file) {
Ok(_) => Ok(()),
Err(local_err) => Err(local_err.into()),
}
}


#[cfg(test)]
mod tests {
use super::*;
use std::io::prelude::*;
use tempfile::NamedTempFile;

#[test]
fn test_embedded_copy() -> anyhow::Result<()>{

// Create files
let mut tmp_file_dst = NamedTempFile::new()?;
let path_dst = String::from(tmp_file_dst.path().to_str().unwrap());

// Run our code
copy("exec_script/hello_word.sh".to_string(), path_dst)?;

// Read
let mut contents = String::new();
tmp_file_dst.read_to_string(&mut contents)?;
// Compare
assert!(contents.contains("hello from an embedded shell script"));

Ok(())
}
}
25 changes: 25 additions & 0 deletions implants/eldritch/src/assets/list_impl.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
use anyhow::Result;

pub fn list() -> Result<Vec<String>> {
let mut res: Vec<String> = Vec::new();
for file_path in super::Asset::iter() {
res.push(file_path.to_string());
}

Ok(res)
}


#[cfg(test)]
mod tests {
use super::*;

#[test]
fn test_assets_list() -> anyhow::Result<()>{
let res_all_embedded_files = list()?;

assert_eq!(res_all_embedded_files, ["exec_script/hello_word.sh", "exec_script/main.eld", "print/main.eld"]);

Ok(())
}
}
24 changes: 8 additions & 16 deletions implants/eldritch/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ pub mod file;
pub mod process;
pub mod sys;
pub mod pivot;
pub mod assets;

use serde_json::Map;
use starlark::collections::SmallMap;
Expand All @@ -15,13 +16,17 @@ use starlark::values::{Value, AllocValue};
use file::FileLibrary;
use process::ProcessLibrary;
use sys::SysLibrary;
use assets::AssetsLibrary;
use pivot::PivotLibrary;

pub fn get_eldritch() -> anyhow::Result<Globals> {
#[starlark_module]
fn eldritch(builder: &mut GlobalsBuilder) {
const file: FileLibrary = FileLibrary();
const process: ProcessLibrary = ProcessLibrary();
const sys: SysLibrary = SysLibrary();
const pivot: PivotLibrary = PivotLibrary();
const assets: AssetsLibrary = AssetsLibrary();
}

let globals = GlobalsBuilder::extended().with(eldritch).build();
Expand Down Expand Up @@ -114,35 +119,22 @@ mod tests {
use std::thread;

use super::*;
use starlark::environment::{GlobalsBuilder};
use starlark::{starlark_module};
use starlark::assert::Assert;
use tempfile::NamedTempFile;

use super::file::FileLibrary;
use super::process::ProcessLibrary;
use super::sys::SysLibrary;
use super::pivot::PivotLibrary;

// just checks dir...
#[test]
fn test_library_bindings() {
#[starlark_module]
fn globals(builder: &mut GlobalsBuilder) {
const file: FileLibrary = FileLibrary();
const process: ProcessLibrary = ProcessLibrary();
const sys: SysLibrary = SysLibrary();
const pivot: PivotLibrary = PivotLibrary();
}

let globals = get_eldritch().unwrap();
hulto marked this conversation as resolved.
Show resolved Hide resolved
let mut a = Assert::new();
a.globals_add(globals);
a.globals(globals);
a.all_true(
r#"
dir(file) == ["append", "compress", "copy", "download", "exists", "hash", "is_dir", "is_file", "mkdir", "read", "remove", "rename", "replace", "replace_all", "template", "timestomp", "write"]
dir(process) == ["kill", "list", "name"]
dir(sys) == ["dll_inject", "exec", "is_linux", "is_macos", "is_windows", "shell"]
dir(pivot) == ["arp_scan", "bind_proxy", "ncat", "port_forward", "port_scan", "smb_exec", "ssh_exec", "ssh_password_spray"]
dir(assets) == ["copy","list"]
"#,
);
}
Expand Down
1 change: 1 addition & 0 deletions implants/golem/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ gazebo.version = "0.8.1"
itertools = "0.10"
thiserror = "1.0.30"
lsp-types = "0.93.0"
rust-embed = { version = "6.6.0" }

[dev-dependencies]
assert_cmd = "2.0.6"
Expand Down
1 change: 1 addition & 0 deletions implants/golem/embed_files_golem_prod/main.eld
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
print("EMO_KIDS_RULE")
Loading