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
#include "frida_shim.h"
// frida-core.h is an amalgam that includes GLib/GObject types.
#include <frida-core.h>
#include <stdlib.h>
#include <string.h>

// Context owned by the Rust side; wraps Frida device + injector handles.
struct HookFridaCtx {
  FridaDeviceManager * manager;
  FridaDevice * device;
  FridaInjector * injector;
};

static gboolean
hook_debug_enabled(void) {
  return getenv("HOOK_INJECT_DEBUG") != NULL;
}

static void
hook_debug(const char * message) {
  if (hook_debug_enabled())
    g_printerr("%s\n", message);
}

// Map Frida/GLib errors into the small enum we expose to Rust.
static HookFridaErrorKind hook_error_kind_from_gerror(GError * err) {
  if (err == NULL)
    return HOOK_FRIDA_ERROR_RUNTIME;

  if (g_error_matches(err, FRIDA_ERROR, FRIDA_ERROR_INVALID_ARGUMENT))
    return HOOK_FRIDA_ERROR_INVALID_ARGUMENT;
  if (g_error_matches(err, FRIDA_ERROR, FRIDA_ERROR_PERMISSION_DENIED))
    return HOOK_FRIDA_ERROR_PERMISSION_DENIED;
  if (g_error_matches(err, FRIDA_ERROR, FRIDA_ERROR_PROCESS_NOT_FOUND))
    return HOOK_FRIDA_ERROR_PROCESS_NOT_FOUND;
  if (g_error_matches(err, FRIDA_ERROR, FRIDA_ERROR_NOT_SUPPORTED) ||
      g_error_matches(err, FRIDA_ERROR, FRIDA_ERROR_EXECUTABLE_NOT_SUPPORTED) ||
      g_error_matches(err, FRIDA_ERROR, FRIDA_ERROR_INVALID_OPERATION))
    return HOOK_FRIDA_ERROR_NOT_SUPPORTED;

  return HOOK_FRIDA_ERROR_RUNTIME;
}

// Store an error kind + a copied message for the Rust side.
static void hook_set_error(GError * err, int32_t * error_kind_out, char ** error_out) {
  if (error_kind_out != NULL) {
    if (err == NULL)
      *error_kind_out = HOOK_FRIDA_ERROR_NONE;
    else
      *error_kind_out = (int32_t) hook_error_kind_from_gerror(err);
  }

  if (error_out == NULL) {
    return;
  }

  if (err == NULL || err->message == NULL) {
    *error_out = g_strdup("unknown error");
  } else {
    *error_out = g_strdup(err->message);
  }
}

static gboolean
hook_should_try_device_fallback(GError * err) {
  if (err == NULL)
    return FALSE;

  return g_error_matches(err, FRIDA_ERROR, FRIDA_ERROR_NOT_SUPPORTED) ||
      g_error_matches(err, FRIDA_ERROR, FRIDA_ERROR_PERMISSION_DENIED);
}

HookFridaCtx *
hook_frida_new(int32_t * error_kind_out, char ** error_out) {
  // Initialize Frida and create a local-device injector.
  frida_init();
  hook_debug("hook-frida: frida_init done");

  HookFridaCtx * ctx = g_new0(HookFridaCtx, 1);
  ctx->manager = frida_device_manager_new();
  hook_debug("hook-frida: device manager created");
  // Prefer the helper injector for broader macOS compatibility.
  const char * mode = getenv("HOOK_INJECT_INJECTOR");
  if (mode != NULL && g_strcmp0(mode, "inprocess") == 0) {
    ctx->injector = frida_injector_new_inprocess();
  } else {
    ctx->injector = frida_injector_new();
  }
  hook_debug("hook-frida: injector created");

  GError * error = NULL;
  ctx->device = frida_device_manager_get_device_by_type_sync(
      ctx->manager,
      FRIDA_DEVICE_TYPE_LOCAL,
      0,
      NULL,
      &error);
  hook_debug("hook-frida: device lookup finished");
  if (error != NULL) {
    hook_set_error(error, error_kind_out, error_out);
    g_error_free(error);
    hook_frida_free(ctx);
    return NULL;
  }

  if (error_kind_out != NULL)
    *error_kind_out = HOOK_FRIDA_ERROR_NONE;
  return ctx;
}

