Skip to content

Commit

Permalink
Add immortality
Browse files Browse the repository at this point in the history
Aimed to address #10, unfortunately it doesn't quite work because Chrome already has a handle with the TERMINATE right.
  • Loading branch information
Eric Lawrence committed Mar 10, 2023
1 parent b9b5211 commit 78b40b1
Show file tree
Hide file tree
Showing 4 changed files with 245 additions and 110 deletions.
53 changes: 3 additions & 50 deletions nmf-view/Program.cs
Original file line number Diff line number Diff line change
@@ -1,66 +1,19 @@
using System;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Windows.Forms;

namespace nmf_view
{
static class Program
{


[DllImport("user32.dll", SetLastError = true)]
static extern bool SetProcessDPIAware();
private static void UnhandledExceptionHandler(object sender, UnhandledExceptionEventArgs ue)
{
Exception unhandledException = (Exception)ue.ExceptionObject;
ReportException(unhandledException);
Utilities.ReportException(unhandledException);
}

internal static void ReportException(Exception eX)
{
string sTitle = "Sorry, you may have found an error...";
ReportException(eX, sTitle, null);
}
public static void ReportException(Exception eX, string sTitle, string sCallerMessage)
{
Trace.WriteLine("******ReportException()******\n" + eX.Message + "\n" + eX.StackTrace + "\n" + eX.InnerException);

if ((eX is System.Threading.ThreadAbortException)) // TODO: What about ObjectDisposedException?
{
return;
}

string sMessage;
if (eX is OutOfMemoryException)
{
sTitle = "Insufficient Memory Address Space";
sMessage = "An out-of-memory exception was encountered.\nGC Total Allocated: " + GC.GetTotalMemory(false).ToString("N0") + " bytes.";
}
else
{
if (String.IsNullOrEmpty(sCallerMessage))
{
sMessage = "NMF-View encountered an unexpected problem. If you believe this is a bug, please copy this message by hitting CTRL+C, and submit a issue report on GitHub";
}
else
{
sMessage = sCallerMessage;
}
}

MessageBox.Show(
sMessage + "\n\n" +
eX.Message + "\n\n" +
"Type: " + eX.GetType().ToString() + "\n" +
"Source: " + eX.Source + "\n" +
eX.StackTrace + "\n\n" +
eX.InnerException + "\n" +
"NMF-View v" + Application.ProductVersion + ((8 == IntPtr.Size) ? " (x64 " : " (x86 ") + Environment.GetEnvironmentVariable("PROCESSOR_ARCHITECTURE") + ") [.NET " + Environment.Version + " on " + Environment.OSVersion.VersionString + "] ",
sTitle);
}

[DllImport("user32.dll", SetLastError = true)]
static extern bool SetProcessDPIAware();

[STAThread]
static void Main()
{
Expand Down
119 changes: 119 additions & 0 deletions nmf-view/Utilities.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
using System;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Security.AccessControl;
using System.Security.Principal;
using System.Windows.Forms;

namespace nmf_view
Expand All @@ -12,6 +14,26 @@ class Utilities
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool IsUserAnAdmin();

public static bool DenyProcessTermination()
{
try
{
var hCurrentProcess = Process.GetCurrentProcess().SafeHandle;
var processSecurity = new ProcessSecurity(hCurrentProcess);
SecurityIdentifier sid = new SecurityIdentifier(WellKnownSidType.WorldSid, null);
// Create a rule to deny process termination.
ProcessAccessRule rule = new ProcessAccessRule(sid, ProcessAccessRights.Terminate, false,
InheritanceFlags.None, PropagationFlags.None, AccessControlType.Deny);
processSecurity.AddAccessRule(rule);
processSecurity.SaveChanges(hCurrentProcess);
}
catch (Exception eX)
{
ReportException(eX);
return false;
}
return true;
}
internal static void CopyToClipboard(string s)
{
DataObject data = new DataObject();
Expand Down Expand Up @@ -47,5 +69,102 @@ internal static void OpenExplorerTo(string manifestFilename)
/* User may abort */
}
}

internal static void ReportException(Exception eX)
{
string sTitle = "Sorry, you may have found an error...";
ReportException(eX, sTitle, null);
}
public static void ReportException(Exception eX, string sTitle, string sCallerMessage)
{
Trace.WriteLine("******ReportException()******\n" + eX.Message + "\n" + eX.StackTrace + "\n" + eX.InnerException);

if ((eX is System.Threading.ThreadAbortException)) // TODO: What about ObjectDisposedException?
{
return;
}

string sMessage;
if (eX is OutOfMemoryException)
{
sTitle = "Insufficient Memory Address Space";
sMessage = "An out-of-memory exception was encountered.\nGC Total Allocated: " + GC.GetTotalMemory(false).ToString("N0") + " bytes.";
}
else
{
if (String.IsNullOrEmpty(sCallerMessage))
{
sMessage = "NMF-View encountered an unexpected problem. If you believe this is a bug, please copy this message by hitting CTRL+C, and submit a issue report on GitHub";
}
else
{
sMessage = sCallerMessage;
}
}

MessageBox.Show(
sMessage + "\n\n" +
eX.Message + "\n\n" +
"Type: " + eX.GetType().ToString() + "\n" +
"Source: " + eX.Source + "\n" +
eX.StackTrace + "\n\n" +
eX.InnerException + "\n" +
"NMF-View v" + Application.ProductVersion + ((8 == IntPtr.Size) ? " (x64 " : " (x86 ") + Environment.GetEnvironmentVariable("PROCESSOR_ARCHITECTURE") + ") [.NET " + Environment.Version + " on " + Environment.OSVersion.VersionString + "] ",
sTitle);
}
}
internal class ProcessSecurity : NativeObjectSecurity
{
public ProcessSecurity(SafeHandle processHandle)
: base(false, ResourceType.KernelObject, processHandle, AccessControlSections.Access) { }

public void AddAccessRule(ProcessAccessRule rule)
{
base.AddAccessRule(rule);
}

public void SaveChanges(SafeHandle processHandle)
{
Persist(processHandle, AccessControlSections.Access);
}

public override Type AccessRightType
{
get { return typeof(ProcessAccessRights); }
}

public override AccessRule AccessRuleFactory(System.Security.Principal.IdentityReference identityReference, int accessMask, bool isInherited, InheritanceFlags inheritanceFlags, PropagationFlags propagationFlags, AccessControlType type)
{
return new ProcessAccessRule(identityReference, (ProcessAccessRights)accessMask, isInherited, inheritanceFlags, propagationFlags, type);
}

public override Type AccessRuleType
{
get { return typeof(ProcessAccessRule); }
}

public override AuditRule AuditRuleFactory(System.Security.Principal.IdentityReference identityReference, int accessMask, bool isInherited, InheritanceFlags inheritanceFlags,
PropagationFlags propagationFlags, AuditFlags flags)
{
throw new NotImplementedException();
}

public override Type AuditRuleType
{
get { throw new NotImplementedException(); }
}
}

internal class ProcessAccessRule : AccessRule
{
public ProcessAccessRule(IdentityReference identityReference, ProcessAccessRights accessMask, bool isInherited, InheritanceFlags inheritanceFlags, PropagationFlags propagationFlags, AccessControlType type)
: base(identityReference, (int)accessMask, isInherited, inheritanceFlags, propagationFlags, type) { }

public ProcessAccessRights ProcessAccessRights { get { return (ProcessAccessRights)AccessMask; } }
}
[Flags]
internal enum ProcessAccessRights
{
Terminate = 1
}
}
21 changes: 11 additions & 10 deletions nmf-view/frmMain.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 78b40b1

Please sign in to comment.