Skip to content

Commit

Permalink
Decrease output size for reversible encoding
Browse files Browse the repository at this point in the history
  • Loading branch information
KvanTTT committed Jul 24, 2021
1 parent 47d3655 commit 3974595
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 54 deletions.
73 changes: 45 additions & 28 deletions Confuser.Renamer/NameService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ internal class NameService : INameService {
readonly byte[] nameId = new byte[8];
readonly Dictionary<string, string> _originalToObfuscatedNameMap = new Dictionary<string, string>();
readonly Dictionary<string, string> _obfuscatedToOriginalNameMap = new Dictionary<string, string>();
readonly Dictionary<string, string> _prefixesMap = new Dictionary<string, string>();
internal ReversibleRenamer reversibleRenamer;

public NameService(ConfuserContext context) {
Expand Down Expand Up @@ -370,46 +371,62 @@ public string GetOriginalFullName(IDnlibDef obj) =>
public void SetIsRenamed(IDnlibDef def) => context.Annotations.Set(def, IsRenamedKey, true);

string ExtractActualName(IDnlibDef dnlibDef, bool forceShortNames = false) {
string result;

var shortNames = forceShortNames ||
GetParam(dnlibDef, "shortNames")?.Equals("true", StringComparison.OrdinalIgnoreCase) ==
true;
var renameMode = GetRenameMode(dnlibDef);

if (dnlibDef is TypeDef typeDef) {
return typeDef.DeclaringType != null
? $"{CompressTypeName(typeDef.DeclaringType.FullName, renameMode)}/{dnlibDef.Name}"
: dnlibDef.FullName;
}

if (shortNames) {
result = dnlibDef is TypeDef ? dnlibDef.FullName : (string)dnlibDef.Name;
return dnlibDef.Name;
}
else {
if (dnlibDef is TypeDef) {
result = dnlibDef.FullName;
}
else {
var resultBuilder = new StringBuilder();

if (dnlibDef is IMemberDef memberDef) {
resultBuilder.Append(memberDef.DeclaringType?.FullName);
resultBuilder.Append("::");
resultBuilder.Append(dnlibDef.Name);

if (memberDef is MethodDef methodDef) {
resultBuilder.Append('(');
if (methodDef.Signature is MethodSig methodSig) {
var methodParams = methodSig.Params;
for (var index = 0; index < methodParams.Count; index++) {
resultBuilder.Append(methodParams[index]);
if (index < methodParams.Count - 1) {
resultBuilder.Append(',');
}
}

var resultBuilder = new StringBuilder();
if (dnlibDef is IMemberDef memberDef) {
var declaringTypeName = CompressTypeName(memberDef.DeclaringType?.FullName ?? "", renameMode);
resultBuilder.Append(declaringTypeName);
resultBuilder.Append("::");
resultBuilder.Append(dnlibDef.Name);

if (memberDef is MethodDef methodDef) {
resultBuilder.Append('(');
if (methodDef.Signature is MethodSig methodSig) {
var methodParams = methodSig.Params;
for (var index = 0; index < methodParams.Count; index++) {
resultBuilder.Append(CompressTypeName(methodParams[index].ToString(), renameMode));
if (index < methodParams.Count - 1) {
resultBuilder.Append(',');
}
resultBuilder.Append(')');
}
}

result = resultBuilder.ToString();
resultBuilder.Append(')');
}
}

return result;
return resultBuilder.ToString();
}

string CompressTypeName(string typeName, RenameMode renameMode)
{
if (renameMode == RenameMode.Reversible)
{
if (!_prefixesMap.TryGetValue(typeName, out string prefix))
{
IncrementNameId();
prefix = Utils.EncodeString(nameId, alphaNumCharset);
_prefixesMap.Add(typeName, prefix);
}

return prefix;
}

return typeName;
}
}
}
51 changes: 25 additions & 26 deletions Tests/MessageDeobfuscation.Test/MessageDeobfuscationTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,18 +52,24 @@ await Run(
eventId = "_b";
}

void CheckName(string expectedFullName, string expectedShortName, string obfuscatedName) {
var fullName = deobfuscator.DeobfuscateSymbol(obfuscatedName, false);
Assert.Equal(expectedFullName, fullName);
Assert.Equal(expectedShortName, MessageDeobfuscator.ExtractShortName(fullName));
}

CheckName("MessageDeobfuscation.Class", "MessageDeobfuscation.Class",
classId, deobfuscator);
classId);
CheckName("MessageDeobfuscation.Class/NestedClass", "NestedClass",
nestedClassId, deobfuscator);
nestedClassId);
CheckName("MessageDeobfuscation.Class::Method(System.String,System.Int32)", "Method",
methodId, deobfuscator);
methodId);
CheckName("MessageDeobfuscation.Class::Field", "Field",
fieldId, deobfuscator);
fieldId);
CheckName("MessageDeobfuscation.Class::Property", "Property",
propertyId, deobfuscator);
propertyId);
CheckName("MessageDeobfuscation.Class::Event", "Event",
eventId, deobfuscator);
eventId);

