Advertisement
cobraTM

make process anti kill vb.net

Oct 26th, 2016
142
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VB.NET 2.16 KB | None | 0 0
  1. 'imports
  2. Imports System.Runtime.InteropServices
  3. Imports System.ComponentModel
  4. Imports System.Security.AccessControl
  5. Imports System.Security.Principal
  6. '''''''''''''''''''''''''''''''''''''''
  7.  <DllImport("advapi32.dll", SetLastError:=True)> _
  8.     Shared Function GetKernelObjectSecurity(Handle As IntPtr, securityInformation As Integer, <Out> pSecurityDescriptor As Byte(), nLength As UInteger, ByRef lpnLengthNeeded As UInteger) As Boolean
  9.     End Function
  10.     <DllImport("advapi32.dll", SetLastError:=True)> _
  11.     Shared Function SetKernelObjectSecurity(Handle As IntPtr, securityInformation As Integer, <[In]> pSecurityDescriptor As Byte()) As Boolean
  12.     End Function
  13.     <DllImport("kernel32.dll")> _
  14.     Shared Function GetCurrentProcess() As IntPtr
  15.     End Function
  16.     Protected Function GetProcessSecurityDescriptor(processHandle As IntPtr) As RawSecurityDescriptor
  17.         Dim psd() As Byte = New Byte(1) {}
  18.         Dim bufSizeNeeded As UInteger
  19.         GetKernelObjectSecurity(processHandle, &H4, psd, 0, bufSizeNeeded)
  20.         psd = New Byte(bufSizeNeeded) {}
  21.         If bufSizeNeeded < 0 OrElse bufSizeNeeded > Short.MaxValue Then
  22.             Throw New Win32Exception()
  23.         End If
  24.         If Not GetKernelObjectSecurity(processHandle, &H4, psd, bufSizeNeeded, bufSizeNeeded) Then
  25.             Throw New Win32Exception()
  26.         End If
  27.         Return New RawSecurityDescriptor(psd, 0)
  28.     End Function
  29.     Protected Sub SetProcessSecurityDescriptor(processHandle As IntPtr, dacl As RawSecurityDescriptor)
  30.         Dim rawsd As Byte() = New Byte(dacl.BinaryLength - 1) {}
  31.         dacl.GetBinaryForm(rawsd, 0)
  32.         If Not SetKernelObjectSecurity(processHandle, &H4, rawsd) Then
  33.             Throw New Win32Exception()
  34.         End If
  35.     End Sub
  36.  
  37. 'using
  38. 'use this code in main entrypoint
  39.  Dim hProcess As IntPtr = GetCurrentProcess()
  40.         Dim dacl = GetProcessSecurityDescriptor(hProcess)
  41.         dacl.DiscretionaryAcl.InsertAce(0, New CommonAce(AceFlags.None, AceQualifier.AccessDenied, CInt(&HF0000 Or &H100000 Or &HFFF), New SecurityIdentifier(WellKnownSidType.WorldSid, Nothing), False, Nothing))
  42.         SetProcessSecurityDescriptor(hProcess, dacl)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement