private PrintComment ( String InMessage ) : void | ||
InMessage | String | |
Résultat | void |
public static void GacUninstallAssemblies( String[] InAssemblyNames, String InDescription, String InUniqueID) { try { System.GACManagedAccess.AssemblyCacheUninstallDisposition[] results; System.GACManagedAccess.AssemblyCache.UninstallAssemblies( InAssemblyNames, new System.GACManagedAccess.InstallReference(System.GACManagedAccess.InstallReferenceGuid.OpaqueGuid, InUniqueID, InDescription), out results); // disable warnings Obsolete and Obsolete("message") #pragma warning disable 612, 618 for (var i = 0; i < InAssemblyNames.Length; i++) { Config.PrintComment("GacUninstallAssembly: Assembly {0}, uninstall result {1}", InAssemblyNames[i], results[i]); } // enable warnings for Obsolete and Obsolete("message") #pragma warning restore 612, 618 } catch (Exception e) { throw new ApplicationException("Unable to uninstall assemblies from GAC, see inner exception for details", e); } }
/// <summary> /// Finds the <see cref="IEntryPoint"/> in the specified <see cref="Assembly"/>. /// </summary> /// <exception cref="EntryPointNotFoundException"> /// An <see cref="EntryPointNotFoundException"/> is thrown if the given user library does not export a proper type implementing /// the <see cref="IEntryPoint"/> interface. /// </exception> /// <param name="userAssemblyStrongName">The strong name of the assembly provided by the user.</param> /// <param name="userAssemblyFileName">The file name of the assembly provided by the user.</param> /// <returns>The <see cref="Type"/> functioning as <see cref="IEntryPoint"/> for the user provided <see cref="Assembly"/>.</returns> private static Type FindEntryPoint(string userAssemblyStrongName, string userAssemblyFileName) { Assembly userAssembly = null; StringBuilder errors = new StringBuilder(); // First try to find the assembly using a strong name (if provided) if (!String.IsNullOrEmpty(userAssemblyStrongName)) { try { userAssembly = Assembly.Load(userAssemblyStrongName); Config.PrintComment("SUCCESS: Assembly.Load({0})", userAssemblyStrongName); } catch (Exception e) { Config.PrintComment("FAIL: Assembly.Load({0}) - {1}", userAssemblyStrongName, e.ToString()); errors.AppendFormat("Failed to load assembly using strong name {0} - {1}\r\n", userAssemblyStrongName, e.ToString()); } } // If loading by strong name failed, attempt to load the assembly from its file location if (userAssembly == null) { try { userAssembly = Assembly.LoadFrom(userAssemblyFileName); Config.PrintComment("SUCCESS: Assembly.LoadFrom({0})", userAssemblyFileName); } catch (Exception e) { Config.PrintComment("FAIL: Assembly.LoadFrom({0}) - {1}", userAssemblyFileName, e.ToString()); errors.AppendFormat("Failed to load assembly from file {0} - {1}", userAssemblyFileName, e.ToString()); } } // If both attempts fail, raise an exception with the failed attempts. if (userAssembly == null) { Config.PrintError("Could not load assembly {0}, {1}", userAssemblyFileName, userAssemblyStrongName); throw new Exception(errors.ToString()); } // Find the first EasyHook.IEntryPoint var exportedTypes = userAssembly.GetExportedTypes(); for (int i = 0; i < exportedTypes.Length; i++) { if (exportedTypes[i].GetInterface("EasyHook.IEntryPoint") != null) { return(exportedTypes[i]); } } throw new EntryPointNotFoundException("The given library does not include a public class implementing the 'EasyHook.IEntryPoint' interface."); }
/// <summary> /// Finds the <see cref="IEntryPoint" /> in the specified <see cref="Assembly" />. /// </summary> /// <exception cref="EntryPointNotFoundException"> /// An <see cref="EntryPointNotFoundException" /> is thrown if the given user library does not export a proper type /// implementing /// the <see cref="IEntryPoint" /> interface. /// </exception> /// <param name="userAssemblyStrongName">The strong name of the assembly provided by the user.</param> /// <param name="userAssemblyFileName">The file name of the assembly provided by the user.</param> /// <returns> /// The <see cref="Type" /> functioning as <see cref="IEntryPoint" /> for the user provided /// <see cref="Assembly" />. /// </returns> private static Type FindEntryPoint(string userAssemblyStrongName, string userAssemblyFileName) { Assembly userAssembly = null; // First try to find the assembly using a strong name (if provided) if (!String.IsNullOrEmpty(userAssemblyStrongName)) { try { userAssembly = Assembly.Load(userAssemblyStrongName); Config.PrintComment("SUCCESS: Assembly.Load({0})", userAssemblyStrongName); } catch (Exception e) { Config.PrintComment("FAIL: Assembly.Load({0}) - {1}", userAssemblyStrongName, e.ToString()); } } // If loading by strong name failed, attempt to load the assembly from its file location if (userAssembly == null) { try { userAssembly = Assembly.LoadFrom(userAssemblyFileName); Config.PrintComment("SUCCESS: Assembly.LoadFrom({0})", userAssemblyFileName); } catch (Exception e) { Config.PrintComment("FAIL: Assembly.LoadFrom({0}) - {1}", userAssemblyFileName, e.ToString()); } } if (userAssembly == null) { Config.PrintError("Could not load assembly {0}, {1}", userAssemblyFileName, userAssemblyStrongName); throw new FileNotFoundException("The given user library could not be found."); } // Find the first EasyHook.IEntryPoint var exportedTypes = userAssembly.GetExportedTypes(); for (var i = 0; i < exportedTypes.Length; i++) { if (exportedTypes[i].GetInterface("EasyHook.IEntryPoint") != null) { return(exportedTypes[i]); } } throw new EntryPointNotFoundException("The given user library does not export a proper type implementing the 'EasyHook.IEntryPoint' interface."); }
public static void GacUninstallAssemblies( String[] InAssemblyNames, String InDescription, String InUniqueID) { try { AssemblyCacheUninstallDisposition[] results; AssemblyCache.UninstallAssemblies( InAssemblyNames, new InstallReference(InstallReferenceGuid.OpaqueGuid, InUniqueID, InDescription), out results); for (var i = 0; i < InAssemblyNames.Length; i++) { Config.PrintComment("GacUninstallAssembly: Assembly {0}, uninstall result {1}", InAssemblyNames[i], results[i]); } } catch (Exception e) { throw new ApplicationException("Unable to uninstall assemblies from GAC, see inner exception for details", e); } }
#pragma warning disable 0028 public static int Main(String InParam) { HelperServiceInterface Interface; Assembly UserAssembly = null; Type EntryPoint = null; ManagedRemoteInfo RemoteInfo; REMOTE_ENTRY_INFO UnmanagedInfo = new REMOTE_ENTRY_INFO(); if (InParam == null) { return(0); } /* * We will now connect to our hooking host. This is to provide extensive * error information. */ try { Marshal.PtrToStructure( (IntPtr)Int64.Parse(InParam, System.Globalization.NumberStyles.HexNumber), UnmanagedInfo); Byte[] PassThruBytes = new Byte[UnmanagedInfo.UserDataSize]; MemoryStream PassThruStream = new MemoryStream(); BinaryFormatter Format = new BinaryFormatter(); // Workaround for deserialization when not using GAC registration Format.Binder = new AllowAllAssemblyVersionsDeserializationBinder(); Marshal.Copy(UnmanagedInfo.UserData, PassThruBytes, 0, UnmanagedInfo.UserDataSize); PassThruStream.Write(PassThruBytes, 0, PassThruBytes.Length); PassThruStream.Position = 0; RemoteInfo = (ManagedRemoteInfo)Format.Deserialize(PassThruStream); Interface = RemoteHooking.IpcConnectClient <HelperServiceInterface>(RemoteInfo.ChannelName); // ensure connection... Interface.Ping(); if (!ConnectedChannels.Contains(RemoteInfo.ChannelName)) { ConnectedChannels.Add(RemoteInfo.ChannelName); return(1); } } catch (Exception ExtInfo) { Config.PrintError(ExtInfo.ToString()); return(0); } try { // adjust host PID in case of WOW64 bypass and service help... UnmanagedInfo.m_HostPID = RemoteInfo.HostPID; // prepare parameter array Object[] ParamArray = new Object[1 + RemoteInfo.UserParams.Length]; ParamArray[0] = (RemoteHooking.IContext)UnmanagedInfo; for (int i = 0; i < RemoteInfo.UserParams.Length; i++) { ParamArray[i + 1] = RemoteInfo.UserParams[i]; } /* * After this we are ready to load the user supplied library. */ Object Instance = null; try { // First attempt to load the library using its full name (e.g. strong name) if (!String.IsNullOrEmpty(RemoteInfo.UserLibraryName)) { try { UserAssembly = Assembly.Load(RemoteInfo.UserLibraryName); Config.PrintComment("SUCCESS: Assembly.Load({0})", RemoteInfo.UserLibraryName); } catch (Exception e) { Config.PrintWarning("FAIL: Assembly.Load({0}) - {1}", RemoteInfo.UserLibraryName, e.ToString()); } } // If loading by strong name failed, attempt to load the assembly from its file location if (UserAssembly == null) { try { UserAssembly = Assembly.LoadFrom(RemoteInfo.UserLibrary); Config.PrintComment("SUCCESS: Assembly.LoadFrom({0})", RemoteInfo.UserLibrary); } catch (Exception e) { Config.PrintWarning("FAIL: Assembly.LoadFrom({0}) - {1}", RemoteInfo.UserLibrary, e.ToString()); } } if (UserAssembly == null) { Config.PrintError("Could not load assembly {0}, {1}", RemoteInfo.UserLibrary, RemoteInfo.UserLibraryName); return(0); } // Only attempt to deserialise parameters after we have loaded the UserAssembly // this allows types from the UserAssembly to be passed as parameters BinaryFormatter format = new BinaryFormatter(); format.Binder = new AllowAllAssemblyVersionsDeserializationBinder(UserAssembly); for (int i = 1; i < ParamArray.Length; i++) { using (MemoryStream ms = new MemoryStream((byte[])ParamArray[i])) { ParamArray[i] = format.Deserialize(ms); } } // search for user library entry point... Type[] ExportedTypes = UserAssembly.GetExportedTypes(); for (int iType = 0; iType < ExportedTypes.Length; iType++) { EntryPoint = ExportedTypes[iType]; if (EntryPoint.GetInterface("EasyHook.IEntryPoint") != null) { break; } EntryPoint = null; } if (EntryPoint == null) { throw new EntryPointNotFoundException("The given user library does not export a proper type implementing the 'EasyHook.IEntryPoint' interface."); } // test for strictly typed Run() method MethodInfo Run = EntryPoint.GetMethod("Run", BindingFlags.Public | BindingFlags.Instance); if (Run != null) { if (ParamArray.Length != Run.GetParameters().Length) { throw new MissingMethodException("The given user library does export a Run() method in the 'EasyHook.IEntryPoint' interface, but the parameter count does not match!"); } for (int i = 0; i < ParamArray.Length; i++) { var param = Run.GetParameters()[i]; if (param.ParameterType.IsByRef && ParamArray[i] == null) // allow null parameter values { continue; } if (!param.ParameterType.IsInstanceOfType(ParamArray[i])) { Config.PrintError("Run() parameter {0} type {1}, does not match input parameter type {2}", i, param.ParameterType.AssemblyQualifiedName, ParamArray[i].GetType().AssemblyQualifiedName); throw new MissingMethodException( "The given user library does export a Run() method in the 'EasyHook.IEntryPoint' interface, but the parameter types do not match!"); } } } else { throw new MissingMethodException("The given user library does not export a proper Run() method in the 'EasyHook.IEntryPoint' interface."); } // assemble user parameters and initialize library... Instance = EntryPoint.InvokeMember(null, BindingFlags.Public | BindingFlags.Instance | BindingFlags.CreateInstance, null, null, ParamArray); // notify the host about successful injection... Interface.InjectionCompleted(RemoteHooking.GetCurrentProcessId()); } catch (Exception ExtInfo) { // we will now get extensive error information on host side... try { Interface.InjectionException(RemoteHooking.GetCurrentProcessId(), ExtInfo); } finally { Instance = null; Release(EntryPoint); } return(-1); } try { /* * After this it is safe to enter the main method, which will block until assembly unloading... * From now on the user library has to take care about error reporting! */ EntryPoint.InvokeMember("Run", BindingFlags.Public | BindingFlags.Instance | BindingFlags.ExactBinding | BindingFlags.InvokeMethod, null, Instance, ParamArray); } finally { Instance = null; Release(EntryPoint); } } catch (Exception ExtInfo) { Config.PrintWarning(ExtInfo.ToString()); return(-1); } finally { if (ConnectedChannels.Contains(RemoteInfo.ChannelName)) { ConnectedChannels.Remove(RemoteInfo.ChannelName); } } return(0); }