Skip to content

Commit 2a86d51

Browse files
authored
Split stdlib into seperate impl files. (#1538)
* Split stdlib into seperate impl files. * fmt
1 parent 7ffd027 commit 2a86d51

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

73 files changed

+3482
-2806
lines changed
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
use alloc::string::String;
2+
use alloc::sync::Arc;
3+
use alloc::vec::Vec;
4+
5+
use crate::TaskWrapper;
6+
7+
#[cfg(feature = "stdlib")]
8+
use crate::agent::Agent;
9+
#[cfg(feature = "stdlib")]
10+
use pb::c2;
11+
12+
pub fn claim_tasks(agent: Arc<dyn Agent>) -> Result<Vec<TaskWrapper>, String> {
13+
let req = c2::ClaimTasksRequest { beacon: None };
14+
let resp = agent.claim_tasks(req)?;
15+
Ok(resp.tasks.into_iter().map(TaskWrapper).collect())
16+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
use alloc::string::String;
2+
use alloc::sync::Arc;
3+
use alloc::vec::Vec;
4+
5+
#[cfg(feature = "stdlib")]
6+
use crate::agent::Agent;
7+
#[cfg(feature = "stdlib")]
8+
use pb::c2;
9+
10+
pub fn fetch_asset(agent: Arc<dyn Agent>, name: String) -> Result<Vec<u8>, String> {
11+
let req = c2::FetchAssetRequest { name };
12+
agent.fetch_asset(req)
13+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
use alloc::string::String;
2+
use alloc::sync::Arc;
3+
4+
#[cfg(feature = "stdlib")]
5+
use crate::agent::Agent;
6+
7+
pub fn get_callback_interval(agent: Arc<dyn Agent>) -> Result<i64, String> {
8+
agent.get_callback_interval().map(|i| i as i64)
9+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
use alloc::collections::BTreeMap;
2+
use alloc::string::String;
3+
use alloc::sync::Arc;
4+
use eldritch_core::Value;
5+
6+
#[cfg(feature = "stdlib")]
7+
use crate::agent::Agent;
8+
9+
pub fn get_config(agent: Arc<dyn Agent>) -> Result<BTreeMap<String, Value>, String> {
10+
let config = agent.get_config()?;
11+
let mut result = BTreeMap::new();
12+
for (k, v) in config {
13+
// Try to parse numbers, otherwise keep as string
14+
if let Ok(i) = v.parse::<i64>() {
15+
result.insert(k, Value::Int(i));
16+
} else if let Ok(b) = v.parse::<bool>() {
17+
result.insert(k, Value::Bool(b));
18+
} else {
19+
result.insert(k, Value::String(v));
20+
}
21+
}
22+
Ok(result)
23+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
use alloc::string::String;
2+
use alloc::sync::Arc;
3+
4+
#[cfg(feature = "stdlib")]
5+
use crate::agent::Agent;
6+
7+
pub fn get_transport(agent: Arc<dyn Agent>) -> Result<String, String> {
8+
agent.get_transport()
9+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
use alloc::string::String;
2+
use alloc::sync::Arc;
3+
use alloc::vec::Vec;
4+
5+
use crate::TaskWrapper;
6+
7+
#[cfg(feature = "stdlib")]
8+
use crate::agent::Agent;
9+
10+
pub fn list_tasks(agent: Arc<dyn Agent>) -> Result<Vec<TaskWrapper>, String> {
11+
let tasks = agent.list_tasks()?;
12+
Ok(tasks.into_iter().map(TaskWrapper).collect())
13+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
use alloc::string::String;
2+
use alloc::sync::Arc;
3+
use alloc::vec::Vec;
4+
5+
#[cfg(feature = "stdlib")]
6+
use crate::agent::Agent;
7+
8+
pub fn list_transports(agent: Arc<dyn Agent>) -> Result<Vec<String>, String> {
9+
agent.list_transports()
10+
}

implants/lib/eldritchv2/stdlib/eldritch-libagent/src/std.rs renamed to implants/lib/eldritchv2/stdlib/eldritch-libagent/src/std/mod.rs

Lines changed: 32 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,22 @@ use crate::{CredentialWrapper, FileWrapper, ProcessListWrapper, TaskWrapper};
1010

1111
#[cfg(feature = "stdlib")]
1212
use crate::agent::Agent;
13-
#[cfg(feature = "stdlib")]
14-
use pb::c2;
13+
14+
pub mod claim_tasks_impl;
15+
pub mod fetch_asset_impl;
16+
pub mod get_callback_interval_impl;
17+
pub mod get_config_impl;
18+
pub mod get_transport_impl;
19+
pub mod list_tasks_impl;
20+
pub mod list_transports_impl;
21+
pub mod report_credential_impl;
22+
pub mod report_file_impl;
23+
pub mod report_process_list_impl;
24+
pub mod report_task_output_impl;
25+
pub mod set_callback_interval_impl;
26+
pub mod set_callback_uri_impl;
27+
pub mod stop_task_impl;
28+
pub mod terminate_impl;
1529

1630
// We need manual Debug impl, and we need to put the macro on the struct.
1731
#[eldritch_library_impl(AgentLibrary)]
@@ -36,103 +50,65 @@ impl StdAgentLibrary {
3650

3751
impl AgentLibrary for StdAgentLibrary {
3852
fn get_config(&self) -> Result<BTreeMap<String, Value>, String> {
39-
let config = self.agent.get_config()?;
40-
let mut result = BTreeMap::new();
41-
for (k, v) in config {
42-
// Try to parse numbers, otherwise keep as string
43-
if let Ok(i) = v.parse::<i64>() {
44-
result.insert(k, Value::Int(i));
45-
} else if let Ok(b) = v.parse::<bool>() {
46-
result.insert(k, Value::Bool(b));
47-
} else {
48-
result.insert(k, Value::String(v));
49-
}
50-
}
51-
Ok(result)
53+
get_config_impl::get_config(self.agent.clone())
5254
}
5355

5456
fn _terminate_this_process_clowntown(&self) -> Result<(), String> {
55-
::std::process::exit(0);
57+
terminate_impl::terminate()
5658
}
5759

5860
fn set_callback_interval(&self, interval: i64) -> Result<(), String> {
59-
self.agent.set_callback_interval(interval as u64)
61+
set_callback_interval_impl::set_callback_interval(self.agent.clone(), interval)
6062
}
6163

6264
// Interactivity
6365
fn fetch_asset(&self, name: String) -> Result<Vec<u8>, String> {
64-
let req = c2::FetchAssetRequest { name };
65-
self.agent.fetch_asset(req)
66+
fetch_asset_impl::fetch_asset(self.agent.clone(), name)
6667
}
6768

6869
fn report_credential(&self, credential: CredentialWrapper) -> Result<(), String> {
69-
let req = c2::ReportCredentialRequest {
70-
task_id: self.task_id,
71-
credential: Some(credential.0),
72-
};
73-
self.agent.report_credential(req).map(|_| ())
70+
report_credential_impl::report_credential(self.agent.clone(), self.task_id, credential)
7471
}
7572

7673
fn report_file(&self, file: FileWrapper) -> Result<(), String> {
77-
let req = c2::ReportFileRequest {
78-
task_id: self.task_id,
79-
chunk: Some(file.0),
80-
};
81-
self.agent.report_file(req).map(|_| ())
74+
report_file_impl::report_file(self.agent.clone(), self.task_id, file)
8275
}
8376

8477
fn report_process_list(&self, list: ProcessListWrapper) -> Result<(), String> {
85-
let req = c2::ReportProcessListRequest {
86-
task_id: self.task_id,
87-
list: Some(list.0),
88-
};
89-
self.agent.report_process_list(req).map(|_| ())
78+
report_process_list_impl::report_process_list(self.agent.clone(), self.task_id, list)
9079
}
9180

9281
fn report_task_output(&self, output: String, error: Option<String>) -> Result<(), String> {
93-
let task_error = error.map(|msg| c2::TaskError { msg });
94-
let output_msg = c2::TaskOutput {
95-
id: self.task_id,
96-
output,
97-
error: task_error,
98-
exec_started_at: None,
99-
exec_finished_at: None,
100-
};
101-
let req = c2::ReportTaskOutputRequest {
102-
output: Some(output_msg),
103-
};
104-
self.agent.report_task_output(req).map(|_| ())
82+
report_task_output_impl::report_task_output(self.agent.clone(), self.task_id, output, error)
10583
}
10684

10785
fn claim_tasks(&self) -> Result<Vec<TaskWrapper>, String> {
108-
let req = c2::ClaimTasksRequest { beacon: None };
109-
let resp = self.agent.claim_tasks(req)?;
110-
Ok(resp.tasks.into_iter().map(TaskWrapper).collect())
86+
claim_tasks_impl::claim_tasks(self.agent.clone())
11187
}
11288

11389
// Agent Configuration
11490
fn get_transport(&self) -> Result<String, String> {
115-
self.agent.get_transport()
91+
get_transport_impl::get_transport(self.agent.clone())
11692
}
11793

11894
fn list_transports(&self) -> Result<Vec<String>, String> {
119-
self.agent.list_transports()
95+
list_transports_impl::list_transports(self.agent.clone())
12096
}
97+
12198
fn set_callback_uri(&self, uri: String) -> Result<(), String> {
122-
self.agent.set_callback_uri(uri)
99+
set_callback_uri_impl::set_callback_uri(self.agent.clone(), uri)
123100
}
124101

125102
fn get_callback_interval(&self) -> Result<i64, String> {
126-
self.agent.get_callback_interval().map(|i| i as i64)
103+
get_callback_interval_impl::get_callback_interval(self.agent.clone())
127104
}
128105

129106
// Task Management
130107
fn list_tasks(&self) -> Result<Vec<TaskWrapper>, String> {
131-
let tasks = self.agent.list_tasks()?;
132-
Ok(tasks.into_iter().map(TaskWrapper).collect())
108+
list_tasks_impl::list_tasks(self.agent.clone())
133109
}
134110

135111
fn stop_task(&self, task_id: i64) -> Result<(), String> {
136-
self.agent.stop_task(task_id)
112+
stop_task_impl::stop_task(self.agent.clone(), task_id)
137113
}
138114
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
use alloc::string::String;
2+
use alloc::sync::Arc;
3+
4+
use crate::CredentialWrapper;
5+
6+
#[cfg(feature = "stdlib")]
7+
use crate::agent::Agent;
8+
#[cfg(feature = "stdlib")]
9+
use pb::c2;
10+
11+
pub fn report_credential(
12+
agent: Arc<dyn Agent>,
13+
task_id: i64,
14+
credential: CredentialWrapper,
15+
) -> Result<(), String> {
16+
let req = c2::ReportCredentialRequest {
17+
task_id,
18+
credential: Some(credential.0),
19+
};
20+
agent.report_credential(req).map(|_| ())
21+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
use alloc::string::String;
2+
use alloc::sync::Arc;
3+
4+
use crate::FileWrapper;
5+
6+
#[cfg(feature = "stdlib")]
7+
use crate::agent::Agent;
8+
#[cfg(feature = "stdlib")]
9+
use pb::c2;
10+
11+
pub fn report_file(agent: Arc<dyn Agent>, task_id: i64, file: FileWrapper) -> Result<(), String> {
12+
let req = c2::ReportFileRequest {
13+
task_id,
14+
chunk: Some(file.0),
15+
};
16+
agent.report_file(req).map(|_| ())
17+
}

0 commit comments

Comments
 (0)