Skip to content

Commit

Permalink
🔀 Merge pull request #29 from Steve-xmh/master
Browse files Browse the repository at this point in the history
Some enhancements for developer experiences
  • Loading branch information
ShellWen committed Mar 5, 2024
2 parents 75b8708 + d6b8a58 commit d2b7663
Show file tree
Hide file tree
Showing 9 changed files with 159 additions and 130 deletions.
10 changes: 10 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,13 @@ resolver = "2"
version = "0.1.0"
edition = "2021"
authors = ["ShellWen <me@shellwen.com>"]

[workspace.dependencies]
clap = { version = "4.5", features = ["derive"] }
tracing = "0.1"
tracing-subscriber = "0.3"

[profile.release]
opt-level = "z"
lto = true
codegen-units = 1
16 changes: 9 additions & 7 deletions crates/core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,17 @@ authors.workspace = true
crate-type = ["cdylib"]

[dependencies]
frida-gum = { version = "0.13.2", features = ["auto-download", "invocation-listener"] }
lazy_static = "1.4.0"
ctor = "0.2.4"
toml = "0.8.1"
serde = { version = "1.0.188", features = ["derive"] }
regex = "1.10.2"
ctor = "0.2"
frida-gum = { version = "0.13", features = ["auto-download", "invocation-listener"] }
once_cell = "1.19"
regex = "1.10"
serde = { version = "1.0", features = ["derive"] }
toml = "0.8"
tracing = { workspace = true }
tracing-subscriber = { workspace = true }

