hook-inject 0.1.0

Cross-platform process injection via Frida Core.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
use std::ffi::{CStr, CString, OsStr};
use std::os::raw::{c_char, c_int};
use std::ptr;

use crate::library::LibrarySource;
use crate::{Error, Library, Process, Program, Result, Stdio};

#[repr(C)]
struct HookFridaCtx {
    _private: [u8; 0],
}

unsafe extern "C" {
    fn hook_frida_new(error_kind_out: *mut c_int, error_out: *mut *mut c_char)
    -> *mut HookFridaCtx;
    fn hook_frida_free(ctx: *mut HookFridaCtx);

    fn hook_frida_inject_process(
        ctx: *mut HookFridaCtx,
        pid: i32,
        library_path: *const c_char,
        entrypoint: *const c_char,
        data: *const c_char,
        out_id: *mut u32,
        error_kind_out: *mut c_int,
        error_out: *mut *mut c_char,
    ) -> c_int;

    fn hook_frida_inject_blob(
        ctx: *mut HookFridaCtx,
        pid: i32,
        blob: *const u8,
        blob_len: usize,
        entrypoint: *const c_char,
        data: *const c_char,
        out_id: *mut u32,
        error_kind_out: *mut c_int,
        error_out: *mut *mut c_char,
    ) -> c_int;

    fn hook_frida_inject_launch(
        ctx: *mut HookFridaCtx,
        program: *const c_char,
        argv: *const *const c_char,
        envp: *const *const c_char,
        cwd: *const c_char,
        stdio: i32,
        library_path: *const c_char,
        entrypoint: *const c_char,
        data: *const c_char,
        out_pid: *mut u32,
        out_id: *mut u32,
        error_kind_out: *mut c_int,
        error_out: *mut *mut c_char,
    ) -> c_int;

    fn hook_frida_spawn(
        ctx: *mut HookFridaCtx,
        program: *const c_char,
        argv: *const *const c_char,
        envp: *const *const c_char,
        cwd: *const c_char,
        stdio: i32,
        out_pid: *mut u32,
        error_kind_out: *mut c_int,
        error_out: *mut *mut c_char,
    ) -> c_int;

    fn hook_frida_resume(
        ctx: *mut HookFridaCtx,
        pid: u32,
        error_kind_out: *mut c_int,
        error_out: *mut *mut c_char,
    ) -> c_int;

    fn hook_frida_demonitor(
        ctx: *mut HookFridaCtx,
        id: u32,
        error_kind_out: *mut c_int,
        error_out: *mut *mut c_char,
    ) -> c_int;
    fn hook_frida_string_free(s: *mut c_char);
}

pub(crate) fn init() -> Result<FridaBackend> {
    unsafe {
        let mut err_ptr: *mut c_char = ptr::null_mut();
        let mut err_kind: c_int = HOOK_FRIDA_ERROR_NONE;
        let ctx = hook_frida_new(
            &mut err_kind as *mut c_int,
            &mut err_ptr as *mut *mut c_char,
        );
        if ctx.is_null() {
            let msg = read_error(err_ptr);
            return Err(Error::runtime_unavailable(msg));
        }

        Ok(FridaBackend { ctx })
    }
}

pub(super) struct FridaBackend {
    ctx: *mut HookFridaCtx,
}

// Frida's injector context is used only through its C API, which is designed
// for concurrent use; we treat the opaque pointer as Send/Sync here.
unsafe impl Send for FridaBackend {}
unsafe impl Sync for FridaBackend {}

impl Drop for FridaBackend {
    fn drop(&mut self) {
        unsafe {
            if !self.ctx.is_null() {
                hook_frida_free(self.ctx);
                self.ctx = ptr::null_mut();
            }
        }
    }
}

