Skip to main content

mythic_facade/
mythic_facade.rs

1//! Example demonstrating the `#![no_std]` mythic-c2 library — full agent lifecycle.
2//!
3//! The library crate compiles as `#![no_std]` (`cargo build`).  This example
4//! binary links std only for `fn main()` — the API surface shown here uses
5//! only `alloc` / `core` types and works identically in a true no_std implant.
6//!
7//! **Note:** `HttpC2` is a **stub** that returns empty strings.  Running this
8//! example will panic at `.unwrap()` because the decode step receives an empty
9//! response.  A real transport must return valid base64-encoded Mythic wire
10//! packets.  See the unit tests for working encode/decode roundtrips.
11
12use mythic::{Aes256HmacCrypto, C2Transport, MythicAgent, MythicError, TaskResponse};
13use uuid::Uuid;
14
15// ── C2 transport stub ──────────────────────────────────────
16
17/// A fake HTTP transport for demonstration purposes.
18/// In a real implant this would make actual HTTP(S) requests.
19struct HttpC2 {
20    key_b64: Option<String>,
21}
22
23impl C2Transport for HttpC2 {
24    fn get_aes_psk(&self) -> Option<String> {
25        self.key_b64.clone()
26    }
27
28    // 加密时覆盖 random_iv 提供真随机 IV(默认返回 Crypto 错误)
29    fn random_iv(&self) -> Result<[u8; 16], MythicError> {
30        // 实际部署: getrandom::getrandom(&mut iv)?; Ok(iv)
31        Ok([0u8; 16]) // 演示用零 IV
32    }
33
34    fn checkin(&self, pkt: &str) -> Result<String, MythicError> {
35        eprintln!("[HTTP] checkin  → {} bytes", pkt.len());
36        // Real impl: POST to <server>/agent_message
37        Ok(String::new())
38    }
39
40    fn get_tasking(&self, pkt: &str) -> Result<String, MythicError> {
41        eprintln!("[HTTP] get_task → {} bytes", pkt.len());
42        // Real impl: GET <server>/agent_message with base64 body
43        Ok(String::new())
44    }
45
46    fn post_response(&self, pkt: &str) -> Result<String, MythicError> {
47        eprintln!("[HTTP] post_resp → {} bytes", pkt.len());
48        Ok(String::new())
49    }
50}
51
52fn main() {
53    let payload_uuid = Uuid::parse_str("f0f0f0f0-1111-2222-3333-444444444444").unwrap();
54
55    // ── Plaintext checkin ─────────────────────────────────
56    {
57        let c2 = HttpC2 { key_b64: None };
58        let agent = MythicAgent::easy_checkin(
59            payload_uuid,
60            &c2,
61            vec!["10.0.0.1".into()],
62            Some("linux".into()),
63            Some("root".into()),
64            Some("web01".into()),
65            Some(1337),
66            Some("x86_64".into()),
67            None, None, None, None, None, None,
68        )
69        .unwrap();
70        println!("Plaintext callback UUID: {}", agent.callback_uuid());
71    }
72
73    // ── Static-key checkin ────────────────────────────────
74    {
75        let key = Aes256HmacCrypto::new([0xAB; 32]).key_b64();
76        let c2 = HttpC2 { key_b64: Some(key) };
77        let agent = MythicAgent::easy_checkin(
78            payload_uuid,
79            &c2,
80            vec!["192.168.1.100".into()],
81            Some("windows".into()),
82            Some("admin".into()),
83            Some("DESKTOP-XYZ".into()),
84            Some(2048),
85            Some("x86_64".into()),
86            None, None, None, None, None, None,
87        )
88        .unwrap();
89        println!("Static-key callback UUID: {}", agent.callback_uuid());
90    }
91
92    // ── Full lifecycle: get_tasking → post_response ───────
93    {
94        let c2 = HttpC2 { key_b64: None };
95
96        // 1. Checkin
97        let agent = MythicAgent::easy_checkin(
98            payload_uuid,
99            &c2,
100            vec!["10.0.0.2".into()],
101            Some("linux".into()),
102            Some("operator".into()),
103            Some("implant01".into()),
104            Some(9999),
105            Some("aarch64".into()),
106            None, None, None, None, None, None,
107        )
108        .unwrap();
109
110        // 2. Poll for tasks
111        match agent.get_tasking(1, &c2) {
112            Ok(resp) => {
113                for task in &resp.tasks {
114                    println!("Received task {}: {}", task.id, task.command);
115
116                    // 3. Execute and respond
117                    let _ = agent.post_response(
118                        vec![TaskResponse::completed(task.id, "task executed successfully")],
119                        &c2,
120                    );
121                }
122            }
123            Err(e) => eprintln!("get_tasking failed: {e}"),
124        }
125    }
126
127    println!("All demo scenarios complete.");
128}