Obfuscation macros are now defined more easily

This commit is contained in:
Michael Mikovsky
2026-02-20 15:47:58 -07:00
parent ba1772e512
commit 5a60f3f503
14 changed files with 118 additions and 84 deletions
Generated
-4
View File
@@ -622,18 +622,14 @@ dependencies = [
name = "ush-obfuscate" name = "ush-obfuscate"
version = "0.1.0" version = "0.1.0"
dependencies = [ dependencies = [
"aes",
"base62", "base62",
"block-padding 0.4.2", "block-padding 0.4.2",
"cbc",
"getrandom", "getrandom",
"hex", "hex",
"hex-literal", "hex-literal",
"proc-macro2", "proc-macro2",
"quote", "quote",
"rand", "rand",
"regex",
"sha2",
"static_init", "static_init",
"syn 2.0.114", "syn 2.0.114",
] ]
+2 -1
View File
@@ -31,7 +31,8 @@ default = []
log = [] log = []
log_debug = ["log"] log_debug = ["log"]
obfuscate = ["ush-obfuscate/obfuscate"] obfuscate_aes = ["ush-obfuscate/obfuscate_aes"]
obfuscate_ref = ["ush-obfuscate/obfuscate_ref"]
[dependencies] [dependencies]
chrono = { workspace = true } chrono = { workspace = true }
+2 -2
View File
@@ -2,7 +2,7 @@
macro_rules! log { macro_rules! log {
($level:expr, $fmt:tt) => {{ ($level:expr, $fmt:tt) => {{
use $crate::obfuscate; use $crate::obfuscate;
let log_result = obfuscate::format_obs!($fmt); let log_result = obfuscate::sym_format!($fmt);
$crate::logger::add_record( $crate::logger::add_record(
$level, $level,
@@ -18,7 +18,7 @@ macro_rules! log {
}}; }};
($level:expr, $fmt:tt, $($arg:expr),*) => {{ ($level:expr, $fmt:tt, $($arg:expr),*) => {{
use $crate::obfuscate; use $crate::obfuscate;
let log_result = obfuscate::format_obs!($fmt, $($arg),*); let log_result = obfuscate::sym_format!($fmt, $($arg),*);
$crate::logger::add_record( $crate::logger::add_record(
$level, $level,
+11 -11
View File
@@ -1,15 +1,15 @@
use crate::obfuscate::symbol; use crate::obfuscate::sym;
pub const LOGGER: &'static str = symbol!("Logger"); pub const LOGGER: &'static str = sym!("Logger");
pub const TYPE_TREE: &'static str = symbol!("Tree"); pub const TYPE_TREE: &'static str = sym!("Tree");
pub const TYPE_QUEUE: &'static str = symbol!("Queue"); pub const TYPE_QUEUE: &'static str = sym!("Queue");
pub const CMD_GET: &'static str = symbol!("Get"); pub const CMD_GET: &'static str = sym!("Get");
pub const CMD_POLL: &'static str = symbol!("Poll"); pub const CMD_POLL: &'static str = sym!("Poll");
pub const CMD_GET_LENGTH: &'static str = symbol!("GetLength"); pub const CMD_GET_LENGTH: &'static str = sym!("GetLength");
pub const CMD_GET_CHILDREN: &'static str = symbol!("GetChildren"); pub const CMD_GET_CHILDREN: &'static str = sym!("GetChildren");
pub const ERR_UNSUPPORTED_METHOD: &'static str = symbol!("UnsupportedMethod"); pub const ERR_UNSUPPORTED_METHOD: &'static str = sym!("UnsupportedMethod");
pub const ERR_INVALID_COMMAND: &'static str = symbol!("InvalidCommand"); pub const ERR_INVALID_COMMAND: &'static str = sym!("InvalidCommand");
pub const ERR_INVALID_CHILD: &'static str = symbol!("InvalidChild"); pub const ERR_INVALID_CHILD: &'static str = sym!("InvalidChild");
+10 -7
View File
@@ -6,22 +6,25 @@ authors.workspace = true
include.workspace = true include.workspace = true
[features] [features]
obfuscate = [] default = ["obfuscate_none"]
obfuscate_none = []
obfuscate_aes = []
obfuscate_ref = []
[lib] [lib]
proc-macro = true proc-macro = true
[dependencies] [dependencies]
aes = "0.8.4" base62 = {path = "../base62"}
# aes = "0.8.4"
block-padding = "0.4.1" block-padding = "0.4.1"
cbc = "0.1.2" # cbc = "0.1.2"
getrandom = "0.3.4" getrandom = "0.3.4"
hex = "0.4.3" hex = "0.4.3"
hex-literal = "1.1.0" hex-literal = "1.1.0"
regex = "1.12.2" # regex = "1.12.2"
sha2 = "0.10.9" # sha2 = "0.10.9"
base62 = {path = "../base62"}
# Common # Common
static_init = { workspace = true } static_init = { workspace = true }
+2 -2
View File
@@ -3,7 +3,7 @@ use quote::quote;
use syn::parse::{Parse, ParseStream}; use syn::parse::{Parse, ParseStream};
use syn::{Expr, Lit, Token, parse_macro_input}; use syn::{Expr, Lit, Token, parse_macro_input};
pub fn format_obs(input: TokenStream) -> TokenStream { pub fn sym_format(input: TokenStream) -> TokenStream {
let PrintlnArgs { format_str, args } = parse_macro_input!(input as PrintlnArgs); let PrintlnArgs { format_str, args } = parse_macro_input!(input as PrintlnArgs);
let segments = parse_format_string(&format_str); let segments = parse_format_string(&format_str);
@@ -21,7 +21,7 @@ pub fn format_obs(input: TokenStream) -> TokenStream {
match segment { match segment {
FormatSegment::Static(text) => { FormatSegment::Static(text) => {
parts.push(quote! { parts.push(quote! {
obfuscate::symbol!(#text).to_string() obfuscate::sym!(#text).to_string()
}); });
} }
FormatSegment::Dynamic(spec, idx) => { FormatSegment::Dynamic(spec, idx) => {
+20 -23
View File
@@ -1,43 +1,40 @@
#![feature(proc_macro_quote)] #![feature(proc_macro_quote)]
#![feature(proc_macro_span)] #![feature(proc_macro_span)]
#![allow(dead_code, unused_macros, unused_imports)]
mod env;
mod format_helper;
mod proc_impl_switcher;
mod obfuscate;
// Types of symbolic reference
mod symbolic_aes;
mod symbolic_ref;
use proc_macro::TokenStream; use proc_macro::TokenStream;
use quote::quote; use quote::quote;
mod env; use proc_impl_switcher::proc_impl;
mod format_helper;
#[allow(dead_code, unused_imports)]
mod no_obfuscate;
#[allow(dead_code, unused_imports)]
mod obfuscate;
#[cfg(not(feature = "obfuscate"))]
use no_obfuscate as obs;
#[cfg(feature = "obfuscate")]
use obfuscate as obs;
// String obfuscation
#[proc_macro] #[proc_macro]
pub fn obs(input: TokenStream) -> TokenStream { pub fn obs(input: TokenStream) -> TokenStream {
obs::xor(input) proc_impl::xor(input)
} }
#[proc_macro_attribute] #[proc_macro_attribute]
pub fn obfuscated_symbol(_attr: TokenStream, item: TokenStream) -> TokenStream { pub fn sym_fn(_attr: TokenStream, item: TokenStream) -> TokenStream {
obs::aes_fn_name(_attr, item) proc_impl::sym_fn(item)
} }
#[proc_macro] #[proc_macro]
pub fn symbol(input: TokenStream) -> TokenStream { pub fn sym(input: TokenStream) -> TokenStream {
obs::aes_str(input) proc_impl::sym(input)
} }
#[proc_macro] #[proc_macro]
pub fn junk_asm(input: TokenStream) -> TokenStream { pub fn junk_asm(input: TokenStream) -> TokenStream {
obs::junk_asm(input) proc_impl::junk_asm(input)
} }
#[proc_macro] #[proc_macro]
@@ -57,6 +54,6 @@ pub fn file_symbol(_input: TokenStream) -> TokenStream {
} }
#[proc_macro] #[proc_macro]
pub fn format_obs(input: TokenStream) -> TokenStream { pub fn sym_format(input: TokenStream) -> TokenStream {
format_helper::format_obs(input) format_helper::sym_format(input)
} }
-27
View File
@@ -1,27 +0,0 @@
use proc_macro::TokenStream;
use quote::quote;
use syn::{ItemFn, LitStr, parse_macro_input};
pub fn xor(input: TokenStream) -> TokenStream {
let input = parse_macro_input!(input as LitStr);
(quote::quote! {
String::from(#input)
})
.into()
}
pub fn aes_fn_name(_attr: TokenStream, item: TokenStream) -> TokenStream {
let func = parse_macro_input!(item as ItemFn);
TokenStream::from(quote! {
#[unsafe(no_mangle)]
#func
})
}
pub fn aes_str(input: TokenStream) -> TokenStream {
input
}
pub fn junk_asm(_input: TokenStream) -> TokenStream {
TokenStream::new()
}
-2
View File
@@ -1,7 +1,5 @@
mod obs_junk_asm; mod obs_junk_asm;
mod obs_xor; mod obs_xor;
mod sym_aes_strings;
pub use obs_junk_asm::junk_asm; pub use obs_junk_asm::junk_asm;
pub use obs_xor::xor; pub use obs_xor::xor;
pub use sym_aes_strings::*;
+66
View File
@@ -0,0 +1,66 @@
/// Call some other function
macro_rules! passtrough {
($name:tt, $ref:expr) => {
pub fn $name(input: TokenStream) -> TokenStream {
$ref(input)
}
};
}
/// Just return the underlying string
macro_rules! unwrap_string {
($func:tt) => {
pub fn $func(input: TokenStream) -> TokenStream {
let input = parse_macro_input!(input as LitStr);
(quote::quote! {
#input
})
.into()
}
};
}
/// Delete the content
macro_rules! delete {
($func:tt) => {
pub fn $func(_: TokenStream) -> TokenStream {
(quote::quote! {}).into()
}
};
}
#[cfg(feature = "obfuscate_none")]
pub mod proc_impl {
use proc_macro::TokenStream;
use syn::{LitStr, parse_macro_input};
unwrap_string!(xor);
delete!(junk_asm);
unwrap_string!(sym);
unwrap_string!(sym_fn);
}
#[cfg(feature = "obfuscate_aes")]
pub mod proc_impl {
use proc_macro::TokenStream;
passtrough!(xor, crate::obfuscate::xor);
passtrough!(junk_asm, crate::obfuscate::junk_asm);
passtrough!(sym, crate::symbolic_aes::aes_str);
passtrough!(sym_fn, crate::symbolic_aes::aes_fn_name);
}
#[cfg(feature = "obfuscate_ref")]
pub mod proc_impl {
use proc_macro::TokenStream;
use syn::{LitStr, parse_macro_input};
unwrap_string!(xor);
delete!(junk_asm);
unwrap_string!(sym);
unwrap_string!(sym_fn);
}
@@ -6,9 +6,8 @@ use syn::{ItemFn, LitStr, parse_macro_input};
use crate::env::get_encryption_key; use crate::env::get_encryption_key;
/// Obfuscate function names by encrypting in AES /// Obfuscate function names by encrypting in AES
pub fn aes_fn_name(_attr: TokenStream, item: TokenStream) -> TokenStream { pub fn aes_fn_name(item: TokenStream) -> TokenStream {
// Parse the input function // Parse the input function
let func = parse_macro_input!(item as ItemFn); let func = parse_macro_input!(item as ItemFn);
// Get the original function name // Get the original function name
+1
View File
@@ -0,0 +1 @@
+1 -1
View File
@@ -8,7 +8,7 @@ edition = "2024"
default = ["log"] default = ["log"]
log = ["unshell/log"] log = ["unshell/log"]
log_debug = ["unshell/log_debug"] log_debug = ["unshell/log_debug"]
obfuscate = ["unshell/obfuscate"] obfuscate = ["unshell/obfuscate_aes"]
[dependencies] [dependencies]
unshell.path = "../" unshell.path = "../"
+2 -2
View File
@@ -3,7 +3,7 @@ extern crate unshell;
use unshell::{ use unshell::{
Value, info, Value, info,
obfuscate::{junk_asm, symbol}, obfuscate::{junk_asm, sym},
tree::{Tree, TreeElement, symbols}, tree::{Tree, TreeElement, symbols},
}; };
@@ -25,7 +25,7 @@ fn main() {
fn test123(manager: &mut Tree) -> bool { fn test123(manager: &mut Tree) -> bool {
let result = manager.send_message( let result = manager.send_message(
Value::String(symbol!("Logger").to_string()), Value::String(sym!("Logger").to_string()),
Value::String(symbols::CMD_GET.to_string()), Value::String(symbols::CMD_GET.to_string()),
); );