2011-09-22 10:55:30 +08:00
|
|
|
|
/*
|
2012-01-10 06:02:47 +08:00
|
|
|
|
Copyright (C) 2011-2012 de4dot@gmail.com
|
2011-09-22 10:55:30 +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;
|
|
|
|
|
using System.Collections.Generic;
|
2012-11-01 23:42:02 +08:00
|
|
|
|
using dot10.DotNet;
|
|
|
|
|
using dot10.DotNet.Emit;
|
2011-12-09 16:02:06 +08:00
|
|
|
|
using de4dot.code.AssemblyClient;
|
2011-09-22 10:55:30 +08:00
|
|
|
|
using de4dot.blocks;
|
|
|
|
|
|
2011-12-09 16:02:06 +08:00
|
|
|
|
namespace de4dot.code {
|
2012-01-20 02:16:44 +08:00
|
|
|
|
abstract class StringInlinerBase : MethodReturnValueInliner {
|
2011-10-27 02:41:50 +08:00
|
|
|
|
protected override void inlineReturnValues(IList<CallResult> callResults) {
|
|
|
|
|
foreach (var callResult in callResults) {
|
|
|
|
|
var block = callResult.block;
|
|
|
|
|
int num = callResult.callEndIndex - callResult.callStartIndex + 1;
|
2011-09-22 10:55:30 +08:00
|
|
|
|
|
2012-01-06 00:24:31 +08:00
|
|
|
|
var decryptedString = callResult.returnValue as string;
|
|
|
|
|
if (decryptedString == null)
|
|
|
|
|
continue;
|
|
|
|
|
|
2011-11-10 21:51:19 +08:00
|
|
|
|
int ldstrIndex = callResult.callStartIndex;
|
2012-01-06 00:24:31 +08:00
|
|
|
|
block.replace(ldstrIndex, num, Instruction.Create(OpCodes.Ldstr, decryptedString));
|
2011-11-10 21:51:19 +08:00
|
|
|
|
|
2012-01-06 00:16:38 +08:00
|
|
|
|
// If it's followed by castclass string, remove it
|
|
|
|
|
if (ldstrIndex + 1 < block.Instructions.Count) {
|
|
|
|
|
var instr = block.Instructions[ldstrIndex + 1];
|
|
|
|
|
if (instr.OpCode.Code == Code.Castclass && instr.Operand.ToString() == "System.String")
|
|
|
|
|
block.remove(ldstrIndex + 1, 1);
|
|
|
|
|
}
|
|
|
|
|
|
2011-11-10 21:51:19 +08:00
|
|
|
|
// If it's followed by String.Intern(), then nop out that call
|
|
|
|
|
if (ldstrIndex + 1 < block.Instructions.Count) {
|
|
|
|
|
var instr = block.Instructions[ldstrIndex + 1];
|
|
|
|
|
if (instr.OpCode.Code == Code.Call) {
|
2012-11-01 23:42:02 +08:00
|
|
|
|
var calledMethod = instr.Operand as IMethod;
|
2011-11-10 21:51:19 +08:00
|
|
|
|
if (calledMethod != null &&
|
|
|
|
|
calledMethod.FullName == "System.String System.String::Intern(System.String)") {
|
|
|
|
|
block.remove(ldstrIndex + 1, 1);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2012-11-11 12:31:11 +08:00
|
|
|
|
Logger.v("Decrypted string: {0}", Utils.toCsharpString(decryptedString));
|
2011-09-22 10:55:30 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2012-01-20 02:16:44 +08:00
|
|
|
|
class DynamicStringInliner : StringInlinerBase {
|
2011-09-22 10:55:30 +08:00
|
|
|
|
IAssemblyClient assemblyClient;
|
|
|
|
|
Dictionary<int, int> methodTokenToId = new Dictionary<int, int>();
|
|
|
|
|
|
2011-10-27 02:41:50 +08:00
|
|
|
|
class MyCallResult : CallResult {
|
2011-09-22 10:55:30 +08:00
|
|
|
|
public int methodId;
|
2012-11-01 23:42:02 +08:00
|
|
|
|
public MethodSpec gim;
|
|
|
|
|
public MyCallResult(Block block, int callEndIndex, int methodId, MethodSpec gim)
|
2011-09-22 10:55:30 +08:00
|
|
|
|
: base(block, callEndIndex) {
|
|
|
|
|
this.methodId = methodId;
|
2012-07-28 10:22:17 +08:00
|
|
|
|
this.gim = gim;
|
2011-09-22 10:55:30 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2012-01-14 19:34:42 +08:00
|
|
|
|
public override bool HasHandlers {
|
|
|
|
|
get { return methodTokenToId.Count != 0; }
|
|
|
|
|
}
|
|
|
|
|
|
2012-01-20 02:16:44 +08:00
|
|
|
|
public DynamicStringInliner(IAssemblyClient assemblyClient) {
|
2011-09-22 10:55:30 +08:00
|
|
|
|
this.assemblyClient = assemblyClient;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void init(IEnumerable<int> methodTokens) {
|
|
|
|
|
methodTokenToId.Clear();
|
|
|
|
|
foreach (var methodToken in methodTokens) {
|
|
|
|
|
if (methodTokenToId.ContainsKey(methodToken))
|
|
|
|
|
continue;
|
|
|
|
|
methodTokenToId[methodToken] = assemblyClient.Service.defineStringDecrypter(methodToken);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2012-11-01 23:42:02 +08:00
|
|
|
|
protected override CallResult createCallResult(IMethod method, MethodSpec gim, Block block, int callInstrIndex) {
|
2011-09-22 10:55:30 +08:00
|
|
|
|
int methodId;
|
2012-11-01 23:42:02 +08:00
|
|
|
|
if (!methodTokenToId.TryGetValue(method.MDToken.ToInt32(), out methodId))
|
2011-09-22 10:55:30 +08:00
|
|
|
|
return null;
|
2012-07-28 10:22:17 +08:00
|
|
|
|
return new MyCallResult(block, callInstrIndex, methodId, gim);
|
2011-09-22 10:55:30 +08:00
|
|
|
|
}
|
|
|
|
|
|
2011-10-27 02:41:50 +08:00
|
|
|
|
protected override void inlineAllCalls() {
|
|
|
|
|
var sortedCalls = new Dictionary<int, List<MyCallResult>>();
|
|
|
|
|
foreach (var tmp in callResults) {
|
|
|
|
|
var callResult = (MyCallResult)tmp;
|
|
|
|
|
List<MyCallResult> list;
|
|
|
|
|
if (!sortedCalls.TryGetValue(callResult.methodId, out list))
|
|
|
|
|
sortedCalls[callResult.methodId] = list = new List<MyCallResult>(callResults.Count);
|
|
|
|
|
list.Add(callResult);
|
2011-09-22 10:55:30 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
foreach (var methodId in sortedCalls.Keys) {
|
|
|
|
|
var list = sortedCalls[methodId];
|
|
|
|
|
var args = new object[list.Count];
|
|
|
|
|
for (int i = 0; i < list.Count; i++) {
|
|
|
|
|
AssemblyData.SimpleData.pack(list[i].args);
|
|
|
|
|
args[i] = list[i].args;
|
|
|
|
|
}
|
2012-11-01 23:42:02 +08:00
|
|
|
|
var decryptedStrings = assemblyClient.Service.decryptStrings(methodId, args, Method.MDToken.ToInt32());
|
2011-09-22 10:55:30 +08:00
|
|
|
|
if (decryptedStrings.Length != args.Length)
|
|
|
|
|
throw new ApplicationException("Invalid decrypted strings array length");
|
|
|
|
|
AssemblyData.SimpleData.unpack(decryptedStrings);
|
2012-07-28 10:22:17 +08:00
|
|
|
|
for (int i = 0; i < list.Count; i++)
|
|
|
|
|
list[i].returnValue = (string)decryptedStrings[i];
|
2011-09-22 10:55:30 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2012-01-20 02:16:44 +08:00
|
|
|
|
class StaticStringInliner : StringInlinerBase {
|
2012-11-22 16:14:51 +08:00
|
|
|
|
MethodDefAndDeclaringTypeDict<Func<MethodDef, MethodSpec, object[], string>> stringDecrypters = new MethodDefAndDeclaringTypeDict<Func<MethodDef, MethodSpec, object[], string>>();
|
2011-09-22 10:55:30 +08:00
|
|
|
|
|
2012-01-14 19:34:42 +08:00
|
|
|
|
public override bool HasHandlers {
|
2011-09-22 10:55:30 +08:00
|
|
|
|
get { return stringDecrypters.Count != 0; }
|
|
|
|
|
}
|
|
|
|
|
|
2012-11-01 23:42:02 +08:00
|
|
|
|
public IEnumerable<MethodDef> Methods {
|
2012-01-01 19:06:01 +08:00
|
|
|
|
get { return stringDecrypters.getKeys(); }
|
2011-09-22 10:55:30 +08:00
|
|
|
|
}
|
|
|
|
|
|
2011-10-27 02:41:50 +08:00
|
|
|
|
class MyCallResult : CallResult {
|
2012-11-01 23:42:02 +08:00
|
|
|
|
public IMethod IMethod;
|
|
|
|
|
public MethodSpec gim;
|
|
|
|
|
public MyCallResult(Block block, int callEndIndex, IMethod method, MethodSpec gim)
|
2011-09-22 10:55:30 +08:00
|
|
|
|
: base(block, callEndIndex) {
|
2012-11-01 23:42:02 +08:00
|
|
|
|
this.IMethod = method;
|
2012-07-28 10:22:17 +08:00
|
|
|
|
this.gim = gim;
|
2011-09-22 10:55:30 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2012-11-01 23:42:02 +08:00
|
|
|
|
public void add(MethodDef method, Func<MethodDef, MethodSpec, object[], string> handler) {
|
2011-09-22 10:55:30 +08:00
|
|
|
|
if (method != null)
|
2011-12-31 23:15:38 +08:00
|
|
|
|
stringDecrypters.add(method, handler);
|
2011-09-22 10:55:30 +08:00
|
|
|
|
}
|
|
|
|
|
|
2011-10-27 02:41:50 +08:00
|
|
|
|
protected override void inlineAllCalls() {
|
|
|
|
|
foreach (var tmp in callResults) {
|
|
|
|
|
var callResult = (MyCallResult)tmp;
|
2012-11-01 23:42:02 +08:00
|
|
|
|
var handler = stringDecrypters.find(callResult.IMethod);
|
|
|
|
|
callResult.returnValue = handler((MethodDef)callResult.IMethod, callResult.gim, callResult.args);
|
2011-09-22 10:55:30 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2012-11-01 23:42:02 +08:00
|
|
|
|
protected override CallResult createCallResult(IMethod method, MethodSpec gim, Block block, int callInstrIndex) {
|
2011-12-31 23:15:38 +08:00
|
|
|
|
if (stringDecrypters.find(method) == null)
|
2011-09-22 10:55:30 +08:00
|
|
|
|
return null;
|
2012-07-28 10:22:17 +08:00
|
|
|
|
return new MyCallResult(block, callInstrIndex, method, gim);
|
2011-09-22 10:55:30 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|