Skip to content

Commit

Permalink
feat(obf): fixed exempts, merged enterprise fixes, improved speed
Browse files Browse the repository at this point in the history
  • Loading branch information
terminalsin committed Dec 4, 2023
1 parent 8aebf83 commit 4bb9d94
Show file tree
Hide file tree
Showing 17 changed files with 388 additions and 61 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@
import dev.skidfuscator.obfuscator.transform.impl.SwitchTransformer;
import dev.skidfuscator.obfuscator.transform.impl.flow.*;
import dev.skidfuscator.obfuscator.transform.impl.flow.condition.BasicConditionTransformer;
import dev.skidfuscator.obfuscator.transform.impl.flow.driver.DriverTransformer;
import dev.skidfuscator.obfuscator.transform.impl.flow.exception.BasicExceptionTransformer;
import dev.skidfuscator.obfuscator.transform.impl.misc.AhegaoTransformer;
import dev.skidfuscator.obfuscator.transform.impl.number.NumberTransformer;
Expand Down Expand Up @@ -74,8 +73,10 @@
import org.piwik.java.tracking.PiwikRequest;
import org.topdank.byteengineer.commons.data.JarClassData;
import org.topdank.byteengineer.commons.data.JarContents;
import org.topdank.byteengineer.commons.data.JarResource;

