Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

frida-trace does not hook any functions that are loaded with dlopen #177

Open
jyn514 opened this issue Jan 17, 2025 · 0 comments
Open

frida-trace does not hook any functions that are loaded with dlopen #177

jyn514 opened this issue Jan 17, 2025 · 0 comments

Comments

@jyn514
Copy link

jyn514 commented Jan 17, 2025

Steps to reproduce:

  1. Create a project like this:
  // main.c
  #include <dlfcn.h>
  #include <stdio.h>
  #include <error.h>

  int main() {
    void *handle = dlopen("./shared.so", RTLD_NOW);
    if (!handle) error(1, 0, "%s", dlerror());
    // Clear dlerror.
    dlerror();
    char *(*foo)() = dlsym(handle, "foo");
    char *last_error = dlerror();
    if (last_error) error(1, 0, "%s", last_error);
    puts(foo());
  }

─────────────────────────────────────────────────────────────

  // shared.c
  char *foo() {
    return "shared function";
  }
  1. Build the project: gcc -g -shared -o shared.so shared.c && gcc -g -o main main.c
  2. Run main under frida-trace: frida-trace -s foo -- ./main. Observe that it says "Started tracing 0 functions."

I think this code here needs to also run whenever dlopen is called (and maybe also on dlclose?): https://github.com/frida/frida-tools/blob/main/agents/tracer/agent.ts#L198-L217

I tried this patch but I couldn't get frida-tools to build locally.

diff --git a/agents/tracer/agent.ts b/agents/tracer/agent.ts
index db24781..7030d01 100644
--- a/agents/tracer/agent.ts
+++ b/agents/tracer/agent.ts
@@ -31,13 +31,28 @@ class Agent {
             }
         }
 
-        this.start(spec).catch(e => {
+        let start = () => this.start(spec).catch(e => {
             send({
                 type: "agent:error",
                 message: e.message
             });
         });
 
+        start();
+
+        Interceptor.attach(Module.getExportByName('libc.so', 'dlopen'), {
+            onEnter: function (args) {
+                let path = args[0].readCString();
+                console.log(`rehook functions on dlopen(${path})`);
+            },
+            onLeave: async function (retval) {
+                if(!retval.isNull()) {
+                    await start();
+                    console.log("hooked dlopen");
+                }
+            }
+        });
+
         return {
             id: Process.id,
             platform: Process.platform,
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant