myph-v2~ rust shellcode loader with evasive capabilities |
| git clone https://github.com/djnnvx/myph-v2.git |
| log | files | refs | archive | README |
code_context.rs (3307B)
1 pub struct CodeContext { 2 technique_name: String, 3 generation_stage: String, 4 } 5 6 impl CodeContext { 7 pub fn new(technique_name: &str, stage: &str) -> Self { 8 Self { 9 technique_name: technique_name.to_string(), 10 generation_stage: stage.to_string(), 11 } 12 } 13 14 pub fn add_context_marker(&self, code: &str) -> String { 15 let marker = self.generate_marker(); 16 format!("{}\n{}", marker, code) 17 } 18 19 fn generate_marker(&self) -> String { 20 format!( 21 "// Generated by: {} ({})", 22 self.technique_name, self.generation_stage 23 ) 24 } 25 26 pub fn add_stage_markers(technique_name: &str, code: &str) -> String { 27 let mut result = String::new(); 28 29 result.push_str(&format!("// === Technique: {} ===\n", technique_name)); 30 31 let lines: Vec<&str> = code.lines().collect(); 32 let mut imports_added = false; 33 let mut const_added = false; 34 let mut func_added = false; 35 36 for line in lines { 37 let trimmed = line.trim(); 38 39 if !imports_added && (trimmed.starts_with("use ") || trimmed.starts_with("extern ")) { 40 result.push_str("// --- Imports & Declarations ---\n"); 41 imports_added = true; 42 } 43 44 if !const_added && trimmed.starts_with("const ") { 45 result.push_str("// --- Constants ---\n"); 46 const_added = true; 47 } 48 49 if !func_added && (trimmed.starts_with("pub fn ") || trimmed.starts_with("fn ")) { 50 result.push_str("// --- Main Implementation ---\n"); 51 func_added = true; 52 } 53 54 result.push_str(line); 55 result.push('\n'); 56 } 57 58 result 59 } 60 } 61 62 #[cfg(test)] 63 mod tests { 64 use super::*; 65 66 #[test] 67 fn test_context_marker_generation() { 68 let context = CodeContext::new("CreateFiber", "static_generation"); 69 let code = "fn test() {}"; 70 let result = context.add_context_marker(code); 71 72 assert!(result.contains("// Generated by: CreateFiber (static_generation)")); 73 assert!(result.contains("fn test() {}")); 74 } 75 76 #[test] 77 fn test_stage_markers() { 78 let code = r#" 79 use std::ptr; 80 81 const MEM_COMMIT: u32 = 0x1000; 82 83 pub fn execute_shellcode(shellcode: &[u8]) { 84 unsafe { 85 VirtualAlloc(ptr::null_mut(), shellcode.len(), MEM_COMMIT, PAGE_READWRITE); 86 } 87 } 88 "#; 89 90 let result = CodeContext::add_stage_markers("CreateFiber", code); 91 92 assert!(result.contains("// === Technique: CreateFiber ===")); 93 assert!(result.contains("// --- Imports & Declarations ---")); 94 assert!(result.contains("// --- Constants ---")); 95 assert!(result.contains("// --- Main Implementation ---")); 96 } 97 98 #[test] 99 fn test_marker_order() { 100 let code = r#" 101 use std::ptr; 102 const X: u32 = 1; 103 pub fn test() {} 104 "#; 105 106 let result = CodeContext::add_stage_markers("Test", code); 107 let imports_pos = result.find("// --- Imports & Declarations ---").unwrap(); 108 let const_pos = result.find("// --- Constants ---").unwrap(); 109 let impl_pos = result.find("// --- Main Implementation ---").unwrap(); 110 111 assert!(imports_pos < const_pos); 112 assert!(const_pos < impl_pos); 113 } 114 }