Sign in to follow this  
Followers 0
Ghost1987

MakeMeAdmin (In AutoIt)

13 posts in this topic

#1 ·  Posted (edited)

This script was inspired by Aaron Margosis "MakeMeAdmin.cmd"

Good explanations why to use LUA (Limited user account) in windows can be found here, on nonamin.

#NoTrayIcon
#RequireAdmin
#include <Security.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <EditConstants.au3>
#include <Array.au3>
#include <WinAPI.au3>
;Administrators [S-1-5-32-544] ;Users [S-1-5-32-545] ;Guests [S-1-5-32-546] ;Power Users [S-1-5-32-547]
Global $GroupSID = "S-1-5-32-544", $Delay=100
Global $aLocalAdminGroupName = _Security__LookupAccountSid($GroupSID)
    If @error Then 
        MsgBox(0, "Error", _WinAPI_GetLastErrorMessage())
        Exit
    EndIf
Global $aUserEnum = _NetUserEnum()
    If @error Then 
        MsgBox(0, "Error", _WinAPI_GetLastErrorMessage())
        ; Exit
    EndIf
Global $sUserEnum = _ArrayToString($aUserEnum, "|", 1, $aUserEnum[0]+1)
Global $aCompSID = _Security__LookupAccountName(@ComputerName)
    If @error Then 
        MsgBox(0, "Error", _WinAPI_GetLastErrorMessage())
        ; Exit
    EndIf
Global $LocalAdminName = _Security__LookupAccountSid($aCompSID[0] & "-500")
    If @error Then 
        MsgBox(0, "Error", _WinAPI_GetLastErrorMessage())
        ; Exit
    EndIf
For $i = 1 to $aUserEnum[0]+1 Step 1
    If $aCompSID[2] == 3 Then
        Local $IsAdmin = _IsAdmin($aUserEnum[$i])
        If $IsAdmin == False Then
            $sUserEnumDefault = $aUserEnum[$i]
            ExitLoop
        EndIf
    Else
        $sUserEnumDefault = $aUserEnum[1]
        ExitLoop
    EndIf
Next
GUI($sUserEnum, $sUserEnumDefault)
Exit

