-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathMethodXposedize.py
More file actions
221 lines (189 loc) · 8.64 KB
/
MethodXposedize.py
File metadata and controls
221 lines (189 loc) · 8.64 KB
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
# -*- coding: utf-8 -*-
#? shortcut=Shift+P
# Get the JavaScript template of the method for Xposed hook.
# Author: 22s1mple
# Usage:
# - Position the caret on a method name in decompiled source view.
# - Press Shift+P
import string
import re
import collections
import sys
import urllib
from urlparse import urlparse
from com.pnfsoftware.jeb.client.api import IScript
from com.pnfsoftware.jeb.client.api import IScript, IGraphicalClientContext
from com.pnfsoftware.jeb.core import RuntimeProjectUtil
from com.pnfsoftware.jeb.core.events import JebEvent, J
from com.pnfsoftware.jeb.core.output import AbstractUnitRepresentation, UnitRepresentationAdapter
from com.pnfsoftware.jeb.core.units.code import ICodeUnit, ICodeItem
from com.pnfsoftware.jeb.core.units.code.java import IJavaSourceUnit, IJavaStaticField, IJavaNewArray, IJavaConstant, \
IJavaCall, IJavaField, IJavaMethod, IJavaClass
from com.pnfsoftware.jeb.core.actions import ActionTypeHierarchyData
from com.pnfsoftware.jeb.core.actions import ActionRenameData
from com.pnfsoftware.jeb.core.util import DecompilerHelper
from com.pnfsoftware.jeb.core.output.text import ITextDocument
from com.pnfsoftware.jeb.core.units.code.android import IDexUnit
from com.pnfsoftware.jeb.core.actions import ActionOverridesData
from com.pnfsoftware.jeb.core.units import UnitUtil
from com.pnfsoftware.jeb.core.units import UnitAddress
from com.pnfsoftware.jeb.core.actions import Actions, ActionContext, ActionCommentData, ActionRenameData, \
ActionXrefsData
FMT_CLZ = 'Class<?> {class_name} = XposedHelpers.findClass("{class_path}", classLoader);'
FMT_MTD_NO_PARAMS = 'Method {full_method_name} = XposedHelpers.findMethodExact({class_name}, "{method_name}");'
FMT_MTD_WITH_PARAMS = 'Method {full_method_name} = XposedHelpers.findMethodExact({class_name}, "{method_name}", {params});'
FMT_NO_PARAMS = """XposedHelpers.findAndHookMethod("%s", classLoader, "%s", new XC_MethodHook() {
@Override
protected void beforeHookedMethod(MethodHookParam param) throws Throwable {
super.beforeHookedMethod(param);
}
@Override
protected void afterHookedMethod(MethodHookParam param) throws Throwable {
super.afterHookedMethod(param);
}
});"""
FMT_WITH_PARAMS = """XposedHelpers.findAndHookMethod("%s", classLoader, "%s", %s, new XC_MethodHook() {
@Override
protected void beforeHookedMethod(MethodHookParam param) throws Throwable {
super.beforeHookedMethod(param);
}
@Override
protected void afterHookedMethod(MethodHookParam param) throws Throwable {
result = param.getResult();
StringBuffer sb = new StringBuffer();
sb.append("<" + method.getDeclaringClass() + " method=" + MethodDescription(param).toString() + ">\\n");
try {
for (int i = 0; i < args.length; i++) {
sb.append("<Arg index=" + i + ">" + translate(args[i]) + "</Arg>\\n");
}
} catch (Throwable e) {
sb.append("<Error>" + e.getLocalizedMessage() + "</Error>\\n");
} finally {
}
try {
sb.append("<Result>" + translate(result) + "</Result>\\n");
} catch (Throwable e) {
sb.append("<Error>" + e.getLocalizedMessage() + "</Error>\\n");
} finally {
sb.append("</" + method.getDeclaringClass() + " method=" + MethodDescription(param).toString() + ">\\n");
}
XposedBridge.log(sb.toString());
}
});"""
class MethodXposedize(IScript):
def calcItemVal(self, ItemId):
# hack by @JEB Official: only work for 32-bit numbers, may be disabled in the future
return str(hex(ItemId & 0xFFFFFFFF))[:-1].lower()
def getItemOriginalName(self, viewName, itemId, viewAddress):
actCntx = ActionContext(self.focusUnit, Actions.RENAME, itemId, viewAddress)
actData = ActionRenameData()
originalName = ""
if self.focusUnit.prepareExecution(actCntx, actData):
try:
originalName = actData.getOriginalName()
assert (viewName == actData.getCurrentName())
except Exception as e:
print(e)
return originalName
def run(self, ctx):
self.ctx = ctx
engctx = ctx.getEnginesContext()
if not engctx:
print('Back-end engines not initialized')
return
projects = engctx.getProjects()
if not projects:
print('There is no opened project')
return
self.prj = projects[0]
if not isinstance(self.ctx, IGraphicalClientContext):
print('This script must be run within a graphical client')
return
self.focusFragment = ctx.getFocusedFragment()
self.focusUnit = self.focusFragment.getUnit() # JavaSourceUnit
self.activeItem = self.focusFragment.getActiveItem()
self.activeItemVal = self.calcItemVal(self.activeItem.getItemId())
if not isinstance(self.focusUnit, IJavaSourceUnit):
print('This script must be run within IJavaSourceUnit')
return
if not self.focusFragment:
print("You Should pick one method name before run this script.")
return
viewMethodSig = self.focusFragment.getActiveAddress()
self.isInitMethod = "<init>" in viewMethodSig and self.activeItem.toString().find('cid=CLASS_NAME') != 0
clz, mtd = self.findMethodByItemId(viewMethodSig, self.activeItemVal)
if not mtd:
print('Could not find method: %s' % viewMethodSig)
return
paramList = [x.getAddress() for x in mtd.getParameterTypes()]
currentClassName = clz.getName()
currentClassPath = clz.getAddress()[1:-1].replace('/', '.')
realClassName = self.getItemOriginalName(currentClassName, clz.getItemId(), viewMethodSig)
realClassPath = currentClassPath.replace(currentClassName, realClassName, 1)
currentFullClassName = clz.getAddress()[1:-1].replace('/', '_')
currentMethodName = mtd.getName()
realMethodName = self.getItemOriginalName(currentMethodName, mtd.getItemId(), viewMethodSig)
realMethodSig = viewMethodSig.replace(currentMethodName, realMethodName, 1)
print(FMT_CLZ.format(
class_name=currentFullClassName,
class_path=realClassPath
))
if len(paramList) == 0:
print FMT_MTD_NO_PARAMS.format(
full_method_name=currentClassName + "_" + currentMethodName,
class_name=currentFullClassName,
method_name=realMethodName
)
print FMT_NO_PARAMS % (realClassPath, realMethodName)
else:
PL = ', '.join([self.toXposed(x) for x in paramList])
print FMT_MTD_WITH_PARAMS.format(
full_method_name=currentClassName + "_" + currentMethodName,
class_name=currentFullClassName,
method_name=realMethodName,
params=PL
)
print FMT_WITH_PARAMS % (realClassPath, realMethodName, PL)
# copy from [@LeadroyaL/JebScript](https://github.com/LeadroyaL/JebScript/blob/master/FastXposed.py)
def toXposed(self, param):
depth = 0
while param[depth] == '[':
depth += 1
# input: Ljava/lang/String; return: "java.lang.String"
# input: [Ljava/lang/String; return: "java.lang.String[]"
if param[-1] == ';':
return '"' + param[depth + 1:-1].replace('/', '.') + "[]" * depth + '"'
# input: I, return: int.class
# input: [I, return: int[].class
else:
return self.basicTypeMap[param[depth]] + "[]" * depth + ".class"
basicTypeMap = {
'C': u'char',
'B': u'byte',
'D': u'double',
'F': u'float',
'I': u'int',
'J': u'long',
'L': u'ClassName',
'S': u'short',
'Z': u'boolean',
'[': u'Reference',
}
def findMethodByItemId(self, mtdSig, itemId):
self.codeUnit = RuntimeProjectUtil.findUnitsByType(self.prj, ICodeUnit, False)
if not self.codeUnit: return None
for unit in self.codeUnit:
classes = unit.getClasses()
if not classes: continue
for c in classes:
cAddr = c.getAddress()
if not cAddr: continue
if mtdSig.find(cAddr) == 0:
mtdlist = c.getMethods()
if not mtdlist: continue
for m in mtdlist:
if self.isInitMethod and m.getAddress() == mtdSig:
return c, m
elif itemId == self.calcItemVal(m.getItemId()):
return c, m
return None