-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMainWindow.xaml.cs
More file actions
201 lines (196 loc) · 7.64 KB
/
MainWindow.xaml.cs
File metadata and controls
201 lines (196 loc) · 7.64 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
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Diagnostics;
using Microsoft.Win32;
using System.IO;
using dll_injector;
namespace dll_injector_gui
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
private BackgroundWorker processScanner;
private DateTime scanTimer = DateTime.Now;
private TimeSpan preferredScanInterval = new TimeSpan(0, 0, 0, 0, 250);
private int scanCount = 0;
private Process selectedProcess = null;
private Process tmpSelectedProcess = null;
public MainWindow()
{
InitializeComponent();
processScanner = new BackgroundWorker();
processScanner.DoWork += ProcessScanner_DoWork;
processScanner.RunWorkerCompleted += ProcessScanner_RunWorkerCompleted;
processScanner.RunWorkerAsync();
//listProcesses.
listDLLs.KeyDown += ListDLLs_KeyDown;
listProcesses.KeyDown += ListProcesses_KeyDown;
listProcesses.MouseDoubleClick += ListProcesses_MouseDoubleClick;
listProcesses.SelectionChanged += ListProcesses_SelectionChanged;
}
private void ListProcesses_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (listProcesses.SelectedItem is FormattedProcess)
{
FormattedProcess selected = (FormattedProcess)listProcesses.SelectedItem;
tmpSelectedProcess = selected.Process;
}
}
private void ListProcesses_KeyDown(object sender, KeyEventArgs e)
{
if(e.Key == Key.Enter)
{
SelectProcess();
}
}
private void SelectProcess()
{
if (listProcesses.SelectedItem is FormattedProcess)
{
FormattedProcess selected = (FormattedProcess)listProcesses.SelectedItem;
selectedProcess = selected.Process;
radioProcessPID.IsChecked = true;
lblTarget.Text = selectedProcess.Id.ToString();
}
}
private void ListProcesses_MouseDoubleClick(object sender, MouseButtonEventArgs e)
{
SelectProcess();
}
private void UpdateProcessInfo()
{
StringBuilder sb = new StringBuilder();
Process selected = tmpSelectedProcess;
if(selected == null && selectedProcess != null)
{
selected = selectedProcess;
}
if(selected != null)
{
try
{
sb.AppendLine("Process Info:");
sb.AppendLine($"Name: {selected.ProcessName} ID: {selected.Id}");
sb.AppendLine($"Window Name: {selected.MainWindowTitle}");
sb.AppendLine($"Location: {selected.MainModule.FileName}");
sb.AppendLine($"Handle: {selected.Handle}");
sb.AppendLine($"Start Time: {selected.StartTime}");
sb.AppendLine($"Handle: {selected.Handle}");
sb.AppendLine($"Main Window Handle: {selected.MainWindowHandle}");
sb.AppendLine($"RAM Usage: {(selected.PrivateMemorySize64 / 1024 / 1024).ToString("###,##0.00 MB")}");
// sb.AppendLine($"Virtual Mem Usage: {(selected.VirtualMemorySize64 / 1024 / 1024).ToString("###,##0.00 MB")}"); // This gives corrupt (absurdly large) values. Ref: https://github.com/dotnet/runtime/issues/22184
sb.AppendLine($"Threads: {selected.Threads.Count}");
sb.AppendLine($"Priority: {selected.PriorityClass}");
}
catch (Exception E)
{
sb.AppendLine("Access Denied");
}
}
txtProcessInfo.Text = sb.ToString();
}
private bool CheckReflexInject()
{
if(selectedProcess is Process && (bool)chkReflex.IsChecked)
{
DllInjector dllInjector = new DllInjector(selectedProcess);
foreach (FormattedFileInfo fileInfo in listDLLs.Items)
{
if (fileInfo.FileInfo.Exists)
{
DllInjector.InjectReturnStatus status = dllInjector.InjectDll(fileInfo.FileInfo);
//TODO: Display errors
}
}
// TODO: Display message, automatically close window to avoid detection.
return true;
}
return false;
}
private void ListDLLs_KeyDown(object sender, KeyEventArgs e)
{
if(e.Key == Key.Delete)
{
listDLLs.Items.Remove(listDLLs.SelectedItem);
}
}
ProcessComparer processComparer = new ProcessComparer();
private void ProcessScanner_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
List<Process> processes = new List<Process>();
processes.AddRange((Process[])e.Result);
int selectedIndex = listProcesses.SelectedIndex;
processes.Sort(processComparer);
listProcesses.Items.Clear();
foreach (Process process in processes)
{
listProcesses.Items.Add(new FormattedProcess(process));
}
listProcesses.SelectedIndex = selectedIndex;
TimeSpan scanTime = DateTime.Now.Subtract(scanTimer);
lblScanTime.Content = $"Scan time: {(int)scanTime.TotalMilliseconds}ms ago"; // scan #: {++scanCount}";
scanTimer = DateTime.Now;
UpdateProcessInfo();
if (!CheckReflexInject())
{
if (scanTime < preferredScanInterval)
{
processScanner.RunWorkerAsync(preferredScanInterval - scanTime);
}
else
{
processScanner.RunWorkerAsync();
}
}
}
private void ProcessScanner_DoWork(object sender, DoWorkEventArgs e)
{
if(e.Argument != null)
{
TimeSpan timeSpan = (TimeSpan)e.Argument;
System.Threading.Thread.Sleep(timeSpan);
}
Process[] processes = Process.GetProcesses();
e.Result = processes;
}
private void lblBrowseDLL_Click(object sender, RoutedEventArgs e)
{
OpenFileDialog openFile = new OpenFileDialog();
openFile.Filter = "DLL files (*.dll)|*.dll|All files (*.*)|*.*";
if ((bool)openFile.ShowDialog())
{
string[] fileNames = openFile.FileNames;
foreach(string fileName in fileNames)
{
FileInfo fileInfo = new FileInfo(fileName);
if (fileInfo.Exists)
{
listDLLs.Items.Add(new FormattedFileInfo(fileInfo));
}
}
}
}
}
public class ProcessComparer : IComparer<Process>
{
public int Compare(Process a, Process b)
{
return a.ProcessName.CompareTo(b.ProcessName);
}
}
}