Sign in to follow this  
Followers 0
Yeik

runas with any file.

11 posts in this topic

#1 ·  Posted (edited)

This is my first AutoIT project, I work a lot installing software for a big company,

this script will accept domain logins, or local accounts, just type for the username:

domain\\username

It will create a small little window that you can drag a file into that will open it up.

It tells you if the user account has been locked out, or if you typed in a bad

username/password (just says that your credentials are incorrect)

Please let me know what you may think, any suggestions, improvements etc.

I know it has been done before, but it never seemed to quite work right when

I would test them.

EDIT: 2/29/08

Uploaded compiled script with updates, will now run files that dont have an extension (will ask you what you want to open with)

GUI box will now stay on top.

EDIT: 3/1/08

Uploaded new compiled script, shouldn\'t give errors if the application is placed on a server location.

Uploaded .au3 file

EDIT: 3/2/08

Working on some new features, stay tuned, will be uploading them soon. Would like to know if anybody has any they would like to request while I am working on it!

note: only tested on XP, will test Windows 2000 soon, Someone has tested and said it works in Vista!

EDIT: 3/3/08

Fixed a bug on closing the cmd process that spawns to run the files, if the file was too big, and had to be downloaded, it would not open if the program hasn\'t started yet, if you are running a file from the internet, or over a slow link, you might want to remove or put a longer sleep on the _waitkill() function.

EDIT: 04/02/90

I updated it with the newer version of autoit that uses runas instead of the old style it was using. Also made it so it executes itself as the different user. I have it set up to copy itself to @temp if it doesn't have rights, but that can be changed to any directory. I have code in there that should do a delayed delete if it is in the temp folder, but I haven't really tested it yet.

CODE has been updated as of 04/02/09

#cs ---------------------
    credits:
    I borrowed the GUI from:
    ;~ RunAS Box
    ;~ Author: Shaun Burdick
    ;~ Date: 11/30/2006

    The rest i have slowly pieced together.
    It should open any file that has an extension associated with
    a program. I have tested it on .rar .zip .dat .exe .reg .txt .vbs .msi, shortcuts, url links, among others.
    it works with a file that doesn't have an extension, asking you what you want to open it with.
    if you have any issues with opening a file, let me know.
    Enjoy

    Yeik 04-02-2009
#ce ---------

#include <GUIConstants.au3>
#include <WindowsConstants.au3>

$oMyError = ObjEvent("AutoIt.Error", "ComError")
Opt("GUIOnEventMode", 1)
Global $username
Global $password
Global $domain
Global $Browse

__main__()
Func __main__()
    if UBound($cmdline) > 3 Then
        $username = $cmdline[1]
        $domain = $cmdline[2]
        $password = $cmdline[3]
        if @UserName = $username Then
        Else
            _switchuser($username, $domain, $password)
        EndIf
    Else
    setcredentials()
    EndIf
    runwindow()
EndFunc

