Guest User

Untitled

a guest
Apr 14th, 2016
248
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.15 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Linq;
  5. using System.Runtime.InteropServices;
  6. using System.Security.AccessControl;
  7. using System.Security.Principal;
  8. using System.Text;
  9. using System.Threading.Tasks;
  10.  
  11. namespace test
  12. {
  13.     class Program
  14.     {
  15.         [DllImport("advapi32.dll", SetLastError = true)]
  16.         private static extern bool GetKernelObjectSecurity(IntPtr handle, int secInfo, [Out] byte[] sDescriptor, uint len, out uint len2);
  17.  
  18.         private static RawSecurityDescriptor getProcessSecurityDescriptor(IntPtr processHandle)
  19.         {
  20.             const int DACL_SECURITY_INFORMATION = 0x00000004;
  21.             byte[] psecdesc = new byte[0];
  22.             uint BytesNeed;
  23.  
  24.             GetKernelObjectSecurity(processHandle, DACL_SECURITY_INFORMATION, psecdesc, 0, out BytesNeed);
  25.             if (BytesNeed < 0 || BytesNeed > short.MaxValue)
  26.                 throw new Win32Exception();
  27.  
  28.             if (!GetKernelObjectSecurity(processHandle,
  29.                 DACL_SECURITY_INFORMATION,
  30.                 psecdesc = new byte[BytesNeed],
  31.                 BytesNeed, out BytesNeed)) throw new Win32Exception();
  32.  
  33.  
  34.             return new RawSecurityDescriptor(psecdesc, 0);
  35.         }
  36.  
  37.         [DllImport("advapi32.dll", SetLastError = true)]
  38.         private static extern bool SetKernelObjectSecurity(IntPtr Handle, int secInfo, [In] byte[] psd);
  39.  
  40.         [DllImport("kernel32.dll")]
  41.         private static extern IntPtr GetCurrentProcess();  //uchwyt do danego procesu jako typ IntPtr (int*)
  42.  
  43.         private static void setProcessSecurityDescriptor(IntPtr processHandle, RawSecurityDescriptor dacl) //ustawiamy DACL
  44.         {
  45.             const int DACL_SECURITY_INFORMATION = 0x00000004;
  46.             byte[] rawSecurityDescriptor = new byte[dacl.BinaryLength];
  47.             dacl.GetBinaryForm(rawSecurityDescriptor, 0);
  48.             if (!SetKernelObjectSecurity(processHandle, DACL_SECURITY_INFORMATION, rawSecurityDescriptor)) throw new Win32Exception();
  49.         }
  50.  
  51.  
  52.  
  53.         [Flags]
  54.         private enum PAR //flagi dostępu do procesów --> Process Access Rights po kolei z MSDN
  55.         {
  56.             PROCESS_CREATE_PROCESS = 0x0080,                                        //do tworzenia procesu
  57.             PROCESS_CREATE_THREAD = 0x0002,                                         //do tworzenia wątku
  58.             SYNCHRONIZE = 0x00100000,                                               //synchronizacja (?)
  59.             PROCESS_DUP_HANDLE = 0x0040,                                            //do łapania uchwytu
  60.             PROCESS_QUERY_INFORMATION = 0x0400,                                     //do zdobywania informacji o procesie (token etc.)
  61.             PROCESS_QUERY_LIMITED_INFORMATION = 0x1000,                             //do zdobywania innych informacji, analogiczne
  62.             PROCESS_SET_QUOTA = 0x0100,                                             //do ingerencji w working set
  63.             PROCESS_SET_INFORMATION = 0x0200,                                       //do ustawiania dla procesu X informacji
  64.             WRITE_OWNER = 0x00080000,                                               //process owner - "posiadacz" procesu - chodzi o konto
  65.             PROCESS_SUSPEND_RESUME = 0x0800,                                        //jak wskazuje nazwa, wstrzymaj lub wznów
  66.             PROCESS_TERMINATE = 0x0001,                                             //zakończ proces
  67.             PROCESS_VM_OPERATION = 0x0008,                                          //zawiera w sobie funkcję - WriteProcessMemory (kernel32.dll -> zawiera się w <windows.h>)
  68.             PROCESS_VM_READ = 0x0010,                                               //do odczytywania pamięci procesu
  69.             PROCESS_VM_WRITE = 0x0020,                                              //do nadpisywania pamięci procesu
  70.             DELETE = 0x00010000,                                                    //do usuwania niektórych rzeczy
  71.             READ_CONTROL = 0x00020000,                                              //kontrola odczytu          
  72.             WRITE_DAC = 0x00040000,                                                 //advice DACL
  73.             STANDARD_RIGHTS_REQUIRED = 0x000f0000,                                  //nie mam pojęcia jak to wyjaśnić, ale nazwa STANDARD_RIGHTS_REQUIRED powinna być wystarczająca ;)
  74.             PROCESS_ALL_ACCESS = (STANDARD_RIGHTS_REQUIRED | SYNCHRONIZE | 0xFFF),  //reszta
  75.         }
  76.  
  77.         public static void TurnOnSelfProtection()
  78.         {
  79.             IntPtr hProcess = GetCurrentProcess();
  80.             var dacl = getProcessSecurityDescriptor(hProcess);
  81.             dacl.DiscretionaryAcl.InsertAce(0, new CommonAce(AceFlags.None, AceQualifier.AccessDenied /* blokujemy dostęp do procesu */, (int)PAR.PROCESS_ALL_ACCESS, new SecurityIdentifier(WellKnownSidType.WorldSid, null), false, null));
  82.             setProcessSecurityDescriptor(hProcess, dacl);
  83.         } // do wykorzystania w innej klasie
  84.  
  85.  
  86.  
  87.  
  88.         static void Main(string[] args)
  89.         {
  90.             TurnOnSelfProtection();
  91.             Console.Write("Try to kill me! :)");
  92.             Console.ReadKey();
  93.         }
  94.     }
  95. }
Add Comment
Please, Sign In to add comment