2011-11-06 21:42:52 +08:00
|
|
|
|
/*
|
2012-01-10 06:02:47 +08:00
|
|
|
|
Copyright (C) 2011-2012 de4dot@gmail.com
|
2011-11-06 21:42:52 +08:00
|
|
|
|
|
|
|
|
|
This file is part of de4dot.
|
|
|
|
|
|
|
|
|
|
de4dot is free software: you can redistribute it and/or modify
|
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
|
|
de4dot is distributed in the hope that it will be useful,
|
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
|
along with de4dot. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
using System.Text;
|
|
|
|
|
using Mono.Cecil;
|
|
|
|
|
using de4dot.blocks;
|
|
|
|
|
|
2011-12-09 16:02:06 +08:00
|
|
|
|
namespace de4dot.code.deobfuscators.Xenocode {
|
2011-11-06 21:42:52 +08:00
|
|
|
|
class StringDecrypter {
|
|
|
|
|
const int STRING_DECRYPTER_KEY_CONST = 1789;
|
|
|
|
|
ModuleDefinition module;
|
|
|
|
|
TypeDefinition stringDecrypterType;
|
|
|
|
|
MethodDefinition stringDecrypterMethod;
|
|
|
|
|
|
|
|
|
|
public bool Detected {
|
|
|
|
|
get { return stringDecrypterMethod != null; }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public TypeDefinition Type {
|
|
|
|
|
get { return stringDecrypterType; }
|
|
|
|
|
}
|
|
|
|
|
|
2011-11-06 22:24:30 +08:00
|
|
|
|
public MethodDefinition Method {
|
2011-11-06 21:42:52 +08:00
|
|
|
|
get { return stringDecrypterMethod; }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public StringDecrypter(ModuleDefinition module) {
|
|
|
|
|
this.module = module;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void find() {
|
|
|
|
|
foreach (var type in module.Types) {
|
|
|
|
|
if (type.HasFields)
|
|
|
|
|
continue;
|
|
|
|
|
if (type.Methods.Count < 1 || type.Methods.Count > 3)
|
|
|
|
|
continue;
|
|
|
|
|
if (type.HasProperties || type.HasEvents)
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
MethodDefinition method = null;
|
|
|
|
|
foreach (var m in type.Methods) {
|
|
|
|
|
if (m.Name == ".ctor" || m.Name == ".cctor")
|
|
|
|
|
continue;
|
|
|
|
|
if (DotNetUtils.isMethod(m, "System.String", "(System.String,System.Int32)")) {
|
|
|
|
|
method = m;
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
method = null;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
if (method == null || method.Body == null)
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
bool foundConstant = false;
|
|
|
|
|
foreach (var instr in method.Body.Instructions) {
|
|
|
|
|
if (DotNetUtils.isLdcI4(instr) && DotNetUtils.getLdcI4Value(instr) == STRING_DECRYPTER_KEY_CONST) {
|
|
|
|
|
foundConstant = true;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (!foundConstant)
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
stringDecrypterType = type;
|
|
|
|
|
stringDecrypterMethod = method;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public string decrypt(string es, int magic) {
|
|
|
|
|
int newLen = es.Length / 4;
|
|
|
|
|
var sb = new StringBuilder(newLen);
|
|
|
|
|
for (int i = 0; i < newLen * 4; i += 4) {
|
|
|
|
|
char c = (char)((es[i] - 'a') +
|
|
|
|
|
((es[i + 1] - 'a') << 4) +
|
|
|
|
|
((es[i + 2] - 'a') << 8) +
|
|
|
|
|
((es[i + 3] - 'a') << 12) - magic);
|
|
|
|
|
magic += STRING_DECRYPTER_KEY_CONST;
|
|
|
|
|
sb.Append(c);
|
|
|
|
|
}
|
|
|
|
|
return sb.ToString();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|