Func setcredentials()
    Local $user
    Local $error
    $defdom = ""
    If $username Then $username = 0
    If $domain Then $domain = 0
    If $password Then $password = 0
    $user = InputBox("Username", 'Enter your username', $defdom, "", 200, 100, -1, -1)
    If @error Then _exit()
    $password = InputBox("Password", 'Enter your password', "", "*M", 200, 100, -1, -1)
    If @error Then _exit()
    If StringInStr($user, "\") Then
        $domain = StringLeft($user, StringInStr($user, "\") - 1)
        $username = StringTrimLeft($user, StringLen($domain) + 1)
    ElseIf StringInStr($user, "@") Then
        $username = StringLeft($user, StringInStr($user, "@") - 1)
        $domain = StringTrimLeft($user, StringLen($username) + 1)
    Else
        $domain = @ComputerName
        $username = $user
    EndIf
    testcredentials()
EndFunc   ;==>setcredentials


Func testcredentials() ;Test to make sure you entered valid domain, username, and password
    Local $error
    Local $procpid
    $procpid = RunAs($username, $domain, $password, 1, @SystemDir &"\cmd.exe", "", @SW_HIDE)
    $error = @extended
    If ProcessExists($procpid) Then RunAs($username, $domain, $password, 1, "taskkill /F /T /IM " & $procpid, "", @SW_HIDE); Close test process
    If $error <> 0 Then
        Switch $error
            Case 1326
                MsgBox(4096, "ERROR", "Your credentials were wrong. ")
                setcredentials()
            Case 87
                MsgBox(4096, "ERROR", "can't find file")
                _exit()
            Case 1909
                MsgBox(4096, "ERROR", "Account is locked out")
                setcredentials()
            Case Else
                MsgBox(4096, "ERROR", "unkown error")
                MsgBox(4096, "", $error)
                _exit()
        EndSwitch
    EndIf
    _switchuser($username, $domain, $password)
EndFunc   ;==>testcredentials

Func _switchuser($usr, $dmn, $pwd)
    local $tmperror
    ;MsgBox(0, "script file name", @ScriptFullPath)
    if StringRight(@ScriptFullPath, 3) = "exe" Then
        RunAs($usr, $dmn, $pwd, 1, @ScriptFullPath & " " & $usr & ' ' & $dmn & ' ' & $pwd)
        $tmperror = @extended
        if $tmperror Then
            FileCopy(@ScriptFullPath, @TempDir)
            RunAs($usr, $dmn, $pwd, 1, @TempDir & '\' & @ScriptName & " " & $usr & ' ' & $dmn & ' ' & $pwd)
            $tmperror = @extended
            If $tmperror Then
                MsgBox(0, "Error", "That user doesn't have proper rights to the file or the temp folder. Error: " & $tmperror, 15)
            EndIf
        EndIf
    Else
        RunAs($usr, $dmn, $pwd, 4, @AutoItExe & ' /autoit3executescript ' &'"' & @ScriptFullPath &'"' & " " & $usr & ' ' & $dmn & ' ' & $pwd)
        ;MsgBox(0, "script execute", @AutoItExe & ' /autoit3executescript ' &'"' & @ScriptFullPath &'"' & " " & $usr & ' ' & $dmn & ' ' & $pwd)
        if @extended Then
            MsgBox(0, "File Rights", "That account doesn't have rights to autoit or the script", 10)
        EndIf
    EndIf
    _exit()
EndFunc

Func runfile($filename)
    Local $error
    Local $procpid
    $fileext = "noext"
    ;MsgBox(0, "filename", $filename)
    If StringInStr($filename, ".") Then
        $filepath = StringLeft($filename, StringInStr($filename, ".") - 1)
        $fileext = StringTrimLeft($filename, StringLen($filename) + 1)
        $error = 0
    EndIf
    Switch $fileext
        Case "exe", "bat", "com"
            ;MsgBox(0, "case exe", "case 1, exe")
            Run($filename)
            $error = @extended
        Case "noext"
            ;MsgBox(0, "case noext", "case 2, no extension")
            ShellExecute("rundll32.exe", "shell32.dll, OpenAs_RunDLL " & $filename)
            $error = @extended
        Case Else
            ;MsgBox(0, "case else", "case 3, else")
            $procpid = Run(@ComSpec & ' /c ' & '"' & $filename & '"', '', @SW_HIDE)
            $error = @extended
            ;MsgBox(0, "case else", "case 3, after. " & $error)
    EndSwitch
    ;MsgBox(0, "extended error", "error: " & $error)
    if $procpid Then Run("taskkill /F /IM " & $procpid, "", @SW_HIDE)
    If $error <> 0 Then
        Switch $error
            Case 1326
                MsgBox(4096, "ERROR", "Your credentials were wrong. ")
                setcredentials()
            Case 267
                MsgBox(4096, "Error", $username & " does not have rights to " & $filename)
            Case 87
                MsgBox(4096, "ERROR", "can't find file:" & $filename)
            Case 1909
                MsgBox(4096, "ERROR", "Account is locked out")
                setcredentials()
            Case Else
                MsgBox(4096, "ERROR", "unkown error")
                MsgBox(4096, "", "Error:" & $error & " file:" & $filename)
        EndSwitch
    EndIf
EndFunc   ;==>runfile

Func _killcmd($pid)
    $wait = TimerInit() + 10
    Do
    Until $wait <= TimerInit()
    MsgBox(4096, "Kill", "Time to kill " & $pid)
    If ProcessExists($pid) Then ProcessClose($pid)
     If ProcessExists($pid) Then
         Run("taskkill /F /IM " & $pid, "", @SW_HIDE)
         MsgBox(4096, "Kill", "still exists " & $pid)
    EndIf
EndFunc

Func runwindow()
    Local $strBKCOLOR = 0x0000FF;Blue
    Local $strCOLOR = 0xFFFBF0;Off-White
    Local $intWindowHeight = 175
    Local $intWindowWidth = 130
    $ptrDragBox = GUICreate("RunAS: " & $username, $intWindowHeight, _
            $intWindowWidth + 25, -1, -1, -1, $WS_EX_ACCEPTFILES)
    $ptrDropLabel = GUICtrlCreateLabel("Run as: " & @LF & $domain & @LF & $username & @LF & @LF & "Drag File here", 1, 1, $intWindowHeight, $intWindowWidth)
    $Browse = GUICtrlCreateButton("Browse", 5, $intWindowWidth + 1)
    GUICtrlSetOnEvent(-1, "_browse")
    GUICtrlSetState($ptrDropLabel, $GUI_DROPACCEPTED)
    GUICtrlSetBkColor($ptrDropLabel, $strBKCOLOR)
    GUICtrlSetColor($ptrDropLabel, $strCOLOR)
    GUICtrlSetFont($ptrDropLabel, 16, "BOLD")
    GUISetState(@SW_SHOW, $ptrDragBox)
    ;GUISetOnEvent($GUI_EVENT_MINIMIZE,"hide")
    GUISetOnEvent($GUI_EVENT_CLOSE, "_exit")
    GUISetOnEvent($GUI_EVENT_DROPPED, "filerun")
    While 1
        Sleep(100000)
    WEnd
EndFunc   ;==>runwindow

Func _browse()
    Local $openfilename
    $openfilename = FileOpenDialog("open file as", @WindowsDir & "\", "All (*.*)", 1 + 4)
    If $openfilename Then runfile($openfilename)
EndFunc   ;==>_browse

Func filerun()
    runfile(@GUI_DragFile)
EndFunc   ;==>filerun

Func _exit()
    If @ScriptDir = @TempDir Then
        Run(@ComSpec & ' /c ping 127.0.0.1 & ping 127.0.0.1 & del ' & @ScriptFullPath, "", @SW_HIDE)
    EndIf
    Exit
EndFunc   ;==>_exit

Func ComError()
    If IsObj($oMyError) Then
        $HexNumber = Hex($oMyError.number, 8)
        SetError($HexNumber)
    Else
        SetError(1)
    EndIf
EndFunc   ;==>ComError

EDIT:

I have been editing this and making it more efficient,

Please let me know if you would like to see the updates

runasbox.au3

Edited by Yeik

func get_quote()
   local $quote 
   switch random(1, 3, 1)
    case 1
     $quote = '"' & "A human being should be able to change a diaper, plan an invasion, butcher a hog, conn a ship, " & _
       "design a building, write a sonnet, balance accounts, build a wall, set a bone, comfort the dying, take orders, give " & _
       "orders, cooperate, act alone, solve equations, analyze a new problem, pitch manure, program a computer, cook a tasty meal, " & _
       "fight efficiently, die gallantly. Specialization is for insects." & '"' & " Robert A. Heinlein"
    case 2
     $quote =  '"' & "Within each of us lies the power of our consent to health and sickness, to riches and poverty, to freedom " & _
       "and to slavery. It is we who control these, and not another." & '"' & " Richard Bach (Illusions)"
    case 3
     $quote ='"' & "Don't handicap your children by making their lives easy." & '"' & " Robert A. Heinlein"
   EndSwitch
   MsgBox(0, "Quote for the moment", $quote & @CRLF)
EndFunc

get_quote()

Share this post


Link to post
Share on other sites



Updated and added exe (didn't update the listed code)


func get_quote()
   local $quote 
   switch random(1, 3, 1)
    case 1
     $quote = '"' & "A human being should be able to change a diaper, plan an invasion, butcher a hog, conn a ship, " & _
       "design a building, write a sonnet, balance accounts, build a wall, set a bone, comfort the dying, take orders, give " & _
       "orders, cooperate, act alone, solve equations, analyze a new problem, pitch manure, program a computer, cook a tasty meal, " & _
       "fight efficiently, die gallantly. Specialization is for insects." & '"' & " Robert A. Heinlein"
    case 2
     $quote =  '"' & "Within each of us lies the power of our consent to health and sickness, to riches and poverty, to freedom " & _
       "and to slavery. It is we who control these, and not another." & '"' & " Richard Bach (Illusions)"
    case 3
     $quote ='"' & "Don't handicap your children by making their lives easy." & '"' & " Robert A. Heinlein"
   EndSwitch
   MsgBox(0, "Quote for the moment", $quote & @CRLF)
EndFunc

get_quote()

Share this post


Link to post
Share on other sites

Looks nice, but I think you should get rid of Case 267.

MsgBox(4096,"ERROR", "Path is invalid")

It appears that you require the user to be a local admin on the box to run this program. I often have the case where I want to run mmc, regedit, iisreset, or cmd as a certain user that is a local admin on another machine to view their services or registry but might not necessarily be a local admin on my machine.

I commented out Case 267 and this worked great for me. This will be a useful tool. Thanks.


-- If the apocalypse comes... beep me.

Share this post


Link to post
Share on other sites

the 267 error has been popping up oddly for me.

Also I can only start getting the error message for 267 if i use the browse button to open a file the account i am using

doesn't have access to, then if i drag a file to it, then it tells me the same thing until i use browse to open a file that the account has access to.

I use the 267 error because i dont like it when i go to open something and nothing happens.

I have not thought of using it to run files from a remote host on a local computer, i think there could be a better way to do that, so you can run it under the local admin account but access it with the other account.

Looks nice, but I think you should get rid of Case 267.

MsgBox(4096,"ERROR", "Path is invalid")

It appears that you require the user to be a local admin on the box to run this program. I often have the case where I want to run mmc, regedit, iisreset, or cmd as a certain user that is a local admin on another machine to view their services or registry but might not necessarily be a local admin on my machine.

I commented out Case 267 and this worked great for me. This will be a useful tool. Thanks.


func get_quote()
   local $quote 
   switch random(1, 3, 1)
    case 1
     $quote = '"' & "A human being should be able to change a diaper, plan an invasion, butcher a hog, conn a ship, " & _
       "design a building, write a sonnet, balance accounts, build a wall, set a bone, comfort the dying, take orders, give " & _
       "orders, cooperate, act alone, solve equations, analyze a new problem, pitch manure, program a computer, cook a tasty meal, " & _
       "fight efficiently, die gallantly. Specialization is for insects." & '"' & " Robert A. Heinlein"
    case 2
     $quote =  '"' & "Within each of us lies the power of our consent to health and sickness, to riches and poverty, to freedom " & _
       "and to slavery. It is we who control these, and not another." & '"' & " Richard Bach (Illusions)"
    case 3
     $quote ='"' & "Don't handicap your children by making their lives easy." & '"' & " Robert A. Heinlein"
   EndSwitch
   MsgBox(0, "Quote for the moment", $quote & @CRLF)
EndFunc

get_quote()

Share this post


Link to post
Share on other sites

Hi!

Under Vista, it's OK (possible) only if UAC is OFF.

Share this post


Link to post
Share on other sites

Hi!

Under Vista, it's OK (possible) only if UAC is OFF.

Cool,

i was thinking of adding the code for Vista compatibility, but wasn't sure what this did to the code in XP, i will test it.

Thanks for the test.

; This script requires full Administrative rights

#requireadmin


func get_quote()
   local $quote 
   switch random(1, 3, 1)
    case 1
     $quote = '"' & "A human being should be able to change a diaper, plan an invasion, butcher a hog, conn a ship, " & _
       "design a building, write a sonnet, balance accounts, build a wall, set a bone, comfort the dying, take orders, give " & _
       "orders, cooperate, act alone, solve equations, analyze a new problem, pitch manure, program a computer, cook a tasty meal, " & _
       "fight efficiently, die gallantly. Specialization is for insects." & '"' & " Robert A. Heinlein"
    case 2
     $quote =  '"' & "Within each of us lies the power of our consent to health and sickness, to riches and poverty, to freedom " & _
       "and to slavery. It is we who control these, and not another." & '"' & " Richard Bach (Illusions)"
    case 3
     $quote ='"' & "Don't handicap your children by making their lives easy." & '"' & " Robert A. Heinlein"
   EndSwitch
   MsgBox(0, "Quote for the moment", $quote & @CRLF)
EndFunc

get_quote()

Share this post


Link to post
Share on other sites

#7 ·  Posted (edited)

Hi! (bis)

> #requireadmin

Attn: if UAC is on, "#requireadmin" will raise a question, with impossibility of automating the answer.

Another thing: I experimented, also, a different way. Create a service (sc.exe) with a different account + launch this service + delete the service. (On XP, it's OK ; on Vista, with then account "SYSTEM", this run in the "SYSTEM console". And it's... odd).

But the UAC cannot be circumvented. On Vista with UAC on, I no have found any way, for launch a file/program with elevated privileges (Aïe, my english!) without manually intervention.

Edited by Michel Claveau

Share this post


Link to post
Share on other sites

Hi! (ter)

I experimented also the schtasks way (create a new task, under another account + run the task + delete the task).

Contrainsts are similary (but not identical) to services (sc.exe)

Share this post


Link to post
Share on other sites

Hi! (ter)

I experimented also the schtasks way (create a new task, under another account + run the task + delete the task).

Contrainsts are similary (but not identical) to services (sc.exe)

having this program run as a service via the system is probably not a good idea. What i want to do is be able to make the gui, or script itself runas the user, so i might create a script that will gather the account information, then launch the runas script with the account information. I think this would help fix a few issues.


func get_quote()
   local $quote 
   switch random(1, 3, 1)
    case 1
     $quote = '"' & "A human being should be able to change a diaper, plan an invasion, butcher a hog, conn a ship, " & _
       "design a building, write a sonnet, balance accounts, build a wall, set a bone, comfort the dying, take orders, give " & _
       "orders, cooperate, act alone, solve equations, analyze a new problem, pitch manure, program a computer, cook a tasty meal, " & _
       "fight efficiently, die gallantly. Specialization is for insects." & '"' & " Robert A. Heinlein"
    case 2
     $quote =  '"' & "Within each of us lies the power of our consent to health and sickness, to riches and poverty, to freedom " & _
       "and to slavery. It is we who control these, and not another." & '"' & " Richard Bach (Illusions)"
    case 3
     $quote ='"' & "Don't handicap your children by making their lives easy." & '"' & " Robert A. Heinlein"
   EndSwitch
   MsgBox(0, "Quote for the moment", $quote & @CRLF)
EndFunc

get_quote()

Share this post


Link to post
Share on other sites

*bump* for changes and updates.


func get_quote()
   local $quote 
   switch random(1, 3, 1)
    case 1
     $quote = '"' & "A human being should be able to change a diaper, plan an invasion, butcher a hog, conn a ship, " & _
       "design a building, write a sonnet, balance accounts, build a wall, set a bone, comfort the dying, take orders, give " & _
       "orders, cooperate, act alone, solve equations, analyze a new problem, pitch manure, program a computer, cook a tasty meal, " & _
       "fight efficiently, die gallantly. Specialization is for insects." & '"' & " Robert A. Heinlein"
    case 2
     $quote =  '"' & "Within each of us lies the power of our consent to health and sickness, to riches and poverty, to freedom " & _
       "and to slavery. It is we who control these, and not another." & '"' & " Richard Bach (Illusions)"
    case 3
     $quote ='"' & "Don't handicap your children by making their lives easy." & '"' & " Robert A. Heinlein"
   EndSwitch
   MsgBox(0, "Quote for the moment", $quote & @CRLF)
EndFunc

get_quote()

Share this post


Link to post
Share on other sites

It does not accept empty field for password, if a user has no password

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