Func GUI($sUserEnum, $sUserEnumDefault)
    Dim $aApplications[9]
    $aApplications[0] = "explorer.exe /SEPARATE,::{20D04FE0-3AEA-1069-A2D8-08002B30309D}\::{21EC2020-3AEA-1069-A2DD-08002B30309D}" 
    $aApplications[1] = "regedit"
    $aApplications[2] = "explorer /SEPARATE,::{20D04FE0-3AEA-1069-A2D8-08002B30309D}"
    $aApplications[3] = "mmc.exe compmgmt.msc /s"
    $aApplications[4] = "cmd.exe"
    $aApplications[5] = "mmc.exe gpedit.msc"
    $aApplications[6] = @WindowsDir & "\pchealth\helpctr\binaries\msconfig.exe"
    $aApplications[7] = @CommonFilesDir & "\Microsoft Shared\MSInfo\msinfo32.exe"
    $aApplications[8] = "taskmgr.exe"
    Local $sApplications = _ArrayToString($aApplications, "|")
    Local $Delay=100
    If $CmdLine[0] == 0 And IsAdmin() == 1 Then
        $hwnd = GUICreate("MakeMeAdmin", 290, 130, -1, -1, $WS_CAPTION + $WS_SYSMENU, $WS_EX_APPWINDOW + $WS_EX_TOPMOST)
        GUICtrlCreateLabel("Path:", 5, 8, 60, 20)
        $Combo_2 = GUICtrlCreateCombo("", 60, 5, 200, 20)
        GUICtrlSetData($Combo_2, $sApplications)
        $Button_1 = GUICtrlCreateButton("...", 265, 5, 20, 20)
        GUICtrlCreateLabel("Username:", 5, 33, 60, 20)
        $Combo_1 = GUICtrlCreateCombo("", 60, 30, 200, 20)
        GUICtrlSetData($Combo_1, $sUserEnum, $sUserEnumDefault)
        GUICtrlCreateLabel("Password:", 5, 58, 60, 20)
        $Input_1 = GUICtrlCreateInput("", 60, 55, 200, 20, $ES_PASSWORD + $ES_AUTOHSCROLL)
        GUICtrlCreateLabel("Delay:", 5, 83, 60, 20)
        $Input_2 = GUICtrlCreateInput("100", 60, 80, 50, 20)
        GUICtrlCreateLabel("time to wait (in ms)", 115, 83, 120, 20)
        GUICtrlSetLimit($Input_2, 4)
        $UpDown_1 = GUICtrlCreateUpdown($Input_2)
        GUICtrlSetLimit($UpDown_1, 9999, 100)
        $Button_2 = GUICtrlCreateButton("OK", 5, 105, 140, 20)
        $Button_3 = GUICtrlCreateButton("Exit", 145, 105, 140, 20)
        $Button_4 = GUICtrlCreateButton("?", 265, 80, 20, 20)
        GUICtrlSetState($Button_1, $GUI_DEFBUTTON + $GUI_FOCUS)
        GUISetState(@SW_SHOW)
        While 1
            $msg = GUIGetMsg()
            $UserName = GUICtrlRead($Combo_1)
            $Password = GUICtrlRead($Input_1)
            $Delay = GUICtrlRead($Input_2)
            $Path = GUICtrlRead($Combo_2)
            If $msg = $Button_2 Then
                Local $IsAdmin = _IsAdmin($UserName)
                Local $CheckUserCredentials = _LogonUser($UserName,$Password)
                Local $msg = ""
                If $CheckUserCredentials == False Then
                    $msg &= "Incorrect Login/Password." & @CR & "Blank passwords are forbidden!" & @CR
                    GUICtrlSetData($Input_1, "")
                EndIf
                If $IsAdmin == True Then
                    $msg &= "Specified account is member of local administrator group!" & @CR
                    GUICtrlSetData($Input_1, "")
                EndIf    
                If $CheckUserCredentials == True And $IsAdmin == False Then ExitLoop
                    MsgBox(0, "Error!", $msg, 0, $hwnd)
            EndIf
            If $msg = $GUI_EVENT_CLOSE Or $msg = $Button_3 Then Exit
            If $msg = $Button_4 Then
                MsgBox(0, "MakeMeAdmin", "Script requires Administrator privileges" & @CR & "made by Serj (Ghost_1987@inbox.ru)", 0, $hwnd)
            EndIf   
            If $msg = $Button_1 Then
                $Path = FileOpenDialog("Browse", @ScriptDir & "\", "Applications (*.exe)", 1 + 2)
                If @error == 1 Then
                    MsgBox(0,"Error!","No File(s) chosen", 0, $hwnd)
                ElseIf @error == 0 Then
                    GUICtrlSetData($Combo_2, $Path)
                    GUICtrlSetState($Input_1, $GUI_FOCUS)
                    GUICtrlSetState($Button_2, $GUI_DEFBUTTON)
                EndIf
            EndIf
            If $msg = $Combo_2 Then
                GUICtrlSetState($Input_1, $GUI_FOCUS)
                GUICtrlSetState($Button_2, $GUI_DEFBUTTON)
            EndIf
        WEnd
        GUIDelete()
        _Main($UserName, $Password, $Path, $Delay)
    Else
        MsgBox(0, "MakeMeAdmin", "Script requires Administrator privileges" & @CR & "made by Serj (Ghost_1987@inbox.ru)")
        Exit
    EndIf
EndFunc    ;GUI

Func _Main($UserName, $Password, $Path, $Delay)
    Local $aLocalGroupNames = _NetUserGetLocalGroups($UserName)
    If @error Then 
        MsgBox(0, "Error", _WinAPI_GetLastErrorMessage())
        Exit
    EndIf
    _ArraySearch($aLocalGroupNames, $LocalAdminGroupName[0], 1)
    If @error Then
        If $Delay < 100 Then $Delay = 100
        _NetLocalGroupAddMember($UserName, $LocalAdminGroupName[0])
        If @error Then 
            MsgBox(0, "Error", _WinAPI_GetLastErrorMessage())
            Exit
        EndIf
        Sleep ($Delay)
        RunAs($UserName, @ComputerName, $Password, 0, $Path)
        If @error Then 
            MsgBox(0, "Error", _WinAPI_GetLastErrorMessage())
            Exit
        EndIf
        Sleep ($Delay)
        _NetLocalGroupDelMembers($UserName, $LocalAdminGroupName[0])
        If @error Then 
            MsgBox(0, "Error", _WinAPI_GetLastErrorMessage())
            Exit
        EndIf
    Else
        MsgBox(0, "Error", "Specified account already in local administrator group!")
    EndIf
EndFunc    ;_Main

Func _NetLocalGroupAddMember($sUsername, $sGroup, $sServer = '')
    Local $twUser = DllStructCreate("wchar["& StringLen($sUsername)+1 &"]")
    Local $tpUser = DllStructCreate("ptr")
    DllStructSetData($twUser, 1, $sUsername)
    DllStructSetData($tpUser, 1, DllStructGetPtr($twUser))
    Local $aRet = DllCall("netapi32.dll", "int", "NetLocalGroupAddMembers", "wstr", $sServer, "wstr", $sGroup, "int", 3, "ptr", DllStructGetPtr($tpUser), "int", 1 )
    If $aRet[0] Then Return SetError(1, $aRet[0], False)
    Return True
EndFunc    ;_NetLocalGroupAddMember

Func _NetLocalGroupDelMembers($sUsername, $sGroup, $sServer = '')
    Local $twUser = DllStructCreate("wchar["& StringLen($sUsername)+1 &"]")
    Local $tpUser = DllStructCreate("ptr")
    DllStructSetData($twUser, 1, $sUsername)
    DllStructSetData($tpUser, 1, DllStructGetPtr($twUser))
    Local $aRet = DllCall("netapi32.dll", "int", "NetLocalGroupDelMembers", "wstr", $sServer, "wstr", $sGroup, "int", 3, "ptr", DllStructGetPtr($tpUser), "int", 1 )
    If $aRet[0] Then Return SetError(1, $aRet[0], False)
    Return True
EndFunc    ;_NetLocalGroupDelMembers

Func _NetUserEnum($sServer = "") ; array[0] contains number of elements
    Local $tBufPtr = DllStructCreate("ptr")
    Local $tEntriesRead = DllStructCreate("dword")
    Local $tTotalEntries = DllStructCreate("dword")
    Local $aRet = DllCall("Netapi32.dll", "int", "NetUserEnum", "wstr", $sServer, "dword", 1, "dword", 2, "ptr", DllStructGetPtr($tBufPtr), "dword", -1, "ptr", DllStructGetPtr($tEntriesRead), "ptr", DllStructGetPtr($tTotalEntries), "ptr", 0 )
    If $aRet[0] Then Return SetError(1, $aRet[0], False)
    Local Const $UF_ACCOUNTDISABLE = 0x2
    Local $iEntriesRead = DllStructGetData($tEntriesRead,1)
    Local $pBuf = DllStructGetData($tBufPtr,1)
    Local $aUserEnum[1] = [0]
    Local $sUserInfo1 = "ptr;ptr;dword;dword;ptr;ptr;dword;ptr"
    Local $tUserInfo1 = DllStructCreate ($sUserInfo1)
    Local $zUserInfo1 = DllStructGetSize($tUserInfo1)
    For $i=1 To $iEntriesRead
        $tUserInfo1 = DllStructCreate($sUserInfo1, $pBuf+($i-1)*$zUserInfo1)
        $tName = DllStructCreate("wchar[256]", DllStructGetData($tUserInfo1,1))
        $tFlag = DllStructGetData($tUserInfo1,7)
        If BitAnd($tFlag, $UF_ACCOUNTDISABLE)=0 Then
            $aUserEnum[0] += 1
            ReDim $aUserEnum[$aUserEnum[0]+1]
            $aUserEnum[$aUserEnum[0]] = DllStructGetData($tName,1)
        EndIf
    Next
    DllCall("Netapi32.dll", "int", "NetApiBufferFree", "ptr", $pBuf)
    Return $aUserEnum
EndFunc    ;_NetUserEnum

Func _NetUserGetLocalGroups($sUsername, $sServer = "") ; array[0] contains number of elements
    Local CONST $LG_INCLUDE_INDIRECT = 0x1
    Local $tBufPtr = DllStructCreate("ptr")
    Local $ptBufPtr = DllStructGetPtr($tBufPtr)
    Local $tEntriesRead = DllStructCreate("dword")
    Local $ptEntriesRead = DllStructGetPtr($tEntriesRead)
    Local $tTotalEntries = DllStructCreate("dword")
    Local $ptTotalEntries = DllStructGetPtr($tTotalEntries)
    Local $aRet = DllCall("Netapi32.dll", "int", "NetUserGetLocalGroups", "wstr", $sServer, "wstr", $sUsername, "dword", 0, "dword", $LG_INCLUDE_INDIRECT, "ptr", $ptBufPtr, "dword", -1, "ptr", $ptEntriesRead, "ptr", $ptTotalEntries)
    If $aRet[0] Then Return SetError(1, $aRet[0], False)
    Local $iEntriesRead = DllStructGetData($tEntriesRead, 1)
    Local $pBuf = DllStructGetData($tBufPtr, 1)
    Local $sLocalGroupUsersInfo0 = "ptr"
    Local $tLocalGroupUsersInfo0 = DllStructCreate($sLocalGroupUsersInfo0)
    Local $zLocalGroupUsersInfo0 = DllStructGetSize($tLocalGroupUsersInfo0)
    Local $tLocalGroupName
    Local $aLocalGroupNames[1] = [0]
    For $i = 1 To $iEntriesRead
        $tLocalGroupUsersInfo0 = DllStructCreate($sLocalGroupUsersInfo0, $pBuf + ($i - 1) * $zLocalGroupUsersInfo0)
        $tLocalGroupName = DllStructCreate("wchar[256]", DllStructGetData($tLocalGroupUsersInfo0, 1))
        $aLocalGroupNames[0] += 1
        ReDim $aLocalGroupNames[$aLocalGroupNames[0]+1]
        $aLocalGroupNames[$aLocalGroupNames[0]] = DllStructGetData($tLocalGroupName,1)
    Next
    DllCall("Netapi32.dll", "int", "NetApiBufferFree", "ptr", $pBuf)
    Return $aLocalGroupNames
EndFunc    ;_NetUserGetLocalGroups

Func _LogonUser($sUsername, $sPassword, $sServer = '.') ; Returns True if user exists 
    $stToken = DllStructCreate("int")
    Local $aRet = DllCall("advapi32.dll", "int", "LogonUser", _
            "str", $sUsername, "str", $sServer, "str", $sPassword, "dword", 3, "dword", 0, "ptr", DllStructGetPtr($stToken))
    ;$hToken = DllStructGetData($stToken, 1)
    If Not @error And $aRet[0] <> 0 Then
        Return True
    EndIf  
    Return False
EndFunc    ;_LogonUser

Func _IsAdmin($UserName)
    Local $aLocalGroupNames = _NetUserGetLocalGroups($UserName)
    Local $ArraySearch = _ArraySearch($aLocalGroupNames, $aLocalAdminGroupName[0], 1)
    If $ArraySearch == -1 Then 
        Return False
    Else
        Return True
    EndIf
EndFunc    ;_IsAdmin

Any suggestion for code improvement are welcomed.

Please post your comments, I would like to here what people think of this script.

P.S. PM me, if I won't respond for to long.

Edited by Ghost1987

Share this post


Link to post
Share on other sites



Updated script.

Removed command line support - it was unnecessary and was breaking application command line arguments handling

Added Combo control with predefined application.

Now you can also pass command line arguments to applications that you launch (in Combo/Edit control).

Still waiting for some comments ;-).

