-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
289 lines (252 loc) · 11 KB
/
Program.cs
File metadata and controls
289 lines (252 loc) · 11 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
281
282
283
284
285
286
287
288
using System;
using System.ComponentModel;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Threading;
using System.Windows.Threading;
namespace FridaClrInjector
{
internal static class Program
{
private static int Main(string[] args)
{
try
{
string scriptPath = GetArg(args, "--script");
string pidStr = GetArg(args, "--pid");
string spawnPath = GetArg(args, "--spawn");
string spawnArgs = GetArg(args, "--args"); // optional (parsed with CommandLineToArgvW)
string procName = GetArg(args, "--name"); // optional alternative to --pid
string deviceId = GetArg(args, "--device"); // optional
if (HasFlag(args, "--help") || HasFlag(args, "-h"))
{
PrintUsage();
return 0;
}
if (string.IsNullOrWhiteSpace(scriptPath))
{
Console.Error.WriteLine("Missing --script");
PrintUsage();
return 2;
}
scriptPath = Path.GetFullPath(scriptPath);
if (!File.Exists(scriptPath))
{
Console.Error.WriteLine("Script not found: " + scriptPath);
return 2;
}
int modeCount = 0;
if (!string.IsNullOrWhiteSpace(pidStr)) modeCount++;
if (!string.IsNullOrWhiteSpace(spawnPath)) modeCount++;
if (!string.IsNullOrWhiteSpace(procName)) modeCount++;
if (modeCount != 1)
{
Console.Error.WriteLine("Specify exactly one of: --pid, --spawn, --name");
PrintUsage();
return 2;
}
// frida-clr marshals callbacks onto a WPF Dispatcher; keep one alive on an STA thread.
Dispatcher dispatcher = null;
var dispatcherReady = new ManualResetEvent(false);
var dispatcherThread = new Thread(() =>
{
dispatcher = Dispatcher.CurrentDispatcher;
dispatcherReady.Set();
Dispatcher.Run();
})
{
IsBackground = true
};
dispatcherThread.SetApartmentState(ApartmentState.STA);
dispatcherThread.Start();
dispatcherReady.WaitOne();
// Create device manager and pick a device.
var dm = new Frida.DeviceManager(dispatcher);
var devices = dm.EnumerateDevices();
var device = !string.IsNullOrWhiteSpace(deviceId)
? devices.FirstOrDefault(d => string.Equals(d.Id, deviceId, StringComparison.OrdinalIgnoreCase))
: devices.FirstOrDefault(d => d.Type == Frida.DeviceType.Local);
if (device == null)
{
Console.Error.WriteLine(string.IsNullOrWhiteSpace(deviceId)
? "No local Frida device found."
: "No device found with id: " + deviceId);
ShutdownDispatcher(dispatcher, dispatcherThread);
return 1;
}
uint targetPid;
bool spawned = false;
if (!string.IsNullOrWhiteSpace(spawnPath))
{
spawnPath = Path.GetFullPath(spawnPath);
if (!File.Exists(spawnPath))
{
Console.Error.WriteLine("Spawn target not found: " + spawnPath);
ShutdownDispatcher(dispatcher, dispatcherThread);
return 2;
}
string[] argv = BuildArgv(spawnPath, spawnArgs);
// Spawn suspended so we can inject before the first instructions run.
targetPid = device.Spawn(spawnPath, argv, null, null, null);
spawned = true;
Console.WriteLine("[+] Spawned PID: " + targetPid);
}
else if (!string.IsNullOrWhiteSpace(pidStr))
{
if (!uint.TryParse(pidStr, out targetPid))
{
Console.Error.WriteLine("Invalid PID: " + pidStr);
ShutdownDispatcher(dispatcher, dispatcherThread);
return 2;
}
}
else
{
// Attach by process name (first match).
var procs = device.EnumerateProcesses(Frida.Scope.Minimal);
var matches = procs.Where(p => string.Equals(p.Name, procName, StringComparison.OrdinalIgnoreCase)).ToArray();
if (matches.Length == 0)
{
Console.Error.WriteLine("No process found with name: " + procName);
ShutdownDispatcher(dispatcher, dispatcherThread);
return 1;
}
if (matches.Length > 1)
{
Console.Error.WriteLine("Multiple processes matched. Use --pid instead. Matches:");
foreach (var p in matches)
Console.Error.WriteLine(" " + p.Pid + " " + p.Name);
ShutdownDispatcher(dispatcher, dispatcherThread);
return 1;
}
targetPid = matches[0].Pid;
}
var session = device.Attach(targetPid);
session.Detached += (s, e) =>
{
Console.WriteLine("[session] detached: " + e.Reason);
};
string scriptSource = File.ReadAllText(scriptPath);
var script = session.CreateScript(scriptSource, Path.GetFileName(scriptPath));
script.Message += (s, e) =>
{
Console.WriteLine("[script] " + e.Message);
};
script.Load();
Console.WriteLine("[+] Script loaded: " + scriptPath);
if (spawned)
{
device.Resume(targetPid);
Console.WriteLine("[+] Resumed PID: " + targetPid);
}
var done = new ManualResetEvent(false);
Console.CancelKeyPress += (s, e) =>
{
e.Cancel = true;
done.Set();
};
Console.WriteLine("[*] Press Enter or Ctrl+C to detach...");
ThreadPool.QueueUserWorkItem(_ =>
{
try { Console.ReadLine(); } catch { }
done.Set();
});
done.WaitOne();
try { script.Unload(); } catch { }
try { session.Detach(); } catch { }
ShutdownDispatcher(dispatcher, dispatcherThread);
return 0;
}
catch (Exception ex)
{
Console.Error.WriteLine("[!] Fatal: " + ex);
return 1;
}
}
private static void PrintUsage()
{
Console.Error.WriteLine("Usage:");
Console.Error.WriteLine(" FridaClrInjector.exe --pid <pid> --script <file.js> [--device <id>]");
Console.Error.WriteLine(" FridaClrInjector.exe --name <process.exe> --script <file.js> [--device <id>]");
Console.Error.WriteLine(" FridaClrInjector.exe --spawn <fullpath.exe> [--args \"<args>\"] --script <file.js> [--device <id>]");
Console.Error.WriteLine();
Console.Error.WriteLine("Examples:");
Console.Error.WriteLine(" FridaClrInjector.exe --pid 1234 --script hooks\\hook_messagebox.js");
Console.Error.WriteLine(" FridaClrInjector.exe --spawn \"C:\\\\Windows\\\\SysWOW64\\\\notepad.exe\" --script hooks\\hook_createfilew.js");
}
private static bool HasFlag(string[] args, string name)
{
return args.Any(a => string.Equals(a, name, StringComparison.OrdinalIgnoreCase));
}
private static string GetArg(string[] args, string name)
{
for (int i = 0; i < args.Length; i++)
{
if (string.Equals(args[i], name, StringComparison.OrdinalIgnoreCase) && i + 1 < args.Length)
return args[i + 1];
}
return null;
}
private static string[] BuildArgv(string exePath, string argString)
{
if (string.IsNullOrWhiteSpace(argString))
return new[] { exePath };
// Use Windows' own command-line parsing rules (quotes/backslashes) so argv matches what the
// target would see as Environment.GetCommandLineArgs().
string cmdline = QuoteArg(exePath) + " " + argString;
string[] parsed = ParseCommandLine(cmdline);
if (parsed.Length == 0)
return new[] { exePath };
// Ensure argv[0] is the real executable path.
parsed[0] = exePath;
return parsed;
}
private static string QuoteArg(string s)
{
if (string.IsNullOrEmpty(s))
return "\"\"";
if (s.IndexOfAny(new[] { ' ', '\t', '\n', '\v', '"' }) == -1)
return s;
return "\"" + s.Replace("\"", "\\\"") + "\"";
}
private static string[] ParseCommandLine(string cmdLine)
{
IntPtr argv = CommandLineToArgvW(cmdLine, out int argc);
if (argv == IntPtr.Zero)
throw new Win32Exception(Marshal.GetLastWin32Error(), "CommandLineToArgvW failed");
try
{
var result = new string[argc];
for (int i = 0; i < argc; i++)
{
IntPtr p = Marshal.ReadIntPtr(argv, i * IntPtr.Size);
result[i] = Marshal.PtrToStringUni(p);
}
return result;
}
finally
{
LocalFree(argv);
}
}
private static void ShutdownDispatcher(Dispatcher dispatcher, Thread dispatcherThread)
{
try
{
dispatcher.BeginInvokeShutdown(DispatcherPriority.Normal);
}
catch { }
try
{
if (dispatcherThread.IsAlive)
dispatcherThread.Join();
}
catch { }
}
[DllImport("shell32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
private static extern IntPtr CommandLineToArgvW(string lpCmdLine, out int pNumArgs);
[DllImport("kernel32.dll", SetLastError = true)]
private static extern IntPtr LocalFree(IntPtr hMem);
}
}