Break On
$Computer = "TestComp"
$SID = AddLocalAdmin2VNC($Computer)
Function AddLocalAdmin2VNC($Computer)
Dim $oWMI, $oAccount, $LocalAdminSID, $arySID, $Hex4, $Hex5, $Hex6, $Hex7
Dim $ProcessSID, $oReg, $HKEY_LOCAL_MACHINE, $RegKey, $RegValueName, $RegValue, $aryBinarySID
$oWMI = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" + $Computer + "\root\cimv2")
If @Error
$ = MessageBox("Could not connect to " + $Computer + ".","Error")
Return
EndIf
$oAccount = $oWMI.Get("Win32_UserAccount.Name='Administrator',Domain='" + $Computer + "'")
$LocalAdminSID = $oAccount.SID
$arySID = Split($LocalAdminSID,"-")
$Hex4 = CDbl($arySID[4])
$Hex4 = ToHex($Hex4)
$Hex4 = ConvertSID($Hex4)
$Hex5 = CDbl($arySID[5])
$Hex5 = ToHex($Hex5)
$Hex5 = ConvertSID($Hex5)
$Hex6 = CDbl($arySID[6])
$Hex6 = ToHex($Hex6)
$Hex6 = ConvertSID($Hex6)
$Hex7 = CDbl($arySID[7])
$Hex7 = ToHex($Hex7)
$Hex7 = ConvertSID($Hex7)
$ProcessSID = $Hex4 + "," + $Hex5 + "," + $Hex6 + "," + $Hex7
$oReg = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" + $Computer + "\root\default:StdRegProv")
If @Error
$ = MessageBox("Could not connect WMI Registry to " + $Computer + ".","Error")
Return
EndIf
$HKEY_LOCAL_MACHINE = CInt(2147483650.0)
$RegKey = "SOFTWARE\ORL\WinVNC3"
$RegValueName = "ACL"
$RegValue = "2,0,44,0,1,0,0,0,0,0,36,0,3,0,0,0,1,5,0,0,0,0,0,5,21,0,0,0," + $ProcessSID + ",0,0"
$aryBinarySID = Split($RegValue,",")
$ = $oReg.CreateKey($HKEY_LOCAL_MACHINE, $RegKey)
If @Error
$ = MessageBox("Could not create VNC Security registry key on " + $Computer + ".","Error")
Return
EndIf
$ = $oReg.SetBinaryValue($HKEY_LOCAL_MACHINE, $RegKey, $RegValueName, $aryBinarySID)
If @Error
$ = MessageBox("Could not modify VNC Security on " + $Computer + ".","Error")
Return
EndIF
EndFunction
Function ConvertSID($Hex)
Dim $Start, $HexStr, $NewHex, $aryTmp, $Element, $tmpElement
While Len($Hex) < 4
$Hex = "" + "0" + $Hex
Loop
$Start = 1
$HexStr = SubStr($Hex,$Start,2)
While $HexStr <> ""
$NewHex = $NewHex + $HexStr + ","
$Start = $Start + 2
$HexStr = SubStr($Hex,$Start,2)
Loop
$Hex = SubStr($NewHex,1,Len($NewHex) - 1)
ReDim $NewHex
$aryTmp = Split($Hex,",")
For Each $Element In $aryTmp
$tmpElement = Execute("Exit &" + $Element)
$NewHex = "" + $tmpElement + "," + $NewHex
Next
$ConvertSID = SubStr($NewHex,1,Len($NewHex) - 1)
EndFunction
Function ToHex($_V)
Dim $_Hc, $_aD[0], $_aV, $_Dv, $_sH
$_Hc = '0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'
If VarType($_V) <> 5 ; must get a double!
Exit 87
EndIf
$_I = -1 ; index pointer
$_Dv = 1.0 ; initial value
While $_V > $_Dv ; determine required multipliers
$_I = $_I + 1
ReDim Preserve $_aD[$_I]
$_aD[$_I] = $_Dv
$_Dv = 256.0 * $_Dv
Loop
;Evaluate each hex octet and convert to a hex string
For $_I = UBound($_aD) To 0 Step -1
If $_V > $_aD[$_I]
$_aV = Int($_V / $_aD[$_I])
$_V = $_V - 1.0 * ($_aD[$_I] * $_aV)
$_sH = $_sH + $_Hc[$_aV / 16] + $_Hc[$_aV Mod 16]
Else
$_sH = $_sH + '00'
EndIf
Next
$ToHex = $_sH
Exit 0
EndFunction