-
Notifications
You must be signed in to change notification settings - Fork 5
/
realmencryptionhelper_hook.py
65 lines (59 loc) · 1.99 KB
/
realmencryptionhelper_hook.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#!/usr/bin/env python3
#
# Hooks RealmConfiguration Builder to dump the Realm database encryption key.
# The key can be used as-is with Realm Studio to open the database.
#
# Reference: https://realm.io/docs/java/latest/api/io/realm/RealmConfiguration.Builder.html
# @Author: Quentin Kaiser <[email protected]
#
###############################################################################
import frida
import sys
def on_message(message, data):
if "payload" in message:
print("[+] {}".format(message["payload"]))
else:
print("[!] {}".format(message["description"]))
jscode = """
Java.perform(function() {
String.prototype.hexEncode = function(){
var hex, i;
var result = "";
for (i=0; i<this.length; i++) {
hex = this.charCodeAt(i).toString(16);
result += ("000"+hex).slice(-2);
}
return result;
}
var byte_array_to_string = function(value) {
var buffer = Java.array('byte', value);
var result = "";
for(var i = 0; i < buffer.length; ++i){
result += (String.fromCharCode(buffer[i] & 0xff));
}
return result;
}
var RealmConfigurationBuilder = Java.use('io.realm.RealmConfiguration$Builder');
RealmConfigurationBuilder.encryptionKey.implementation = function(key) {
send("RealmConfigurationBuilder.encryptionKey(key=" + byte_array_to_string(key).hexEncode() + ")");
return this.encryptionKey(key);
}
});
"""
if __name__ == "__main__":
if len(sys.argv) < 2:
print("Usage: %s pkg" % (sys.argv[0]))
sys.exit(-1)
device = frida.get_usb_device()
pid = device.spawn([sys.argv[1]])
process = frida.get_usb_device().attach(pid)
print("[+] Attached to {}".format(sys.argv[1]))
script = process.create_script(jscode)
script.on('message', on_message)
script.load()
print("[+] Script loaded.")
device.resume(pid)
try:
sys.stdin.read()
except KeyboardInterrupt as e:
sys.exit(0)