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
|
/*
* Fernflower - The Analytical Java Decompiler
* http://www.reversed-java.com
*
* (C) 2008 - 2010, Stiver
*
* This software is NEITHER public domain NOR free software
* as per GNU License. See license.txt for more details.
*
* This software is distributed WITHOUT ANY WARRANTY; without
* even the implied warranty of MERCHANTABILITY or FITNESS FOR
* A PARTICULAR PURPOSE.
*/
package org.jetbrains.java.decompiler.modules.decompiler.deobfuscator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Set;
import org.jetbrains.java.decompiler.modules.decompiler.StatEdge;
import org.jetbrains.java.decompiler.modules.decompiler.stats.BasicBlockStatement;
import org.jetbrains.java.decompiler.modules.decompiler.stats.Statement;
public class IrreducibleCFGDeobfuscator {
public static boolean isStatementIrreducible(Statement statement) {
class Node {
public Integer id;
public Set<Node> preds = new HashSet<Node>();
public Set<Node> succs = new HashSet<Node>();
public Node(Integer id) {this.id = id;}
}
HashMap<Integer, Node> mapNodes = new HashMap<Integer, Node>();
// checking exceptions and creating nodes
for(Statement stat : statement.getStats()) {
if(!stat.getSuccessorEdges(StatEdge.TYPE_EXCEPTION).isEmpty()) {
return false;
}
mapNodes.put(stat.id, new Node(stat.id));
}
// connecting nodes
for(Statement stat : statement.getStats()) {
Node node = mapNodes.get(stat.id);
for(Statement succ : stat.getNeighbours(StatEdge.TYPE_REGULAR, Statement.DIRECTION_FORWARD)) {
Node nodeSucc = mapNodes.get(succ.id);
node.succs.add(nodeSucc);
nodeSucc.preds.add(node);
}
}
// transforming and reducing the graph
for(;;) {
int ttype = 0;
Node node = null;
for(Node nd : mapNodes.values()) {
if(nd.succs.contains(nd)) { // T1
ttype = 1;
} else if(nd.preds.size() == 1) { // T2
ttype = 2;
}
if(ttype != 0) {
node = nd;
break;
}
}
if(node != null) {
if(ttype == 1) {
node.succs.remove(node);
node.preds.remove(node);
} else {
Node pred = node.preds.iterator().next();
pred.succs.addAll(node.succs);
pred.succs.remove(node);
for(Node succ : node.succs) {
succ.preds.remove(node);
succ.preds.add(pred);
}
mapNodes.remove(node.id);
}
} else { // no transformation applicable
return mapNodes.size() > 1; // reducible iff one node remains
}
}
}
private static Statement getCandidateForSplitting(Statement statement) {
Statement candidateForSplitting = null;
int sizeCandidateForSplitting = Integer.MAX_VALUE;
int succsCandidateForSplitting = Integer.MAX_VALUE;
for(Statement stat : statement.getStats()) {
Set<Statement> setPreds = stat.getNeighboursSet(StatEdge.TYPE_REGULAR, Statement.DIRECTION_BACKWARD);
if(setPreds.size() > 1) {
int succCount = stat.getNeighboursSet(StatEdge.TYPE_REGULAR, Statement.DIRECTION_FORWARD).size();
if(succCount <= succsCandidateForSplitting) {
int size = getStatementSize(stat)*(setPreds.size()-1);
if(succCount < succsCandidateForSplitting ||
size < sizeCandidateForSplitting) {
candidateForSplitting = stat;
sizeCandidateForSplitting = size;
succsCandidateForSplitting = succCount;
}
}
}
}
return candidateForSplitting;
}
public static boolean splitIrreducibleNode(Statement statement) {
Statement splitnode = getCandidateForSplitting(statement);
if(splitnode == null) {
return false;
}
StatEdge enteredge = splitnode.getPredecessorEdges(StatEdge.TYPE_REGULAR).iterator().next();
// copy the smallest statement
Statement splitcopy = copyStatement(splitnode, null, new HashMap<Statement, Statement>());
initCopiedStatement(splitcopy);
// insert the copy
splitcopy.setParent(statement);
statement.getStats().addWithKey(splitcopy, splitcopy.id);
// switch input edges
for(StatEdge prededge : splitnode.getPredecessorEdges(Statement.STATEDGE_DIRECT_ALL)) {
if(prededge.getSource() == enteredge.getSource() ||
prededge.closure == enteredge.getSource()) {
splitnode.removePredecessor(prededge);
prededge.getSource().changeEdgeNode(Statement.DIRECTION_FORWARD, prededge, splitcopy);
splitcopy.addPredecessor(prededge);
}
}
// connect successors
for(StatEdge succ : splitnode.getSuccessorEdges(Statement.STATEDGE_DIRECT_ALL)) {
splitcopy.addSuccessor(new StatEdge(succ.getType(), splitcopy, succ.getDestination(), succ.closure));
}
return true;
}
private static int getStatementSize(Statement statement) {
int res = 0;
if(statement.type == Statement.TYPE_BASICBLOCK) {
res = ((BasicBlockStatement)statement).getBlock().getSeq().length();
} else {
for(Statement stat: statement.getStats()) {
res+=getStatementSize(stat);
}
}
return res;
}
private static Statement copyStatement(Statement from, Statement to, HashMap<Statement, Statement> mapAltToCopies) {
if(to == null) {
// first outer invocation
to = from.getSimpleCopy();
mapAltToCopies.put(from, to);
}
// copy statements
for(Statement st : from.getStats()) {
Statement stcopy = st.getSimpleCopy();
to.getStats().addWithKey(stcopy, stcopy.id);
mapAltToCopies.put(st, stcopy);
}
// copy edges
for(int i=0;i<from.getStats().size();i++) {
Statement stold = from.getStats().get(i);
Statement stnew = to.getStats().get(i);
for(StatEdge edgeold : stold.getSuccessorEdges(Statement.STATEDGE_DIRECT_ALL)) {
// type cannot be TYPE_EXCEPTION (checked in isIrreducibleTriangle)
StatEdge edgenew = new StatEdge(edgeold.getType(), stnew,
mapAltToCopies.containsKey(edgeold.getDestination())?mapAltToCopies.get(edgeold.getDestination()):edgeold.getDestination(),
mapAltToCopies.containsKey(edgeold.closure)?mapAltToCopies.get(edgeold.closure):edgeold.closure);
stnew.addSuccessor(edgenew);
}
}
// recurse statements
for(int i=0;i<from.getStats().size();i++) {
Statement stold = from.getStats().get(i);
Statement stnew = to.getStats().get(i);
copyStatement(stold, stnew, mapAltToCopies);
}
return to;
}
private static void initCopiedStatement(Statement statement) {
statement.initSimpleCopy();
statement.setCopied(true);
for(Statement st : statement.getStats()) {
st.setParent(statement);
initCopiedStatement(st);
}
}
}
|