Skip to content

Commit

Permalink
Merge pull request r0ysue#3 from SeeFlowerX/main
Browse files Browse the repository at this point in the history
给console日志记录添加自定义颜色函数
  • Loading branch information
r0ysue authored Feb 27, 2021
2 parents afef201 + 34c0bdf commit 1152749
Showing 1 changed file with 55 additions and 25 deletions.
80 changes: 55 additions & 25 deletions r0tracer.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,32 @@
var ByPassTracerPid = function () {
var fgetsPtr = Module.findExportByName("libc.so", "fgets");
var fgets = new NativeFunction(fgetsPtr, 'pointer', ['pointer', 'int', 'pointer']);
Interceptor.replace(fgetsPtr, new NativeCallback(function (buffer, size, fp) {
var retval = fgets(buffer, size, fp);
var bufstr = Memory.readUtf8String(buffer);
if (bufstr.indexOf("TracerPid:") > -1) {
Memory.writeUtf8String(buffer, "TracerPid:\t0");
console.log("tracerpid replaced: " + Memory.readUtf8String(buffer));
}
return retval;
}, 'pointer', ['pointer', 'int', 'pointer']));
};
setImmediate(ByPassTracerPid);

(function(){
let Color = {RESET: "\x1b[39;49;00m", Black: "0;01", Blue: "4;01", Cyan: "6;01", Gray: "7;11", "Green": "2;01", Purple: "5;01", Red: "1;01", Yellow: "3;01"};
let LightColor = {RESET: "\x1b[39;49;00m", Black: "0;11", Blue: "4;11", Cyan: "6;11", Gray: "7;01", "Green": "2;11", Purple: "5;11", Red: "1;11", Yellow: "3;11"};
var colorPrefix = '\x1b[3', colorSuffix = 'm'
for (let c in Color){
if (c == "RESET") continue;
console[c] = function(message){
console.log(colorPrefix + Color[c] + colorSuffix + message + Color.RESET);
}
console["Light" + c] = function(message){
console.log(colorPrefix + LightColor[c] + colorSuffix + message + Color.RESET);
}
}
})();
function uniqBy(array, key) {
var seen = {};
return array.filter(function (item) {
Expand Down Expand Up @@ -43,7 +72,7 @@ function inspectObject(obj, input) {
if (isInstance || Boolean(fields[i].toString().indexOf("static ") >= 0)) {
// output = output.concat("\t\t static static static " + fields[i].toString());
var className = obj_class.toString().trim().split(" ")[1];
// console.log("className is => ",className);
// console.Red("className is => ",className);
var fieldName = fields[i].toString().split(className.concat(".")).pop();
var fieldType = fields[i].toString().split(" ").slice(-2)[0];
var fieldValue = undefined;
Expand All @@ -64,7 +93,7 @@ function traceMethod(targetClassMethod) {
var targetMethod = targetClassMethod.slice(delim + 1, targetClassMethod.length)
var hook = Java.use(targetClass);
var overloadCount = hook[targetMethod].overloads.length;
console.log("Tracing Method : " + targetClassMethod + " [" + overloadCount + " overload(s)]");
console.Red("Tracing Method : " + targetClassMethod + " [" + overloadCount + " overload(s)]");
for (var i = 0; i < overloadCount; i++) {
hook[targetMethod].overloads[i].implementation = function () {
//初始化输出
Expand All @@ -73,31 +102,32 @@ function traceMethod(targetClassMethod) {
for (var p = 0; p < 100; p++) {
output = output.concat("==");
}
output = output.concat("\r\n")
console.Gray(output);
var output = "";
//域值
output = inspectObject(this, output);
console.Blue(output);
var output = "";
//进入函数
output = output.concat("\n*** entered " + targetClassMethod);
output = output.concat("\r\n")
if (arguments.length) console.log();
if (arguments.length) console.Black();
//参数
for (var j = 0; j < arguments.length; j++) {
output = output.concat("arg[" + j + "]: " + arguments[j] + " => " + JSON.stringify(arguments[j]));
output = output.concat("\r\n")
}
//调用栈
output = output.concat(Java.use("android.util.Log").getStackTraceString(Java.use("java.lang.Throwable").$new()));
output = output.concat("\r\n")
console.Green(output);
var output = "";
var retval = this[targetMethod].apply(this, arguments);
//返回值
output = output.concat("\nretval: " + retval + " => " + JSON.stringify(retval));
output = output.concat("\r\n")
// inspectObject(this)
// inspectObject(this)
//离开函数
output = output.concat("\n*** exiting " + targetClassMethod);
output = output.concat("\r\n")
//最终输出
console.log(output);
console.Black(output);
return retval;
}
}
Expand Down Expand Up @@ -138,63 +168,63 @@ function traceClass(targetClass) {
for (var p = 0; p < 100; p++) {
output = output.concat("+");
}
console.warn(output);
console.Green(output);
}
function hook(white, black, target = null) {
console.log("start")
console.Red("start")
if (!(target === null)) {
console.warn("Begin enumerateClassLoaders ...")
console.LightGreen("Begin enumerateClassLoaders ...")
Java.enumerateClassLoaders({
onMatch: function (loader) {
try {
if (loader.findClass(target)) {
console.log("Successfully found loader")
console.log(loader);
console.Red("Successfully found loader")
console.Blue(loader);
Java.classFactory.loader = loader;
console.log("Switch Classloader Successfully ! ")
console.Red("Switch Classloader Successfully ! ")
}
}
catch (error) {
console.log(" continuing :" + error)
console.Red(" continuing :" + error)
}
},
onComplete: function () {
console.log("EnumerateClassloader END")
console.Red("EnumerateClassloader END")
}
})
}
console.warn("Begin Search Class...")
console.Red("Begin Search Class...")
var targetClasses = new Array();
Java.enumerateLoadedClasses({
onMatch: function (className) {
if (className.toString().indexOf(white) >= 0 &&
className.toString().indexOf(black) < 0
) {
console.log("Found Class => ", className)
console.Black("Found Class => ", className)
targetClasses.push(className);
traceClass(className);
}
}, onComplete: function () {
console.log("Search Class Completed!")
console.Black("Search Class Completed!")
}
})
var output = "On Total Tracing :"+String(targetClasses.length)+" classes :\r\n";
targetClasses.forEach(function(target){
output = output.concat(target);
output = output.concat("\r\n")
})
console.warn(output+"Start Tracing ...")
console.Green(output+"Start Tracing ...")
}
function main() {
Java.perform(function () {
console.warn("r0tracer begin ... !")
console.Purple("r0tracer begin ... !")
/*
//以下三种模式,取消注释某一行以开启
*/
//A. 简易trace单个函数
// traceClass("javax.crypto.Cipher")
traceClass("javax.crypto.Cipher")
//B. 黑白名单trace多个函数,第一个参数是白名单(包含关键字),第二个参数是黑名单(不包含的关键字)
hook("javax.crypto.Cipher", "$");
// hook("javax.crypto.Cipher", "$");
//C. 报某个类找不到时,将某个类名填写到第三个参数,比如找不到com.roysue.check类。(前两个参数依旧是黑白名单)
// hook("com.roysue.check"," ","com.roysue.check");
})
Expand Down

0 comments on commit 1152749

Please sign in to comment.