myph-v2.git


myph-v2

~ rust shellcode loader with evasive capabilities
git clone https://github.com/djnnvx/myph-v2.git
log | files | refs | archive | README

internals_utils.rs (2809B)

      1 /// Returns the hex_to_bytes function implementation as a string
      2 pub fn internals_hex_to_bytes() -> &'static str {
      3     r#"/// Converts a hexadecimal string to bytes
      4 ///
      5 /// Returns `Ok(Vec<u8>)` if the hex string is valid, otherwise returns an error.
      6 pub fn hex_to_bytes(hex: &str) -> Result<Vec<u8>, String> {
      7     let hex = hex.trim_start_matches("0x");
      8 
      9     if (hex.len() & 1) != 0 {
     10         return Err("Hex string must have even length".to_string());
     11     }
     12 
     13     (0..hex.len())
     14         .step_by(2)
     15         .map(|i| {
     16             u8::from_str_radix(&hex[i..i + 2], 16)
     17                 .map_err(|e| format!("Invalid hex character: {}", e))
     18         })
     19         .collect()
     20 }"#
     21 }
     22 
     23 /// Returns the bytes_to_hex function implementation as a string
     24 pub fn internals_bytes_to_hex() -> &'static str {
     25     r#"/// Converts bytes to a hexadecimal string
     26 pub fn bytes_to_hex(bytes: &[u8]) -> String {
     27     use std::fmt::Write;
     28     bytes.iter().fold(String::with_capacity(bytes.len() * 2), |mut output, b| {
     29         write!(output, "{:02x}", b).unwrap();
     30         output
     31     })
     32 }"#
     33 }
     34 
     35 /// Returns the xor_bytes function implementation as a string
     36 pub fn internals_xor_bytes() -> &'static str {
     37     r#"/// Applies XOR operation between data and a repeating key
     38 ///
     39 /// If the key is shorter than the data, it will be repeated cyclically.
     40 pub fn xor_bytes(data: &[u8], key: &[u8]) -> Vec<u8> {
     41     if key.is_empty() {
     42         return data.to_vec();
     43     }
     44 
     45     data.iter()
     46         .enumerate()
     47         .map(|(i, &byte)| byte ^ key[i % key.len()])
     48         .collect()
     49 }"#
     50 }
     51 
     52 /// Returns the pad_pkcs7 function implementation as a string
     53 pub fn internals_pad_pkcs7() -> &'static str {
     54     r#"/// Pads data to the specified block size using PKCS#7 padding
     55 ///
     56 /// Returns the padded data.
     57 pub fn pad_pkcs7(data: &[u8], block_size: usize) -> Vec<u8> {
     58     let padding_len = block_size - (data.len() % block_size);
     59     let mut padded = data.to_vec();
     60     padded.extend(vec![padding_len as u8; padding_len]);
     61     padded
     62 }"#
     63 }
     64 
     65 /// Returns the unpad_pkcs7 function implementation as a string
     66 pub fn internals_unpad_pkcs7() -> &'static str {
     67     r#"/// Removes PKCS#7 padding from data
     68 ///
     69 /// Returns `Ok(Vec<u8>)` with the unpadded data if valid padding is found,
     70 /// otherwise returns an error.
     71 pub fn unpad_pkcs7(data: &[u8]) -> Result<Vec<u8>, String> {
     72     if data.is_empty() {
     73         return Err("Data is empty".to_string());
     74     }
     75 
     76     let padding_len = data[data.len() - 1] as usize;
     77 
     78     if padding_len == 0 || padding_len > data.len() {
     79         return Err("Invalid padding length".to_string());
     80     }
     81 
     82     for &byte in &data[data.len() - padding_len..] {
     83         if byte != padding_len as u8 {
     84             return Err("Invalid padding bytes".to_string());
     85         }
     86     }
     87 
     88     Ok(data[..data.len() - padding_len].to_vec())
     89 }"#
     90 }