I ran your script and confirmed that it works. I think its good.

only thing i would change is that after its successful the message box that comes up says "error" "compleated successfully"

very nice


0x616e2069646561206973206c696b652061206d616e20776974686f7574206120626f64792c20746f206669676874206f6e6520697320746f206e657665722077696e2e2e2e2e

Share this post


Link to post
Share on other sites

I was looking all over the net for a way to do this!

Can this script be modified to not require Admin privileges to run the script, or password be hard-coded into it?

Also, can the path , username, & password be hard-coded in as well, without the prompt?

Share this post


Link to post
Share on other sites

I was looking all over the net for a way to do this!

Can this script be modified to not require Admin privileges to run the script, or password be hard-coded into it?

Also, can the path , username, & password be hard-coded in as well, without the prompt?

yes, quite easy, but its not my script so i wont.


0x616e2069646561206973206c696b652061206d616e20776974686f7574206120626f64792c20746f206669676874206f6e6520697320746f206e657665722077696e2e2e2e2e

Share this post


Link to post
Share on other sites

So it can be modified quite easily to run without administrator privileges...

hmmm...

Share this post


Link to post
Share on other sites

#6 ·  Posted (edited)

I ran your script and confirmed that it works. I think its good.

only thing i would change is that after its successful the message box that comes up says "error" "compleated successfully"

very nice

Well this is only for debugging, you can just comment out strings with this code

If @error Then
        MsgBox(0, "Error", _WinAPI_GetLastErrorMessage())
        ; Exit
    EndIf

