Listing admins for remote computers

Listing admins for remote computers

Question:
Is there a way to pass in a local workstation’s host name and get a return of a list of the local administrators for that workstation? I have seen this in the Hyena program and wanted to do the same from VB5/VB6 code.

Answer:
On a form with a command button and a listbox, paste the following code:

Private Sub Command1_Click()   Dim vntRtn As Variant   Dim lngNumItems As Long   Dim lngLoop As Long      Me.List1.Clear      vntRtn = Module1.GetAdminUsers("PDCNEW")      On Error Resume Next   lngNumItems = UBound(vntRtn)   On Error GoTo 0      If lngNumItems > 0 Then      For lngLoop = 1 To lngNumItems         Me.List1.AddItem vntRtn(lngLoop)      Next lngLoop   End If   End Sub

In a BAS file called Module1, paste the following code:

Option ExplicitPrivate Declare Function NetUserEnum _   Lib "netapi32" _   (abytServer As Byte, _    ByVal Level As Long, _    ByVal Flags As Long, _    lngBuffer As Long, _    ByVal MaxLen As Long, _    lpEntriesRead As Long, _    lpTotalEntries As Long, _    lpResume_Handle As Long) As LongPrivate Declare Function NetApiBufferFree _   Lib "netapi32" _   (ByVal pBuffer As Long) As LongPrivate Declare Sub CopyMem _   Lib "kernel32" Alias "RtlMoveMemory" _   (pTo As Any, _    uFrom As Any, _    ByVal lSize As Long)Private Declare Function lstrlenW _   Lib "kernel32" _   (ByVal lpString As Long) As LongPrivate Declare Function lstrlen _   Lib "kernel32" _   (ByVal lpString As Long) As LongPrivate Const USER_PRIV_GUEST       As Long = &H0Private Const USER_PRIV_USER        As Long = &H1Private Const USER_PRIV_ADMIN       As Long = &H2Private Const constUserInfo1        As Long = 1Private Type USER_INFO_1_API    usri1_name          As Long    usri1_password      As Long    usri1_password_age  As Long    usri1_priv          As Long    usri1_home_dir      As Long    usri1_comment       As Long    usri1_flags         As Long    usri1_script_path   As LongEnd TypePrivate Type USER_INFO_1    usri1_name          As String    usri1_password      As Long    usri1_password_age  As Long    usri1_priv          As Long    usri1_home_dir      As String    usri1_comment       As String    usri1_flags         As Long    usri1_script_path   As StringEnd TypePrivate Const NERR_Success As Long = 0&Private Const ERROR_MORE_DATA As Long = 234&Public Function GetAdminUsers(Optional ByVal xi_strServer As String) As Variant   Dim lngBuffer           As Long   Dim abytServer()        As Byte   Dim atypUsers()         As USER_INFO_1_API   Dim astrAdmins()        As String   Dim strName             As String   Dim lngTotalUsers       As Long   Dim lngTotalUsersRead   As Long   Dim lngRtn              As Long   Dim lngResumeHwnd       As Long   Dim lngNumAdmins        As Long   Dim lngLevel            As Long   Dim lngLoop             As Long   Const Flags& = 0                   If Len(xi_strServer) = 0 Then      abytServer = ""   Else      If InStr(1, xi_strServer, "\", vbTextCompare) <= 0 Then         xi_strServer = "\" & xi_strServer      Else         ' Already OK      End If      abytServer = xi_strServer & vbNullChar   End If          'call API to Enumerate users   If Len(xi_strServer) <> 0 Then      lngRtn = NetUserEnum(abytServer(0), constUserInfo1, _                           Flags, lngBuffer, _                           &H4000, lngTotalUsersRead, _                           lngTotalUsers, lngResumeHwnd)   Else      lngRtn = NetUserEnum(ByVal 0&, constUserInfo1, _                           Flags, lngBuffer, _                           &H4000, lngTotalUsersRead, _                           lngTotalUsers, lngResumeHwnd)   End If               If lngTotalUsersRead > 0 Then      ReDim atypUsers(0 To lngTotalUsersRead - 1)      CopyMem atypUsers(0), _              ByVal lngBuffer, _              Len(atypUsers(0)) * lngTotalUsersRead      lngLoop = 0      lngNumAdmins = 0            For lngLoop = 0 To lngTotalUsers - 1         strName = PointerToStringW(atypUsers(lngLoop).usri1_name)         lngLevel = atypUsers(lngLoop).usri1_priv         If lngLevel = USER_PRIV_ADMIN Then            lngNumAdmins = lngNumAdmins + 1            ReDim Preserve astrAdmins(1 To lngNumAdmins)            astrAdmins(lngNumAdmins) = strName         End If      Next lngLoop   End If      ' Set the return value   GetAdminUsers = astrAdmins   If lngBuffer Then      NetApiBufferFree lngBuffer   End If      End FunctionPrivate Function PointerToStringW(lpStringW As Long) As String   Dim Buffer() As Byte   Dim nLen As Long      If lpStringW Then      nLen = lstrlenW(lpStringW) * 2      If nLen Then         ReDim Buffer(0 To (nLen - 1)) As Byte         CopyMem Buffer(0), ByVal lpStringW, nLen         PointerToStringW = Buffer      End If   End IfEnd Function

Share the Post:
XDR solutions

The Benefits of Using XDR Solutions

Cybercriminals constantly adapt their strategies, developing newer, more powerful, and intelligent ways to attack your network. Since security professionals must innovate as well, more conventional endpoint detection solutions have evolved

AI is revolutionizing fraud detection

How AI is Revolutionizing Fraud Detection

Artificial intelligence – commonly known as AI – means a form of technology with multiple uses. As a result, it has become extremely valuable to a number of businesses across

AI innovation

Companies Leading AI Innovation in 2023

Artificial intelligence (AI) has been transforming industries and revolutionizing business operations. AI’s potential to enhance efficiency and productivity has become crucial to many businesses. As we move into 2023, several

data fivetran pricing

Fivetran Pricing Explained

One of the biggest trends of the 21st century is the massive surge in analytics. Analytics is the process of utilizing data to drive future decision-making. With so much of

kubernetes logging

Kubernetes Logging: What You Need to Know

Kubernetes from Google is one of the most popular open-source and free container management solutions made to make managing and deploying applications easier. It has a solid architecture that makes

ransomware cyber attack

Why Is Ransomware Such a Major Threat?

One of the most significant cyber threats faced by modern organizations is a ransomware attack. Ransomware attacks have grown in both sophistication and frequency over the past few years, forcing

data dictionary

Tools You Need to Make a Data Dictionary

Data dictionaries are crucial for organizations of all sizes that deal with large amounts of data. they are centralized repositories of all the data in organizations, including metadata such as