import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.nio.file.*;
import java.util.*;
Expand Down Expand Up @@ -303,7 +304,7 @@ public void run() {
System.out.println("┌────────────────────────────[ Results ]────────────────────────────┐");

for (Transformer transformer : transformers) {
System.out.println("│ " + pad(transformer.getResult(), 130) + "│");
System.out.println("│ " + pad(transformer.getResult().replace("Annotation", "@"), 130) + "│");
}
System.out.println("└───────────────────────────────────────────────────────────────────┘\n\n");

Expand Down Expand Up @@ -345,8 +346,8 @@ public void run() {
EventBus.end();

_cleanup();
_dump();

_dump();

SkidProgressBar.RENDER_THREAD.shutdown();
IntegerBlockPredicateRenderer.DEBUG = false;
Expand Down Expand Up @@ -632,7 +633,7 @@ protected List<Transformer> _loadTransformer() {
public List<Transformer> getTransformers() {
final List<Transformer> transformers = new ArrayList<>();

if (true) {
if (false) {
if (tsConfig.hasPath("stringEncryption.type")) {
switch (tsConfig.getEnum(StringEncryptionType.class, "stringEncryption.type")) {
case STANDARD: transformers.add(new StringTransformer(this)); break;
Expand All @@ -648,10 +649,6 @@ public List<Transformer> getTransformers() {
new BasicConditionTransformer(this),
new BasicExceptionTransformer(this),
new BasicRangeTransformer(this),

// Patch
new DriverTransformer(this),

/*
new FlatteningFlowTransformer(this),*/
new AhegaoTransformer(this)
Expand Down Expand Up @@ -688,7 +685,7 @@ private void _verify() {
try {
classSource.getClassTree().verify();
} catch (Exception e) {
LOGGER.warn("\n" +
LOGGER.error("\n" +
"-----------------------------------------------------\n"
+ "/!\\ Skidfuscator failed to compute some libraries!\n"
+ "It it advised to read https://github.com/terminalsin/skidfuscator-java-obfuscator/wiki/Libraries\n"
Expand All @@ -699,7 +696,7 @@ private void _verify() {
: " " + e.getCause().getMessage() + "\n"
)
+ "-----------------------------------------------------\n"
);
, e);

if (!CLOUD)
System.exit(1);
Expand Down Expand Up @@ -820,6 +817,9 @@ private void run(final String phaseName, final Caller caller) {
exemptAnalysis.isExempt(
(Class<? extends Transformer>) listenerClazz,
e
) || exemptAnalysis.isExempt(
(Class<? extends Transformer>) listenerClazz,
e.owner
)
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,25 @@ public SkidClassTree(ApplicationClassSource source, boolean allowPhantomClasses,
this.skidfuscator = skidfuscator;
}

@Override
public void init() {
for (ClassNode node : source.iterate()) {
if (skidfuscator.getExemptAnalysis().isExempt(node))
continue;

addVertex(node);
}
}

@Override
public void verify() {
try (final ProgressWrapper wrapper = ProgressUtil.progressCheck(
source.size(),
"Verified classpath for " + source.size() + " classes"
)){
for (ClassNode node : source.iterate()) {
verifyVertex(node);
wrapper.tick();
verifyVertex(node);
}
}
}
Expand All @@ -37,6 +47,29 @@ public void verify() {
public void verifyVertex(ClassNode cn) {
if (skidfuscator.getExemptAnalysis().isExempt(cn))
return;
super.verifyVertex(cn);

if(cn == null) {
throw new IllegalStateException("Vertex is null!");
}

if (!containsVertex(cn)) {
addVertex(cn);
}

if(cn != rootNode) {
ClassNode sup = cn.node.superName != null
? requestClass0(cn.node.superName, cn.getName())
: rootNode;
if(sup == null) {
throw new IllegalStateException(String.format("No superclass %s for %s", cn.node.superName, cn.getName()));
}

for (String s : cn.node.interfaces) {
ClassNode iface = requestClass0(s, cn.getName());
if(iface == null) {
throw new IllegalStateException(String.format("No superinterface %s for %s", s, cn.getName()));
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -1368,6 +1368,10 @@ private void fixRanges() {
range.getNodes().stream().filter(BasicBlock::isEmpty).forEach(e -> {
e.add(new NopStmt());
});

if (range.getNodes().isEmpty()) {
cfg.removeRange(range);
}
}
}

Expand Down Expand Up @@ -1629,7 +1633,7 @@ private void dumpRange(ExceptionRange<BasicBlock> er) {
BasicBlock nextBlock = range.get(rangeIdx + 1);
int nextOrderIdx = order.indexOf(nextBlock);
if (nextOrderIdx - orderIdx > 1) { // blocks in-between, end the handler and begin anew
System.err.println("\r\n[warn] Had to split up a range: " + m + "\n");
Skidfuscator.LOGGER.post("\r\n[warn] Had to split up a range: " + m + "\n");
Label end = getLabel(order.get(orderIdx + 1));
assert start != end : "Label assigned is semantically identical.";
m.node.visitTryCatchBlock(start, end, handler, type.getInternalName());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@
import org.mapleir.ir.code.expr.invoke.*;
import org.objectweb.asm.*;
import org.objectweb.asm.commons.JSRInlinerAdapter;
import org.objectweb.asm.tree.AnnotationNode;
import org.objectweb.asm.tree.TypeAnnotationNode;
import org.objectweb.asm.tree.*;

import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
Expand Down Expand Up @@ -103,6 +102,7 @@ public int compare(MethodNode o1, MethodNode o2) {
return args1.size() - args2.size();
}
}).forEach(method -> {
skidfuscator.getIrFactory().getFor(method);
getGroup(skidfuscator, method);

if (method.node.visibleAnnotations != null) {
Expand Down Expand Up @@ -243,17 +243,93 @@ public void recacheInvokes() {
}

private void setupInvoke() {
final List<ClassNode> nodes = skidfuscator
.getClassSource()
.getClassTree()
.vertices()
.stream()
.filter(e -> {
return skidfuscator.getClassSource().isApplicationClass(e.getName());
})
.collect(Collectors.toList());
try (ProgressWrapper invocationBar = ProgressUtil.progressCheck(
nodes.size(),
"Resolved invocation path for " + nodes.size() + " nodes"
)) {
nodes.forEach(c -> {
for (MethodNode method : c.getMethods()) {
final SkidControlFlowGraph cfg = (SkidControlFlowGraph) skidfuscator.getIrFactory().getUnsafe(method);
if (cfg == null) {
for (SkidMethodNode skidMethod : methods) {
for (AbstractInsnNode instruction : skidMethod.node.instructions) {

final SkidControlFlowGraph cfg = skidfuscator.getIrFactory().getFor(method);
final ClassMethodHash target;
final SkidInvocation skidInvocation;

if (cfg == null) {
System.err.println("Failed to compute CFG for method " + method.toString());
if (instruction instanceof InvokeDynamicInsnNode) {
final InvokeDynamicInsnNode e = (InvokeDynamicInsnNode) instruction;

if (!e.bsm.getOwner().equals("java/lang/invoke/LambdaMetafactory")
|| !e.bsm.getName().equals("metafactory")) {
return;
//throw new IllegalStateException("Invalid invoke dynamic!");
}

assert (e.bsmArgs.length == 3 && e.bsmArgs[1] instanceof Handle);
final Handle boundFunc = (Handle) e.bsmArgs[1];

// Patch for implicit funtions
// TODO: Fix this
if (boundFunc.getName().startsWith("lambda$new$")) {
final String returnType = Type.getReturnType(e.desc).getClassName().replace(".", "/");
//System.out.println("Attempting to locate " + returnType);
final ClassNode targetClass = skidfuscator.getClassSource().findClassNode(returnType);

if (!(targetClass instanceof SkidClassNode))
return;

assert targetClass.getMethods().size() == 1 : "Implicit Function must be single method!";
final SkidMethodNode methodNode = (SkidMethodNode) targetClass.getMethods().get(0);

methodNode.getGroup().setImplicitFunction(true);
//System.out.println("Found implicit function: " + methodNode.toString());
return;
}

target = new ClassMethodHash(boundFunc.getName(), boundFunc.getDesc(), boundFunc.getOwner());
skidInvocation = new SkidInvocation(
method,
e
);
} else if (instruction instanceof MethodInsnNode) {
final MethodInsnNode e = (MethodInsnNode) instruction;
target = new ClassMethodHash(e.name, e.desc, e.owner);
skidInvocation = new SkidInvocation(
method,
e
);
} else {
continue;
}

final SkidGroup targetGroup = hashToGroupMap.get(target);

if (targetGroup != null) {
targetGroup.getInvokers().add(skidInvocation);
}

final MethodNode targetMethod = hashToMethodMap.get(target);
if (targetMethod != null) {
if (targetMethod instanceof SkidMethodNode) {
((SkidMethodNode) targetMethod).addInvocation(skidInvocation);
}

methodToInvocationsMap.computeIfAbsent(targetMethod, e -> {
return new ArrayList<>(Collections.singleton(skidInvocation));
});
}
}
}
continue;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ public void download() throws IOException {
try {
cn = factory.create(db, name);

if (skidfuscator.getExemptAnalysis().isExempt(cn)) {
if (false && skidfuscator.getExemptAnalysis().isExempt(cn)) {
phantomContents.getClassContents().add(new JarClassData(
name,
db,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,15 +73,15 @@ public void dump(File file) throws IOException {
throw e;
}

contents.getClassContents().remove(cn);
//contents.getClassContents().remove(cn);
jos.flush();
progressBar.tick();
}

for (JarResource res : new LinkedList<>(contents.getResourceContents())) {
resourcesDumped += dumpResource(jos, res.getName(), res.getData());

contents.getResourceContents().remove(res);
//contents.getResourceContents().remove(res);
progressBar.tick();
}
}
Expand Down
Loading

0 comments on commit 4bb9d94

Please sign in to comment.