This repository was archived by the owner on Jan 27, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
Copy pathCompressor.cs
113 lines (100 loc) · 2.9 KB
/
Compressor.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
using System;
using System.IO;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Text;
namespace Confuser.Runtime {
internal static class Compressor {
static byte[] key;
static GCHandle Decrypt(uint[] data, uint seed) {
var w = new uint[0x10];
var k = new uint[0x10];
ulong s = seed;
for (int i = 0; i < 0x10; i++) {
s = (s * s) % 0x143fc089;
k[i] = (uint)s;
w[i] = (uint)((s * s) % 0x444d56fb);
}
Mutation.Crypt(w, k);
Array.Clear(k, 0, 0x10);
var b = new byte[data.Length << 2];
uint h = 0;
for (int i = 0; i < data.Length; i++) {
uint d = data[i] ^ w[i & 0xf];
w[i & 0xf] = (w[i & 0xf] ^ d) + 0x3ddb2819;
b[h + 0] = (byte)(d >> 0);
b[h + 1] = (byte)(d >> 8);
b[h + 2] = (byte)(d >> 16);
b[h + 3] = (byte)(d >> 24);
h += 4;
}
Array.Clear(w, 0, 0x10);
byte[] j = Lzma.Decompress(b);
Array.Clear(b, 0, b.Length);
GCHandle g = GCHandle.Alloc(j, GCHandleType.Pinned);
var z = (uint)(s % 0x8a5cb7);
for (int i = 0; i < j.Length; i++) {
j[i] ^= (byte)s;
if ((i & 0xff) == 0)
s = (s * s) % 0x8a5cb7;
}
return g;
}
[STAThread]
static int Main(string[] args) {
var l = (uint)Mutation.KeyI0;
uint[] q = Mutation.Placeholder(new uint[Mutation.KeyI0]);
Assembly a = Assembly.GetExecutingAssembly();
Module n = a.ManifestModule;
GCHandle h = Decrypt(q, (uint)Mutation.KeyI1);
var b = (byte[])h.Target;
Module m = a.LoadModule("koi", b);
Array.Clear(b, 0, b.Length);
h.Free();
Array.Clear(q, 0, q.Length);
key = n.ResolveSignature(Mutation.KeyI2);
AppDomain.CurrentDomain.AssemblyResolve += Resolve;
// For some reasons, reflection on Assembly would not discover the types unless GetTypes is called.
m.GetTypes();
MethodBase e = m.ResolveMethod(key[0] | (key[1] << 8) | (key[2] << 16) | (key[3] << 24));
var g = new object[e.GetParameters().Length];
if (g.Length != 0)
g[0] = args;
object r = e.Invoke(null, g);
if (r is int)
return (int)r;
return 0;
}
static Assembly Resolve(object sender, ResolveEventArgs e) {
byte[] b = Encoding.UTF8.GetBytes(new AssemblyName(e.Name).FullName.ToUpperInvariant());
Stream m = null;
if (b.Length + 4 <= key.Length) {
for (int i = 0; i < b.Length; i++)
b[i] *= key[i + 4];
string n = Convert.ToBase64String(b);
m = Assembly.GetEntryAssembly().GetManifestResourceStream(n);
}
if (m != null) {
var d = new uint[m.Length >> 2];
var t = new byte[0x100];
int r;
int o = 0;
while ((r = m.Read(t, 0, 0x100)) > 0) {
Buffer.BlockCopy(t, 0, d, o, r);
o += r;
}
uint s = 0x6fff61;
foreach (byte c in b)
s = s * 0x5e3f1f + c;
GCHandle h = Decrypt(d, s);
var f = (byte[])h.Target;
Assembly a = Assembly.Load(f);
Array.Clear(f, 0, f.Length);
h.Free();
Array.Clear(d, 0, d.Length);
return a;
}
return null;
}
}
}