Skip to content

Instantly share code, notes, and snippets.

@tdaron
Created March 7, 2024 09:21
Show Gist options
  • Save tdaron/1ba0a04457f063323fae4739900dff79 to your computer and use it in GitHub Desktop.
Save tdaron/1ba0a04457f063323fae4739900dff79 to your computer and use it in GitHub Desktop.
import frida
import sys
# Define the JavaScript code to be injected
js_code = """
Interceptor.attach(Module.findExportByName(null, "calloc"), {
onEnter: function(args) {
console.log("calloc(" + args[0] + ") called");
console.log("count = "+Interceptor.count);
Interceptor.count++;
var stack = Thread.backtrace(this.context, Backtracer.ACCURATE);
console.log("Stack trace:");
for (var i = 0; i < stack.length; i++) {
var symbol = DebugSymbol.fromAddress(stack[i]);
if (symbol !== null) {
console.log(" " + symbol.toString());
} else {
console.log(" Unknown function");
}
}
}
});
Interceptor.attach(Module.findExportByName(null, "malloc"), {
onEnter: function(args) {
console.log("malloc(" + args[0] + ") called");
console.log("count = "+Interceptor.count);
Interceptor.count++;
var stack = Thread.backtrace(this.context, Backtracer.ACCURATE);
console.log("Stack trace:");
for (var i = 0; i < stack.length; i++) {
var symbol = DebugSymbol.fromAddress(stack[i]);
if (symbol !== null) {
console.log(" " + symbol.toString());
} else {
console.log(" Unknown function");
}
}
}
});
Interceptor.count = 0;
"""
def on_message(message, data):
print(message)
# Adjust this to the path of your compiled C program if necessary
program_path = "./kmeans"
# Spawn the process and attach to it
process = frida.spawn(program_path)
session = frida.attach(process)
# Load the JavaScript code into the Frida session
script = session.create_script(js_code)
script.on('message', on_message)
script.load()
# Continue the execution of the program
frida.resume(process)
# Prevent the Python script from exiting prematurely
input("Press Enter to exit...\n")
session.detach()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment