Skip to content

Commit

Permalink
223 implement assetsread and assetsread bytes (#225)
Browse files Browse the repository at this point in the history
* Impl, test, and docs.

* read binary impl, test, and docs.

* update lib test.

* Fix test?

* Might be faster.
  • Loading branch information
hulto authored Jul 22, 2023
1 parent bc4c246 commit 24a7a08
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 1 deletion.
10 changes: 10 additions & 0 deletions docs/_docs/user-guide/eldritch.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,16 @@ deploy_agent()

The <b>assets.list</b> method returns a list of asset names that the agent is aware of.

### assets.read_binary
`assets.read_binary(src: str) -> List<int>`

The <b>assets.read_binary</b> method returns a list of u32 numbers representing the asset files bytes.

### assets.read
`assets.read(src: str) -> str`

The <b>assets.read</b> method returns a UTF-8 string representation of the asset file.

---

## File
Expand Down
11 changes: 11 additions & 0 deletions implants/lib/eldritch/src/assets.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
mod copy_impl;
mod list_impl;
mod read_binary_impl;
mod read_impl;

use allocative::Allocative;
use derive_more::Display;
Expand Down Expand Up @@ -68,4 +70,13 @@ fn methods(builder: &mut MethodsBuilder) {
if false { println!("Ignore unused this var. _this isn't allowed by starlark. {:?}", this); }
list_impl::list()
}
fn read_binary(this: AssetsLibrary, src: String) -> anyhow::Result<Vec<u32>> {
if false { println!("Ignore unused this var. _this isn't allowed by starlark. {:?}", this); }
read_binary_impl::read_binary(src)
}
fn read(this: AssetsLibrary, src: String) -> anyhow::Result<String> {
if false { println!("Ignore unused this var. _this isn't allowed by starlark. {:?}", this); }
read_impl::read(src)
}

}
27 changes: 27 additions & 0 deletions implants/lib/eldritch/src/assets/read_binary_impl.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
use anyhow::Result;

pub fn read_binary(src: String) -> Result<Vec<u32>> {
let src_file_bytes = 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.")),
};
let result = src_file_bytes.iter().map(|x| *x as u32).collect::<Vec<u32>>();
// let mut result = Vec::new();
// for byt: Vec<u32>e in src_file_bytes.iter() {
// result.push(*byte as u32);
// }
Ok(result)
}


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

#[test]
fn test_assets_read_binary() -> anyhow::Result<()>{
let res = read_binary("print/main.eld".to_string())?;
assert_eq!(res, [112,114,105,110,116,40,34,84,104,105,115,32,115,99,114,105,112,116,32,106,117,115,116,32,112,114,105,110,116,115,34,41]);
Ok(())
}
}
26 changes: 26 additions & 0 deletions implants/lib/eldritch/src/assets/read_impl.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
use anyhow::Result;

pub fn read(src: String) -> Result<String> {
let src_file_bytes = 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.")),
};
let mut result = String::new();
for byte in src_file_bytes.iter() {
result.push(*byte as char);
}
Ok(result)
}


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

#[test]
fn test_assets_read() -> anyhow::Result<()>{
let res = read("print/main.eld".to_string())?;
assert_eq!(res, r#"print("This script just prints")"#);
Ok(())
}
}
2 changes: 1 addition & 1 deletion implants/lib/eldritch/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ dir(file) == ["append", "compress", "copy", "download", "exists", "hash", "is_di
dir(process) == ["kill", "list", "name"]
dir(sys) == ["dll_inject", "exec", "get_ip", "get_os", "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"]
dir(assets) == ["copy","list","read","read_binary"]
"#,
);
}
Expand Down

0 comments on commit 24a7a08

Please sign in to comment.