Add skeleton for a function discovery tool

This commit is contained in:
Ole André Vadla Ravnås
2014-01-02 16:35:08 +01:00
parent e9d0f58339
commit 4a6325eb16
4 changed files with 110 additions and 0 deletions
+4
View File
@@ -8,6 +8,10 @@ do_substitution = sed -e 's,[@]pythondir[@],$(pythondir),g' \
-e 's,[@]PACKAGE[@],$(PACKAGE),g' \
-e 's,[@]VERSION[@],$(VERSION),g'
frida-discover: frida-discover.in Makefile
$(do_substitution) < $(srcdir)/frida-discover.in > $@
chmod +x $@
frida-trace: frida-trace.in Makefile
$(do_substitution) < $(srcdir)/frida-trace.in > $@
chmod +x $@
+10
View File
@@ -0,0 +1,10 @@
#!/usr/bin/env python
import sys
sys.path.insert(1, '@pythondir@')
import frida
import frida.discoverer
if __name__ == '__main__':
frida.discoverer.main()
+95
View File
@@ -0,0 +1,95 @@
class Discoverer(object):
def __init__(self):
self._script = None
def start(self, process):
def on_message(message, data):
print message, data
source = self._create_discover_script()
self._script = process._session.create_script(source)
self._script.on("message", on_message)
self._script.load()
def stop(self):
if self._script is not None:
try:
self._script.unload()
except:
pass
self._script = None
def _create_discover_script(self):
return """
var pending = [];
var active = [];
Process.enumerateThreads({
onMatch: function (thread) {
pending.push(thread);
},
onComplete: function () {
var currentThreadId = Process.getCurrentThreadId();
pending = pending.filter(function (thread) {
return thread.id !== currentThreadId;
});
var processNext = function processNext() {
if (pending.length === 0) {
return;
}
active.forEach(function (thread) {
send("unfollow(" + thread.id + ")");
Stalker.unfollow(thread.id);
});
active = pending.splice(0, 4);
active.forEach(function (thread) {
send("follow(" + thread.id + ")");
Stalker.follow(thread.id, {
events: { call: true },
onCallSummary: function (summary) {
send("summary from " + thread.id);
}
});
});
setTimeout(processNext, 2000);
};
setTimeout(processNext, 0);
}
});
"""
def main():
import frida
from optparse import OptionParser
import sys
usage = "usage: %prog [options] process-name-or-id"
parser = OptionParser(usage=usage)
(options, args) = parser.parse_args()
if len(args) != 1:
parser.error("process name or id must be specified")
try:
target = int(args[0])
except:
target = args[0]
try:
process = frida.attach(target)
except Exception, e:
print >> sys.stderr, "Failed to attach: %s" % e
sys.exit(1)
d = Discoverer()
d.start(process)
print "Discovery started"
print "Press ENTER to stop"
raw_input()
print "Stopping..."
d.stop()
process.detach()
frida.shutdown()
sys.exit(0)
if __name__ == '__main__':
main()
+1
View File
@@ -33,6 +33,7 @@ setup(
author_email="ole.andre.ravnas@tillitech.com",
entry_points={
'console_scripts': [
'frida-discover = frida.discoverer:main',
'frida-trace = frida.tracer:main'
]
},