我翻译了一篇C++写的代码原作者是sinister地址:http://forum.eviloctal.com/read-htm-tid-19527.html下面是我用VB翻译的代码。搞了快一天了。很多API声明都找不到好在我终于把API声明搞定,但是在最后关键的地方出了几个问题大家一起来看下怎么解决。Option Explicit
 
Private Const PROCESS_CREATE_THREAD = &H2
Private Const PROCESS_QUERY_INFORMATION = &H400
Private Const PROCESS_VM_WRITE = &H20
Private Const PROCESS_VM_OPERATION = &H8
Private Const PROCESS_VM_READ = &H10
Private Const PROCESS_TERMINATE = 1
Private Const READ_CONTROL = &H20000
Private Const WRITE_DAC = &H40000
Private Const MEM_COMMIT = &H1000
Private Const MEM_RELEASE = &H8000
Private Const PAGE_READWRITE = &H4
Private Const INFINITE = &HFFFFFFFF
Private Const STANDARD_RIGHTS_REQUIRED = &HF0000
Private Const TOKEN_ASSIGN_PRIMARY = &H1
Private Const TOKEN_DUPLICATE = (&H2)
Private Const TOKEN_IMPERSONATE = (&H4)
Private Const TOKEN_QUERY = (&H8)
Private Const TOKEN_QUERY_SOURCE = (&H10)
Private Const TOKEN_ADJUST_PRIVILEGES = (&H20)
Private Const TOKEN_ADJUST_GROUPS = (&H40)
Private Const TOKEN_ADJUST_DEFAULT = (&H80)
Private Const TOKEN_ALL_ACCESS = (STANDARD_RIGHTS_REQUIRED Or TOKEN_ASSIGN_PRIMARY Or _
TOKEN_DUPLICATE Or TOKEN_IMPERSONATE Or TOKEN_QUERY Or TOKEN_QUERY_SOURCE Or _
TOKEN_ADJUST_PRIVILEGES Or TOKEN_ADJUST_GROUPS Or TOKEN_ADJUST_DEFAULT)
Private Const SE_PRIVILEGE_ENABLED = &H2
Private Const ANYSIZE_ARRAY = 1
Private Const SE_DEBUG_NAME = "SeDebugPrivilege"Private Const DACL_SECURITY_INFORMATION = &H4Private Const GRANT_ACCESS = 1Private Type LUID
    lowpart As Long
    highpart As Long
End TypePrivate Type LUID_AND_ATTRIBUTES
    pLuid As LUID
    Attributes As Long
End TypePrivate Type TOKEN_PRIVILEGES
    PrivilegeCount As Long
    Privileges(ANYSIZE_ARRAY) As LUID_AND_ATTRIBUTES
End TypePrivate Type TRUSTEE
    pMultipleTrustee As Long
    MultipleTrusteeOperation As Long
    TrusteeForm As Long
    TrusteeType As Long
    ptstrName As String
End TypePrivate Type EXPLICIT_ACCESS
    grfAccessPermissions As Long
    grfAccessMode As Long
    grfInheritance As Long
    pTRUSTEE As TRUSTEE
End TypePrivate Declare Sub BuildExplicitAccessWithName Lib "advapi32.dll" Alias _
    "BuildExplicitAccessWithNameA" _
    (ea As Any, _
    ByVal TrusteeName As String, _
    ByVal AccessPermissions As Long, _
    ByVal AccessMode As Integer, _
    ByVal Inheritance As Long)
    
Private Declare Function SetEntriesInAcl Lib "advapi32.dll" Alias "SetEntriesInAclA" (ByVal CountofExplicitEntries As Long, ea As Any, ByVal OldAcl As Long, NewAcl As Long) As LongPrivate Declare Function GetNamedSecurityInfo Lib "advapi32.dll" Alias _
    "GetNamedSecurityInfoA" _
    (ByVal ObjName As String, _
    ByVal SE_OBJECT_TYPE As Long, _
    ByVal SecInfo As Long, _
    ByVal pSid As Long, _
    ByVal pSidGroup As Long, _
    pDacl As Long, _
    ByVal pSacl As Long, _
    pSecurityDescriptor As Long) As Long
    