impl FridaBackend {
    pub(super) fn inject_launch(
        &self,
        spec: &mut Program,
        library: &Library,
    ) -> Result<(Process, u64)> {
        match library.source() {
            LibrarySource::Path(_) => self.inject_launch_path(spec, library),
            LibrarySource::Blob(_) => {
                let process = self.spawn(spec)?;
                let id = self.inject_blob(process, library)?;
                self.resume(process)?;
                Ok((process, id))
            }
        }
    }

    pub(super) fn inject_process(&self, process: Process, library: &Library) -> Result<u64> {
        match library.source() {
            LibrarySource::Path(_) => self.inject_process_path(process, library),
            LibrarySource::Blob(_) => self.inject_blob(process, library),
        }
    }

    fn inject_launch_path(&self, spec: &mut Program, library: &Library) -> Result<(Process, u64)> {
        let program_path = spec.command().get_program();
        let program = os_str_to_cstring(program_path, "program")?;
        let library_path = match library.source() {
            LibrarySource::Path(path) => os_str_to_cstring(path, "library_path")?,
            LibrarySource::Blob(_) => {
                return Err(Error::invalid_input(
                    "library must be a file path for launch",
                ));
            }
        };
        let entrypoint = library.entrypoint();
        let data = library.data();

        let argv_storage = build_argv(spec, &program)?;
        let envp_storage = build_envp(spec)?;
        let cwd = spec
            .command()
            .get_current_dir()
            .map(|dir| os_str_to_cstring(dir, "cwd"))
            .transpose()?;

        let mut err_ptr: *mut c_char = ptr::null_mut();
        let mut err_kind: c_int = HOOK_FRIDA_ERROR_NONE;
        let mut pid_out: u32 = 0;
        let mut id_out: u32 = 0;

        let ok = unsafe {
            hook_frida_inject_launch(
                self.ctx,
                program.as_ptr(),
                argv_storage.ptrs.as_ptr(),
                envp_storage.ptrs.as_ptr(),
                cwd.as_ref().map(|s| s.as_ptr()).unwrap_or(ptr::null()),
                map_stdio(spec.stdio_value()),
                library_path.as_ptr(),
                entrypoint.as_ptr(),
                data.as_ptr(),
                &mut pid_out as *mut u32,
                &mut id_out as *mut u32,
                &mut err_kind as *mut c_int,
                &mut err_ptr as *mut *mut c_char,
            )
        };

        if ok <= 0 {
            return Err(new_frida_error(err_kind, err_ptr, None));
        }

        let process = unsafe { Process::from_pid_unchecked(pid_out as i32) };
        Ok((process, id_out as u64))
    }

    fn inject_process_path(&self, process: Process, library: &Library) -> Result<u64> {
        let library_path = match library.source() {
            LibrarySource::Path(path) => os_str_to_cstring(path, "library_path")?,
            LibrarySource::Blob(_) => {
                return Err(Error::invalid_input("library must be a file path"));
            }
        };
        let entrypoint = library.entrypoint();
        let data = library.data();

        let mut err_ptr: *mut c_char = ptr::null_mut();
        let mut err_kind: c_int = HOOK_FRIDA_ERROR_NONE;
        let mut id_out: u32 = 0;

        let ok = unsafe {
            hook_frida_inject_process(
                self.ctx,
                process.pid(),
                library_path.as_ptr(),
                entrypoint.as_ptr(),
                data.as_ptr(),
                &mut id_out as *mut u32,
                &mut err_kind as *mut c_int,
                &mut err_ptr as *mut *mut c_char,
            )
        };

        if ok <= 0 {
            return Err(new_frida_error(err_kind, err_ptr, None));
        }

        Ok(id_out as u64)
    }