Assert.Equal(_expectedDeobfuscatedOutput, deobfuscatedMessage);
return Task.Delay(0);
Expand Down Expand Up @@ -96,8 +102,8 @@ public static IEnumerable<object[]> RenameModeAndExpectedObfuscatedOutput() =>
public async Task MessageDeobfuscationWithPassword() {
var expectedObfuscatedOutput = new[] {
"Exception",
" at oQmpV$y2k2b9P3d6GP1cxGPuRtKaNIZvZcKpZXSfKFG8.V1M$X52eDxP6ElgdFrRDlF0KSZU31AmQaiXXgzyoeJJ4KV64JBpi0Bh25Xdje$vCxw.fUHV$KyBiFTUH0$GNDHVx6XvtlZWHnzVgRO9N2M$jw5ysYWJWaUSMQYtPDT$wa$6MarZQoNxnbR_9cn$A2XXvRY(String )",
" at EbUjRcrC76NnA7RJlhQffrfp$vMGHdDfqtVFtWrAOPyD.swzvaIVl3W8yDi8Ii3P1j_V9JC8eVu2JgvNNjeVDYc4bOHH37cCBf0_3URE_8UcWPQ()"
" at oQmpV$y2k2b9P3d6GP1cxGPuRtKaNIZvZcKpZXSfKFG8.CE8t0VDPQk9$jgv1XuRwt1k.FhsPrCLqIAaPKe7abGklvY4(String )",
" at EbUjRcrC76NnA7RJlhQffrfp$vMGHdDfqtVFtWrAOPyD.xgIw9voebB21PlxPFA_hs60()"
};
string password = "password";
await Run(
Expand All @@ -113,29 +119,22 @@ await Run(
var deobfuscator = new MessageDeobfuscator(password);
var deobfuscatedMessage = deobfuscator.DeobfuscateMessage(string.Join(Environment.NewLine, expectedObfuscatedOutput));

CheckName("MessageDeobfuscation.Class", "MessageDeobfuscation.Class",
"oQmpV$y2k2b9P3d6GP1cxGPuRtKaNIZvZcKpZXSfKFG8", deobfuscator);
CheckName("MessageDeobfuscation.Class/NestedClass", "NestedClass",
"V1M$X52eDxP6ElgdFrRDlF0KSZU31AmQaiXXgzyoeJJ4KV64JBpi0Bh25Xdje$vCxw", deobfuscator);
CheckName("MessageDeobfuscation.Class::Method(System.String,System.Int32)", "Method",
"CPiRF0I_h5xVXKPEtJXNA7cLoPPS4vhkcjcJi6MAreEi2dBd0rRGyabz9ko1cgWS46oQIMTt_U99FxMd$wpcMBI", deobfuscator);
CheckName("MessageDeobfuscation.Class::Field", "Field",
"EbUjRcrC76NnA7RJlhQffrdNtUhGQ3K5irENJz724HX_R45xF8Tm$vzXOkAiVX4bXA", deobfuscator);
CheckName("MessageDeobfuscation.Class::Property", "Property",
"jpG4Jhvg51oUy7PG8hUhxDmJkR1IVttcUFtkOHedcZ2BvCUAjb2SvUsd3q9IoA2LEQ", deobfuscator);
CheckName("MessageDeobfuscation.Class::Event", "Event",
"NdAzNJfOt9g8GfsT5YEikaIAKenWzJC2RbWKmG8rcYU4f2t_KXIZp4wSkiAmLQe8sA", deobfuscator);
void CheckName(string expectedName, string obfuscatedName) {
var name = deobfuscator.DeobfuscateSymbol(obfuscatedName, true);
Assert.Equal(expectedName, name);
}

CheckName("MessageDeobfuscation.Class", "oQmpV$y2k2b9P3d6GP1cxGPuRtKaNIZvZcKpZXSfKFG8");
CheckName("NestedClass", "CE8t0VDPQk9$jgv1XuRwt1k");
CheckName("Method", "jevJU4p4yNrAYGqN7GkRWaI");
CheckName("Field", "3IS4xsnUsvDQZop6e4WmNVw");
CheckName("Property", "917VMBMNYHd0kfnnNkgeJ10");
CheckName("Event", "AIyINk7kgFLFc73Md8Nu8Z0");

Assert.Equal(_expectedDeobfuscatedOutput, deobfuscatedMessage);
return Task.Delay(0);
}
);
}

void CheckName(string expectedFullName, string expectedShortName, string obfuscatedName, MessageDeobfuscator messageDeobfuscator) {
var fullName = messageDeobfuscator.DeobfuscateSymbol(obfuscatedName, false);
Assert.Equal(expectedFullName, fullName);
Assert.Equal(expectedShortName, MessageDeobfuscator.ExtractShortName(fullName));
}
}
}

0 comments on commit 3974595

Please sign in to comment.