[target.'cfg(target_os = "windows")'.dependencies]
windows = { version = "0.51.1", features = [
windows = { version = "0.54", features = [
"Win32_Foundation",
"Win32_System_Console",
] }
2 changes: 1 addition & 1 deletion crates/core/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ pub(crate) struct ConfigRule {
pub(crate) processors: Vec<SourceProcessor>,
}

#[derive(Deserialize, Debug)]
#[derive(Deserialize, Default, Debug)]
pub(crate) struct Config {
#[serde(default)]
pub(crate) identifiers: Identifiers,
Expand Down
12 changes: 6 additions & 6 deletions crates/core/src/core.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use std::ops::Deref;

use tracing::*;

use crate::config::Config;
use crate::matcher::SourceMatcher;
use crate::source::Source;
Expand All @@ -15,6 +17,7 @@ pub(crate) unsafe fn process_script(
let isolate = v8_context_get_isolate(v8_context);
let resource_name = string_from_local_string(isolate, (*v8_source)._resource_name);
let source_string = string_from_local_string(isolate, (*v8_source)._source_string);
debug!("Processing source: {resource_name}");
let mut source = Source {
resource_name,
source_string,
Expand All @@ -23,17 +26,14 @@ pub(crate) unsafe fn process_script(
let (rule_name, rule) = rule_item;
let is_match = &rule.matcher.deref().matches(&source);
if *is_match {
println!(
"[*] Rule {} matched in {}",
rule_name, &source.resource_name
);
info!("Rule {} matched in {}", rule_name, &source.resource_name);
let processors = &rule.processors;
processors.iter().for_each(|processor_item| {
let processor = processor_item;
let result = processor.process(&mut source);
if result.is_err() {
println!(
"[!] Processor {:#?} process failed: {}",
error!(
"Processor {:#?} process failed: {}",
processor,
result.err().unwrap()
);
Expand Down
113 changes: 60 additions & 53 deletions crates/core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,16 @@ use std::path::Path;
use ctor::ctor;
use frida_gum::interceptor::{InvocationContext, InvocationListener};
use frida_gum::{interceptor::Interceptor, Gum};
use lazy_static::lazy_static;
use once_cell::sync::Lazy;
use tracing::*;
use tracing_subscriber::fmt::time::uptime;

use crate::config::{Config, ReadFromFile};
use crate::core::process_script;
use crate::identifier::Identifier;
use crate::v8_sys::{V8Context, V8Source};
use crate::{
config::{Config, ReadFromFile},
identifier::Identifier,
};

mod config;
mod core;
Expand All @@ -18,11 +22,26 @@ mod processor;
mod source;
mod v8_sys;

lazy_static! {
static ref GUM: Gum = unsafe { Gum::obtain() };
}
static GUM: Lazy<Gum> = Lazy::new(|| unsafe { Gum::obtain() });

static mut CONFIG: Option<Config> = None;
static CONFIG: Lazy<Config> = Lazy::new(|| {
let config_file_path = std::env::var("V8_KILLER_CONFIG_FILE_PATH");
match config_file_path {
Ok(config_file_path) => {
info!("V8_KILLER_CONFIG_FILE_PATH: {config_file_path}");
let path = Path::new(&config_file_path);
let config = Config::load_from_toml(path);
info!("Read Config success: {config:#?}");
config
}
Err(_) => {
warn!("V8_KILLER_CONFIG_FILE_PATH not found");
warn!("Please set V8_KILLER_CONFIG_FILE_PATH to config file path");
warn!("V8 Killer will only tracing source code without config file");
Default::default()
}
}
});

// v8::ScriptCompiler::CompileFunctionInternal(v8::Local<v8::Context>, v8::ScriptCompiler::Source*, unsigned long, v8::Local<v8::String>*, unsigned long, v8::Local<v8::Object>*, v8::ScriptCompiler::CompileOptions, v8::ScriptCompiler::NoCacheReason, v8::Local<v8::ScriptOrModule>*)
struct V8ScriptCompilerCompileFunctionInternalListener;
Expand All @@ -38,8 +57,7 @@ impl InvocationListener for V8ScriptCompilerCompileFunctionInternalListener {
let context = frida_context.arg(1) as *const V8Context;
#[cfg(target_os = "windows")]
let source = frida_context.arg(2) as *mut V8Source;
let config = CONFIG.as_ref().unwrap();
process_script(config, context, source);
process_script(&CONFIG, context, source);
}
}

Expand All @@ -48,6 +66,11 @@ impl InvocationListener for V8ScriptCompilerCompileFunctionInternalListener {

#[ctor]
fn init() {
tracing_subscriber::fmt()
.with_timer(uptime())
.with_max_level(tracing::Level::DEBUG)
.init();

// Fix no output in the Windows GUI subsystem programs
// See also: [#11](https://github.com/ShellWen/v8_killer/issues/11)
#[cfg(target_os = "windows")]
Expand All @@ -57,51 +80,35 @@ fn init() {
let _ = AttachConsole(ATTACH_PARENT_PROCESS);
}

// 读取环境变量
let config_file_path = std::env::var("V8_KILLER_CONFIG_FILE_PATH");
match config_file_path {
Ok(config_file_path) => {
println!("[*] V8_KILLER_CONFIG_FILE_PATH: {}", config_file_path);
let path = Path::new(&config_file_path);
let config = Config::load_from_toml(path);
println!("[*] Read Config success");
println!("[*] Config: {:?}", config);
unsafe {
CONFIG = Some(config);
}
let mut interceptor = Interceptor::obtain(&GUM);

interceptor.begin_transaction();

let v8_script_compiler_compile_function_internal = unsafe { CONFIG.as_ref().unwrap() }
.identifiers
.V8_SCRIPT_COMPILER_COMPILE_FUNCTION_INTERNAL
.identify();

match v8_script_compiler_compile_function_internal {
None => {
println!("[-] v8_script_compiler_compile_function_internal not found")
}
Some(addr) => {
println!(
"[*] v8_script_compiler_compile_function_internal found: {:?}",
addr.0
);
let mut v8_script_compiler_compile_function_internal_listener =
V8ScriptCompilerCompileFunctionInternalListener;
interceptor.attach(
addr,
&mut v8_script_compiler_compile_function_internal_listener,
);
}
}

interceptor.end_transaction();
info!("V8 Killer has been injected and started!");

let mut interceptor = Interceptor::obtain(&GUM);

interceptor.begin_transaction();

let v8_script_compiler_compile_function_internal = CONFIG
.identifiers
.V8_SCRIPT_COMPILER_COMPILE_FUNCTION_INTERNAL
.identify();

match v8_script_compiler_compile_function_internal {
None => {
error!("v8_script_compiler_compile_function_internal not found");
error!("source processing will not work properly");
}
Err(_) => {
println!("[-] WARN: V8_KILLER_CONFIG_FILE_PATH not found");
println!("[-] WARN: Please set V8_KILLER_CONFIG_FILE_PATH to config file path");
println!("[-] WARN: Without config file, V8 Killer will do nothing");
Some(addr) => {
info!(
"v8_script_compiler_compile_function_internal found: {:?}",
addr.0
);
let mut v8_script_compiler_compile_function_internal_listener =
V8ScriptCompilerCompileFunctionInternalListener;
interceptor.attach(
addr,
&mut v8_script_compiler_compile_function_internal_listener,
);
}
}

interceptor.end_transaction();
}
20 changes: 2 additions & 18 deletions crates/core/src/v8_sys.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,6 @@ type v8__String__NewFromUtf8 = unsafe extern "C" fn(

pub(crate) unsafe fn v8_context_get_isolate(context: *const V8Context) -> *const V8Isolate {
let v8_context_get_isolate_ptr = CONFIG
.as_ref()
.unwrap()
.identifiers
.V8_CONTEXT_GET_ISOLATE
.identify()
Expand All @@ -93,13 +91,7 @@ pub(super) unsafe fn v8_string_utf8_length(
this: *const V8String,
isolate: *const V8Isolate,
) -> usize {
let v8_string_utf8_length_ptr = CONFIG
.as_ref()
.unwrap()
.identifiers
.V8_STRING_UTF8LENGTH
.identify()
.unwrap();
let v8_string_utf8_length_ptr = CONFIG.identifiers.V8_STRING_UTF8LENGTH.identify().unwrap();
let v8_string_utf8_length_func: v8__String__Utf8Length =
std::mem::transmute(v8_string_utf8_length_ptr.0);

Expand All @@ -114,13 +106,7 @@ pub(crate) unsafe fn v8_string_write_utf8(
nchars_ref: *mut usize,
options: c_int,
) -> c_int {
let v8_string_write_utf8_ptr = CONFIG
.as_ref()
.unwrap()
.identifiers
.V8_STRING_WRITE_UTF8
.identify()
.unwrap();
let v8_string_write_utf8_ptr = CONFIG.identifiers.V8_STRING_WRITE_UTF8.identify().unwrap();
let v8_string_write_utf8_func: v8__String__WriteUtf8 =
std::mem::transmute(v8_string_write_utf8_ptr.0);

Expand All @@ -134,8 +120,6 @@ pub(crate) unsafe fn v8_string_new_from_utf8(
length: i32,
) -> V8Local<V8String> {
let v8_string_new_from_utf8_ptr = CONFIG
.as_ref()
.unwrap()
.identifiers
.V8_STRING_NEW_FROM_UTF8
.identify()
Expand Down
5 changes: 4 additions & 1 deletion crates/launcher/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,12 @@ edition.workspace = true
authors.workspace = true

[dependencies]
clap = { workspace = true }
tracing = { workspace = true }
tracing-subscriber = { workspace = true }

[target.'cfg(target_os = "windows")'.dependencies]
windows = { version = "0.51.1", features = [
windows = { version = "0.54", features = [
"Win32_System_Threading",
"Win32_System_Console",
"Win32_System_Memory",
Expand Down
Loading

0 comments on commit d2b7663

Please sign in to comment.