Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
8 changes: 8 additions & 0 deletions docs/_docs/user-guide/eldritch.md
Original file line number Diff line number Diff line change
Expand Up @@ -773,3 +773,11 @@ The <b>crypto.to_json</b> method converts given type to JSON text.
crypto.to_json({"foo": "bar"})
"{\"foo\":\"bar\"}"
```

## Time

### time.sleep

`time.sleep(secs: float)`

The <b>time.sleep</b> method sleeps the task for the given number of seconds.
4 changes: 4 additions & 0 deletions implants/lib/eldritch/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ pub mod sys;
pub mod pivot;
pub mod assets;
pub mod crypto;
pub mod time;

use std::sync::mpsc::Sender;
use serde_json::Map;
Expand All @@ -21,6 +22,7 @@ use sys::SysLibrary;
use assets::AssetsLibrary;
use pivot::PivotLibrary;
use crate::crypto::CryptoLibrary;
use time::TimeLibrary;

pub fn get_eldritch() -> anyhow::Result<Globals> {
#[starlark_module]
Expand All @@ -31,6 +33,7 @@ pub fn get_eldritch() -> anyhow::Result<Globals> {
const pivot: PivotLibrary = PivotLibrary();
const assets: AssetsLibrary = AssetsLibrary();
const crypto: CryptoLibrary = CryptoLibrary();
const time: TimeLibrary = TimeLibrary();
}

let globals = GlobalsBuilder::extended_by(
Expand Down Expand Up @@ -194,6 +197,7 @@ dir(sys) == ["dll_inject", "exec", "get_env", "get_ip", "get_os", "get_pid", "ge
dir(pivot) == ["arp_scan", "bind_proxy", "ncat", "port_forward", "port_scan", "smb_exec", "ssh_copy", "ssh_exec", "ssh_password_spray"]
dir(assets) == ["copy","list","read","read_binary"]
dir(crypto) == ["aes_decrypt_file", "aes_encrypt_file", "decode_b64", "encode_b64", "from_json", "hash_file", "to_json"]
dir(time) == ["sleep"]
"#,
);
}
Expand Down
56 changes: 56 additions & 0 deletions implants/lib/eldritch/src/time.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
mod sleep_impl;

use allocative::Allocative;
use derive_more::Display;

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

use serde::{Serialize,Serializer};

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

#[allow(non_upper_case_globals)]
#[starlark_value(type = "sys_library")]
impl<'v> StarlarkValue<'v> for TimeLibrary {

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

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

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

fn unpack_value(value: Value<'v>) -> Option<Self> {
Some(*value.downcast_ref::<TimeLibrary>().unwrap())
}
}

// This is where all of the "Time.X" impl methods are bound
#[starlark_module]
fn methods(builder: &mut MethodsBuilder) {
fn sleep<'v>(this: TimeLibrary, secs: f64) -> anyhow::Result<NoneType> {
if false { println!("Ignore unused this var. _this isn't allowed by starlark. {:?}", this); }
sleep_impl::sleep(secs);
Ok(NoneType{})
}
}
17 changes: 17 additions & 0 deletions implants/lib/eldritch/src/time/sleep_impl.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
pub fn sleep(seconds: f64) {
std::thread::sleep(std::time::Duration::from_secs_f64(seconds));
}

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

#[test]
fn test_sleep() {
let before = Local::now();
sleep(5.0);
let after = Local::now();
assert!((after.signed_duration_since(before).num_milliseconds() - 5000).abs() <= 250);
}
}