But if you could specify where exactly you've encountered this MsgBox (after which instruction) I'll try to correct this.

For some strange reason I've never encountered this Error message on my computer.

I am using Windows XP SP3.

Edited by Ghost1987

Share this post


Link to post
Share on other sites

I was looking all over the net for a way to do this!

Can this script be modified to not require Admin privileges to run the script, or password be hard-coded into it?

Also, can the path , username, & password be hard-coded in as well, without the prompt?

Please explain what exactly are you trying to achieve?

You can just take out GUI related functions. This will require path, username, password specified manually.

To lunch script with out admin privileges just remove #RequireAdmin from the script, but without these privileges script won't work!

To hard code admin credentials you can make simple runas script and with it launch MakeMeAdmin.

Share this post


Link to post
Share on other sites

#8 ·  Posted (edited)

This is another script on the same subject, MakeMeAdmin Tray Menu.

I use this script at home and at work PC.

It makes LUA on windows xp usable ;-).

However this script won't work out of the box in Windows Xp other then Russian edition.

Slight modification is needed.

I am providing this script as it is, without needed modification so some one else can modify it for their windows version.

I can't physically modify this script because I don't have access to PC with Windows XP English edition installed.

But I'll help out with modification if some one will encounter any problems.

#NoTrayIcon
#RequireAdmin
#include <Misc.au3>
#include <Constants.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <EditConstants.au3>
#include <Security.au3>
#include <Array.au3>
#include <Process.au3>
#include <WinAPI.au3>
Opt("TrayMenuMode", 1)
Opt("MustDeclareVars", 0)
Opt("WinTitleMatchMode", 3)
Opt("MouseCoordMode", 2)

Global $sOccurenceName = "AdminTray", $UserName, $Password, $SplashTimerDiff, $SplashTimerInit, $_FuncTimerDiff, $_FuncTimerInit, $GroupSID = "S-1-5-32-544"
;Administrators [S-1-5-32-544] ;Users [S-1-5-32-545] ;Guests [S-1-5-32-546] ;Power Users [S-1-5-32-547]
If _Singleton($sOccurenceName, 1) = 0 Then
    Msgbox(0,"Ошибка","Программа уже запущена!") ; Script is already running
    Exit
EndIF
Global $ExplorerPID = 0
Global $Dll = DllOpen("user32.dll")
Global $GroupSID = "S-1-5-32-544", $Delay=500
Global $aLocalAdminGroupName = _Security__LookupAccountSid($GroupSID)
    If @error Then 
        MsgBox(0, "Ошибка", _WinAPI_GetLastErrorMessage())
        Exit
    EndIf
Global $aUserEnum = _NetUserEnum()
    If @error Then 
        MsgBox(0, "Ошибка", _WinAPI_GetLastErrorMessage())
        Exit
    EndIf
Global $sUserEnum = _ArrayToString($aUserEnum, "|", 1, $aUserEnum[0]+1)
Global $aCompSID = _Security__LookupAccountName(@ComputerName)
    If @error Then 
        MsgBox(0, "Ошибка", _WinAPI_GetLastErrorMessage())
        Exit
    EndIf
Global $LocalAdminName = _Security__LookupAccountSid($aCompSID[0] & "-500")
    If @error Then 
        MsgBox(0, "Ошибка", _WinAPI_GetLastErrorMessage())
        Exit
    EndIf

GetAcctCredentials()
TrayMenu()
_terminate()

Func GetAcctCredentials()
    Local $sUserEnumDefault = $aUserEnum[1]
    For $i = 1 to $aUserEnum[0] Step 1
        If $aCompSID[2] == 3 Then
            Local $IsAdmin = _IsAdmin($aUserEnum[$i])
            If $IsAdmin == False Then
                Local $sUserEnumDefault = $aUserEnum[$i]
                ExitLoop
            EndIf
        EndIf
    Next
    TraySetState(2)
    $hWnd = GUICreate("Настройки:", 220, 78, -1, -1, $WS_CAPTION + $WS_SYSMENU, $WS_EX_APPWINDOW + $WS_EX_TOPMOST) ; Setup
        GUICtrlCreateLabel("Логин:", 5, 7, 40, 20)
        $Combo_1 = GUICtrlCreateCombo("", 60, 5, 155, 20)
        GUICtrlSetData($Combo_1, $sUserEnum, $sUserEnumDefault)
        GUICtrlSetTip($Combo_1, "Логин пользователя") ; Login
        GUICtrlCreateLabel("Пароль:", 5, 30, 40, 20) ; Password
        $Input_1 = GUICtrlCreateInput("", 60, 28, 155, 20, $ES_PASSWORD + $ES_AUTOHSCROLL)
        GUICtrlSetTip($Input_1, "Пароль пользователя") ; Account password
        $Button_1 = GUICtrlCreateButton("Сохранить", 5, 51, 105, 23) ; Save
        $Button_2 = GUICtrlCreateButton("Выйти", 110, 51, 105, 23) ; Exit
        GUICtrlSetState($Input_1, $GUI_FOCUS)
        GUICtrlSetState($Button_1, $GUI_DEFBUTTON)
    GUISetState(@SW_SHOW)
    While 1
        $msg = GUIGetMsg()
        $UserName = GUICtrlRead($Combo_1)
        $Password = GUICtrlRead($Input_1)
        If $msg = $Button_1 Then
            Local $IsAdmin = _IsAdmin($UserName)
            Local $CheckUserCredentials = _LogonUser($UserName,$Password)
            Local $msg = ""
            If $CheckUserCredentials == False Then
                $msg &= "Не верный логин/пароль." & @CR & "Пустые пароли не поддерживаются!" & @CR ; Incorrect Login/Password. Blank passwords are forbidden!
                GUICtrlSetData($Input_1, "")
            EndIf
            If $IsAdmin == True Then
                $msg &= "Указанный пользователь находится в локальной группе администраторов!" & @CR ; Specified account is member of local administrator group!
                GUICtrlSetData($Input_1, "")
            EndIf    
            If $CheckUserCredentials == True And $IsAdmin == False Then ExitLoop
                MsgBox(0, "Ошибка", $msg, 0, $hWnd)
        EndIf
        If $msg = $Button_2 Or $msg = $GUI_EVENT_CLOSE Then Exit
    WEnd
    GUIDelete()
    TraySetState(1)
