|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.IO; |
| 4 | +using System.Text.RegularExpressions; |
| 5 | + |
| 6 | +namespace Confuser.Renamer { |
| 7 | + public class MessageDeobfuscator { |
| 8 | + static readonly Regex MapSymbolMatcher = new Regex("_[a-zA-Z0-9]+"); |
| 9 | + static readonly Regex PasswordSymbolMatcher = new Regex("[a-zA-Z0-9_$]{23,}"); |
| 10 | + |
| 11 | + readonly Dictionary<string, string> _symbolMap; |
| 12 | + readonly ReversibleRenamer _renamer; |
| 13 | + |
| 14 | + public static MessageDeobfuscator Load(string symbolMapFileName) { |
| 15 | + if (symbolMapFileName is null) |
| 16 | + throw new ArgumentNullException(nameof(symbolMapFileName)); |
| 17 | + |
| 18 | + var symbolMap = new Dictionary<string, string>(); |
| 19 | + using (var reader = new StreamReader(File.OpenRead(symbolMapFileName))) { |
| 20 | + var line = reader.ReadLine(); |
| 21 | + while (line != null) { |
| 22 | + int tabIndex = line.IndexOf('\t'); |
| 23 | + if (tabIndex == -1) |
| 24 | + throw new FileFormatException(); |
| 25 | + symbolMap.Add(line.Substring(0, tabIndex), line.Substring(tabIndex + 1)); |
| 26 | + line = reader.ReadLine(); |
| 27 | + } |
| 28 | + } |
| 29 | + |
| 30 | + return new MessageDeobfuscator(symbolMap); |
| 31 | + } |
| 32 | + |
| 33 | + public MessageDeobfuscator(Dictionary<string, string> map) => _symbolMap = map ?? throw new ArgumentNullException(nameof(map)); |
| 34 | + |
| 35 | + public MessageDeobfuscator(string password) => _renamer = new ReversibleRenamer(password); |
| 36 | + |
| 37 | + public string Deobfuscate(string obfuscatedMessage) { |
| 38 | + if (_symbolMap != null) { |
| 39 | + return MapSymbolMatcher.Replace(obfuscatedMessage, DecodeSymbolMap); |
| 40 | + } |
| 41 | + |
| 42 | + return PasswordSymbolMatcher.Replace(obfuscatedMessage, DecodeSymbolPassword); |
| 43 | + } |
| 44 | + |
| 45 | + string DecodeSymbolMap(Match match) { |
| 46 | + var symbol = match.Value; |
| 47 | + if (_symbolMap.TryGetValue(symbol, out string result)) |
| 48 | + return ExtractShortName(result); |
| 49 | + return ExtractShortName(symbol); |
| 50 | + } |
| 51 | + |
| 52 | + string DecodeSymbolPassword(Match match) { |
| 53 | + var sym = match.Value; |
| 54 | + try { |
| 55 | + return ExtractShortName(_renamer.Decrypt(sym)); |
| 56 | + } |
| 57 | + catch { |
| 58 | + return sym; |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + static string ExtractShortName(string fullName) { |
| 63 | + const string doubleParen = "::"; |
| 64 | + int doubleParenIndex = fullName.IndexOf(doubleParen, StringComparison.Ordinal); |
| 65 | + if (doubleParenIndex != -1) { |
| 66 | + int resultStringStartIndex = doubleParenIndex + doubleParen.Length; |
| 67 | + int parenIndex = fullName.IndexOf('(', doubleParenIndex); |
| 68 | + return fullName.Substring(resultStringStartIndex, |
| 69 | + (parenIndex == -1 ? fullName.Length : parenIndex) - resultStringStartIndex); |
| 70 | + } |
| 71 | + |
| 72 | + int slashIndex = fullName.IndexOf('/'); |
| 73 | + if (slashIndex != -1) { |
| 74 | + return fullName.Substring(slashIndex + 1); |
| 75 | + } |
| 76 | + |
| 77 | + return fullName; |
| 78 | + } |
| 79 | + } |
| 80 | +} |
0 commit comments