    fn inject_blob(&self, process: Process, library: &Library) -> Result<u64> {
        let bytes = match library.source() {
            LibrarySource::Blob(bytes) => bytes,
            LibrarySource::Path(_) => {
                return Err(Error::invalid_input("library is not a blob"));
            }
        };
        let entrypoint = library.entrypoint();
        let data = library.data();

        let mut err_ptr: *mut c_char = ptr::null_mut();
        let mut err_kind: c_int = HOOK_FRIDA_ERROR_NONE;
        let mut id_out: u32 = 0;

        let ok = unsafe {
            hook_frida_inject_blob(
                self.ctx,
                process.pid(),
                bytes.as_ptr(),
                bytes.len(),
                entrypoint.as_ptr(),
                data.as_ptr(),
                &mut id_out as *mut u32,
                &mut err_kind as *mut c_int,
                &mut err_ptr as *mut *mut c_char,
            )
        };

        if ok <= 0 {
            return Err(new_frida_error(err_kind, err_ptr, None));
        }

        Ok(id_out as u64)
    }

    pub(super) fn spawn(&self, spec: &mut Program) -> Result<Process> {
        let program_path = spec.command().get_program();
        let program = os_str_to_cstring(program_path, "program path")?;

        let argv_storage = build_argv(spec, &program)?;
        let envp_storage = build_envp(spec)?;
        let cwd = spec
            .command()
            .get_current_dir()
            .map(|dir| {
                CString::new(dir.to_string_lossy().as_bytes())
                    .map_err(|_| Error::invalid_input("cwd contains NUL"))
            })
            .transpose()?;

        let mut err_ptr: *mut c_char = ptr::null_mut();
        let mut err_kind: c_int = HOOK_FRIDA_ERROR_NONE;
        let mut pid_out: u32 = 0;

        let ok = unsafe {
            hook_frida_spawn(
                self.ctx,
                program.as_ptr(),
                argv_storage.ptrs.as_ptr(),
                envp_storage.ptrs.as_ptr(),
                cwd.as_ref().map(|s| s.as_ptr()).unwrap_or(ptr::null()),
                map_stdio(spec.stdio_value()),
                &mut pid_out as *mut u32,
                &mut err_kind as *mut c_int,
                &mut err_ptr as *mut *mut c_char,
            )
        };

        if ok <= 0 {
            return Err(new_frida_error(err_kind, err_ptr, None));
        }

        let process = unsafe { Process::from_pid_unchecked(pid_out as i32) };
        Ok(process)
    }

    pub(super) fn resume(&self, process: Process) -> Result<()> {
        let mut err_ptr: *mut c_char = ptr::null_mut();
        let mut err_kind: c_int = HOOK_FRIDA_ERROR_NONE;
        let ok = unsafe {
            hook_frida_resume(
                self.ctx,
                process.pid() as u32,
                &mut err_kind as *mut c_int,
                &mut err_ptr as *mut *mut c_char,
            )
        };
        if ok <= 0 {
            return Err(new_frida_error(err_kind, err_ptr, Some(process.pid())));
        }
        Ok(())
    }

    pub(super) fn uninject(&self, id: u64) -> Result<()> {
        if id == 0 {
            return Ok(());
        }

        let mut err_ptr: *mut c_char = ptr::null_mut();
        let mut err_kind: c_int = HOOK_FRIDA_ERROR_NONE;
        let ok = unsafe {
            hook_frida_demonitor(
                self.ctx,
                id as u32,
                &mut err_kind as *mut c_int,
                &mut err_ptr as *mut *mut c_char,
            )
        };
        if ok <= 0 {
            return Err(new_frida_error(err_kind, err_ptr, None));
        }
        Ok(())
    }
}

struct CArgv {
    _cstrings: Vec<CString>,
    ptrs: Vec<*const c_char>,
}

struct CEnvp {
    _cstrings: Vec<CString>,
    ptrs: Vec<*const c_char>,
}

fn build_argv(spec: &Program, program: &CString) -> Result<CArgv> {
    // Frida expects a NULL-terminated argv array; keep owned CStrings alive.
    let mut cstrings = Vec::new();
    cstrings.push(program.clone());
    for arg in spec.command().get_args() {
        let s = CString::new(arg.to_string_lossy().as_bytes())
            .map_err(|_| Error::invalid_input("arg contains NUL"))?;
        cstrings.push(s);
    }

    let mut ptrs: Vec<*const c_char> = cstrings.iter().map(|s| s.as_ptr()).collect();
    ptrs.push(ptr::null());

    Ok(CArgv {
        _cstrings: cstrings,
        ptrs,
    })
}

