This repository has been archived by the owner on Jul 26, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Program.cs
292 lines (250 loc) · 10.8 KB
/
Program.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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Mono.Cecil;
using Mono.Cecil.Cil;
namespace osuPatcher
{
internal class Program
{
/// <summary>
/// osu! assembly.
/// </summary>
private static AssemblyDefinition Assembly;
/// <summary>
/// A bool to tell to patcher if it will mantain the https connection.
/// </summary>
private static bool KeepHttps;
/// <summary>
/// A list of <seealso cref="Tuple{T1, T2}"/> storing the replaces that the patcher will force osu to do in its requests.
/// </summary>
private static List<Tuple<string, string>> Replaces;
/// <summary>
/// The entry point of Patcher
/// </summary>
private static void Main()
{
if (!File.Exists("osu!.exe"))
{
Console.WriteLine("osu!.exe not found.");
Environment.Exit(-1);
}
KeepHttps = false;
Replaces = new List<Tuple<string, string>> // <oldString, newString>
{
Tuple.Create("osu.ppy.sh", "localhost"),
Tuple.Create("a.ppy.sh", "localhost"),
Tuple.Create("c.ppy.sh", "localhost:890"),
Tuple.Create("c1.ppy.sh", "localhost:890")
};
Console.WriteLine("Trying to patch osu..");
if (PatcherV2() || PatcherV1())
{
Console.WriteLine("Success! Saving the patched osu..");
try
{
Assembly.Write("osu!_patched.exe");
Console.WriteLine("Saved.");
}
catch (Exception e)
{
Console.WriteLine("Error: "+e.Message);
}
}
else Console.WriteLine("Failed, none of the patch processes were completed successfully.");
Console.WriteLine();
Console.WriteLine("Press any key to exit");
Console.ReadKey(true);
}
/// <summary>
/// First version of patcher, working in all versions newer than fallback and older than 08042016.
/// </summary>
/// <returns>Patched sucessfully?</returns>
private static bool PatcherV1()
{
Assembly = AssemblyDefinition.ReadAssembly("osu!.exe");
string[] signature =
{
"ldarg.0",
"call",
"ldc.i4",
"call",
"callvirt",
"brtrue.s",
"ldarg.0",
"ldc.i4",
"call",
"ldarg.0"
};
foreach (TypeDefinition type in Assembly.MainModule.Types.Where(type => type.Name != "<Module>"))
{
foreach (MethodDefinition method in type.Methods.Where(method => method.Body != null && method.Body.Instructions.Count > 10))
{
for (int i = 0; i != 10; i++) //Search by signature in current method
{
if (method.Body.Instructions[i].OpCode.ToString() != signature[i])
goto cont;
}
//If found, start the patch process
ILProcessor worker = method.Body.GetILProcessor();
Instruction curi = worker.Body.Instructions[1];
curi = curi.Next; //ldc.i4
ReplaceProtectionWith("http://", ref curi, worker);
curi = curi.Next; //brtrue
if (curi.OpCode.ToString() != "brtrue.s")
{
Console.WriteLine("Semething is very wrong..");
return false;
}
if (!KeepHttps)
{
curi = curi.Next.Next; //ldc.i4
ReplaceProtectionWith("http://", ref curi, worker); //ldarg.0
curi = curi.Next.Next; //ldc.i4
ReplaceProtectionWith("https://", ref curi, worker);
}
else
curi = worker.Body.Instructions[worker.Body.Instructions.IndexOf(curi) + 8];
curi = curi.Next; //reference of Method Replace
MethodReference methodRC = curi.Operand as MethodReference;
foreach (Tuple<string, string> pair in Replaces)
{
InsertReplace(pair.Item1, pair.Item2, ref curi, worker, methodRC);
}
return true;
cont:;
}
}
return false;
}
/// <summary>
/// Second version of patcher, currently working in versions newer than 08042016.
/// </summary>
/// <returns>Patched sucessfully?</returns>
private static bool PatcherV2()
{
Assembly = AssemblyDefinition.ReadAssembly("osu!.exe");
bool P1 = false;
string[] signatureP2 =
{
"ldarg.0",
"call",
"leave.s",
"stloc.0",
"ldarg.0",
"ldloc.0",
"ldc.i4.0",
"call"
};
int P2 = 0; //P2 has two matchs
string[] signatureP3 =
{
"ldarg.1",
"ldc.i4",
"call",
"callvirt",
"brtrue.s",
"ldc.i4",
"call",
"ldarg.1"
};
bool P3 = false;
foreach (TypeDefinition type in Assembly.MainModule.Types.Where(type => type.Name != "<Module>"))
{
foreach (MethodDefinition method in type.Methods.Where(method => method.Body != null && method.Body.Instructions.Count > 8))
{
if (!P1 && method.Body.Instructions[3].OpCode.ToString() == "call" &&
method.Body.Instructions[3].Operand.ToString().Contains("Guid:"))
{
//Disable the "unsigned executable" error.
ILProcessor _worker = method.Body.GetILProcessor();
_worker.Replace(method.Body.Instructions.Last().Previous, _worker.Create(OpCodes.Ldc_I4_1));
P1 = true;
continue;
}
if (P2<2)
{
for (int i = 0; i != 8; i++) //Search by signature2 in current method
{
if (method.Body.Instructions[i].OpCode.ToString() != signatureP2[i])
goto exitP2;
}
//If found, remove method content
ILProcessor _worker = method.Body.GetILProcessor();
foreach (Instruction ins in _worker.Body.Instructions.ToList())
_worker.Remove(ins);
_worker.Body.ExceptionHandlers.Clear();
_worker.Emit(OpCodes.Ret);
++P2;
continue;
}
exitP2:;
if (!P3)
{
for (int i = 0; i != 8; i++) //Search by signature in current method
{
if (method.Body.Instructions[i].OpCode.ToString() != signatureP3[i])
goto exitP3;
}
//If found, start the patch process
ILProcessor worker = method.Body.GetILProcessor();
Instruction curi = worker.Body.Instructions[1];
ReplaceProtectionWith("http://", ref curi, worker);
curi = curi.Next; //brtrue
if (curi.OpCode.ToString() != "brtrue.s")
{
Console.WriteLine("Semething is very wrong..");
return false;
}
if (!KeepHttps)
{
curi = curi.Next; //ldc.i4
ReplaceProtectionWith("http://", ref curi, worker); //ldarg.0
curi = curi.Next; //ldc.i4
ReplaceProtectionWith("https://", ref curi, worker);
}
else
curi = worker.Body.Instructions[worker.Body.Instructions.IndexOf(curi) + 8];
curi = curi.Next; //reference of Method Replace
MethodReference methodRC = curi.Operand as MethodReference;
foreach (Tuple<string, string> pair in Replaces)
{
InsertReplace(pair.Item1, pair.Item2, ref curi, worker, methodRC);
}
P3 = true;
}
exitP3:;
}
if (P1 && P2==2 && P3) break;
}
return P1 && P2==2 && P3;
}
/// <summary>
/// Insert an replace method after the current IL instruction.
/// </summary>
/// <param name="from"></param>
/// <param name="to">Replace all occurencies of <seealso cref="from"/> by it</param>
/// <param name="curi">Current IL instruction</param>
/// <param name="worker">Current IL worker</param>
/// <param name="methodRC">An reference to replace method to use as base</param>
private static void InsertReplace(string from, string to, ref Instruction curi, ILProcessor worker, MethodReference methodRC)
{
worker.InsertAfter(curi, curi = worker.Create(OpCodes.Ldstr, from));
worker.InsertAfter(curi, curi = worker.Create(OpCodes.Ldstr, to));
worker.InsertAfter(curi, curi = worker.Create(OpCodes.Callvirt, methodRC));
}
/// <summary>
/// Replace an Eazfuscator protected string with an unprotected string.
/// </summary>
/// <param name="str"></param>
/// <param name="curi">Current IL instruction</param>
/// <param name="worker">Current IL worker</param>
private static void ReplaceProtectionWith(string str, ref Instruction curi, ILProcessor worker)
{
worker.Replace(curi, curi = worker.Create(OpCodes.Ldstr, str));
worker.Remove(curi.Next);
curi = curi.Next;
}
}
}