Skip to content

Instantly share code, notes, and snippets.

@tprynn
Last active February 1, 2022 13:34
Show Gist options
  • Save tprynn/711c2caa1687fe66b4c496324089f826 to your computer and use it in GitHub Desktop.
Save tprynn/711c2caa1687fe66b4c496324089f826 to your computer and use it in GitHub Desktop.
Frida utility functions
module.exports = {
hexStringToIntArray: function(str) {
var res = []
for(var i = 0; i < str.length; i += 2) {
res.push(parseInt(str.substring(i, i+2), 16))
}
return res
},
byteArrayToHexString: function(array) {
try {
// Frida has two representations depending on how you get the array
if ("fieldReturnType" in array) {
array = array.value
}
else if (array.type != "byte") {
console.log("Unexpected array type")
}
// The most straightforward way to get the array to a standard JS object
array = JSON.parse(JSON.stringify(array))
var res = "";
for(var i = 0; i < array.length; i++){
var hex = (array[i] & 255).toString(16)
if(hex.length < 2) hex = "0" + hex
res += hex
}
return res
} catch(err) {
console.log(String(err.stack))
return ""
}
},
inspectObject: function(obj) {
const Class = Java.use("java.lang.Class")
const tmp_obj = obj
const fields = Java.cast(tmp_obj.getClass(), Class).getDeclaredFields()
const methods = Java.cast(tmp_obj.getClass(), Class).getMethods()
console.log("inspecting " + tmp_obj.getClass().toString())
console.log("\tfields:")
for (var i in fields) {
console.log("\t\t" + fields[i].toString())
}
console.log("\tmethods:")
for (var i in methods) {
console.log("\t\t" + methods[i].toString())
}
},
inspectJsObj: function(obj) {
console.log(JSON.stringify(obj))
for (var prop in obj) {
const val = prop.startsWith("$") ? "" : obj[prop]
if (obj.hasOwnProperty(prop)) {
console.log("\tself." + prop + " : " + val)
} else {
console.log("\t" + prop + " : " + val)
}
}
},
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment