frida-utils/scripts/utils.ts

85 lines
2.8 KiB
TypeScript
Raw Normal View History

2024-03-19 10:24:23 +01:00
global.allocString = function allocString(str: string): NativePointer {
2024-03-12 21:49:02 +01:00
return Memory.allocUtf8String(str);
2024-03-19 10:24:23 +01:00
};
2024-03-17 12:12:28 +01:00
global.backtrace = function backtrace(): DebugSymbol[] {
2024-03-12 21:49:02 +01:00
const context = Process.enumerateThreads()[0].context;
const backtrace = Thread.backtrace(context);
const symbols = backtrace.map(address => {
return DebugSymbol.fromAddress(address);
});
return symbols;
2024-03-17 12:12:28 +01:00
};
let resume = false;
global.pauseAt = function pauseAt(name: string): void {
2024-03-12 21:49:02 +01:00
const addr = DebugSymbol.getFunctionByName(name);
Interceptor.attach(addr, {
onEnter() {
2024-03-17 12:12:28 +01:00
while (!resume) {
Thread.sleep(1);
}
resume = false;
2024-03-19 10:24:23 +01:00
console.debug("Resumed");
2024-03-12 21:49:02 +01:00
}
});
2024-03-17 12:12:28 +01:00
};
global.resumeBreakpoints = function resumeBreakpoints(): void {
resume = true;
};
global.backtraceAt = function backtraceAt(name: string): void {
2024-03-12 21:49:02 +01:00
const addr = DebugSymbol.getFunctionByName(name);
Interceptor.attach(addr, {
onEnter() {
const backtrace = Thread.backtrace(this.context);
const symbols = backtrace.map(address => {
return DebugSymbol.fromAddress(address);
});
2024-03-19 10:24:23 +01:00
console.debug(JSON.stringify(symbols, null, 4));
2024-03-12 21:49:02 +01:00
}
});
2024-03-17 12:12:28 +01:00
};
global.getFunc = function getFunc(name: string, ret: NativeFunctionReturnType, args: NativeFunctionArgumentType[]): NativeFunction<NativeFunctionReturnValue, NativeFunctionArgumentValue[]> {
2024-03-12 21:49:02 +01:00
const addr = DebugSymbol.getFunctionByName(name);
return new NativeFunction(addr, ret, args);
2024-03-17 12:12:28 +01:00
};
2024-03-19 10:24:23 +01:00
global.readPointer = function readPointer(pointer: NativePointer, type: string): string {
switch (type) {
case "void":
return "void";
case "int":
return pointer.readInt().toString();
case "pointer":
return "\"" + pointer.readUtf8String()! + "\"";
default:
return "undefined";
}
};
global.trace = function trace(name: string, ret_type: string, args_type: string[]): void {
const addr = DebugSymbol.getFunctionByName(name);
Interceptor.attach(addr, {
onEnter(args: InvocationArguments) {
const args_str: string[] = [];
for (let i = 0; i < args_type.length; i++) {
args_str.push(readPointer(args[i], args_type[i]));
}
console.debug(`-> ${name}(${args_str.join(" ")})`);
},
onLeave(retval: InvocationReturnValue) {
console.debug(`<- ${name} = ${readPointer(retval, ret_type)}`);
}
});
};
2024-03-17 12:12:28 +01:00
export { };
declare global {
function allocString(str: string): NativePointer;
function backtrace(): DebugSymbol[];
function pauseAt(name: string): void;
function backtraceAt(name: string): void;
function getFunc(name: string, ret: NativeFunctionReturnType, args: NativeFunctionArgumentType[]): NativeFunction<NativeFunctionReturnValue, NativeFunctionArgumentValue[]>;
function resumeBreakpoints(): void;
2024-03-19 10:24:23 +01:00
function readPointer(pointer: NativePointer, type: string): string;
function trace(name: string, ret_str: string, args_str: string[]): void;
2024-03-12 21:49:02 +01:00
}