Private Declare Function SetNamedSecurityInfo Lib "advapi32.dll" Alias _
    "SetNamedSecurityInfoA" _
    (ByVal ObjName As String, _
    ByVal SE_OBJECT As Long, _
    ByVal SecInfo As Long, _
    ByVal pSid As Long, _
    ByVal pSidGroup As Long, _
    ByVal pDacl As Long, _
    ByVal pSacl As Long) As Long
    
Private Declare Function GetKernelObjectSecurity Lib "advapi32.dll" (ByVal Handle As Long, ByVal RequestedInformation As Long, pSecurityDescriptor As SECURITY_DESCRIPTOR, ByVal nLength As Long, lpnLengthNeeded As Long) As Long
Private Declare Function LocalFree Lib "kernel32" (ByVal hMem As Long) As LongPrivate Declare Function OpenProcessToken Lib "advapi32.dll" (ByVal ProcessHandle As Long, ByVal DesiredAccess As Long, TokenHandle As Long) As Long
Private Declare Function AdjustTokenPrivileges Lib "advapi32.dll" (ByVal TokenHandle As Long, ByVal DisableAllPriv As Long, NewState As TOKEN_PRIVILEGES, ByVal BufferLength As Long, PreviousState As TOKEN_PRIVILEGES, ReturnLength As Long) As Long                'Used to adjust your program's security privileges, can't restore without it!
Private Declare Function LookupPrivilegeValue Lib "advapi32.dll" Alias "LookupPrivilegeValueA" (ByVal lpSystemName As Any, ByVal lpName As String, lpLuid As LUID) As Long
Private Declare Function GetCurrentProcess Lib "kernel32" () As Long '获取当前进程句柄
Private Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal dwProcessId As Long) As Long
Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
Private Declare Function EnumProcesses Lib "psapi.dll" (ByRef lpidProcess As Long, ByVal cb As Long, ByRef cbNeeded As Long) As Long
Private Declare Function GetModuleFileNameExA Lib "psapi.dll" (ByVal hProcess As Long, ByVal hModule As Long, ByVal ModuleName As String, ByVal nSize As Long) As Long
Private Declare Function EnumProcessModules Lib "psapi.dll" (ByVal hProcess As Long, ByRef lphModule As Long, ByVal cb As Long, ByRef cbNeeded As Long) As Long
Private Declare Function GetLastError 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 GetProcessHeap Lib "kernel32" () As Long
Private Declare Function MakeAbsoluteSD Lib "advapi32.dll" (pSelfRelativeSecurityDescriptor As SECURITY_DESCRIPTOR, pAbsoluteSecurityDescriptor As SECURITY_DESCRIPTOR, lpdwAbsoluteSecurityDescriptorSize As Long, pDacl As ACL, lpdwDaclSize As Long, pSacl As ACL, lpdwSaclSize As Long, pOwner As Any, lpdwOwnerSize As Long, pPrimaryGroup As Any, lpdwPrimaryGroupSize As Long) As Long
Private Declare Function SetSecurityDescriptorDacl Lib "advapi32.dll" (pSecurityDescriptor As SECURITY_DESCRIPTOR, ByVal bDaclPresent As Long, pDacl As ACL, ByVal bDaclDefaulted As Long) As Long
Private Declare Function SetKernelObjectSecurity Lib "advapi32.dll" (ByVal Handle As Long, ByVal SecurityInformation As Long, SecurityDescriptor As SECURITY_DESCRIPTOR) As Long
Private Declare Function ImpersonateLoggedOnUser Lib "kernel32" (ByVal hToken As Long) As Long
Private Declare Function DuplicateTokenEx Lib "Advapi32" (ByVal hExistingToken As Long, ByVal dwDesiredAcces As Long, lpTokenAttribute As SECURITY_ATTRIBUTES, ImpersonatonLevel As Integer, ByVal tokenType As Integer, Phandle As Long) As Long
Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (Destination As Any, Source As Any, ByVal Length As Long)
Private Declare Function GetSecurityDescriptorDacl Lib "advapi32.dll" (pSecurityDescriptor As SECURITY_DESCRIPTOR, lpbDaclPresent As Long, pDacl As ACL, lpbDaclDefaulted As Long) As Long
Private Declare Function CreateProcessAsUser Lib "advapi32.dll" Alias "CreateProcessAsUserA" (ByVal hToken As Long, ByVal lpApplicationName As String, ByVal lpCommandLine As String, ByVal lpProcessAttributes As SECURITY_ATTRIBUTES, ByVal lpThreadAttributes As SECURITY_ATTRIBUTES, ByVal bInheritHandles As Long, ByVal dwCreationFlags As Long, ByVal lpEnvironment As String, ByVal lpCurrentDirectory As String, ByVal lpStartupInfo As STARTUPINFO, ByVal lpProcessInformation As PROCESS_INFORMATION) As Long
Private Declare Function HeapFree Lib "kernel32" (ByVal hHeap As Long, ByVal dwFlags As Long, lpMem As Any) As Long
Private Const ERROR_INSUFFICIENT_BUFFER = 122
Private Const HEAP_ZERO_MEMORY As Long = &H8
Private Const ERROR_SUCCESS = 0&Private Type SECURITY_ATTRIBUTES
    nLength As Long
    lpSecurityDescriptor As Long
    bInheritHandle As Long