EndFunc

Func TrayMenu()
    $_FuncTimerInit = TimerInit()
        $explorer_menu = TrayCreateMenu("Проводник") ; Explorer
            $_ExplorerAdmin = TrayCreateItem("Мой компьютер", $explorer_menu) ; My Computer
            TrayItemSetState($_ExplorerAdmin, $TRAY_DEFAULT)
            $cpl = TrayCreateItem("Панель управления", $explorer_menu) ; Control Panel
            $_ExplorerAdminUserProfileDir = TrayCreateItem("Папка профилей", $explorer_menu) ; User profiles folder
            $_RestartShell = TrayCreateItem("Перезагрузить оболочку", $explorer_menu) ; Restart shell (explorer)
        $cpl_menu = TrayCreateMenu("Панель управления") ; Control Panel
            $appwiz = TrayCreateItem("Установка и удаление программ", $cpl_menu) ; Add/Remove programs
            $nusrmgr = TrayCreateItem("Учетные записи пользователей", $cpl_menu) ; User Accounts (nusrmgr)
            $firewall = TrayCreateItem("Брандмауэр Windows", $cpl_menu) ; Firewall
            $sysdm = TrayCreateItem("Свойства системы", $cpl_menu) ; System Properties
            $inetcpl = TrayCreateItem("Свойства обозревателя", $cpl_menu) ; IE Options
            $hdwwiz = TrayCreateItem("Установка оборудования", $cpl_menu) ; Add hardware wizard
            $timedate = TrayCreateItem("Дата и время", $cpl_menu) ; Date and Time
        $util_menu = TrayCreateMenu("Утилиты") ; Utilities
            $compmgmt = TrayCreateItem("Управление компьютером", $util_menu) ; Computer management
            TrayItemSetState($compmgmt, $TRAY_DEFAULT)
            $regedit = TrayCreateItem("Редактор реестра", $util_menu) ; Regedit
            $taskmgr = TrayCreateItem("Диспетчер задач (Win+Space)", $util_menu) ; Task manager
            $gpedit = TrayCreateItem("Групповая политика", $util_menu) ; Group policy editor
            $msconfig = TrayCreateItem("Настройка системы", $util_menu) ; MsConfig
            $msinfo32 = TrayCreateItem("Сведения о системе", $util_menu) ; MsInfo32
        TrayCreateItem("")
        $exititem = TrayCreateItem("Выход (Ctrl+Shift+Backspace)") ; Exit
    TraySetState(1)
    TraySetClick(8)
    TraySetIcon(@ScriptDir & "\" & @ScriptName, 1)
    TraySetToolTip("Быстрый запуск с правами администратора") ; Quick launch with admin rights
        While 1
            $msg = TrayGetMsg()
            HotKeySet("#{SPACE}", "_taskmgr")
            HotKeySet("^+{BS}", "_terminate")
            $_FuncTimerDiff = TimerDiff($_FuncTimerInit)
            If $_FuncTimerDiff > 50 Then _Func()
            $SplashTimerDiff = TimerDiff($SplashTimerInit)
            If $SplashTimerDiff > 4000 Then SplashOff()
            Select
                Case $msg = 0
                    ContinueLoop
                Case $msg = $cpl
                    _ExplorerAdmin("::{20D04FE0-3AEA-1069-A2D8-08002B30309D}\::{21EC2020-3AEA-1069-A2DD-08002B30309D}")
                Case $msg = $appwiz
                    _MakeMeAdmin_RunAs($UserName, $Password, @SystemDir & "\rundll32.exe shell32.dll,Control_RunDLL appwiz.cpl")
                Case $msg = $nusrmgr
                    _MakeMeAdmin_RunAs($UserName, $Password, @SystemDir & "\rundll32.exe shell32.dll,Control_RunDLL nusrmgr.cpl")
                Case $msg = $firewall
                    _MakeMeAdmin_RunAs($UserName, $Password, @SystemDir & "\rundll32.exe shell32.dll,Control_RunDLL firewall.cpl")
                Case $msg = $sysdm
                    _MakeMeAdmin_RunAs($UserName, $Password, @SystemDir & "\rundll32.exe shell32.dll,Control_RunDLL sysdm.cpl")
                Case $msg = $inetcpl
                    _MakeMeAdmin_RunAs($UserName, $Password, @SystemDir & "\rundll32.exe shell32.dll,Control_RunDLL inetcpl.cpl")
                Case $msg = $hdwwiz
                    _MakeMeAdmin_RunAs($UserName, $Password, @SystemDir & "\rundll32.exe shell32.dll,Control_RunDLL hdwwiz.cpl")
                Case $msg = $timedate
                    _MakeMeAdmin_RunAs($UserName, $Password, @SystemDir & "\rundll32.exe shell32.dll,Control_RunDLL timedate.cpl")
                Case $msg = $compmgmt
                    _MakeMeAdmin_RunAs($UserName, $Password, @SystemDir & "\mmc.exe compmgmt.msc /s")
                Case $msg = $regedit
                    _MakeMeAdmin_RunAs($UserName, $Password, "regedit.exe")
                Case $msg = $taskmgr
                    _taskmgr()
                Case $msg = $gpedit
                    _MakeMeAdmin_RunAs($UserName, $Password, @SystemDir & "\mmc.exe gpedit.msc")
                Case $msg = $msconfig
                    _MakeMeAdmin_RunAs($UserName, $Password, @WindowsDir & "\pchealth\helpctr\binaries\msconfig.exe")
                Case $msg = $msinfo32
                    _MakeMeAdmin_RunAs($UserName, $Password, @WindowsDir & "\pchealth\helpctr\binaries\HelpCtr.exe -mode hcp://system/sysinfo/msinfo.xml")
                Case $msg = $_ExplorerAdmin Or $msg = $TRAY_EVENT_PRIMARYDOWN
                    _ExplorerAdmin("::{20D04FE0-3AEA-1069-A2D8-08002B30309D}")
                Case $msg = $_ExplorerAdminUserProfileDir
                    _ExplorerAdmin("/SELECT," & @UserProfileDir)
                Case $msg = $_RestartShell
                    _RestartShell()
                Case $msg = $exititem
                    ExitLoop
            EndSelect
        WEnd
EndFunc

Func _Sleep($Delay)
    TraySetIcon(@SystemDir & "\shdocvw.dll", 112)
    TraySetClick(0)
    Sleep($Delay)
    TraySetClick(8)
    TraySetIcon(@ScriptDir & "\" & @ScriptName, 1)
EndFunc    ;_Sleep

Func _taskmgr()
    _MakeMeAdmin_RunAs($UserName, $Password, @SystemDir & "\taskmgr.exe")
EndFunc    ;_taskmgr

Func _ExplorerAdmin($_ExplorerAdminPath)
    $SplashTimerDiff = TimerDiff($SplashTimerInit)
    If $SplashTimerDiff > 4000 Then
        $SplashTimerInit = TimerInit()
        Local $iIndex = $GWL_EXSTYLE
        Local $iValue = $WS_EX_WINDOWEDGE + $WS_EX_TOPMOST + $WS_EX_TRANSPARENT
        Local $hWnd = SplashTextOn("", "Не забываем обновлять окно проводника!", 350, 24, -1, -1, 33) ; Don't forget to manually refresh explorer window
        _WinAPI_SetWindowLong($hWnd, $iIndex, $iValue)
        WinSetTrans($hWnd, "", 200)
    EndIf
    If _ProcessGetName($ExplorerPID) == "explorer.exe" Then
        _MakeMeAdmin_RunAs($UserName, $Password, "explorer.exe " & $_ExplorerAdminPath)
    Else
        $ExplorerPID = _MakeMeAdmin_RunAs($UserName, $Password, "explorer.exe /SEPARATE," & $_ExplorerAdminPath)
    EndIf
EndFunc    ;_ExplorerAdmin

Func _KillExplorer()
    SplashOff()
    If _ProcessGetName($ExplorerPID) == "explorer.exe" Then ProcessClose($ExplorerPID)
EndFunc    ;_KillExplorer

Func _RestartShell()
    _KillExplorer()
    ProcessClose("explorer.exe")
EndFunc    ;_RestartShell

Func _terminate()
    DllClose($Dll)
    _KillExplorer()
    Exit
EndFunc    ;_terminate

Func _Func()
    $_FuncTimerInit = TimerInit()
    Local $sWindowName_1 = "Запуск от имени другого пользователя" ; Runas dialog caption (shell32.dll -> Dialog -> 1007 -> Caption)
    Local $sWindowName_2 = "Установка программы от другого имени" ; Install as different user dialog caption (shell32.dll -> Dialog -> 1053 -> Caption)
    Local $sWindowName_3 = "Найдено новое оборудование" ; Install new hardware dialog caption (newdev.dll -> String Table -> 133 -> 2123)
    If WinExists($sWindowName_1) And WinActive($sWindowName_1) And _IsPressed("04", $Dll) Then
        Local $hWnd = WinGetHandle($sWindowName_1)
        Local $aMousePos = MouseGetPos()
        Local $aControlPos = ControlGetPos($hWnd, "", "Button5")
        If @error <> 1 And IsArray($aControlPos) And $aMousePos[0] >= $aControlPos[0] And $aMousePos[0] <= $aControlPos[0]+$aControlPos[2] And $aMousePos[1] >= $aControlPos[1] And $aMousePos[1] <= $aControlPos[1]+$aControlPos[3] Then
            WinSetState($hWnd, "", @SW_HIDE)
            _NetLocalGroupAddMember($UserName, $aLocalAdminGroupName[0])
                If @error Then 
                    MsgBox(0, "Ошибка", _WinAPI_GetLastErrorMessage())
                EndIf
            _Sleep($Delay)
            ControlCommAnd($hWnd, "", "Button2", "UnCheck", "")
            ControlCommAnd($hWnd, "", "Button3", "Check", "")
            ControlDisable($hWnd, "", "ComboBox1")
            ControlSetText($hWnd, "", "Edit1", $UserName)
            ControlSetText($hWnd, "", "Edit2", $Password)
            ControlClick($hWnd, "", "Button5", "primary")
            _Sleep($Delay*3)
            _NetLocalGroupDelMembers($UserName, $aLocalAdminGroupName[0])
                If @error Then 
                    MsgBox(0, "Ошибка", _WinAPI_GetLastErrorMessage())
                EndIf
            WinSetState($hWnd, "", @SW_SHOW)
        EndIf
    EndIf
    If WinExists($sWindowName_2) And WinActive($sWindowName_2) And _IsPressed("04", $Dll) Then
        Local $hWnd = WinGetHandle($sWindowName_2)
        Local $aMousePos = MouseGetPos()
        Local $aControlPos = ControlGetPos($hWnd, "", "Button6")
        If @error <> 1 And IsArray($aControlPos) And $aMousePos[0] >= $aControlPos[0] And $aMousePos[0] <= $aControlPos[0]+$aControlPos[2] And $aMousePos[1] >= $aControlPos[1] And $aMousePos[1] <= $aControlPos[1]+$aControlPos[3] Then
            WinSetState($hWnd, "", @SW_HIDE)
            _NetLocalGroupAddMember($UserName, $aLocalAdminGroupName[0])
                If @error Then 
                    MsgBox(0, "Ошибка", _WinAPI_GetLastErrorMessage())
                EndIf
            _Sleep($Delay)
            ControlCommAnd($hWnd, "", "Button3", "Check", "")
            ControlDisable($hWnd, "", "ComboBox1")
            ControlSetText($hWnd, "", "Edit1", $UserName)
            ControlSetText($hWnd, "", "Edit2", $Password)
            ControlCommAnd($hWnd, "", "Button5", "UnCheck", "")
            ControlClick($hWnd, "", "Button6", "primary")
            _Sleep($Delay*3)
            _NetLocalGroupDelMembers($UserName, $aLocalAdminGroupName[0])
                If @error Then 
                    MsgBox(0, "Ошибка", _WinAPI_GetLastErrorMessage())
                EndIf
            WinSetState($hWnd, "", @SW_SHOW)
        EndIF
    EndIf
    If WinExists($sWindowName_3) And WinActive($sWindowName_3) And _IsPressed("04", $Dll) Then
        Local $hWnd = WinGetHandle($sWindowName_3)
        Local $aMousePos = MouseGetPos()
        Local $aControlPos = ControlGetPos($hWnd, "", "Button2")
        If @error <> 1 And IsArray($aControlPos) And $aMousePos[0] >= $aControlPos[0] And $aMousePos[0] <= $aControlPos[0]+$aControlPos[2] And $aMousePos[1] >= $aControlPos[1] And $aMousePos[1] <= $aControlPos[1]+$aControlPos[3] Then
            WinSetState($hWnd, "", @SW_HIDE)
            _NetLocalGroupAddMember($UserName, $aLocalAdminGroupName[0])
                If @error Then 
                    MsgBox(0, "Ошибка", _WinAPI_GetLastErrorMessage())
                EndIf
            _Sleep($Delay)
            ControlSetText($hWnd, "", "Edit2", $UserName)
            ControlSetText($hWnd, "", "Edit3", $Password)
            ControlClick($hWnd, "", "Button2", "primary")
            _Sleep($Delay*3)
            _NetLocalGroupDelMembers($UserName, $aLocalAdminGroupName[0])
                If @error Then 
                    MsgBox(0, "Ошибка", _WinAPI_GetLastErrorMessage())
                EndIf
            WinSetState($hWnd, "", @SW_SHOW)
        EndIf
    EndIf
EndFunc    ;_Func

Func _MakeMeAdmin_RunAs($UserName, $Password, $ExecPath)
    _NetLocalGroupAddMember($UserName, $aLocalAdminGroupName[0])
        If @error Then 
            MsgBox(0, "Ошибка", _WinAPI_GetLastErrorMessage())
        EndIf
    _Sleep($Delay)
    Local $PID = RunAs($UserName, @ComputerName, $Password, 0, $ExecPath)
        If @error Then 
            MsgBox(0, "Ошибка", _WinAPI_GetLastErrorMessage())
        EndIf
    _Sleep($Delay*3)
    _NetLocalGroupDelMembers($UserName, $aLocalAdminGroupName[0])
        If @error Then 
            MsgBox(0, "Ошибка", _WinAPI_GetLastErrorMessage())
        EndIf
    Return $PID
EndFunc    ;_MakeMeAdmin_RunAs

Func _LogonUser($sUsername, $sPassword, $sServer = '.') ; Returns True if user exists 
    $stToken = DllStructCreate("int")
    Local $aRet = DllCall("advapi32.dll", "int", "LogonUser", _
            "str", $sUsername, "str", $sServer, "str", $sPassword, "dword", 3, "dword", 0, "ptr", DllStructGetPtr($stToken))
    ;$hToken = DllStructGetData($stToken, 1)
    If Not @error And $aRet[0] <> 0 Then
        Return True
    EndIf  
    Return False
EndFunc    ;_LogonUser

Func _IsAdmin($UserName)
    Local $aLocalGroupNames = _NetUserGetLocalGroups($UserName)
    Local $ArraySearch = _ArraySearch($aLocalGroupNames, $aLocalAdminGroupName[0], 1)
    If $ArraySearch == -1 Then 
        Return False
    Else
        Return True
    EndIf
EndFunc    ;_IsAdmin

Func _NetLocalGroupAddMember($sUsername, $sGroup, $sServer = '')
    Local $twUser = DllStructCreate("wchar["& StringLen($sUsername)+1 &"]")
    Local $tpUser = DllStructCreate("ptr")
    DllStructSetData($twUser, 1, $sUsername)
    DllStructSetData($tpUser, 1, DllStructGetPtr($twUser))
    Local $aRet = DllCall("netapi32.dll", "int", "NetLocalGroupAddMembers", "wstr", $sServer, "wstr", $sGroup, "int", 3, "ptr", DllStructGetPtr($tpUser), "int", 1 )
    If $aRet[0] Then Return SetError(1, $aRet[0], False)
    Return True
EndFunc    ;_NetLocalGroupAddMember

Func _NetLocalGroupDelMembers($sUsername, $sGroup, $sServer = '')
    Local $twUser = DllStructCreate("wchar["& StringLen($sUsername)+1 &"]")
    Local $tpUser = DllStructCreate("ptr")
    DllStructSetData($twUser, 1, $sUsername)
    DllStructSetData($tpUser, 1, DllStructGetPtr($twUser))
    Local $aRet = DllCall("netapi32.dll", "int", "NetLocalGroupDelMembers", "wstr", $sServer, "wstr", $sGroup, "int", 3, "ptr", DllStructGetPtr($tpUser), "int", 1 )
    If $aRet[0] Then Return SetError(1, $aRet[0], False)
    Return True
EndFunc    ;_NetLocalGroupDelMembers

Func _NetUserEnum($sServer = "") ; array[0] contains number of elements
    Local $tBufPtr = DllStructCreate("ptr")
    Local $tEntriesRead = DllStructCreate("dword")
    Local $tTotalEntries = DllStructCreate("dword")
    Local $aRet = DllCall("Netapi32.dll", "int", "NetUserEnum", "wstr", $sServer, "dword", 1, "dword", 2, "ptr", DllStructGetPtr($tBufPtr), "dword", -1, "ptr", DllStructGetPtr($tEntriesRead), "ptr", DllStructGetPtr($tTotalEntries), "ptr", 0 )
    If $aRet[0] Then Return SetError(1, $aRet[0], False)
    Local Const $UF_ACCOUNTDISABLE = 0x2
    Local $iEntriesRead = DllStructGetData($tEntriesRead,1)
    Local $pBuf = DllStructGetData($tBufPtr,1)
    Local $aUserEnum[1] = [0]
    Local $sUserInfo1 = "ptr;ptr;dword;dword;ptr;ptr;dword;ptr"
    Local $tUserInfo1 = DllStructCreate ($sUserInfo1)
    Local $zUserInfo1 = DllStructGetSize($tUserInfo1)
    For $i=1 To $iEntriesRead
        $tUserInfo1 = DllStructCreate($sUserInfo1, $pBuf+($i-1)*$zUserInfo1)
        $tName = DllStructCreate("wchar[256]", DllStructGetData($tUserInfo1,1))
        $tFlag = DllStructGetData($tUserInfo1,7)
        If BitAnd($tFlag, $UF_ACCOUNTDISABLE)=0 Then
            $aUserEnum[0] += 1
            ReDim $aUserEnum[$aUserEnum[0]+1]
            $aUserEnum[$aUserEnum[0]] = DllStructGetData($tName,1)
        EndIf
    Next
    DllCall("Netapi32.dll", "int", "NetApiBufferFree", "ptr", $pBuf)
    Return $aUserEnum
EndFunc    ;_NetUserEnum

Func _NetUserGetLocalGroups($sUsername, $sServer = "") ; array[0] contains number of elements
    Local CONST $LG_INCLUDE_INDIRECT = 0x1
    Local $tBufPtr = DllStructCreate("ptr")
    Local $ptBufPtr = DllStructGetPtr($tBufPtr)
    Local $tEntriesRead = DllStructCreate("dword")
    Local $ptEntriesRead = DllStructGetPtr($tEntriesRead)
    Local $tTotalEntries = DllStructCreate("dword")
    Local $ptTotalEntries = DllStructGetPtr($tTotalEntries)
    Local $aRet = DllCall("Netapi32.dll", "int", "NetUserGetLocalGroups", "wstr", $sServer, "wstr", $sUsername, "dword", 0, "dword", $LG_INCLUDE_INDIRECT, "ptr", $ptBufPtr, "dword", -1, "ptr", $ptEntriesRead, "ptr", $ptTotalEntries)
    If $aRet[0] Then Return SetError(1, $aRet[0], False)
    Local $iEntriesRead = DllStructGetData($tEntriesRead, 1)
    Local $pBuf = DllStructGetData($tBufPtr, 1)
    Local $sLocalGroupUsersInfo0 = "ptr"
    Local $tLocalGroupUsersInfo0 = DllStructCreate($sLocalGroupUsersInfo0)
    Local $zLocalGroupUsersInfo0 = DllStructGetSize($tLocalGroupUsersInfo0)
    Local $tLocalGroupName
    Local $aLocalGroupNames[1] = [0]
    For $i = 1 To $iEntriesRead
        $tLocalGroupUsersInfo0 = DllStructCreate($sLocalGroupUsersInfo0, $pBuf + ($i - 1) * $zLocalGroupUsersInfo0)
        $tLocalGroupName = DllStructCreate("wchar[256]", DllStructGetData($tLocalGroupUsersInfo0, 1))
        $aLocalGroupNames[0] += 1
        ReDim $aLocalGroupNames[$aLocalGroupNames[0]+1]
        $aLocalGroupNames[$aLocalGroupNames[0]] = DllStructGetData($tLocalGroupName,1)
    Next
    DllCall("Netapi32.dll", "int", "NetApiBufferFree", "ptr", $pBuf)
    Return $aLocalGroupNames
EndFunc    ;_NetUserGetLocalGroups
Edited by Ghost1987

Share this post


Link to post
Share on other sites

I've updated scripts, AGAIN!

Fixed some stupid bugs.

Added some new functionality, cleaned code.

Wrote comments to ease translation from Russian ;-)

I would be very pleased if someone finds this scripts useful.

Still waiting for comments, so please comment ;-).

Share this post


Link to post
Share on other sites

#10 ·  Posted (edited)

More script updates!

Edited by Ghost1987

Share this post


Link to post
Share on other sites

#11 ·  Posted (edited)

If someone would provide this information:

* Runas dialog caption (shell32.dll -> Dialog -> 1007 -> Caption)

* Install as different user dialog caption (shell32.dll -> Dialog -> 1053 -> Caption)

* Install new hardware dialog caption (newdev.dll -> String Table -> 133 -> 2123)

I'll make English version of AdminTray script so more people could test it out on English version of WinXP.

Edited by Ghost1987

Share this post


Link to post
Share on other sites

Any idea when the translation is done? i tried to do it but doesn't look right

Share this post


Link to post
Share on other sites

The script in the first post doenst work for me..

C:\temp\test.au3(136,60) : WARNING: $LocalAdminGroupName: possibly used before declaration.

_ArraySearch($aLocalGroupNames, $LocalAdminGroupName[0],

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^

C:\temp\test.au3(136,60) : ERROR: $LocalAdminGroupName: undeclared global variable.

_ArraySearch($aLocalGroupNames, $LocalAdminGroupName[0],

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^

C:\temp\test.au3 - 1 error(s), 1 warning(s)

Can someone help me out?

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0