找回密码
 加入
搜索
查看: 861|回复: 7

[系统综合] runas函数和系统下runas使用【已解决】

[复制链接]
发表于 2022-4-26 08:51:11 | 显示全部楼层 |阅读模式
本帖最后由 redapple2008 于 2022-4-26 13:21 编辑

runas函数和系统下runas使用?
au3自带的runas执行完成后未能正常使用,但命令行runas正常,runas需要手动输入密码。有没有好的办法解决密码输入问题?
Local $UserName = "redapple2008"
Local $PassWord = "xxxxxxxx"
Local $sdoscommand = "runas /user:" & $UserName & " " & "cmd.exe"
Run(@ComSpec & " /k" & $sdoscommand, "", @SW_HIDE)
ControlSend($g_hWndCmd, "", "", $PassWord & "{Enter}")

上述代码很不稳定,而且输入法经常引起错误。有没有大神解决一下?

发表于 2022-4-26 09:31:59 | 显示全部楼层
本帖最后由 tubaba 于 2022-4-26 09:33 编辑

#AutoIt3Wrapper_Icon=                                                                                  ;图标,支持EXE,DLL,ICO#AutoIt3Wrapper_OutFile=                                                                         ;输出文件名
#AutoIt3Wrapper_OutFile_Type=exe                                                         ;文件类型
#AutoIt3Wrapper_Compression=4                                                                 ;压缩等级
#AutoIt3Wrapper_UseUpx=y                                                                          ;使用压缩
#AutoIt3Wrapper_UseX64=n                                                                          ;使用32位编译
#AutoIt3Wrapper_Res_Comment=                                                                  ;程序注释
#AutoIt3Wrapper_Res_Description=                                                         ;详细信息
#AutoIt3Wrapper_Res_Fileversion=                                                         ;文件版本
#AutoIt3Wrapper_Res_FileVersion_AutoIncrement=n                         ;自动更新版本
#AutoIt3Wrapper_Res_LegalCopyright=                                                  ;版权
#AutoIt3Wrapper_Res_requestedExecutionLevel=highestAvailable ;执行等级
#AutoIt3Wrapper_Res_SaveSource=y                                                 ;保存源代码到资源段
#AutoIt3Wrapper_Run_AU3Check=                                                          ;语法检查
#AutoIt3Wrapper_Change2CUI=N                                            ;修改输出的程序为CUI(控制台程序)
#AutoIt3Wrapper_run_debug_mode=N

#cs ____________________________________

        Au3 版本:
        脚本作者:
        电子邮件:
        QQ/TM:
        脚本版本:
        脚本功能:

#ce _______________脚本开始_________________


Opt("MustDeclareVars", 0)
;~ #include <WinApi.au3>
;For 'CreateProcessWithLogonW' -> dwLogonFlags

$UserName = 'administrator'
$UserPasswd = 'XXXXXXXXX'
$ProcessName = @ComSpec;替换你要运行的程序全路径 ,参数也可放在这里





Global Const $LOGON_WITH_PROFILE = 0x1
Global Const $LOGON_NETCREDENTIALS_ONLY = 0x2
;For'CreateProcessWithLogonW' -> dwCreationFlags
Global Const $CREATE_DEFAULT_ERROR_MODE = 0x4000000
;Les initialisations
Dim $STARTUPINFO = "", $si = ""
Dim $PROCESS_INFORMATION = "", $pi = ""
;
$STARTUPINFO &= "dword cb;long lpReserved;long lpDesktop;long lpTitle;dword dwX;dword dwY;"
$STARTUPINFO &= "dword dwXSize;dword dwYSize;dword dwXCountChars;dword dwYCountChars;"
$STARTUPINFO &= "dword dwFillAttribute;dword dwFlags;short wShowWindow;short cbReserved2;long lpReserved2;"
$STARTUPINFO &= "long hStdInput;long hStdOutput;long hStdError"
$si = DllStructCreate($STARTUPINFO)
If $si = 0 Then
        Msg(@error)
        Exit
EndIf
DllStructSetData($si, "cb", DllStructGetSize($si))
$ptrSI = DllStructGetPtr($si)
;~ Msg($ptrSI)

$PROCESS_INFORMATION &= "long hProcess;long hThread;long dwProcessId;long dwThreadId"
$pi = DllStructCreate($PROCESS_INFORMATION)
If $pi = 0 Then
        Msg(@error)
        Exit
EndIf
$ptrPI = DllStructGetPtr($pi)
;~ Msg($ptrPI)
$tCommandLine = DllStructCreate("wchar[256]")
DllStructSetData($tCommandLine, 1, $ProcessName)
$arrRet = DllCall("advapi32.dll", "long", "CreateProcessWithLogonW", _
                "wstr", $UserName, _
                "wstr", @ComputerName, _
                "wstr", $UserPasswd, _
                "dword", $LOGON_WITH_PROFILE, _
                "ptr", 0, _
                "wstr", $ProcessName, _
                "dword", $CREATE_DEFAULT_ERROR_MODE, _
                "ptr", 0, _
                "ptr", 0, _
                "ptr", $ptrSI, _
                "ptr", $ptrPI)
