forked from thouger/frida_script
-
Notifications
You must be signed in to change notification settings - Fork 0
/
so.js
150 lines (134 loc) · 5.96 KB
/
so.js
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
function antiAntiFrida() {
var strstr = Module.findExportByName(null, "strstr");
if (null !== strstr) {
Interceptor.attach(strstr, {
onEnter: function (args) {
this.frida = Boolean(0);
this.haystack = args[0];
this.needle = args[1];
if (this.haystack.readCString() !== null && this.needle.readCString() !== null) {
if (this.haystack.readCString().indexOf("frida") !== -1 ||
this.needle.readCString().indexOf("frida") !== -1 ||
this.haystack.readCString().indexOf("gum-js-loop") !== -1 ||
this.needle.readCString().indexOf("gum-js-loop") !== -1 ||
this.haystack.readCString().indexOf("gmain") !== -1 ||
this.needle.readCString().indexOf("gmain") !== -1 ||
this.haystack.readCString().indexOf("linjector") !== -1 ||
this.needle.readCString().indexOf("linjector") !== -1) {
this.frida = Boolean(1);
}
}
},
onLeave: function (retval) {
if (this.frida) {
retval.replace(ptr("0x0"));
}
}
})
}
}
setImmediate(antiAntiFrida)
var isLite = false;
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");
}
return retval;
}, 'pointer', ['pointer', 'int', 'pointer']));
};
setImmediate(ByPassTracerPid);
function dis(address, number) {
for (var i = 0; i < number; i++) {
var ins = Instruction.parse(address);
// console.log("address:" + address + "--dis:" + ins.toString());
address = ins.next;
}
}
//libc->strstr() 从linker里面找到call_function的地址:趁so代码还未执行前就hook
function hook() {
//call_function("DT_INIT", init_func_, get_realpath());
var linkermodule = Process.getModuleByName("linker");
var call_function_addr = null;
var symbols = linkermodule.enumerateSymbols();
for (var i = 0; i < symbols.length; i++) {
var symbol = symbols[i];
// LogPrint(linkername + "->" + symbol.name + "---" + symbol.address);
if (symbol.name.indexOf("__dl__ZL13call_functionPKcPFviPPcS2_ES0_") != -1) {
call_function_addr = symbol.address;
// console.log("linker->" + symbol.name + "---" + symbol.address)
}
}
Interceptor.attach(call_function_addr, {
onEnter: function (args) {
var type = ptr(args[0]).readUtf8String();
var address = args[1];
var sopath = ptr(args[2]).readUtf8String();
// console.log("loadso:" + sopath + "--addr:" + address + "--type:" + type);
if (sopath.indexOf("libnative-lib.so") != -1) {
var libnativemodule = Process.getModuleByName("xxxx.so");//call_function正在加载目标so,这时就拦截下来
var base = libnativemodule.base;
dis(base.add(0x1234).add(1), 10);
var patchaddr = base.add(0x2345);//改so的机器码,避免待会完全加载后运行时就错过时机了!
Memory.patchCode(patchaddr, 4, patchaddr => {
var cw = new ThumbWriter(patchaddr);
cw.putNop();
cw = new ThumbWriter(patchaddr.add(0x2));
cw.putNop();
cw.flush();
});
// console.log("+++++++++++++++++++++++")
dis(base.add(0x1234).add(1), 10);
// console.log("----------------------")
dis(base.add(0x2345).add(1), 10);
Memory.protect(base.add(0x8E78), 4, 'rwx');
base.add(0x1234).writeByteArray([0x00, 0xbf, 0x00, 0xbf]);
// console.log("+++++++++++++++++++++++")
dis(base.add(0x2345).add(1), 10);
}
}
})
}
function main() {
hook();
}
setImmediate(main);
function so_imports() {
Java.perform(function () {
var process_Obj_Module_Arr = Process.enumerateModules();
for (var i = 0; i < process_Obj_Module_Arr.length; i++) {
var name = process_Obj_Module_Arr[i].name;
if (name.indexOf('.so') != -1) {
const hooks = Module.load(name);
var Imports = hooks.enumerateImports();
for (var i = 0; i < Imports.length; i++) {
var Import = Imports[i];
//so名字,函数类型,函数名称,属于的模块,函数地址
console.log("so:", name, "type:", Import.type, 'name:', Import.name, "module:", Import.module, "address:", Import.address)
}
}
}
});
}
function so_exports() {
Java.perform(function () {
var process_Obj_Module_Arr = Process.enumerateModules();
for (var i = 0; i < process_Obj_Module_Arr.length; i++) {
var name = process_Obj_Module_Arr[i].name;
if (name.indexOf('.so') != -1) {
const hooks = Module.load(name);
var Imports = hooks.enumerateExports();
for (var i = 0; i < Imports.length; i++) {
var Import = Imports[i];
//so名字,函数类型,函数名称,属于的模块,函数地址
console.log("so:", name, "type:", Import.type, 'name:', Import.name, "module:", Import.module, "address:", Import.address)
}
}
}
});
}
// setTimeout(frida_Process, 5000);