void
hook_frida_free(HookFridaCtx * ctx) {
  // Release Frida objects and shut down the library.
  if (ctx == NULL)
    return;

  if (ctx->device != NULL)
    g_object_unref(ctx->device);
  if (ctx->manager != NULL)
    g_object_unref(ctx->manager);
  if (ctx->injector != NULL)
    g_object_unref(ctx->injector);

  g_free(ctx);
  frida_shutdown();
}

int
hook_frida_inject_process(HookFridaCtx * ctx,
    int32_t pid,
    const char * library_path,
    const char * entrypoint,
    const char * data,
    uint32_t * out_id,
    int32_t * error_kind_out,
    char ** error_out) {
  if (ctx == NULL || ctx->injector == NULL)
    return 0;

  hook_debug("hook-frida: inject_process starting");
  // Inject the library into an existing process.
  GError * error = NULL;
  guint id = frida_injector_inject_library_file_sync(
      ctx->injector,
      (guint) pid,
      library_path,
      entrypoint,
      data,
      NULL,
      &error);

  if (error != NULL && hook_should_try_device_fallback(error) && ctx->device != NULL) {
    hook_debug("hook-frida: inject_process helper failed, trying device fallback");
    g_error_free(error);
    error = NULL;
    id = frida_device_inject_library_file_sync(
        ctx->device,
        (guint) pid,
        library_path,
        entrypoint,
        data,
        NULL,
        &error);
  }

  if (error != NULL) {
    hook_set_error(error, error_kind_out, error_out);
    g_error_free(error);
    return 0;
  }

  if (out_id != NULL)
    *out_id = id;

  if (error_kind_out != NULL)
    *error_kind_out = HOOK_FRIDA_ERROR_NONE;
  return 1;
}

int
hook_frida_inject_blob(HookFridaCtx * ctx,
    int32_t pid,
    const uint8_t * blob,
    size_t blob_len,
    const char * entrypoint,
    const char * data,
    uint32_t * out_id,
    int32_t * error_kind_out,
    char ** error_out) {
  if (ctx == NULL || ctx->injector == NULL)
    return 0;

  // Inject from an in-memory library blob.
  GError * error = NULL;
  GBytes * bytes = g_bytes_new(blob, blob_len);

  guint id = frida_injector_inject_library_blob_sync(
      ctx->injector,
      (guint) pid,
      bytes,
      entrypoint,
      data,
      NULL,
      &error);

  if (error != NULL && hook_should_try_device_fallback(error) && ctx->device != NULL) {
    g_error_free(error);
    error = NULL;
    id = frida_device_inject_library_blob_sync(
        ctx->device,
        (guint) pid,
        bytes,
        entrypoint,
        data,
        NULL,
        &error);
  }

  g_bytes_unref(bytes);

  if (error != NULL) {
    hook_set_error(error, error_kind_out, error_out);
    g_error_free(error);
    return 0;
  }

  if (out_id != NULL)
    *out_id = id;

  if (error_kind_out != NULL)
    *error_kind_out = HOOK_FRIDA_ERROR_NONE;
  return 1;
}

