PageRenderTime 41ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 1ms

/Visual Studio 2008/VBUACSelfElevation/NativeMethods.vb

#
Visual Basic | 412 lines | 218 code | 46 blank | 148 comment | 0 complexity | 5859751c2b36264e61d3ef7b4da20ceb MD5 | raw file
  1. '***************************** Module Header *******************************\
  2. ' Module Name: NativeMethod.vb
  3. ' Project: VBUACSelfElevation
  4. ' Copyright (c) Microsoft Corporation.
  5. '
  6. ' The file defines the P/Invoke signatures and native data structures.
  7. '
  8. ' This source is subject to the Microsoft Public License.
  9. ' See http://www.microsoft.com/opensource/licenses.mspx#Ms-PL.
  10. ' All other rights reserved.
  11. '
  12. ' THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND,
  13. ' EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED
  14. ' WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
  15. '***************************************************************************/
  16. #Region "Imports directives"
  17. Imports System.Runtime.InteropServices
  18. Imports Microsoft.Win32.SafeHandles
  19. #End Region
  20. ''' <summary>
  21. ''' The TOKEN_INFORMATION_CLASS enumeration type contains values that specify
  22. ''' the type of information being assigned to or retrieved from an access
  23. ''' token.
  24. ''' </summary>
  25. Friend Enum TOKEN_INFORMATION_CLASS
  26. TokenUser = 1
  27. TokenGroups
  28. TokenPrivileges
  29. TokenOwner
  30. TokenPrimaryGroup
  31. TokenDefaultDacl
  32. TokenSource
  33. TokenType
  34. TokenImpersonationLevel
  35. TokenStatistics
  36. TokenRestrictedSids
  37. TokenSessionId
  38. TokenGroupsAndPrivileges
  39. TokenSessionReference
  40. TokenSandBoxInert
  41. TokenAuditPolicy
  42. TokenOrigin
  43. TokenElevationType
  44. TokenLinkedToken
  45. TokenElevation
  46. TokenHasRestrictions
  47. TokenAccessInformation
  48. TokenVirtualizationAllowed
  49. TokenVirtualizationEnabled
  50. TokenIntegrityLevel
  51. TokenUIAccess
  52. TokenMandatoryPolicy
  53. TokenLogonSid
  54. MaxTokenInfoClass
  55. End Enum
  56. ''' <summary>
  57. ''' The WELL_KNOWN_SID_TYPE enumeration type is a list of commonly used
  58. ''' security identifiers (SIDs). Programs can pass these values to the
  59. ''' CreateWellKnownSid function to create a SID from this list.
  60. ''' </summary>
  61. Friend Enum WELL_KNOWN_SID_TYPE
  62. WinNullSid = 0
  63. WinWorldSid = 1
  64. WinLocalSid = 2
  65. WinCreatorOwnerSid = 3
  66. WinCreatorGroupSid = 4
  67. WinCreatorOwnerServerSid = 5
  68. WinCreatorGroupServerSid = 6
  69. WinNtAuthoritySid = 7
  70. WinDialupSid = 8
  71. WinNetworkSid = 9
  72. WinBatchSid = 10
  73. WinInteractiveSid = 11
  74. WinServiceSid = 12
  75. WinAnonymousSid = 13
  76. WinProxySid = 14
  77. WinEnterpriseControllersSid = 15
  78. WinSelfSid = 16
  79. WinAuthenticatedUserSid = 17
  80. WinRestrictedCodeSid = 18
  81. WinTerminalServerSid = 19
  82. WinRemoteLogonIdSid = 20
  83. WinLogonIdsSid = 21
  84. WinLocalSystemSid = 22
  85. WinLocalServiceSid = 23
  86. WinNetworkServiceSid = 24
  87. WinBuiltinDomainSid = 25
  88. WinBuiltinAdministratorsSid = 26
  89. WinBuiltinUsersSid = 27
  90. WinBuiltinGuestsSid = 28
  91. WinBuiltinPowerUsersSid = 29
  92. WinBuiltinAccountOperatorsSid = 30
  93. WinBuiltinSystemOperatorsSid = 31
  94. WinBuiltinPrintOperatorsSid = 32
  95. WinBuiltinBackupOperatorsSid = 33
  96. WinBuiltinReplicatorSid = 34
  97. WinBuiltinPreWindows2000CompatibleAccessSid = 35
  98. WinBuiltinRemoteDesktopUsersSid = 36
  99. WinBuiltinNetworkConfigurationOperatorsSid = 37
  100. WinAccountAdministratorSid = 38
  101. WinAccountGuestSid = 39
  102. WinAccountKrbtgtSid = 40
  103. WinAccountDomainAdminsSid = 41
  104. WinAccountDomainUsersSid = 42
  105. WinAccountDomainGuestsSid = 43
  106. WinAccountComputersSid = 44
  107. WinAccountControllersSid = 45
  108. WinAccountCertAdminsSid = 46
  109. WinAccountSchemaAdminsSid = 47
  110. WinAccountEnterpriseAdminsSid = 48
  111. WinAccountPolicyAdminsSid = 49
  112. WinAccountRasAndIasServersSid = 50
  113. WinNTLMAuthenticationSid = 51
  114. WinDigestAuthenticationSid = 52
  115. WinSChannelAuthenticationSid = 53
  116. WinThisOrganizationSid = 54
  117. WinOtherOrganizationSid = 55
  118. WinBuiltinIncomingForestTrustBuildersSid = 56
  119. WinBuiltinPerfMonitoringUsersSid = 57
  120. WinBuiltinPerfLoggingUsersSid = 58
  121. WinBuiltinAuthorizationAccessSid = 59
  122. WinBuiltinTerminalServerLicenseServersSid = 60
  123. WinBuiltinDCOMUsersSid = 61
  124. WinBuiltinIUsersSid = 62
  125. WinIUserSid = 63
  126. WinBuiltinCryptoOperatorsSid = 64
  127. WinUntrustedLabelSid = 65
  128. WinLowLabelSid = 66
  129. WinMediumLabelSid = 67
  130. WinHighLabelSid = 68
  131. WinSystemLabelSid = 69
  132. WinWriteRestrictedCodeSid = 70
  133. WinCreatorOwnerRightsSid = 71
  134. WinCacheablePrincipalsGroupSid = 72
  135. WinNonCacheablePrincipalsGroupSid = 73
  136. WinEnterpriseReadonlyControllersSid = 74
  137. WinAccountReadonlyControllersSid = 75
  138. WinBuiltinEventLogReadersGroup = 76
  139. WinNewEnterpriseReadonlyControllersSid = 77
  140. WinBuiltinCertSvcDComAccessGroup = 78
  141. End Enum
  142. ''' <summary>
  143. ''' The SECURITY_IMPERSONATION_LEVEL enumeration type contains values
  144. ''' that specify security impersonation levels. Security impersonation
  145. ''' levels govern the degree to which a server process can act on behalf
  146. ''' of a client process.
  147. ''' </summary>
  148. Friend Enum SECURITY_IMPERSONATION_LEVEL
  149. SecurityAnonymous = 0
  150. SecurityIdentification
  151. SecurityImpersonation
  152. SecurityDelegation
  153. End Enum
  154. ''' <summary>
  155. ''' The TOKEN_ELEVATION_TYPE enumeration indicates the elevation type of
  156. ''' token being queried by the GetTokenInformation function or set by
  157. ''' the SetTokenInformation function.
  158. ''' </summary>
  159. Friend Enum TOKEN_ELEVATION_TYPE
  160. TokenElevationTypeDefault = 1
  161. TokenElevationTypeFull
  162. TokenElevationTypeLimited
  163. End Enum
  164. ''' <summary>
  165. ''' The structure represents a security identifier (SID) and its attributes.
  166. ''' SIDs are used to uniquely identify users or groups.
  167. ''' </summary>
  168. <StructLayout(LayoutKind.Sequential)> _
  169. Friend Structure SID_AND_ATTRIBUTES
  170. Public Sid As IntPtr
  171. Public Attributes As UInteger
  172. End Structure
  173. ''' <summary>
  174. ''' The structure indicates whether a token has elevated privileges.
  175. ''' </summary>
  176. <StructLayout(LayoutKind.Sequential)> _
  177. Friend Structure TOKEN_ELEVATION
  178. Public TokenIsElevated As Integer
  179. End Structure
  180. ''' <summary>
  181. ''' The structure specifies the mandatory integrity level for a token.
  182. ''' </summary>
  183. <StructLayout(LayoutKind.Sequential)> _
  184. Friend Structure TOKEN_MANDATORY_LABEL
  185. Public Label As SID_AND_ATTRIBUTES
  186. End Structure
  187. ''' <summary>
  188. ''' Represents a wrapper class for a token handle.
  189. ''' </summary>
  190. Friend Class SafeTokenHandle
  191. Inherits SafeHandleZeroOrMinusOneIsInvalid
  192. Private Sub New()
  193. MyBase.New(True)
  194. End Sub
  195. Friend Sub New(ByVal handle As IntPtr)
  196. MyBase.New(True)
  197. MyBase.SetHandle(handle)
  198. End Sub
  199. <DllImport("kernel32.dll", CharSet:=CharSet.Auto, SetLastError:=True)> _
  200. Friend Shared Function CloseHandle(ByVal handle As IntPtr) As Boolean
  201. End Function
  202. Protected Overrides Function ReleaseHandle() As Boolean
  203. Return SafeTokenHandle.CloseHandle(MyBase.handle)
  204. End Function
  205. End Class
  206. Friend Class NativeMethods
  207. ' Token Specific Access Rights
  208. Public Const STANDARD_RIGHTS_REQUIRED As UInt32 = &HF0000
  209. Public Const STANDARD_RIGHTS_READ As UInt32 = &H20000
  210. Public Const TOKEN_ASSIGN_PRIMARY As UInt32 = 1
  211. Public Const TOKEN_DUPLICATE As UInt32 = 2
  212. Public Const TOKEN_IMPERSONATE As UInt32 = 4
  213. Public Const TOKEN_QUERY As UInt32 = 8
  214. Public Const TOKEN_QUERY_SOURCE As UInt32 = &H10
  215. Public Const TOKEN_ADJUST_PRIVILEGES As UInt32 = &H20
  216. Public Const TOKEN_ADJUST_GROUPS As UInt32 = &H40
  217. Public Const TOKEN_ADJUST_DEFAULT As UInt32 = &H80
  218. Public Const TOKEN_ADJUST_SESSIONID As UInt32 = &H100
  219. Public Const TOKEN_READ As UInt32 = (STANDARD_RIGHTS_READ Or TOKEN_QUERY)
  220. Public Const TOKEN_ALL_ACCESS As UInt32 = (STANDARD_RIGHTS_REQUIRED Or _
  221. TOKEN_ASSIGN_PRIMARY Or TOKEN_DUPLICATE Or TOKEN_IMPERSONATE Or _
  222. TOKEN_QUERY Or TOKEN_QUERY_SOURCE Or TOKEN_ADJUST_PRIVILEGES Or _
  223. TOKEN_ADJUST_GROUPS Or TOKEN_ADJUST_DEFAULT Or TOKEN_ADJUST_SESSIONID)
  224. Public Const ERROR_INSUFFICIENT_BUFFER As Int32 = 122
  225. ' Integrity Levels
  226. Public Const SECURITY_MANDATORY_UNTRUSTED_RID As Integer = 0
  227. Public Const SECURITY_MANDATORY_LOW_RID As Integer = &H1000
  228. Public Const SECURITY_MANDATORY_MEDIUM_RID As Integer = &H2000
  229. Public Const SECURITY_MANDATORY_HIGH_RID As Integer = &H3000
  230. Public Const SECURITY_MANDATORY_SYSTEM_RID As Integer = &H4000
  231. ''' <summary>
  232. ''' The function opens the access token associated with a process.
  233. ''' </summary>
  234. ''' <param name="hProcess">
  235. ''' A handle to the process whose access token is opened.
  236. ''' </param>
  237. ''' <param name="desiredAccess">
  238. ''' Specifies an access mask that specifies the requested types of access
  239. ''' to the access token.
  240. ''' </param>
  241. ''' <param name="hToken">
  242. ''' Outputs a handle that identifies the newly opened access token
  243. ''' when the function returns.
  244. ''' </param>
  245. ''' <returns></returns>
  246. <DllImport("advapi32", CharSet:=CharSet.Auto, SetLastError:=True)> _
  247. Public Shared Function OpenProcessToken( _
  248. ByVal hProcess As IntPtr, _
  249. ByVal desiredAccess As UInt32, _
  250. <Out()> ByRef hToken As SafeTokenHandle) _
  251. As <MarshalAs(UnmanagedType.Bool)> Boolean
  252. End Function
  253. ''' <summary>
  254. ''' The function creates a new access token that duplicates one already
  255. ''' in existence.
  256. ''' </summary>
  257. ''' <param name="ExistingTokenHandle">
  258. ''' A handle to an access token opened with TOKEN_DUPLICATE access.
  259. ''' </param>
  260. ''' <param name="ImpersonationLevel">
  261. ''' Specifies a SECURITY_IMPERSONATION_LEVEL enumerated type that
  262. ''' supplies the impersonation level of the new token.
  263. ''' </param>
  264. ''' <param name="DuplicateTokenHandle">
  265. ''' Outputs a handle to the duplicate token.
  266. ''' </param>
  267. ''' <returns></returns>
  268. <DllImport("advapi32.dll", CharSet:=CharSet.Auto, SetLastError:=True)> _
  269. Public Shared Function DuplicateToken( _
  270. ByVal ExistingTokenHandle As SafeTokenHandle, _
  271. ByVal ImpersonationLevel As SECURITY_IMPERSONATION_LEVEL, _
  272. <Out()> ByRef DuplicateTokenHandle As SafeTokenHandle) _
  273. As Boolean
  274. End Function
  275. ''' <summary>
  276. ''' The function retrieves a specified type of information about an
  277. ''' access token. The calling process must have appropriate access rights
  278. ''' to obtain the information.
  279. ''' </summary>
  280. ''' <param name="hToken">
  281. ''' A handle to an access token from which information is retrieved.
  282. ''' </param>
  283. ''' <param name="tokenInfoClass">
  284. ''' Specifies a value from the TOKEN_INFORMATION_CLASS enumerated type to
  285. ''' identify the type of information the function retrieves.
  286. ''' </param>
  287. ''' <param name="pTokenInfo">
  288. ''' A pointer to a buffer the function fills with the requested
  289. ''' information.
  290. ''' </param>
  291. ''' <param name="tokenInfoLength">
  292. ''' Specifies the size, in bytes, of the buffer pointed to by the
  293. ''' TokenInformation parameter.
  294. ''' </param>
  295. ''' <param name="returnLength">
  296. ''' A pointer to a variable that receives the number of bytes needed for
  297. ''' the buffer pointed to by the TokenInformation parameter.
  298. ''' </param>
  299. ''' <returns></returns>
  300. <DllImport("advapi32.dll", CharSet:=CharSet.Auto, SetLastError:=True)> _
  301. Public Shared Function GetTokenInformation( _
  302. ByVal hToken As SafeTokenHandle, _
  303. ByVal tokenInfoClass As TOKEN_INFORMATION_CLASS, _
  304. ByVal pTokenInfo As IntPtr, _
  305. ByVal tokenInfoLength As Integer, _
  306. <Out()> ByRef returnLength As Integer) _
  307. As <MarshalAs(UnmanagedType.Bool)> Boolean
  308. End Function
  309. ''' <summary>
  310. ''' Sets the elevation required state for a specified button or command
  311. ''' link to display an elevated icon.
  312. ''' </summary>
  313. ''' <remarks></remarks>
  314. Public Const BCM_SETSHIELD As UInt32 = &H160C
  315. ''' <summary>
  316. ''' Sends the specified message to a window or windows. The function
  317. ''' calls the window procedure for the specified window and does not
  318. ''' return until the window procedure has processed the message.
  319. ''' </summary>
  320. ''' <param name="hWnd">
  321. ''' Handle to the window whose window procedure will receive the message.
  322. ''' </param>
  323. ''' <param name="Msg">Specifies the message to be sent.</param>
  324. ''' <param name="wParam">
  325. ''' Specifies additional message-specific information.
  326. ''' </param>
  327. ''' <param name="lParam">
  328. ''' Specifies additional message-specific information.
  329. ''' </param>
  330. ''' <returns></returns>
  331. <DllImport("user32", CharSet:=CharSet.Auto, SetLastError:=True)> _
  332. Public Shared Function SendMessage( _
  333. ByVal hWnd As IntPtr, _
  334. ByVal Msg As UInt32, _
  335. ByVal wParam As Integer, _
  336. ByVal lParam As IntPtr) _
  337. As Integer
  338. End Function
  339. ''' <summary>
  340. ''' The function returns a pointer to a specified subauthority in a
  341. ''' security identifier (SID). The subauthority value is a relative
  342. ''' identifier (RID).
  343. ''' </summary>
  344. ''' <param name="pSid">
  345. ''' A pointer to the SID structure from which a pointer to a subauthority
  346. ''' is to be returned.
  347. ''' </param>
  348. ''' <param name="nSubAuthority">
  349. ''' Specifies an index value identifying the subauthority array element
  350. ''' whose address the function will return.
  351. ''' </param>
  352. ''' <returns>
  353. ''' If the function succeeds, the return value is a pointer to the
  354. ''' specified SID subauthority. To get extended error information, call
  355. ''' GetLastError. If the function fails, the return value is undefined.
  356. ''' The function fails if the specified SID structure is not valid or if
  357. ''' the index value specified by the nSubAuthority parameter is out of
  358. ''' bounds.
  359. ''' </returns>
  360. <DllImport("advapi32.dll", CharSet:=CharSet.Auto, SetLastError:=True)> _
  361. Public Shared Function GetSidSubAuthority( _
  362. ByVal pSid As IntPtr, _
  363. ByVal nSubAuthority As UInt32) _
  364. As IntPtr
  365. End Function
  366. End Class