fn build_envp(spec: &Program) -> Result<CEnvp> {
    // Frida expects envp entries as KEY=VALUE strings, NULL-terminated.
    let mut cstrings = Vec::new();
    for (k, v) in spec.command().get_envs() {
        if let Some(v) = v {
            let mut kv = k.to_string_lossy().into_owned();
            kv.push('=');
            kv.push_str(&v.to_string_lossy());
            let s = CString::new(kv.as_bytes())
                .map_err(|_| Error::invalid_input("env contains NUL"))?;
            cstrings.push(s);
        }
    }

    if cstrings.is_empty() {
        return Ok(CEnvp {
            _cstrings: Vec::new(),
            ptrs: vec![ptr::null()],
        });
    }

    let mut ptrs: Vec<*const c_char> = cstrings.iter().map(|s| s.as_ptr()).collect();
    ptrs.push(ptr::null());

    Ok(CEnvp {
        _cstrings: cstrings,
        ptrs,
    })
}

fn map_stdio(stdio: Stdio) -> i32 {
    match stdio {
        Stdio::Inherit => 0,
        Stdio::Null => 1,
        Stdio::Pipe => 2,
    }
}
fn new_frida_error(err_kind: c_int, err_ptr: *mut c_char, pid: Option<i32>) -> Error {
    let msg = read_error(err_ptr);
    map_frida_error(err_kind, msg, pid)
}

// Mirror the shim's error kind codes to preserve a stable Rust API.
const HOOK_FRIDA_ERROR_NONE: c_int = 0;
const HOOK_FRIDA_ERROR_INVALID_ARGUMENT: c_int = 1;
const HOOK_FRIDA_ERROR_NOT_SUPPORTED: c_int = 2;
const HOOK_FRIDA_ERROR_PERMISSION_DENIED: c_int = 3;
const HOOK_FRIDA_ERROR_PROCESS_NOT_FOUND: c_int = 4;
#[allow(dead_code)]
const HOOK_FRIDA_ERROR_RUNTIME: c_int = 5;

fn map_frida_error(kind: c_int, msg: String, pid: Option<i32>) -> Error {
    // Map Frida error kinds into the public Rust error surface.
    match kind {
        HOOK_FRIDA_ERROR_INVALID_ARGUMENT => Error::invalid_input(msg),
        HOOK_FRIDA_ERROR_NOT_SUPPORTED => Error::not_supported(msg),
        HOOK_FRIDA_ERROR_PERMISSION_DENIED => Error::permission_denied(msg),
        HOOK_FRIDA_ERROR_PROCESS_NOT_FOUND => {
            if let Some(pid) = pid {
                Error::process_not_found(pid)
            } else {
                Error::runtime(msg)
            }
        }
        _ => Error::runtime(msg),
    }
}

fn read_error(ptr: *mut c_char) -> String {
    if ptr.is_null() {
        return "unknown error".to_string();
    }

    unsafe {
        let msg = CStr::from_ptr(ptr).to_string_lossy().into_owned();
        hook_frida_string_free(ptr);
        msg
    }
}

fn os_str_to_cstring(os_str: impl AsRef<OsStr>, var_name: &'static str) -> Result<CString> {
    #[cfg(unix)]
    {
        use std::os::unix::ffi::OsStrExt;

        CString::new(os_str.as_ref().as_bytes())
            .map_err(|err| Error::invalid_input(format_args!("{var_name}: {err}")))
    }

    #[cfg(not(unix))]
    {
        CString::new(os_str.as_ref().to_string_lossy().as_bytes())
            .map_err(|err| Error::invalid_input(format_args!("{var_name}: {err}")))
    }
}