int
hook_frida_inject_launch(HookFridaCtx * ctx,
    const char * program,
    const char * const * argv,
    const char * const * envp,
    const char * cwd,
    int32_t stdio,
    const char * library_path,
    const char * entrypoint,
    const char * data,
    uint32_t * out_pid,
    uint32_t * out_id,
    int32_t * error_kind_out,
    char ** error_out) {
  if (ctx == NULL || ctx->device == NULL || ctx->injector == NULL)
    return 0;

  // Spawn the process suspended, inject, and resume.
  FridaSpawnOptions * options = frida_spawn_options_new();
  if (argv != NULL)
    g_object_set(options, "argv", argv, NULL);
  if (envp != NULL)
    g_object_set(options, "envp", envp, NULL);
  if (cwd != NULL)
    g_object_set(options, "cwd", cwd, NULL);
  g_object_set(options, "stdio", stdio, NULL);

  GError * error = NULL;
  guint pid = frida_device_spawn_sync(ctx->device, program, options, NULL, &error);
  g_object_unref(options);

  if (error != NULL) {
    hook_set_error(error, error_kind_out, error_out);
    g_error_free(error);
    return 0;
  }

  guint id = frida_injector_inject_library_file_sync(
      ctx->injector,
      pid,
      library_path,
      entrypoint,
      data,
      NULL,
      &error);

  if (error != NULL && hook_should_try_device_fallback(error) && ctx->device != NULL) {
    g_error_free(error);
    error = NULL;
    id = frida_device_inject_library_file_sync(
        ctx->device,
        pid,
        library_path,
        entrypoint,
        data,
        NULL,
        &error);
  }

  if (error != NULL) {
    hook_set_error(error, error_kind_out, error_out);
    g_error_free(error);
    return 0;
  }

  frida_device_resume_sync(ctx->device, pid, NULL, &error);
  if (error != NULL) {
    hook_set_error(error, error_kind_out, error_out);
    g_error_free(error);
    return 0;
  }

  if (out_pid != NULL)
    *out_pid = pid;
  if (out_id != NULL)
    *out_id = id;

  if (error_kind_out != NULL)
    *error_kind_out = HOOK_FRIDA_ERROR_NONE;
  return 1;
}

int
hook_frida_spawn(HookFridaCtx * ctx,
    const char * program,
    const char * const * argv,
    const char * const * envp,
    const char * cwd,
    int32_t stdio,
    uint32_t * out_pid,
    int32_t * error_kind_out,
    char ** error_out) {
  if (ctx == NULL || ctx->device == NULL)
    return 0;

  // Spawn the process suspended; caller is responsible for resuming.
  FridaSpawnOptions * options = frida_spawn_options_new();
  if (argv != NULL)
    g_object_set(options, "argv", argv, NULL);
  if (envp != NULL)
    g_object_set(options, "envp", envp, NULL);
  if (cwd != NULL)
    g_object_set(options, "cwd", cwd, NULL);
  g_object_set(options, "stdio", stdio, NULL);

  GError * error = NULL;
  guint pid = frida_device_spawn_sync(ctx->device, program, options, NULL, &error);
  g_object_unref(options);

  if (error != NULL) {
    hook_set_error(error, error_kind_out, error_out);
    g_error_free(error);
    return 0;
  }

  if (out_pid != NULL)
    *out_pid = pid;

  if (error_kind_out != NULL)
    *error_kind_out = HOOK_FRIDA_ERROR_NONE;
  return 1;
}

int
hook_frida_resume(HookFridaCtx * ctx,
    uint32_t pid,
    int32_t * error_kind_out,
    char ** error_out) {
  if (ctx == NULL || ctx->device == NULL)
    return 0;

  // Resume a process spawned in suspended mode.
  GError * error = NULL;
  frida_device_resume_sync(ctx->device, pid, NULL, &error);

  if (error != NULL) {
    hook_set_error(error, error_kind_out, error_out);
    g_error_free(error);
    return 0;
  }

  if (error_kind_out != NULL)
    *error_kind_out = HOOK_FRIDA_ERROR_NONE;
  return 1;
}

int
hook_frida_demonitor(HookFridaCtx * ctx,
    uint32_t id,
    int32_t * error_kind_out,
    char ** error_out) {
  if (ctx == NULL || ctx->injector == NULL)
    return 0;

  // Stop monitoring the injection.
  GError * error = NULL;
  frida_injector_demonitor_sync(ctx->injector, id, NULL, &error);

  if (error != NULL) {
    hook_set_error(error, error_kind_out, error_out);
    g_error_free(error);
    return 0;
  }

  if (error_kind_out != NULL)
    *error_kind_out = HOOK_FRIDA_ERROR_NONE;
  return 1;
}

void
hook_frida_string_free(char * s) {
  // Free strings returned to Rust.
  if (s != NULL)
    g_free(s);
}