FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
bytecode-viewer/plugins/java/XposedGenerator.java at master · strivegithub/bytecode-viewer · GitHub
strivegithub
bytecode-viewer
Repository navigation
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
bytecode-viewer
/
plugins
/
java
/
XposedGenerator.java
Copy path
More file actions
More file actions
Latest commit
History
History
History
331 lines (290 loc) · 11.8 KB
Breadcrumbs
bytecode-viewer
/
plugins
/
java
/
XposedGenerator.java
Copy path
File metadata and controls
331 lines (290 loc) · 11.8 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
import
java
.
io
.
BufferedWriter
;
import
java
.
io
.
File
;
import
java
.
io
.
FileWriter
;
import
java
.
io
.
IOException
;
import
java
.
util
.
ArrayList
;
import
java
.
util
.
List
;
import
java
.
util
.
Scanner
;
import
java
.
util
.
regex
.
Matcher
;
import
java
.
util
.
regex
.
Pattern
;
import
javax
.
swing
.
Box
;
import
javax
.
swing
.
JComboBox
;
import
javax
.
swing
.
JOptionPane
;
import
javax
.
swing
.
JPanel
;
import
org
.
objectweb
.
asm
.
tree
.
ClassNode
;
import
the
.
bytecode
.
club
.
bytecodeviewer
.*;
import
the
.
bytecode
.
club
.
bytecodeviewer
.
api
.*;
import
the
.
bytecode
.
club
.
bytecodeviewer
.
decompilers
.
impl
.
FernFlowerDecompiler
;
import
the
.
bytecode
.
club
.
bytecodeviewer
.
gui
.
resourceviewer
.
viewer
.
ResourceViewer
;
/**
* * This is an Xposed Generator Plugin, used to aid Reverse-Engineering.
* *
* * @author jowasp
**/
public
class
XposedGenerator
extends
Plugin
{
private
static
final
List
<
String
>
methodsNames
=
new
ArrayList
<>();
private
static
final
List
<
String
>
cleanMethodsNames
=
new
ArrayList
<>();
private
static
String
foundPckg
;
@
Override
public
void
execute
(
List
<
ClassNode
>
classNodeList
)
{
//Get actual file class content
ResourceViewer
viewer
=
BytecodeViewer
.
getActiveResource
();
if
(
viewer
==
null
)
{
BytecodeViewer
.
showMessage
(
"Open A Classfile First"
);
return
;
}
String
className
=
viewer
.
getName
();
ClassNode
classnode
=
BytecodeViewer
.
getCurrentlyOpenedClassNode
();
if
(
classnode
==
null
)
{
BytecodeViewer
.
showMessage
(
"Open A Classfile First"
);
return
;
}
//Call XposedGenerator class
parseChosenFileContent
(
className
,
classnode
);
}
public
static
void
parseChosenFileContent
(
String
classname
,
ClassNode
classNode
)
{
try
{
//Parse content - Extract methods after APK /JAR has been extracted
byte
[]
cont
=
ASMUtil
.
nodeToBytes
(
classNode
);
//Use one of the decompilers
//TODO:Allow users to select other decompilers?
FernFlowerDecompiler
decompilefern
=
new
FernFlowerDecompiler
();
//Decompile using Fern
String
decomp
=
decompilefern
.
decompileClassNode
(
classNode
,
cont
);
String
[]
xposedTemplateTypes
= {
"Empty"
,
"Parameters"
,
"Helper"
};
@
SuppressWarnings
({
"unchecked"
,
"rawtypes"
})
JComboBox
xposedTemplateList
=
new
JComboBox
(
xposedTemplateTypes
);
//Set results of parsed methods into a list
List
<
String
>
methodsExtracted
=
ProcessContentExtractedClass
(
decomp
);
String
packgExtracted
=
ProcessContentExtractedPackage
(
decomp
);
//Get a clean list
List
<
String
>
cleanMethods
;
//clear list
cleanMethods
=
ProcessCleanMethodsAll
(
methodsExtracted
);
if
(!
cleanMethods
.
isEmpty
())
{
JComboBox
<
String
>
cb
=
new
JComboBox
<>(
cleanMethods
.
toArray
(
new
String
[
0
]));
//Add Panel elements
//Start Panel
JPanel
myPanel
=
new
JPanel
();
myPanel
.
add
(
Box
.
createHorizontalStrut
(
15
));
myPanel
.
add
(
xposedTemplateList
);
myPanel
.
add
(
cb
);
//output methods to pane box
int
result
=
JOptionPane
.
showConfirmDialog
(
null
,
myPanel
,
"Choose Template and Method for Xposed Module"
,
JOptionPane
.
OK_CANCEL_OPTION
);
myPanel
.
remove
();
if
(
result
==
JOptionPane
.
OK_OPTION
)
{
//Read Chosen Class
Object
cbItem
=
cb
.
getSelectedItem
();
Object
xPosedItem
=
xposedTemplateList
.
getSelectedItem
();
System
.
out
.
println
(
"SELECTED CLASS is"
+
cbItem
);
if
(
cbItem
!=
null
&&
xPosedItem
!=
null
)
{
String
selectedClass
=
cbItem
.
toString
();
String
selectedXposedTemplate
=
xPosedItem
.
toString
();
//WriteXposed Class with extracted data
try
{
WriteXposedModule
(
selectedClass
,
packgExtracted
,
classname
,
selectedXposedTemplate
);
}
catch
(
IllegalArgumentException
e
)
{
e
.
printStackTrace
();
JOptionPane
.
showMessageDialog
(
null
,
"Error"
+
e
);
}
}
}
}
else
{
JOptionPane
.
showMessageDialog
(
null
,
"Class Not Suitable"
);
}
}
catch
(
IllegalArgumentException
e
)
{
e
.
printStackTrace
();
JOptionPane
.
showMessageDialog
(
null
,
"Error"
+
e
);
}
}
public
static
void
WriteXposedModule
(
String
functionToHook
,
String
packageName
,
String
classToHook
,
String
template
)
{
if
(
template
!=
null
&& !
template
.
equals
(
"Empty"
))
{
try
{
//Extract the package name only
String
packageNameOnly
=
packageName
.
substring
(
8
,
packageName
.
length
() -
2
).
trim
();
String
classToHookNameOnly
=
classToHook
;
if
(
classToHookNameOnly
.
endsWith
(
".class"
))
classToHookNameOnly
=
classToHook
.
substring
(
0
,
classToHookNameOnly
.
length
() -
6
);
String
[]
classClean
=
classToHookNameOnly
.
split
(
"/"
);
String
[]
functionSplitValues
=
functionToHook
.
split
(
"
\\
s+"
);
//select
String
onlyClass
=
classClean
[
classClean
.
length
-
1
];
String
onlyFunction
=
CleanUpFunction
(
functionSplitValues
);
//Write Xposed Class
String
XposedClassText
=
"package androidpentesting.com.xposedmodule;"
+
"
\r
\n
"
+
"import de.robv.android.xposed.IXposedHookLoadPackage;"
+
"
\r
\n
"
+
"
\r
\n
"
+
"import de.robv.android.xposed.XC_MethodHook;"
+
"
\r
\n
"
+
"import de.robv.android.xposed.XposedBridge;"
+
"
\r
\n
"
+
"import de.robv.android.xposed.callbacks.XC_LoadPackage.LoadPackageParam;"
+
"
\r
\n
"
+
"import static de.robv.android.xposed.XposedHelpers.findAndHookMethod;"
+
"
\r
\n
"
+
"
\r
\n
"
+
"public class XposedClassTest implements IXposedHookLoadPackage {"
+
"
\r
\n
"
+
"
\r
\n
"
+
" public void handleLoadPackage(final LoadPackageParam lpparam) throws Throwable {"
+
"
\r
\n
"
+
"
\r
\n
"
+
" String classToHook = "
+
"
\"
"
+
packageNameOnly
+
"."
+
onlyClass
+
"
\"
;"
+
"
\r
\n
"
+
" String functionToHook = "
+
"
\"
"
+
onlyFunction
+
"
\"
;"
+
"
\r
\n
"
+
"
\r
\n
"
+
" if (lpparam.packageName.equals("
+
"
\"
"
+
packageNameOnly
+
"
\"
"
+
")){"
+
"
\r
\n
"
+
" XposedBridge.log("
+
"
\"
Loaded app:
\"
"
+
" + lpparam.packageName);"
+
"
\r
\n
"
+
"
\r
\n
"
+
" findAndHookMethod("
+
"
\"
"
+
onlyClass
+
"
\"
"
+
", lpparam.classLoader, "
+
"
\"
"
+
onlyFunction
+
"
\"
"
+
", int.class,"
+
"
\r
\n
"
+
" new XC_MethodHook() {"
+
"
\r
\n
"
+
" @Override"
+
"
\r
\n
"
+
" protected void beforeHookedMethod(MethodHookParam param) throws Throwable {"
+
"
\r
\n
"
+
" //TO BE FILLED BY ANALYST"
+
"
\r
\n
"
+
" }"
+
"
\r
\n
"
+
" });"
+
"
\r
\n
"
+
" }"
+
"
\r
\n
"
+
" }"
+
"
\r
\n
"
+
"}"
+
"
\r
\n
"
;
PluginConsole
gui
=
new
PluginConsole
(
"Xposed Code Generation"
);
gui
.
appendText
(
XposedClassText
);
gui
.
setVisible
(
true
);
}
catch
(
Exception
e
)
{
e
.
printStackTrace
();
}
}
else
{
JOptionPane
.
showMessageDialog
(
null
,
"Empty Template Chosen, Did Not Generate"
);
}
}
private
static
List
<
String
>
ProcessContentExtractedClass
(
String
contentFile
)
{
Scanner
scanner
=
null
;
try
{
scanner
=
new
Scanner
(
contentFile
);
//@TODO : Improve patterns to match other excepts 'public'
String
regexclass
=
"public"
;
Pattern
pattern
=
Pattern
.
compile
(
regexclass
,
Pattern
.
CASE_INSENSITIVE
);
while
(
scanner
.
hasNextLine
())
{
String
line
=
scanner
.
nextLine
();
// process the line
Matcher
matcher
=
pattern
.
matcher
(
line
);
while
(
matcher
.
find
())
{
if
(
matcher
.
group
() !=
null
)
{
System
.
out
.
println
(
"find() found the pattern
\"
"
+
quote
(
line
.
trim
()));
System
.
out
.
println
(
"Function: "
+
CleanUpFunction
(
line
.
trim
().
split
(
"
\\
s+"
)));
methodsNames
.
add
(
quote
(
line
.
trim
()));
}
else
{
methodsNames
.
add
(
"No methods found"
);
}
}
}
if
(
methodsNames
.
isEmpty
())
methodsNames
.
add
(
"No methods found"
);
else
return
methodsNames
;
return
methodsNames
;
}
finally
{
if
(
scanner
!=
null
)
scanner
.
close
();
}
}
private
static
List
<
String
>
ProcessCleanMethodsAll
(
List
<
String
>
rawMethods
)
{
for
(
String
m
:
rawMethods
)
{
//Exclude class declaration
//TODO:add a list containing all possible types
if
(!
m
.
contains
(
"extends"
) && (!
m
.
contains
(
"implements"
) && (
m
.
contains
(
"("
))))
{
cleanMethodsNames
.
add
(
m
);
}
}
return
cleanMethodsNames
;
}
private
static
String
CleanUpFunction
(
String
[]
rawFunction
)
{
String
onlyFunc
=
"functiondummy"
;
for
(
String
m
:
rawFunction
)
{
if
(
m
.
contains
(
"("
))
{
String
[]
split
=
m
.
split
(
"
\\
("
)[
0
].
split
(
" "
);
return
split
[
split
.
length
-
1
];
}
}
return
onlyFunc
;
}
private
static
String
ProcessContentExtractedPackage
(
String
contentFile
)
{
Scanner
scanner
=
null
;
try
{
scanner
=
new
Scanner
(
contentFile
);
String
regexPkg
=
"package"
;
Pattern
patternPkg
=
Pattern
.
compile
(
regexPkg
,
Pattern
.
CASE_INSENSITIVE
);
String
line
=
scanner
.
nextLine
();
// process the line
Matcher
matcher
=
patternPkg
.
matcher
(
line
);
while
(
matcher
.
find
())
{
if
(
matcher
.
group
() !=
null
)
{
System
.
out
.
println
(
"find() found the pattern
\"
"
+
quote
(
line
.
trim
()));
foundPckg
=
quote
(
line
.
trim
());
}
else
{
foundPckg
=
""
;
}
}
try
{
if
(
foundPckg
==
null
||
foundPckg
.
isEmpty
())
foundPckg
=
"No Package Found"
;
}
catch
(
NullPointerException
e
)
{
JOptionPane
.
showMessageDialog
(
null
,
"Error - no package was found in the selected class: "
+
e
);
}
finally
{
scanner
.
close
();
}
}
catch
(
IllegalArgumentException
e
)
{
JOptionPane
.
showMessageDialog
(
null
,
"Error"
+
e
);
if
(
scanner
!=
null
)
scanner
.
close
();
}
finally
{
if
(
scanner
!=
null
)
scanner
.
close
();
}
return
foundPckg
;
}
private
static
String
quote
(
String
aText
)
{
String
QUOTE
=
"'"
;
return
QUOTE
+
aText
+
QUOTE
;
}
}
Back
|
FazBrowse Home
|
New Git URL