forked from yck1509/ConfuserEx
-
-
Notifications
You must be signed in to change notification settings - Fork 370
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
23 changed files
with
547 additions
and
304 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.IO; | ||
using System.Text.RegularExpressions; | ||
|
||
namespace Confuser.Renamer { | ||
public class MessageDeobfuscator { | ||
static readonly Regex MapSymbolMatcher = new Regex("_[a-zA-Z0-9]+", RegexOptions.Compiled); | ||
static readonly Regex PasswordSymbolMatcher = new Regex("[a-zA-Z0-9_$]{23,}", RegexOptions.Compiled); | ||
|
||
readonly Dictionary<string, string> _symbolMap; | ||
readonly ReversibleRenamer _renamer; | ||
|
||
public static MessageDeobfuscator Load(string symbolMapFileName) { | ||
var symbolMap = new Dictionary<string, string>(); | ||
using (var reader = new StreamReader(File.OpenRead(symbolMapFileName))) { | ||
var line = reader.ReadLine(); | ||
while (line != null) { | ||
int tabIndex = line.IndexOf('\t'); | ||
if (tabIndex == -1) | ||
throw new FileFormatException(); | ||
symbolMap.Add(line.Substring(0, tabIndex), line.Substring(tabIndex + 1)); | ||
line = reader.ReadLine(); | ||
} | ||
} | ||
|
||
return new MessageDeobfuscator(symbolMap); | ||
} | ||
|
||
public MessageDeobfuscator(Dictionary<string, string> map) => _symbolMap = map ?? throw new ArgumentNullException(nameof(map)); | ||
|
||
public MessageDeobfuscator(string password) => _renamer = new ReversibleRenamer(password); | ||
|
||
public string Deobfuscate(string obfuscatedMessage) { | ||
if (_symbolMap != null) { | ||
return MapSymbolMatcher.Replace(obfuscatedMessage, DecodeSymbolMap); | ||
} | ||
|
||
return PasswordSymbolMatcher.Replace(obfuscatedMessage, DecodeSymbolPassword); | ||
} | ||
|
||
string DecodeSymbolMap(Match match) { | ||
var symbol = match.Value; | ||
if (_symbolMap.TryGetValue(symbol, out string result)) | ||
return ExtractShortName(result); | ||
return ExtractShortName(symbol); | ||
} | ||
|
||
string DecodeSymbolPassword(Match match) { | ||
var sym = match.Value; | ||
try { | ||
return ExtractShortName(_renamer.Decrypt(sym)); | ||
} | ||
catch { | ||
return sym; | ||
} | ||
} | ||
|
||
string ExtractShortName(string fullName) { | ||
const string doubleParen = "::"; | ||
int doubleParenIndex = fullName.IndexOf(doubleParen); | ||
if (doubleParenIndex != -1) { | ||
int resultStringStartIndex = doubleParenIndex + doubleParen.Length; | ||
int parenIndex = fullName.IndexOf('(', doubleParenIndex); | ||
return fullName.Substring(resultStringStartIndex, | ||
(parenIndex == -1 ? fullName.Length : parenIndex) - resultStringStartIndex); | ||
} | ||
|
||
int slashIndex = fullName.IndexOf('/'); | ||
if (slashIndex != -1) { | ||
return fullName.Substring(slashIndex + 1); | ||
} | ||
|
||
return fullName; | ||
} | ||
} | ||
} |
Oops, something went wrong.