Files
unshell/unshell-obfuscate/src/lib.rs
T

126 lines
3.0 KiB
Rust
Raw Normal View History

2025-11-08 17:58:40 -07:00
#![feature(proc_macro_quote)]
2025-11-09 12:34:52 -07:00
#![feature(proc_macro_span)]
2025-11-08 17:58:40 -07:00
use proc_macro::TokenStream;
use quote::quote;
2025-12-13 16:49:32 -07:00
use syn::parse_macro_input;
2025-11-08 17:58:40 -07:00
2025-11-09 12:34:52 -07:00
mod format_helper;
use format_helper::*;
2025-12-14 13:08:36 -07:00
#[allow(dead_code, unused_imports)]
2025-12-13 16:49:32 -07:00
mod no_obfuscate;
2025-11-08 17:58:40 -07:00
2025-12-14 13:08:36 -07:00
#[allow(dead_code, unused_imports)]
2025-12-13 16:49:32 -07:00
mod obfuscate;
2025-11-13 11:52:01 -07:00
2025-11-08 17:58:40 -07:00
#[cfg(not(feature = "obfuscate"))]
2025-12-13 16:49:32 -07:00
use no_obfuscate as obs;
#[cfg(feature = "obfuscate")]
use obfuscate as obs;
// String obfuscation
2025-11-08 17:58:40 -07:00
#[proc_macro]
2025-12-13 16:49:32 -07:00
pub fn obs(input: TokenStream) -> TokenStream {
obs::obs(input)
2025-11-08 17:58:40 -07:00
}
#[proc_macro_attribute]
pub fn obfuscated_symbol(_attr: TokenStream, item: TokenStream) -> TokenStream {
2025-12-13 16:49:32 -07:00
obs::obfuscated_symbol(_attr, item)
2025-11-08 17:58:40 -07:00
}
#[proc_macro]
pub fn symbol(input: TokenStream) -> TokenStream {
2025-12-13 16:49:32 -07:00
obs::symbol(input)
2025-11-08 17:58:40 -07:00
}
#[proc_macro]
2025-12-13 16:49:32 -07:00
pub fn junk_asm(input: TokenStream) -> TokenStream {
2025-12-14 13:08:36 -07:00
obs::junk_asm(input)
2025-11-08 17:58:40 -07:00
}
2025-11-09 12:34:52 -07:00
2025-12-13 16:49:32 -07:00
//
2025-11-09 12:34:52 -07:00
#[proc_macro]
pub fn file_symbol(_input: TokenStream) -> TokenStream {
// Get the call site span to extract file information
let span = proc_macro::Span::call_site();
let source_file = span.source();
let file_path = source_file.file();
let line_num = source_file.line();
let concatted = format!("{}:{}", file_path, line_num);
// Return as a string literal
let output = quote! {
unshell_obfuscate::symbol!(#concatted)
};
// let output = quote! {
// #concatted
// };
output.into()
}
#[proc_macro]
pub fn format_obs(input: TokenStream) -> TokenStream {
let PrintlnArgs { format_str, args } = parse_macro_input!(input as PrintlnArgs);
let segments = parse_format_string(&format_str);
if segments.is_empty() {
return quote! {
print!("\n")
}
.into();
}
let mut parts = Vec::new();
for segment in segments {
match segment {
FormatSegment::Static(text) => {
parts.push(quote! {
unshell_obfuscate::symbol!(#text).to_string()
});
}
FormatSegment::Dynamic(spec, idx) => {
if idx >= args.len() {
return syn::Error::new(
proc_macro2::Span::call_site(),
format!("argument {} is missing", idx),
)
.to_compile_error()
.into();
}
let arg = &args[idx];
let fmt_spec = if spec.is_empty() {
quote! { "{}" }
} else {
let full_spec = format!("{{{}}}", spec);
quote! { #full_spec }
};
// quote! {
// println!(#fmt_spec, #arg);
// }
parts.push(quote! {
format!(#fmt_spec, #arg)
});
}
}
}
(quote! {
{
let mut string = String::new();
#(
string.push_str(&#parts);
)*
string
}
})
.into()
}