Skip to content

Commit

Permalink
fix(obfuscator): new integer format test + fix weird bug with linkedh…
Browse files Browse the repository at this point in the history
…ashmap
  • Loading branch information
terminalsin committed Aug 14, 2022
1 parent eb964be commit 832f34e
Show file tree
Hide file tree
Showing 8 changed files with 141 additions and 28 deletions.
2 changes: 1 addition & 1 deletion dev.skidfuscator.obfuscator/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ dependencies {
api project(':commons')
implementation 'me.tongfei:progressbar:0.9.3'
implementation 'com.github.matomo-org:matomo-java-tracker:v1.7'
implementation 'com.github.Col-E:jphantom:1.4.3'
api 'com.github.Col-E:jphantom:1.4.3'
implementation 'dev.dirs:directories:26'
testAnnotationProcessor 'org.projectlombok:lombok:1.18.24'

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -231,21 +231,7 @@ public void run() {
*
* Here though shall puteth all your transformers. Enjoy!
*/
for (Listener o : Arrays.asList(
new StringTransformer(this),
//new NegationTransformer(this),
//new FlatteningFlowTransformer(this),
new NumberTransformer(this),
new SwitchTransformer(this),
new BasicSimplifierTransformer(this),
new BasicConditionTransformer(this),
new BasicExceptionTransformer(this),
new BasicRangeTransformer(this),
new AhegaoTransformer(this)
//
)) {
EventBus.register(o);
}
_loadTransformer();

LOGGER.log("Finished loading transformers...");

Expand Down Expand Up @@ -307,7 +293,7 @@ public void run() {

_dump();


IntegerBlockPredicateRenderer.DEBUG = false;
LOGGER.post("Goodbye!");
}

Expand Down Expand Up @@ -526,6 +512,24 @@ protected void _importClasspath() {
LOGGER.log("Finished importing classpath!");
}

protected void _loadTransformer() {
for (Listener o : Arrays.asList(
new StringTransformer(this),
//new NegationTransformer(this),
//new FlatteningFlowTransformer(this),
new NumberTransformer(this),
new SwitchTransformer(this),
new BasicSimplifierTransformer(this),
new BasicConditionTransformer(this),
new BasicExceptionTransformer(this),
new BasicRangeTransformer(this),
new AhegaoTransformer(this)
//
)) {
EventBus.register(o);
}
}

private void _verify() {
/* Checking for errors */
LOGGER.post("Starting verification");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public IntegerBlockPredicateRenderer(Skidfuscator skidfuscator, List<Transformer
super(skidfuscator,"GEN3 Flow", children);
}

public static final boolean DEBUG = false;
public static boolean DEBUG = false;

/**
* Method called when the class methods are iterated over and initialized.
Expand Down Expand Up @@ -761,7 +761,8 @@ void handle(final PostMethodTransformEvent event) {
localGetter,
localSetter,
methodNode.getBlockPredicate(seededBlock),
methodNode.getBlockPredicate(targetSeededBlock)
methodNode.getBlockPredicate(targetSeededBlock),
"Unconditional"
);

if (DEBUG) {
Expand Down Expand Up @@ -822,7 +823,8 @@ void handle(final PostMethodTransformEvent event) {
localGetter,
localSetter,
methodNode.getBlockPredicate(seededBlock),
methodNode.getBlockPredicate(targetSeeded)
methodNode.getBlockPredicate(targetSeeded),
"Conditional"
);
final UnconditionalJumpEdge<BasicBlock> edge = new UnconditionalJumpEdge<>(basicBlock, target);
basicBlock.add(new UnconditionalJumpStmt(target, edge));
Expand Down Expand Up @@ -897,6 +899,8 @@ void handle(final PostMethodTransformEvent event) {
.forEach(stmt -> {
final SkidBlock seededBlock = (SkidBlock) vertex;

final LinkedHashMap<Integer, BasicBlock> targets = new LinkedHashMap<>();

for (Map.Entry<Integer, BasicBlock> entry : stmt.getTargets().entrySet()) {
final int seed = entry.getKey();
final BasicBlock value = entry.getValue();
Expand All @@ -917,7 +921,9 @@ void handle(final PostMethodTransformEvent event) {
localGetter,
localSetter,
methodNode.getBlockPredicate(seededBlock),
methodNode.getBlockPredicate(target)
methodNode.getBlockPredicate(target),
"Switch Entry [" + entry.getKey() + ", og:" + target.getDisplayName()
+ ", redirected: " + basicBlock.getDisplayName() + ")"
);
final UnconditionalJumpEdge<BasicBlock> edge = new UnconditionalJumpEdge<>(basicBlock, target);
basicBlock.add(new UnconditionalJumpStmt(target, edge));
Expand All @@ -927,7 +933,7 @@ void handle(final PostMethodTransformEvent event) {
basicBlock.cfg.addEdge(edge);

// Replace successor
stmt.getTargets().replace(seed, basicBlock);
targets.put(seed, basicBlock);
basicBlock.cfg.addEdge(new SwitchEdge<>(seededBlock, basicBlock, stmt.getOpcode()));

if (DEBUG) {
Expand All @@ -947,6 +953,8 @@ void handle(final PostMethodTransformEvent event) {
}
}

stmt.setTargets(targets);

if (stmt.getDefaultTarget() == null || stmt.getDefaultTarget() == vertex)
return;

Expand All @@ -964,7 +972,8 @@ void handle(final PostMethodTransformEvent event) {
localGetter,
localSetter,
methodNode.getBlockPredicate(seededBlock),
methodNode.getBlockPredicate(target)
methodNode.getBlockPredicate(target),
"Switch Default"
);
final UnconditionalJumpEdge<BasicBlock> edge = new UnconditionalJumpEdge<>(basicBlock, target);
basicBlock.add(new UnconditionalJumpStmt(target, edge));
Expand Down Expand Up @@ -1040,7 +1049,8 @@ void handle(final PostMethodTransformEvent event) {
localGetter,
localSetter,
methodNode.getBlockPredicate(internal),
methodNode.getBlockPredicate(handler)
methodNode.getBlockPredicate(handler),
"Exception Range " + Arrays.toString(blockRange.getTypes().toArray())
);

// Jump to handler
Expand Down Expand Up @@ -1121,8 +1131,32 @@ void handle(final PostMethodTransformEvent event) {
return;
}

/**
* WIP
*/
private BasicBlock createRedirector(final BasicBlock block,
final BasicBlock targetBlock,
final PredicateFlowGetter getter,
final PredicateFlowSetter setter,
final int value,
final int target,
final String type
) {
final BasicBlock redirector = new BasicBlock(block.cfg);
block.cfg.addVertex(redirector);
return null;
}


private void addSeedLoader(final BasicBlock block, final BasicBlock targetBlock, final int index, final PredicateFlowGetter getter, final PredicateFlowSetter local, final int value, final int target) {
private void addSeedLoader(final BasicBlock block,
final BasicBlock targetBlock,
final int index,
final PredicateFlowGetter getter,
final PredicateFlowSetter local,
final int value,
final int target,
final String type
) {
final Expr load = NumberManager.encrypt(
target,
value,
Expand All @@ -1138,7 +1172,12 @@ private void addSeedLoader(final BasicBlock block, final BasicBlock targetBlock,
if (DEBUG) {
final BasicBlock exception = Blocks.exception(
block.cfg,
"Failed to match seed of value " + target
block.getDisplayName() + " --> "
+ targetBlock.getDisplayName()
+ " # Failed to match seed of type "
+ type
+ " and value "
+ target
);

final Stmt jumpStmt = new FakeConditionalJumpStmt(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import dev.skidfuscator.testclasses.TestRun;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.mapleir.asm.ClassHelper;
import org.mapleir.deob.PassGroup;
Expand Down Expand Up @@ -74,7 +75,24 @@ public void receiveAndExecute(List<Map.Entry<String, byte[]>> output) {
final Class<?> clazz = classLoader.loadClass(this.getMainClass().getName());
final TestRun run = (TestRun) clazz.newInstance();
run.run();

if (TestSkidfuscator.SKIP) {
TestSkidfuscator.SKIP = false;
Assertions.fail("Transformers are causing the issue!");
}
} catch (Throwable e) {
System.out.println("------");
e.printStackTrace();
System.out.println("------");

if (!TestSkidfuscator.SKIP) {
TestSkidfuscator.SKIP = true;

test();
} else {
TestSkidfuscator.SKIP = false;
}

try {
skidfuscator.getJarContents().getClassContents().add(
new JarClassData(
Expand Down Expand Up @@ -120,7 +138,7 @@ public void receiveAndExecute(List<Map.Entry<String, byte[]>> output) {
ie.printStackTrace();
}

throw new IllegalStateException("Failed execution", e);
throw new IllegalStateException("Failed execution: " + e.getMessage(), e);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import dev.skidfuscator.obfuscator.creator.SkidApplicationClassSource;
import dev.skidfuscator.obfuscator.event.EventBus;
import dev.skidfuscator.obfuscator.phantom.jphantom.PhantomResolvingJarDumper;
import dev.skidfuscator.obfuscator.predicate.renderer.impl.IntegerBlockPredicateRenderer;
import dev.skidfuscator.obfuscator.skidasm.SkidClassNode;
import dev.skidfuscator.obfuscator.util.MiscUtil;
import dev.skidfuscator.obfuscator.verifier.Verifier;
Expand Down Expand Up @@ -45,8 +46,12 @@ public TestSkidfuscator(Class<?>[] test, Consumer<List<Map.Entry<String, byte[]>
.build());
this.test = test;
this.callback = callback;

IntegerBlockPredicateRenderer.DEBUG = true;
}

public static boolean SKIP = false;

@Override
protected void _importExempt() {
LOGGER.post("Ignoring exempts since we're testing...");
Expand Down Expand Up @@ -128,6 +133,14 @@ protected void _importClasspath() {
LOGGER.log("Finished importing classpath!");
}

@Override
protected void _loadTransformer() {
if (SKIP)
return;

super._loadTransformer();
}

@Override
protected void _dump() {
final PhantomResolvingJarDumper resolver = new PhantomResolvingJarDumper(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package dev.skidfuscator.test.exception;

import dev.skidfuscator.core.SkidTest;
import dev.skidfuscator.testclasses.TestRun;
import dev.skidfuscator.testclasses.exception.IntegerFormatExceptionTestClass;

public class IntegerFormatExceptionTest extends SkidTest {
@Override
public Class<? extends TestRun> getMainClass() {
return IntegerFormatExceptionTestClass.class;
}

@Override
public Class<?>[] getClasses() {
return new Class[]{
IntegerFormatExceptionTestClass.class
};
}
}
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
package dev.skidfuscator.testclasses.conditionals;


import dev.skidfuscator.annotations.Exclude;
import dev.skidfuscator.testclasses.TestRun;

public class Tableswitch implements TestRun {
@Override
public void run() {
System.out.println("Beginning test!");
for (char i = 0; i < 16; i++) {
System.out.println("Running exec " + (int) i);
assert exec(i) == exec_real(i) : "Failed equality check";
}
System.out.println("Ending test!");
}

public int exec(char a) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package dev.skidfuscator.testclasses.exception;

import dev.skidfuscator.testclasses.TestRun;
import org.junit.jupiter.api.Assertions;

public class IntegerFormatExceptionTestClass implements TestRun {
@Override
public void run() {
try {
Integer.decode(":)");
Assertions.fail();
} catch (NumberFormatException e) {
return;
} catch (Exception e) {
Assertions.fail();
}
}
}

0 comments on commit 832f34e

Please sign in to comment.