|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Diagnostics.Contracts; |
| 4 | +using System.Drawing; |
| 5 | +using System.IO.MemoryMappedFiles; |
| 6 | +using System.Runtime.InteropServices; |
| 7 | +using System.Windows.Forms; |
| 8 | +using ReClassNET.Plugins; |
| 9 | +using RGiesecke.DllExport; |
| 10 | +using static ReClassNET.Memory.NativeHelper; |
| 11 | + |
| 12 | +namespace LoadBinaryPlugin |
| 13 | +{ |
| 14 | + public class LoadBinaryPluginExt : Plugin |
| 15 | + { |
| 16 | + private static object sync = new object(); |
| 17 | + |
| 18 | + private static IPluginHost host; |
| 19 | + |
| 20 | + private static string currentFile; |
| 21 | + |
| 22 | + private static Dictionary<IntPtr, MemoryMappedFile> openFiles; |
| 23 | + |
| 24 | + public override Image Icon => Properties.Resources.icon; |
| 25 | + |
| 26 | + public override bool Initialize(IPluginHost host) |
| 27 | + { |
| 28 | + Contract.Requires(host != null); |
| 29 | + |
| 30 | + System.Diagnostics.Debugger.Launch(); |
| 31 | + |
| 32 | + if (host == null) |
| 33 | + { |
| 34 | + throw new ArgumentNullException(nameof(host)); |
| 35 | + } |
| 36 | + |
| 37 | + openFiles = new Dictionary<IntPtr, MemoryMappedFile>(); |
| 38 | + |
| 39 | + return true; |
| 40 | + } |
| 41 | + |
| 42 | + public override void Terminate() |
| 43 | + { |
| 44 | + foreach (var kv in openFiles) |
| 45 | + { |
| 46 | + kv.Value.Dispose(); |
| 47 | + } |
| 48 | + openFiles.Clear(); |
| 49 | + |
| 50 | + host = null; |
| 51 | + } |
| 52 | + |
| 53 | + /// <summary>Gets a <see cref="MemoryMappedFile"/> by its plugin internal identifier.</summary> |
| 54 | + /// <param name="id">The identifier.</param> |
| 55 | + /// <returns>The file or null if the identifier doesn't exist.</returns> |
| 56 | + private static MemoryMappedFile GetMappedFileById(IntPtr id) |
| 57 | + { |
| 58 | + MemoryMappedFile file; |
| 59 | + openFiles.TryGetValue(id, out file); |
| 60 | + return file; |
| 61 | + } |
| 62 | + |
| 63 | + /// <summary>Logs the exception and removes the file.</summary> |
| 64 | + /// <param name="id">The identifier.</param> |
| 65 | + /// <param name="ex">The exception.</param> |
| 66 | + private static void LogErrorAndRemoveFile(IntPtr id, Exception ex) |
| 67 | + { |
| 68 | + Contract.Requires(ex != null); |
| 69 | + |
| 70 | + MemoryMappedFile file; |
| 71 | + if (openFiles.TryGetValue(id, out file)) |
| 72 | + { |
| 73 | + file.Dispose(); |
| 74 | + } |
| 75 | + |
| 76 | + openFiles.Remove(id); |
| 77 | + |
| 78 | + host.Logger.Log(ex); |
| 79 | + } |
| 80 | + |
| 81 | + /// <summary>Queries if the file is valid.</summary> |
| 82 | + /// <param name="process">The file to check.</param> |
| 83 | + /// <returns>True if the file is valid, false if not.</returns> |
| 84 | + [DllExport(CallingConvention = CallingConvention.StdCall)] |
| 85 | + public static bool IsProcessValid(IntPtr process) |
| 86 | + { |
| 87 | + lock (sync) |
| 88 | + { |
| 89 | + return GetMappedFileById(process) != null; |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + /// <summary>Opens the file.</summary> |
| 94 | + /// <param name="pid">The file id.</param> |
| 95 | + /// <param name="desiredAccess">The desired access. (ignored)</param> |
| 96 | + /// <returns>A plugin internal handle to the file.</returns> |
| 97 | + [DllExport(CallingConvention = CallingConvention.StdCall)] |
| 98 | + private static IntPtr OpenRemoteProcess(int pid, int desiredAccess) |
| 99 | + { |
| 100 | + lock (sync) |
| 101 | + { |
| 102 | + try |
| 103 | + { |
| 104 | + var file = MemoryMappedFile.CreateFromFile(currentFile); |
| 105 | + |
| 106 | + var handle = file.SafeMemoryMappedFileHandle.DangerousGetHandle(); |
| 107 | + |
| 108 | + openFiles.Add(handle, file); |
| 109 | + |
| 110 | + return handle; |
| 111 | + } |
| 112 | + catch (Exception ex) |
| 113 | + { |
| 114 | + host.Logger.Log(ex); |
| 115 | + } |
| 116 | + } |
| 117 | + |
| 118 | + return IntPtr.Zero; |
| 119 | + } |
| 120 | + |
| 121 | + /// <summary>Closes the file.</summary> |
| 122 | + /// <param name="process">The file to close.</param> |
| 123 | + [DllExport(CallingConvention = CallingConvention.StdCall)] |
| 124 | + private static void CloseRemoteProcess(IntPtr process) |
| 125 | + { |
| 126 | + lock (sync) |
| 127 | + { |
| 128 | + MemoryMappedFile file; |
| 129 | + if (openFiles.TryGetValue(process, out file)) |
| 130 | + { |
| 131 | + openFiles.Remove(process); |
| 132 | + |
| 133 | + file.Dispose(); |
| 134 | + } |
| 135 | + } |
| 136 | + } |
| 137 | + |
| 138 | + /// <summary>Reads memory of the file.</summary> |
| 139 | + /// <param name="process">The process to read from.</param> |
| 140 | + /// <param name="address">The address to read from.</param> |
| 141 | + /// <param name="buffer">The buffer to read into.</param> |
| 142 | + /// <param name="size">The size of the memory to read.</param> |
| 143 | + /// <returns>True if it succeeds, false if it fails.</returns> |
| 144 | + [DllExport(CallingConvention = CallingConvention.StdCall)] |
| 145 | + private static bool ReadRemoteMemory(IntPtr process, IntPtr address, IntPtr buffer, int size) |
| 146 | + { |
| 147 | + lock (sync) |
| 148 | + { |
| 149 | + var file = GetMappedFileById(process); |
| 150 | + if (file != null) |
| 151 | + { |
| 152 | + try |
| 153 | + { |
| 154 | + using (var stream = file.CreateViewStream(address.ToInt64(), size)) |
| 155 | + { |
| 156 | + var tempBuffer = new byte[size]; |
| 157 | + stream.Read(tempBuffer, 0, size); |
| 158 | + |
| 159 | + Marshal.Copy(tempBuffer, 0, buffer, size); |
| 160 | + |
| 161 | + return true; |
| 162 | + } |
| 163 | + } |
| 164 | + catch (UnauthorizedAccessException) |
| 165 | + { |
| 166 | + // address + size >= file size |
| 167 | + } |
| 168 | + catch (Exception ex) |
| 169 | + { |
| 170 | + LogErrorAndRemoveFile(process, ex); |
| 171 | + } |
| 172 | + } |
| 173 | + |
| 174 | + return false; |
| 175 | + } |
| 176 | + } |
| 177 | + |
| 178 | + /// <summary>Not supported.</summary> |
| 179 | + /// <param name="process">The file to write to.</param> |
| 180 | + /// <param name="address">The address to write to.</param> |
| 181 | + /// <param name="buffer">The memory to write.</param> |
| 182 | + /// <param name="size">The size of the memory to write.</param> |
| 183 | + /// <returns>True if it succeeds, false if it fails.</returns> |
| 184 | + [DllExport(CallingConvention = CallingConvention.StdCall)] |
| 185 | + private static bool WriteRemoteMemory(IntPtr process, IntPtr address, IntPtr buffer, int size) |
| 186 | + { |
| 187 | + // No write support. |
| 188 | + |
| 189 | + return false; |
| 190 | + } |
| 191 | + |
| 192 | + /// <summary>Opens a file browser dialog and reports the selected file.</summary> |
| 193 | + /// <param name="callbackProcess">The callback which gets called for the selected file.</param> |
| 194 | + [DllExport(CallingConvention = CallingConvention.StdCall)] |
| 195 | + private static void EnumerateProcesses(EnumerateProcessCallback callbackProcess) |
| 196 | + { |
| 197 | + if (callbackProcess == null) |
| 198 | + { |
| 199 | + return; |
| 200 | + } |
| 201 | + |
| 202 | + using (var ofd = new OpenFileDialog()) |
| 203 | + { |
| 204 | + ofd.Filter = "All|*.*"; |
| 205 | + |
| 206 | + if (ofd.ShowDialog() == DialogResult.OK) |
| 207 | + { |
| 208 | + currentFile = ofd.FileName; |
| 209 | + |
| 210 | + callbackProcess(currentFile.GetHashCode(), currentFile); |
| 211 | + } |
| 212 | + } |
| 213 | + } |
| 214 | + |
| 215 | + /// <summary>Not supported.</summary> |
| 216 | + /// <param name="process">The process.</param> |
| 217 | + /// <param name="callbackSection">The callback which gets called for every section.</param> |
| 218 | + /// <param name="callbackModule">The callback which gets called for every module.</param> |
| 219 | + [DllExport(CallingConvention = CallingConvention.StdCall)] |
| 220 | + private static void EnumerateRemoteSectionsAndModules(IntPtr process, EnumerateRemoteSectionCallback callbackSection, EnumerateRemoteModuleCallback callbackModule) |
| 221 | + { |
| 222 | + // No modules and sections here. |
| 223 | + } |
| 224 | + } |
| 225 | +} |
0 commit comments