-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRenameObfuscatedMethodAndField.py
More file actions
106 lines (86 loc) · 3.01 KB
/
RenameObfuscatedMethodAndField.py
File metadata and controls
106 lines (86 loc) · 3.01 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
#? name=Rename Obfuscated Classes, shortcut=Ctrl+Shift+R, author=Masata Nishida
# JEB sample script
# http://www.android-decompiler.com/
#
# RenameObfuscatedMethodAndField.py
# Rename obfuscated method and field names.
#
# Run in Jeb2 API.
#
# Copyright (c) 2013 SecureBrain
import re
from com.pnfsoftware.jeb.client.api import IScript
from com.pnfsoftware.jeb.core import RuntimeProjectUtil
from com.pnfsoftware.jeb.core.units.code import ICodeUnit
from com.pnfsoftware.jeb.core.actions import Actions, ActionContext
from com.pnfsoftware.jeb.core.actions import ActionRenameData
import string
class RenameObfuscatedMethodAndField(IScript):
METHOD = 1
FIELD = 2
def run(self, ctx):
engctx = ctx.getEnginesContext()
if not engctx:
print('Back-end engines not initialized')
return
prj = engctx.getProject(0)
if not prj:
print('There is no opened project')
return
codeUnits = RuntimeProjectUtil.findUnitsByType(prj, ICodeUnit, False)
if not codeUnits:
return
self.unit = codeUnits[0]
self.ctx = ctx
# rename obfuscated names
classes = self.unit.getClasses()
for cls in classes:
self.deobfuscate(cls, RenameObfuscatedMethodAndField.METHOD)
self.deobfuscate(cls, RenameObfuscatedMethodAndField.FIELD)
def deobfuscate(self, cls, nType):
if nType == RenameObfuscatedMethodAndField.METHOD:
objs = cls.getMethods()
elif nType == RenameObfuscatedMethodAndField.FIELD:
objs = cls.getFields()
else:
print("Unknown name type")
return
if not objs:
return
names = [o.getName(True) for o in objs]
count = 1
for o in objs:
sign = o.getSignature(True)
name = o.getName(True)
if re.search('[a-zA-Z]+', name):
continue
while True:
new_name = self.gen_name(count)
count += 1
if new_name not in names:
break
if self.rename(o, new_name):
print(u'rename ' + sign + u' to ' + new_name)
def gen_name(self, count):
out = u''
while count:
out += string.ascii_lowercase[count % 26]
count /= 52
return out
def rename(self, obj, new_name):
actCntx = ActionContext(self.unit, Actions.RENAME, obj.getItemId(), obj.getAddress())
actData = ActionRenameData()
actData.setNewName(new_name)
if self.unit.prepareExecution(actCntx, actData):
try:
res = self.unit.executeAction(actCntx, actData)
if not res:
print(u'rename failed [new_name %s]' % new_name)
return False
else:
return True
except Exception,e:
print(e)
return False
else:
return False