If $arrRet[0] = 0 Then
        MsgBox(0, "Error in Dllcall", _GetLastError())
        Exit
EndIf

If Not @error And $arrRet[0] <> 0 Then
        $arrRet = DllCall("kernel32.dll", "int", "CloseHandle", "ptr", DllStructGetPtr($pi, "dwProcessId"))
        If Not @error And $arrRet[0] <> 0 Then
                Msg("Return of 'CloseHandle' : " & $arrRet[0])
        Else
                Msg("Error (CloseHandle): " & @error)
                Msg("Return of 'CloseHandle' : " & $arrRet[0])
                Exit 1
        EndIf
Else
        Msg("Error (CreateProcessWithLogonW) : " & @error)
        Msg("Return of 'CreateProcessWithLogonW' : " & $arrRet[0])
        Exit 2
EndIf
Exit 0
;
Func Msg($letexte)
        MsgBox(0x43000, "Message...", $letexte)
EndFunc   ;==>Msg

Func _GetLastError(Const $_iCallerError = @error, Const $_iCallerExtended = @extended)
        Local $aCall = DllCall("kernel32.dll", "dword", "GetLastError")
        Return SetError($_iCallerError, $_iCallerExtended, $aCall[0])
EndFunc   ;==>_WinAPI_GetLastError
发表于 2022-4-26 09:17:31 | 显示全部楼层
个人应用中没发现自带runas函数有什么问题,确定不是自己的问题的话,可以试试 CreateProcessWithLogonW函数

//BOOL CreateProcessWithLogonW(
        //        LPCWSTR               lpUsername,//用户名
        //        LPCWSTR               lpDomain,//帐户的域或服务器的名称
        //        LPCWSTR               lpPassword,//密码
        //        DWORD                 dwLogonFlags,//登录选项
        //        LPCWSTR               lpApplicationName,//要执行的模块的名称
        //        LPWSTR                lpCommandLine,//要执行的命令行
        //        DWORD                 dwCreationFlags,//控制进程创建方式的标志
        //        LPVOID                lpEnvironment,//指向新进程的环境块的指针
        //        LPCWSTR               lpCurrentDirectory,//进程当前目录的完整路径
        //        LPSTARTUPINFOW        lpStartupInfo,//指向STARTUPINFO结构的指针
        //        LPPROCESS_INFORMATION lpProcessInformation//指向PROCESS_INFORMATION结构的指针,该 结构接收新进程的标识信息,包括进程的句柄。
        //);

 楼主| 发表于 2022-4-26 10:20:14 | 显示全部楼层

这段代码看起来不错,待测试后回复。十分感谢大神出马。

 楼主| 发表于 2022-4-26 13:22:43 | 显示全部楼层
tubaba 发表于 2022-4-26 09:17
个人应用中没发现自带runas函数有什么问题,确定不是自己的问题的话,可以试试 CreateProcessWithLogonW函 ...

本地测试可用,域环境还待测试,感谢!结贴。
 楼主| 发表于 2022-4-26 22:34:43 | 显示全部楼层
---------------------------
Error in Dllcall
---------------------------
请求的操作需要提升。
---------------------------
确定   
---------------------------
 楼主| 发表于 2022-4-27 08:28:23 | 显示全部楼层
小盾牌图标的软件不能用,你知道怎么去掉软件图标的盾牌吗?提示:请求的操作需要提升。
发表于 2022-4-27 08:53:27 | 显示全部楼层
这个问题你可以百度.我没有环境测试.但我猜测:
原因是使用LOGON_WITH_PROFILE标志
可能原因是您使用LOGON_WITH_PROFILE标志。在CreateProcessWithLogonW文档的“备注”部分中,您可以阅读以下内容

  
默认情况下,CreateProcessWithLogonW   不加载指定的用户   配置文件到HKEY_USERS注册表   键。这意味着访问   HKEY_CURRENT_USER中的信息   注册表项可能不会产生结果   这与正常情况一致   交互式登录。这是你的   负责加载用户   注册表配置单元之前进入HKEY_USERS   通过调用CreateProcessWithLogonW   使用LOGON_WITH_PROFILE,或者   调用LoadUserProfile函数。

因此,我建议您尝试使用不带LOGON_WITH_PROFILE标记的相同代码。如果您发现这是问题,并且您确实需要使用该标记,则应在代码中使用LoadUserProfile函数和UnloadUserProfile。


或者另一种 用shellexecute提升目标权限


详见https://www.thinbug.com/q/4713196
您需要登录后才可以回帖 登录 | 加入

本版积分规则

QQ|手机版|小黑屋|AUTOIT CN ( 鲁ICP备19019924号-1 )谷歌 百度

GMT+8, 2024-3-29 23:01 , Processed in 0.080028 second(s), 20 queries .

Powered by Discuz! X3.5 Licensed

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表