PowerShell 如何以安全的方式使用密码,运行需要管理员权限的软件
2023-09-14 09:16:40 时间
需求:客户机在普通用户下需要以管理员方式运行 powershell,类似于 runas 命令
如果要以 runas 提权,请参考这里 Win10 Runas 命令 域用户以管理员权限运行_tom.ma的博客-CSDN博客
常规方法
打开 PowerShell ISE工具,运行如下代码能成功以管理员方式打开 powershell,但在实际环境中都不会允许明文密码的出现
$Username = 'administrator'
$Password = 'xielong.cn'
$Pass = ConvertTo-SecureString $Password -AsPlainText -Force
$Credential = New-Object System.Management.Automation.PSCredential -ArgumentList $Username,$Pass
Start-Process powershell.exe -Credential $Credential
安全方法
1、创建 AES,保存D盘下面
# 先生成 32 位的 Key 并保存在文件 aes.key
$keyFile = "d:\aes.key"
$key = New-Object Byte[] 32
[Security.Cryptography.RNGCryptoServiceProvider]::Create().GetBytes($key)
$key | out-file $keyFile
2、创建密码文件
通过 ConvertFrom-SecureString 命令,我们可以把一个 SecureString 对象转换成一个 Encrypted Standard String(加密后的一个字符串),然后保存到文件中。在创建 Credential 时直接使用前面保存的文件,从而避免明文密码在系统中出现。
# 使用 Key 生成并保存密码文件
Read-Host "Enter Password" -AsSecureString | ConvertFrom-SecureString -key $key | Out-File "d:\pwd.txt"
# 使用密码文件创建和 Key 文件创建 Credential 信息
$userName = "administrator"
$passwdFile = "d:\pwd.txt"
$keyFile = "d:\aes.key"
$key = Get-Content $keyFile
$Credential = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $userName, (Get-Content $passwdFile | ConvertTo-SecureString -Key $key)
# 以管理员权限启动 powershell
Start-Process powershell.exe -Credential $Credential
3、把 aes.key 跟 pwd.txt 拷到要运行的客户机D盘下面,然后运行下面脚本
$userName = "administrator"
$passwdFile = "d:\pwd.txt"
$keyFile = "d:\aes.key"
$key = Get-Content $keyFile
$Credential = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $userName, (Get-Content $passwdFile | ConvertTo-SecureString -Key $key)
# 以管理员权限启动 powershell
Start-Process powershell.exe -Credential $Credential
相关文章
- 管家婆软件新建职员账号并设置密码
- Windows Chrome/Edge同步iCloud密码
- CleanMyMacX软件Cracked版本频繁弹密码窗口解决教程
- Web Albums(iPhone) 的蛋疼明文密码
- Linux下SSH免密码登录配置详解
- Redis 密码设置和查看密码的方法
- MySQL密码验证:确保安全性(mysql验证)
- 标题:MySQL:如何设置安全的配置密码(mysql配置文件密码)
- WiFi密码攻防战 手机无线上网知多少
- MySQL查找密码:一个快速指南(mysql查找密码)
- “洋葱”出世 也许你将不再需要账号和密码
- 记忘认SQLServer账号密码的解决办法(sqlserver密码忘)
- MySQL如何快捷更改密码(mysql更改密码)
- MSSQL:密码管理与设置密码过期(mssql设置密码过期)
- 约翰解开奥卡西亚的密码(john破解oracle)
- 使用Redis设置安全密码的简单方法(redis设置密码软件)
- asp.netmembership密码重设