Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
 
time.now (#404)

* time.now

* Add library test
  • Loading branch information
jabbate19 authored Dec 17, 2023
1 parent 5d43715 commit 9886a2a
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 1 deletion.
4 changes: 4 additions & 0 deletions docs/_docs/user-guide/eldritch.md
Original file line number Diff line number Diff line change
Expand Up @@ -778,6 +778,10 @@ crypto.to_json({"foo": "bar"})

### time.sleep

`time.now() -> int`

The <b>time.now</b> method returns the time since UNIX EPOCH (Jan 01 1970). This uses the local system time.

`time.sleep(secs: float)`

The <b>time.sleep</b> method sleeps the task for the given number of seconds.
2 changes: 1 addition & 1 deletion implants/lib/eldritch/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ dir(sys) == ["dll_inject", "dll_reflect", "exec", "get_env", "get_ip", "get_os",
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"]
dir(time) == ["now", "sleep"]
"#,
);
}
Expand Down
5 changes: 5 additions & 0 deletions implants/lib/eldritch/src/time.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
mod sleep_impl;
mod now_impl;

use allocative::Allocative;
use derive_more::Display;
Expand Down Expand Up @@ -48,6 +49,10 @@ impl<'v> UnpackValue<'v> for TimeLibrary {
#[starlark_module]
#[rustfmt::skip]
fn methods(builder: &mut MethodsBuilder) {
fn now<'v>(this: TimeLibrary) -> anyhow::Result<u64> {
if false { println!("Ignore unused this var. _this isn't allowed by starlark. {:?}", this); }
now_impl::now()
}
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);
Expand Down
19 changes: 19 additions & 0 deletions implants/lib/eldritch/src/time/now_impl.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
use std::time::{SystemTime, UNIX_EPOCH};
use anyhow::Result;

pub fn now() -> Result<u64> {
Ok(SystemTime::now().duration_since(UNIX_EPOCH)?.as_secs())
}

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

#[test]
fn test_now() {
let now_out = now().unwrap() as i64;
let now_test = Local::now().timestamp();
assert!((now_out - now_test).abs() <= 250);
}
}

0 comments on commit 9886a2a

Please sign in to comment.