From cc016cb69f23f068b2bb2eb009346df407c287bb Mon Sep 17 00:00:00 2001 From: Asger Hautop Drewsen Date: Mon, 2 Mar 2015 01:15:46 +0100 Subject: [PATCH 1/3] Get args from manpages for auto-generated tracer --- src/frida/tracer.py | 29 +++++++++++++++++++++++++++-- 1 file changed, 27 insertions(+), 2 deletions(-) diff --git a/src/frida/tracer.py b/src/frida/tracer.py index b24ab4b..e74ff2a 100644 --- a/src/frida/tracer.py +++ b/src/frida/tracer.py @@ -5,6 +5,7 @@ import fnmatch import time import re import binascii +import subprocess from frida.core import ModuleFunction @@ -308,6 +309,30 @@ class Repository(object): self._on_update_callback(function, handler, source) def _create_stub_handler(self, function): + args = "" + argc = 0 + varargs = False + try: + output = subprocess.check_output(["man", "-P", "col -b", "2", function.name], stderr=subprocess.DEVNULL) + match = re.search(r"^SYNOPSIS(?:.|\n)*?((?:^.+$\n)* {5}" + function.name + r"\(.*\n(^.+$\n)*)(?:.|\n)*^DESCRIPTION", output.decode(), re.MULTILINE) + if match: + decl = match.group(1) + for argm in re.finditer(r"([^* ]*)\s*(,|\))", decl): + arg = argm.group(1) + if arg == '...': + args += '+ ", ..."' + varargs = True + continue + + args += '%(pre)s%(arg)s=" + args[%(argc)s]' % {"arg": arg, "argc": argc, "pre": '"' if argc == 0 else '+ ", '} + argc += 1 + + except subprocess.CalledProcessError: + pass + + if args == "": + args = '""' + return """\ /* * Auto-generated by Frida. Please modify to match the signature of %(name)s. @@ -332,7 +357,7 @@ class Repository(object): * use "this" which is an object for keeping state local to an invocation. */ onEnter: function onEnter(log, args, state) { - log("%(name)s()"); + log("%(name)s(" + %(args)s + ")"); }, /** @@ -348,7 +373,7 @@ class Repository(object): onLeave: function onLeave(log, retval, state) { } } -""" % { 'name': function.name } +""" % { "name": function.name, "args": args } class MemoryRepository(Repository): def __init__(self): From 0fdc964480a77f224bb1f8c10e305650a9a77b9a Mon Sep 17 00:00:00 2001 From: Asger Hautop Drewsen Date: Mon, 2 Mar 2015 16:49:00 +0100 Subject: [PATCH 2/3] Also catch OSError --- src/frida/tracer.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/frida/tracer.py b/src/frida/tracer.py index e74ff2a..bed941d 100644 --- a/src/frida/tracer.py +++ b/src/frida/tracer.py @@ -327,7 +327,7 @@ class Repository(object): args += '%(pre)s%(arg)s=" + args[%(argc)s]' % {"arg": arg, "argc": argc, "pre": '"' if argc == 0 else '+ ", '} argc += 1 - except subprocess.CalledProcessError: + except (subprocess.CalledProcessError, OSError): # WindowError or FileNotFoundError pass if args == "": From a0bb52fc0ec88a51a6c37ded6b8ddb2239ad025c Mon Sep 17 00:00:00 2001 From: Asger Hautop Drewsen Date: Mon, 2 Mar 2015 17:23:48 +0100 Subject: [PATCH 3/3] Use os.devnull instead of subprocess.DEVNULL --- src/frida/tracer.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/frida/tracer.py b/src/frida/tracer.py index bed941d..dbd59c8 100644 --- a/src/frida/tracer.py +++ b/src/frida/tracer.py @@ -313,7 +313,8 @@ class Repository(object): argc = 0 varargs = False try: - output = subprocess.check_output(["man", "-P", "col -b", "2", function.name], stderr=subprocess.DEVNULL) + with open(os.devnull, 'w') as devnull: + output = subprocess.check_output(["man", "-P", "col -b", "2", function.name], stderr=devnull) match = re.search(r"^SYNOPSIS(?:.|\n)*?((?:^.+$\n)* {5}" + function.name + r"\(.*\n(^.+$\n)*)(?:.|\n)*^DESCRIPTION", output.decode(), re.MULTILINE) if match: decl = match.group(1)