This repository has been archived by the owner on Dec 8, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 126
/
StringInliner.cs
170 lines (144 loc) · 5.84 KB
/
StringInliner.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
/*
Copyright (C) 2011-2015 [email protected]
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;
using dnlib.DotNet;
using dnlib.DotNet.Emit;
using de4dot.code.AssemblyClient;
using de4dot.blocks;
namespace de4dot.code {
public abstract class StringInlinerBase : MethodReturnValueInliner {
protected override void InlineReturnValues(IList<CallResult> callResults) {
foreach (var callResult in callResults) {
var block = callResult.block;
int num = callResult.callEndIndex - callResult.callStartIndex + 1;
var decryptedString = callResult.returnValue as string;
if (decryptedString == null)
continue;
int ldstrIndex = callResult.callStartIndex;
block.Replace(ldstrIndex, num, OpCodes.Ldstr.ToInstruction(decryptedString));
// 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);
}
// 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) {
var calledMethod = instr.Operand as IMethod;
if (calledMethod != null &&
calledMethod.FullName == "System.String System.String::Intern(System.String)") {
block.Remove(ldstrIndex + 1, 1);
}
}
}
Logger.v("Decrypted string: {0}", Utils.ToCsharpString(decryptedString));
}
}
}
public class DynamicStringInliner : StringInlinerBase {
IAssemblyClient assemblyClient;
Dictionary<int, int> methodTokenToId = new Dictionary<int, int>();
class MyCallResult : CallResult {
public int methodId;
public MethodSpec gim;
public MyCallResult(Block block, int callEndIndex, int methodId, MethodSpec gim)
: base(block, callEndIndex) {
this.methodId = methodId;
this.gim = gim;
}
}
public override bool HasHandlers {
get { return methodTokenToId.Count != 0; }
}
public DynamicStringInliner(IAssemblyClient assemblyClient) {
this.assemblyClient = assemblyClient;
}
public void Initialize(IEnumerable<int> methodTokens) {
methodTokenToId.Clear();
foreach (var methodToken in methodTokens) {
if (methodTokenToId.ContainsKey(methodToken))
continue;
methodTokenToId[methodToken] = assemblyClient.StringDecrypterService.DefineStringDecrypter(methodToken);
}
}
protected override CallResult CreateCallResult(IMethod method, MethodSpec gim, Block block, int callInstrIndex) {
int methodId;
if (!methodTokenToId.TryGetValue(method.MDToken.ToInt32(), out methodId))
return null;
return new MyCallResult(block, callInstrIndex, methodId, gim);
}
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);
}
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;
}
var decryptedStrings = assemblyClient.StringDecrypterService.DecryptStrings(methodId, args, Method.MDToken.ToInt32());
if (decryptedStrings.Length != args.Length)
throw new ApplicationException("Invalid decrypted strings array length");
AssemblyData.SimpleData.Unpack(decryptedStrings);
for (int i = 0; i < list.Count; i++)
list[i].returnValue = (string)decryptedStrings[i];
}
}
}
public class StaticStringInliner : StringInlinerBase {
MethodDefAndDeclaringTypeDict<Func<MethodDef, MethodSpec, object[], string>> stringDecrypters = new MethodDefAndDeclaringTypeDict<Func<MethodDef, MethodSpec, object[], string>>();
public override bool HasHandlers {
get { return stringDecrypters.Count != 0; }
}
public IEnumerable<MethodDef> Methods {
get { return stringDecrypters.GetKeys(); }
}
class MyCallResult : CallResult {
public IMethod IMethod;
public MethodSpec gim;
public MyCallResult(Block block, int callEndIndex, IMethod method, MethodSpec gim)
: base(block, callEndIndex) {
this.IMethod = method;
this.gim = gim;
}
}
public void Add(MethodDef method, Func<MethodDef, MethodSpec, object[], string> handler) {
if (method != null)
stringDecrypters.Add(method, handler);
}
protected override void InlineAllCalls() {
foreach (var tmp in callResults) {
var callResult = (MyCallResult)tmp;
var handler = stringDecrypters.Find(callResult.IMethod);
callResult.returnValue = handler((MethodDef)callResult.IMethod, callResult.gim, callResult.args);
}
}
protected override CallResult CreateCallResult(IMethod method, MethodSpec gim, Block block, int callInstrIndex) {
if (stringDecrypters.Find(method) == null)
return null;
return new MyCallResult(block, callInstrIndex, method, gim);
}
}
}