`

使用VB來建构一个已知的SID

    博客分类:
  • vb
阅读更多
Option Explicit
'
' APIs needed to manipulate the RAW SID
'
Declare Function AllocateAndInitializeSid Lib "advapi32.dll" _
(pIdentifierAuthority
As SID_IDENTIFIER_AUTHORITY, _
ByVal nSubAuthorityCount As Byte, ByVal nSubAuthority0 As Long, _
ByVal nSubAuthority1 As Long, ByVal nSubAuthority2 As Long, _
ByVal nSubAuthority3 As Long, ByVal nSubAuthority4 As Long, _
ByVal nSubAuthority5 As Long, ByVal nSubAuthority6 As Long, _
ByVal nSubAuthority7 As Long, lpPSid As Long) As Long

Declare Sub
FreeSid Lib "advapi32.dll" (ByVal pSid As Long)

Declare Function LookupAccountSid Lib "advapi32.dll" _
Alias "LookupAccountSidA" (ByVal lpSystemName As String, _
ByVal Sid As Long, ByVal Name As String, cbName As Long, _
ByVal ReferencedDomainName As String, _
cbReferencedDomainName
As Long, peUse As Integer) As Long

Private Declare Function
InitializeSid Lib "advapi32.dll" _
(
ByVal Sid As Long, ByVal pIndentifierAuthority As Long, _
ByVal nSubAuthorityCount As Byte) As Long

Private Declare Function
GetSidSubAuthority Lib "advapi32.dll" _
(
ByVal Sid As Long, ByVal nSubAuthority As Long) As Long

Private Declare Function
GetSidSubAuthorityCount Lib "advapi32.dll" _
(
ByVal Sid As Long) As Long

Private Declare Function
GetSidIdentifierAuthority Lib "advapi32.dll" _
(
ByVal Sid As Long) As Long

Private Declare Function
GetSidLengthRequired Lib "advapi32.dll" _
(
ByVal nSubAuthorityCount As Byte) As Long

'
' APIs needed to manipulate pointers in VB
'
Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" _
(hpvDest
As Any, ByVal hpvSource As Long, ByVal cbCopy As Long)

Private Declare Sub CopyDWORDFromPtr Lib "kernel32" Alias "RtlMoveMemory" _
(
ByVal hpvDest As Long, ByVal hpvSource As Long, _
ByVal cbCopy As Long)

Private Declare Sub CopyDWORD Lib "kernel32" Alias "RtlMoveMemory" _
(
ByVal hpvDest As Long, hpvSource As Long, ByVal cbCopy As Long)

Private Declare Function GetProcessHeap Lib "kernel32" () As Long

Private Declare Function
HeapAlloc Lib "kernel32" (ByVal hHeap As Long, _
ByVal dwFlags As Long, ByVal dwBytes As Long) As Long

Private Declare Function
HeapFree Lib "kernel32" (ByVal hHeap As Long, _
ByVal dwFlags As Long, ByVal lpMem As Long) As Long

'
' APIs, structures, and constants necessary to obtain the domain SID
'
Private Declare Function NetApiBufferFree Lib "netapi32" _
(
ByVal Buffer As Long) As Long

Private Declare Function
NetUserModalsGet Lib "netapi32" _
(
ByVal serverName As Long, ByVal level As Long, _
BufPtr
As Long) As Long

Type USER_MODALS_INFO_2
usrmod2_domain_name
As Long
usrmod2_domain_id As Long
End
Type

Public Const NERR_Success = 0

'
' Constants from WINNT.H for the various well-known SIDs, users and groups
'
Public Const SECURITY_WORLD_SID_AUTHORITY = &H1
Public Const SECURITY_NT_AUTHORITY = &H5

Public Const SECURITY_BUILTIN_DOMAIN_RID = &H20&
Public Const DOMAIN_ALIAS_RID_ADMINS = &H220&
Public Const DOMAIN_ALIAS_RID_USERS = &H221&
Public Const SECURITY_LOCAL_SYSTEM_RID = &H12
Public Const SECURITY_WORLD_RID = &H0

Public Const DOMAIN_USER_RID_ADMIN = &H1F4
Public Const DOMAIN_USER_RID_GUEST = &H1F5

Public Const DOMAIN_GROUP_RID_ADMINS = &H200

Type SID_IDENTIFIER_AUTHORITY
Value(
6) As Byte
End
Type

'
' Helper function to lookup a SID and display the name as a test
'
Public Sub DisplayNameOfSid(ByVal lSid As Long)

Dim result As Long
Dim
userName As String
Dim
cbUserName As Long
Dim
domainName As String
Dim
cbDomainName As Long
Dim
peUse As Integer

' Lookup the constructed SID to get the name
userName = Space(255)
domainName = Space(
255)
cbUserName =
255
cbDomainName = 255
result = LookupAccountSid(vbNullString, lSid, userName, cbUserName, _
domainName, cbDomainName, peUse)

If result <> 0 Then
MsgBox userName
End If

End Sub

Public Function
GetWellKnownUserSIDFromRID(ByVal Rid As Long) As Long

Dim
userInfo As USER_MODALS_INFO_2
Dim wszServerName() As Byte
Dim
BufPtr As Long
Dim
result As Long
Dim
pSid As Long
Dim
Index, Count As Long
Dim
SubAuthorityCount As Byte
Dim
domainName As String
Dim
cbDomainName As Long
Dim
peUse As Integer
Dim
srcPtr As Long
Dim
dstPtr As Long

GetWellKnownUserSIDFromRID = 0

' Get the SID of the local machine
result = NetUserModalsGet(ByVal 0&, 2, BufPtr)
If result <> NERR_Success Then
GetWellKnownUserSIDFromRID = 0
Exit Function
End If

' Copy the data in the buffer into USER_MODALS_INFO_2 structure
CopyMemory userInfo, BufPtr, Len(userInfo)

' Allocate storage for the new Sid: account domain Sid + account Rid
CopyMemory SubAuthorityCount, _
GetSidSubAuthorityCount(userInfo.usrmod2_domain_id),
1
Count = SubAuthorityCount
pSid = HeapAlloc(GetProcessHeap(),
0, _
GetSidLengthRequired(SubAuthorityCount +
1))

If pSid <> 0 Then

If
InitializeSid(pSid, _
GetSidIdentifierAuthority(userInfo.usrmod2_domain_id), _
SubAuthorityCount +
1) <> 0 Then

' Copy the existing subauthorities from the account domain Sid
' into the new Sid
For Index = 0 To Count - 1
dstPtr = GetSidSubAuthority(pSid, Index)
srcPtr = GetSidSubAuthority(userInfo.usrmod2_domain_id, Index)
CopyDWORDFromPtr dstPtr, srcPtr,
4
Next Index

' append Rid to new Sid
dstPtr = GetSidSubAuthority(pSid, Index)
CopyDWORD dstPtr, Rid,
4

End If

End If

NetApiBufferFree BufPtr

GetWellKnownUserSIDFromRID = pSid

End Function

Public Sub
ConstructWellKnownUserSids()

Dim lSid As Long

' Construct SID for Well-known user "Administrator"
lSid = GetWellKnownUserSIDFromRID(DOMAIN_USER_RID_ADMIN)
If lSid <> 0 Then

' Use the constructed SID in the application
DisplayNameOfSid lSid

' Free the heap memory block allocated in
' GetWellKnownUserSIDFromRID function for the SID
HeapFree GetProcessHeap(), 0, lSid

End If

' Construct SID for Well-known user "Guest"
lSid = GetWellKnownUserSIDFromRID(DOMAIN_USER_RID_GUEST)
If lSid <> 0 Then

' Use the constructed SID in the application
DisplayNameOfSid lSid

' Free the heap memory block allocated in
' GetWellKnownUserSIDFromRID VB function for the SID
HeapFree GetProcessHeap(), 0, lSid

End If

End Sub

Public Sub
ConstructUniversalAndNTWellKnownSids()

Dim result As Long
Dim
siaNtAuthority As SID_IDENTIFIER_AUTHORITY
Dim lSid As Long

' Construct SID for System "NT well-known SID"
siaNtAuthority.Value(5) = SECURITY_NT_AUTHORITY
result = AllocateAndInitializeSid(siaNtAuthority,
1, _
SECURITY_LOCAL_SYSTEM_RID,
0, 0, 0, 0, 0, 0, 0, lSid)

' Use the constructed SID in the application
DisplayNameOfSid lSid

' Free the memory allocated for the SID using FreeSid() API
FreeSid lSid

' Construct SID for Everyone "Universal well-known SID"
siaNtAuthority.Value(5) = SECURITY_WORLD_SID_AUTHORITY
result = AllocateAndInitializeSid(siaNtAuthority,
1, _
SECURITY_WORLD_RID,
0, 0, 0, 0, 0, 0, 0, lSid)

' Use the constructed SID in the application
DisplayNameOfSid lSid

' Free the memory allocated for the SID using FreeSid() API
FreeSid lSid

' Construct SID for Administrators "Well-known group"
siaNtAuthority.Value(5) = SECURITY_NT_AUTHORITY
result = AllocateAndInitializeSid(siaNtAuthority,
2, _
SECURITY_BUILTIN_DOMAIN_RID, DOMAIN_ALIAS_RID_ADMINS, _
0, 0, 0, 0, 0, 0, lSid)

' Use the constructed SID in the application
DisplayNameOfSid (lSid)

' Free the memory allocated for the SID using FreeSid() API
FreeSid lSid

' Construct SID for Users "Well-known group"
siaNtAuthority.Value(5) = SECURITY_NT_AUTHORITY
result = AllocateAndInitializeSid(siaNtAuthority,
2, _
SECURITY_BUILTIN_DOMAIN_RID, DOMAIN_ALIAS_RID_USERS, _
0, 0, 0, 0, 0, 0, lSid)

' Use the constructed SID in the application
DisplayNameOfSid lSid

' Free the memory allocated for the SID using FreeSid() API
FreeSid lSid

End Sub

 

分享到:
评论

相关推荐

    已知三点坐标求圆心坐标VB代码算法

    在VB(Visual Basic)编程环境中,我们经常需要处理各种几何问题,其中之一就是根据已知的三个点的坐标来求解圆的中心。这个任务在实际应用中非常常见,例如在地图绘制、游戏开发或者数学计算软件中。下面将详细介绍...

    vb源码--已知窗口句柄获得其EXE应用程序路径

    在VB(Visual Basic)编程中,获取已知窗口句柄的EXE应用程序路径是一个常见的需求,这主要涉及到Windows API(应用程序接口)的调用和内存处理。窗口句柄(HWND)是Windows操作系统用来唯一标识一个窗口的对象句柄...

    前方交会 vb代码

    同时,为了便于使用,可以封装这些功能到一个函数或类中,以便在不同的项目中复用。 在提供的压缩包文件“前方交会”中,可能包含了上述VB代码的实现或者相关的说明文档,可以帮助进一步理解和应用这个算法。如果你...

    vb已知方位角,距离推算坐标

    假设我们有一个起点坐标(P0(x0, y0)),已知一个方位角θ(角度单位通常为度)和一个距离d,我们可以通过以下步骤计算目标点P1(x1, y1)的坐标: 1. 将角度θ转换为弧度。在VB中,可以使用`CDbl(Angle * (Math.PI / ...

    vb+sql制作一个信息管理系统的课件

    是一个课件,主要讲了如何使用VB+SQL作一个信息管理系统

    VB获取另一个程序中SysListView32控件中的内容

    综上所述,VB获取另一个程序中SysListView32控件的内容是一个涉及进程间通信、控件识别和Win32 API调用的高级编程主题,需要对VB和Windows API有深入的理解。通过学习和实践这样的例子,开发者可以提高自己的编程...

    使用VB.NET写的CHART控件使用

    使用VB.NET写的CHART控件使用例程,简单易懂.

    已知方位角距离求点坐标(vb源代码)

    在这个案例中,我们有一个名为“已知方位角距离求点坐标”的VB源代码,它专门用于计算导线点的坐标。 首先,我们需要理解几个基本概念: 1. 方位角:这是从正北方向到目标方向的角度,通常以度为单位。在0°至360°...

    使用TreeView的一个实例,Visual Basic6.0,VB6.0源代码编写

    在本实例中,我们将探讨如何在Visual Basic 6.0(VB6.0)中利用`TreeView`控件来创建一个交互式的树状视图。下面我们将详细介绍`TreeView`控件的基本概念、属性、方法和事件,以及如何在VB6.0中编写源代码实现它的...

    使用VB编程的方程式计算器

    在“使用VB编程的方程式计算器”项目中,开发者利用VB6.0这一版本,构建了一个能够计算方程式的应用程序,让使用者无需复杂的数学知识,也能解决各类方程问题。 VB6.0是VB系列的一个经典版本,提供了丰富的控件库和...

    vb6调用excel实例.txt

    vb6调用excel实例:共25行代码,vb6打开excel,vb6获取已经打开的excel,vb6编辑excel,vb6选择excel单元格,vb6获取excel的选择区域,vb6关闭excel

    vb使用Zlib进行zip解压缩的源码

    4. **循环解压每个文件**:对于ZIP文件中的每个数据块(代表一个或多个文件),使用Zlib的解压缩API进行解压,通常包括`inflateInit`、`inflate`和`inflateEnd`等函数。`inflateInit`初始化解压过程,`inflate`进行...

    VB写的人脸识别源码.rar_VB人脸识别_VB识别_vb 人脸识别_vb 人脸识别_人脸

    这个压缩包包含了核心的VB源代码,对于想要在VB中开发类似应用的人来说具有很高的参考价值。 人脸识别技术通常基于两种主要方法:特征提取和模板匹配。特征提取是通过算法提取人脸的关键特征,如眼睛、鼻子和嘴巴的...

    vb梦工程vb6.1底层版

    `VB6.1编译的热键式窗口DLL效果演示.rar`文件是一个示例项目,展示了如何使用VB6.1来创建热键式窗口。热键,也称为快捷键,是通过按下特定组合的键来执行特定操作的方式,可以极大地提高用户的工作效率。这个示例...

    vb宏控件,office使用vb控件

    在IT行业中,VB宏控件(Visual Basic for Applications Macro Controls)是Microsoft Office套件中一个强大的功能,它允许用户通过编程的方式来自动化和扩展Office应用程序的功能。这些控件使得用户不仅能够执行基本...

    使用vb.net实行usb传输(文档)

    然后,我们可以在 Form1.vb 中添加一个按钮控件 Button1,并在点击事件中调用 CheckHidDevice() 函式来检查设备是否连接成功。如果连接成功,我们可以继续进行下一步操作。 在下一步中,我们可以使用 ReadUSB() 函...

    Visual Tkinter 使用VB设计python界面的可视化设计工具(终结版),目前最好用。

    这是一个VB6的IDE插件(Addin),使用VB6的IDE直接设计Python的界面。 Python和VB都是能让人快乐的编程语言,我使用了Python之后,很多自己使用的工具都使用Python开发或改写了,因为最终实现的Python代码实在太短了...

    VB串口通信源码210个

    027、VB串口程序,,是一个串口使用例程,对初学者有用,特别是工控类的 028、VB串口传输文本,实现2台PC间的通信,类似简单的聊天工具 029、VB串口的一个电子称的项目 030、VB串口调试程序,用于通过串口控制松下空调测试 ...

    vb.net使用picturebox绘图

    这个主题探讨了如何利用PictureBox在VB.NET环境中实现图形绘制功能,为初学者提供了一个基础的起点。 首先,VB.NET提供了System.Drawing命名空间,该命名空间包含了许多类,如Graphics、Pen、Brush等,它们是进行...

    vb6.0调用webservice详解

    在VB6.0中调用Web服务,特别是WebService,是一个重要的技术点,它允许传统的VB6应用程序利用基于网络的服务来扩展其功能。由于VB.NET的普及,很多开发者更倾向于使用.NET框架,但考虑到VB6的广泛使用和某些特定场景...

Global site tag (gtag.js) - Google Analytics