End Type

解决方案 »

  1.   

    Private Type ACL
        AclRevision As Byte
        Sbz1 As Byte
        AclSize As Integer
        AceCount As Integer
        Sbz2 As Integer
    End TypePrivate Type SECURITY_DESCRIPTOR
        Revision As Byte
        Sbz1 As Byte
        Control As Long
        Owner As Long
        Group As Long
        Sacl As ACL
        Dacl As ACL
    End TypePrivate Type STARTUPINFO
        cb As Long
        lpReserved As String
        lpDesktop As String
        lpTitle As String
        dwX As Long
        dwY As Long
        dwXSize As Long
        dwYSize As Long
        dwXCountChars As Long
        dwYCountChars As Long
        dwFillAttribute As Long
        dwFlags As Long
        wShowWindow As Integer
        cbReserved2 As Integer
        lpReserved2 As Long
        hStdInput As Long
        hStdOutput As Long
        hStdError As Long
    End TypePrivate Type PROCESS_INFORMATION
        hProcess As Long
        hThread As Long
        dwProcessId As Long
        dwThreadId As Long
    End TypePublic Function EnablePrivilege() As Boolean
        Dim hdlProcessHandle As Long
        Dim hdlTokenHandle As Long
        Dim tmpLuid As LUID
        Dim tkp As TOKEN_PRIVILEGES
        Dim tkpNewButIgnored As TOKEN_PRIVILEGES
        Dim lBufferNeeded As Long
        Dim lp As Long
        hdlProcessHandle = GetCurrentProcess()
        lp = OpenProcessToken(hdlProcessHandle, TOKEN_ALL_ACCESS, hdlTokenHandle)
        lp = LookupPrivilegeValue(vbNullString, "SeDebugPrivilege", tmpLuid)
        tkp.PrivilegeCount = 1
        tkp.Privileges(0).pLuid = tmpLuid
        tkp.Privileges(0).Attributes = SE_PRIVILEGE_ENABLED
        EnablePrivilege = AdjustTokenPrivileges(hdlTokenHandle, False, tkp, Len(tkpNewButIgnored), tkpNewButIgnored, lBufferNeeded)
    End FunctionPrivate Sub Form_Load()
        CreateSystemProcess App.Path & "\test.exe"
    End Sub
    Public Function CreateSystemProcess(ByVal szProcessName As String) As Boolean
        Dim hProcess As Long, dwPid As Long, hToken As Long, hNewToken As Long, pOrigSd As SECURITY_DESCRIPTOR, pNewSd As SECURITY_DESCRIPTOR, dwSDLen As Long, bDAcl As Long, pOldDAcl As ACL, bDefDAcl As Long
        Dim dwRet As Long, pNewDAcl As ACL, pSacl As ACL, dwSidOwnLen As Long, dwSidPrimLen As Long, si As STARTUPINFO, pi As PROCESS_INFORMATION, bError As Boolean
        Dim ea As EXPLICIT_ACCESS, hOrigSd As Long, hOldDAcl As Long, hNewDAcl As Long, dwAclSize As Long, dwSaclSize As Long
        Dim hSacl As Long, hSidOwner As Long, hSidPrimary As Long, hNewSd As Long, pSidOwner As Long, pSidPrimary As Long
        Dim SecurityImpersonation As Integer, TokenPrimary As Integer, ct As SECURITY_ATTRIBUTES, pAttr As SECURITY_ATTRIBUTES
        If Not EnablePrivilege Then
            bError = True
            GoTo Cleanup
        End If
        dwPid = GetSystemProcessID
        If dwPid = 0 Then
            bError = True
            GoTo Cleanup
        End If
        hProcess = OpenProcess(PROCESS_QUERY_INFORMATION, False, dwPid)
        If hProcess = 0 Then
            bError = True
            GoTo Cleanup
        End If
        If OpenProcessToken(hProcess, READ_CONTROL Or WRITE_DAC, hToken) = 0 Then
            bError = True
            GoTo Cleanup
        End If
        BuildExplicitAccessWithName ea, "Everyone", TOKEN_ALL_ACCESS, GRANT_ACCESS, 0
        If GetKernelObjectSecurity(hToken, DACL_SECURITY_INFORMATION, pOrigSd, 0&, dwSDLen) = 0 Then
            If GetLastError = ERROR_INSUFFICIENT_BUFFER Then
                hOrigSd = HeapAlloc(GetProcessHeap, HEAP_ZERO_MEMORY, dwSDLen)
                CopyMemory pOrigSd, ByVal hOrigSd, Len(pOrigSd)
                If GetKernelObjectSecurity(hToken, DACL_SECURITY_INFORMATION, pOrigSd, dwSDLen, dwSDLen) = 0 Then
                    bError = True
                    GoTo Cleanup
                End If
            Else
                bError = True
                GoTo Cleanup
            End If
        Else
            bError = True
            GoTo Cleanup
        End If
        If GetSecurityDescriptorDacl(pOrigSd, bDAcl, pOldDAcl, bDefDAcl) = 0 Then
            bError = True
            GoTo Cleanup
        End If
      

  2.   

    dwRet = SetEntriesInAcl(1, ea, hOldDAcl, hNewDAcl)
        If dwRet <> ERROR_SUCCESS Then
            hNewDAcl = 0
            bError = True
            GoTo Cleanup
        Else
            CopyMemory pNewDAcl, ByVal hNewDAcl, Len(pNewDAcl)
        End If    'c++中pSidOwner和pSidPrimary都是PSID类型我感觉问题就是出在这个函数中
        If MakeAbsoluteSD(pOrigSd, pNewSd, dwSDLen, pOldDAcl, dwAclSize, pSacl, dwSaclSize, pSidOwner, dwSidOwnLen, pSidPrimary, dwSidPrimLen) = 0 Then
            If GetLastError = ERROR_INSUFFICIENT_BUFFER Then
                hOldDAcl = HeapAlloc(GetProcessHeap, HEAP_ZERO_MEMORY, dwAclSize)
                CopyMemory pOldDAcl, ByVal hOldDAcl, Len(pOldDAcl)
                hSacl = HeapAlloc(GetProcessHeap, HEAP_ZERO_MEMORY, dwSaclSize)
                CopyMemory pSacl, ByVal hSacl, Len(pSacl)
                hSidOwner = HeapAlloc(GetProcessHeap, HEAP_ZERO_MEMORY, dwSidOwnLen)
                CopyMemory pSidOwner, ByVal hSidOwner, Len(pSidOwner)
                hSidPrimary = HeapAlloc(GetProcessHeap, HEAP_ZERO_MEMORY, dwSidPrimLen)
                CopyMemory pSidPrimary, ByVal hSidPrimary, Len(pSidPrimary)
                hNewSd = HeapAlloc(GetProcessHeap, HEAP_ZERO_MEMORY, dwSDLen)
                CopyMemory pNewSd, ByVal hNewSd, Len(pNewSd)
                'hNewSd值为空这里应该也有问题
                If MakeAbsoluteSD(pOrigSd, pNewSd, dwSDLen, pOldDAcl, dwAclSize, pSacl, dwSaclSize, pSidOwner, dwSidOwnLen, pSidPrimary, dwSidPrimLen) = 0 Then
                    bError = True
                    GoTo Cleanup
                End If
            Else
                bError = True
                GoTo Cleanup
            End If
        End If    If SetSecurityDescriptorDacl(pNewSd, bDAcl, pNewDAcl, bDefDAcl) = 0 Then
            bError = True
            GoTo Cleanup
        End If
        '执行在这里执行不下去了
        If SetKernelObjectSecurity(hToken, DACL_SECURITY_INFORMATION, pNewSd) = 0 Then
            bError = True
            GoTo Cleanup
        End If
        
        'MsgBox GetLastError
        If OpenProcessToken(hProcess, TOKEN_ALL_ACCESS, hToken) = 0 Then
            bError = True
            GoTo Cleanup
        End If
        
        If DuplicateTokenEx(hToken, TOKEN_ALL_ACCESS, ct, SecurityImpersonation, TokenPrimary, hNewToken) = 0 Then
            bError = True
            GoTo Cleanup
        End If
        Call ImpersonateLoggedOnUser(hNewToken)
        '这里有错误这个函数不知道怎么用
        If CreateProcessAsUser(hNewToken, szProcessName, vbNullString, pAttr, pAttr, False, 0&, vbNullString, vbNullString, si, pi) = 0 Then
            bError = True
            GoTo Cleanup
        End If
        bError = False
    Cleanup:
        On Error Resume Next
        Call HeapFree(GetProcessHeap, 0, pOrigSd)
        Call HeapFree(GetProcessHeap, 0, pNewSd)
        Call HeapFree(GetProcessHeap, 0, pSidPrimary)
        Call HeapFree(GetProcessHeap, 0, pSidOwner)
        Call HeapFree(GetProcessHeap, 0, pSacl)
        Call HeapFree(GetProcessHeap, 0, pOldDAcl)
        Call CloseHandle(pi.hProcess)
        Call CloseHandle(pi.hThread)
        Call CloseHandle(hToken)
        Call CloseHandle(hNewToken)
        Call CloseHandle(hProcess)
        If (bError) Then
            CreateSystemProcess = False
        Else
            CreateSystemProcess = True
        End If
    End FunctionPrivate Function GetSystemProcessID() As Long
        Dim cb As Long
        Dim cbNeeded As Long
        Dim NumElements As Long
        Dim ProcessIDs() As Long
        Dim cbNeeded2 As Long
        Dim NumElements2 As Long
        Dim Modules(1 To 255) As Long
        Dim lRet As Long
        Dim ModuleName As String, Str As String
        Dim nSize As Long
        Dim hProcess As Long
        Dim i As Long, j As Integer
        Dim Restric() As String, longtmp As Long
        cb = 8
        cbNeeded = 96
        Do While cb <= cbNeeded
            cb = cb * 2
            ReDim ProcessIDs(cb / 4) As Long
            lRet = EnumProcesses(ProcessIDs(1), cb, cbNeeded)
        Loop
        NumElements = cbNeeded / 4
        For i = 1 To NumElements
            hProcess = OpenProcess(PROCESS_QUERY_INFORMATION _
            Or PROCESS_VM_READ Or PROCESS_TERMINATE, False, ProcessIDs(i))
            If hProcess <> 0 Then
                lRet = EnumProcessModules(hProcess, Modules(1), 255, cbNeeded2)
                If lRet <> 0 Then
                    ModuleName = Space(255)
                    nSize = 255
                    lRet = GetModuleFileNameExA(hProcess, Modules(1), ModuleName, 255)
                    ModuleName = Left(ModuleName, lRet)
                    If InStr(LCase(ModuleName), "system32\winlogon.exe") Then
                        GetSystemProcessID = ProcessIDs(i)
                        Exit Function
                    End If
                End If
            End If
        Next
    End Function
      

  3.   

    昨晚在家稍微看了下还有个地方写错了
    CopyMemory 应该地址传递.比如CopyMemory pSacl, ByVal hSacl, Len(pSacl)
    应该是CopyMemory pSacl, hSacl, Len(pSacl)
      

  4.   

    我编译了一下程序,作者把include后的好多头文件屏蔽掉了
    执行结果是调出了regedit.exe,这只是结果,我不知到底提升级别了没有,
    但是我愿意和您讨论改写成vb代码的问题
    qq 397667325