PowerShell 配置文件后门 | Windows 后门系列

admin 2024年1月7日08:01:01评论32 views字数 2789阅读9分17秒阅读模式

cmd 没有类似于 bash 的配置文件,但是 powershell 是有的

https://learn.microsoft.com/zh-cn/powershell/module/microsoft.powershell.core/about/about_profiles?view=powershell-7.4

PowerShell 控制台支持以下基本配置文件。配置文件按照执行顺序列出。

  • 所有用户,所有主机
    • Windows - $PSHOMEProfile.ps1
    • Linux - /opt/microsoft/powershell/7/profile.ps1
    • macOS - /usr/local/microsoft/powershell/7/profile.ps1
  • 所有用户,当前主机
    • Windows - $PSHOMEMicrosoft.PowerShell_profile.ps1
    • Linux - /opt/microsoft/powershell/7/Microsoft.PowerShell_profile.ps1
    • macOS - /usr/local/microsoft/powershell/7/Microsoft.PowerShell_profile.ps1
  • 当前用户,所有主机
    • Windows - $HOMEDocumentsPowerShellProfile.ps1
    • Linux - ~/.config/powershell/profile.ps1
    • macOS - ~/.config/powershell/profile.ps1
  • 当前用户,当前主机
    • Windows - $HOMEDocumentsPowerShellMicrosoft.PowerShell_profile.ps1
    • Linux - ~/.config/powershell/Microsoft.PowerShell_profile.ps1
    • macOS - ~/.config/powershell/Microsoft.PowerShell_profile.ps1

$PROFILE 自动变量存储当前会话中可用的 PowerShell 配置文件的路径。

若要查看配置文件路径,请显示 $PROFILE 变量的值。还可以在命令中使用 $PROFILE 变量来表示路径。

$PROFILE 变量存储“当前用户,当前主机”配置文件的路径。其他配置文件保存在 $PROFILE 变量的注释属性中。

例如,$PROFILE 变量在 Windows PowerShell 控制台中具有以下值。

  • 当前用户,当前主机 - $PROFILE
  • 当前用户,当前主机 - $PROFILE.CurrentUserCurrentHost
  • 当前用户,所有主机 - $PROFILE.CurrentUserAllHosts
  • 所有用户,当前主机 - $PROFILE.AllUsersCurrentHost
  • 所有用户,所有主机 - $PROFILE.AllUsersAllHosts

由于每个用户和每个主机应用程序中 $PROFILE 变量的值发生更改,因此请确保在所使用的每个 PowerShell 主机应用程序中显示配置文件变量的值。

若要查看 $PROFILE 变量的当前值,请键入:

PowerShell

$PROFILE | Select-Object *
PowerShell 配置文件后门 | Windows 后门系列
AllUsersAllHosts       : C:WindowsSystem32WindowsPowerShellv1.0profile.ps1
AllUsersCurrentHost : C:WindowsSystem32WindowsPowerShellv1.0Microsoft.PowerShell_profile.ps1
CurrentUserAllHosts : C:UsersAdministratorDocumentsWindowsPowerShellprofile.ps1
CurrentUserCurrentHost : C:UsersAdministratorDocumentsWindowsPowerShellMicrosoft.PowerShell_profile.ps1

这些配置文件中都可以类似 Bash 配置文件一样,在其中放置后门程序

PowerShell 配置文件后门 | Windows 后门系列

默认情况下都不存在这些文件

接下来进行试验

创建 C:WindowsSystem32WindowsPowerShellv1.0profile.ps1 输出字符 I am a Backdoor

Write-Host "I am a Backdoor"
PowerShell 配置文件后门 | Windows 后门系列

cmd 中输入 powershell 进入 powershell

PowerShell 配置文件后门 | Windows 后门系列

创建 C:WindowsSystem32WindowsPowerShellv1.0Microsoft.PowerShell_profile.ps1

输出字符 I am the second Backdoor

PowerShell 配置文件后门 | Windows 后门系列

powershell 中输入 powershell 进入新的 powershell

PowerShell 配置文件后门 | Windows 后门系列

创建 C:UsersAdministratorDocumentsWindowsPowerShellprofile.ps1

输出 I am the third Backdoor

发现连 WindowsPowerShell 这个目录都没有,创建目录及文件

PowerShell 配置文件后门 | Windows 后门系列
PowerShell 配置文件后门 | Windows 后门系列

powershell 中输入 powershell 进入新的 powershell

PowerShell 配置文件后门 | Windows 后门系列

创建 C:UsersAdministratorDocumentsWindowsPowerShellMicrosoft.PowerShell_profile.ps1

输出 I am the fourth Backdoor

刚才已经创建了目录,现在直接创建文件了

PowerShell 配置文件后门 | Windows 后门系列

powershell 中输入 powershell 进入新的 powershell

PowerShell 配置文件后门 | Windows 后门系列

这四个配置文件均可正常使用

尝试重启电脑,再次进入 powershell

PowerShell 配置文件后门 | Windows 后门系列

仍然有效

现在有一个疑问,如果不是进入 powershell 控制台,直接执行正常的 powershell 脚本会执行吗

编写一个向控制台输出 Hello World 的脚本,同时弹出消息框的脚本 demo.ps1

Write-Host "Hello World"

# 弹出一个消息框
Add-Type -AssemblyName PresentationFramework
[System.Windows.MessageBox]::Show("Hello, World!")

在第一个后门文件中额外插入powershell 代码,将 I am a Backdoor 写入到桌面的 backdoor.txt

PowerShell 配置文件后门 | Windows 后门系列

先是在 cmd 中进行测试

powershell ./demo.ps1
PowerShell 配置文件后门 | Windows 后门系列
PowerShell 配置文件后门 | Windows 后门系列

删除 backdoor.txt 图形化右键执行 demo.ps1

PowerShell 配置文件后门 | Windows 后门系列
PowerShell 配置文件后门 | Windows 后门系列
PowerShell 配置文件后门 | Windows 后门系列

也就是说这类后门对所有的 powershell 程序有效

往期文章

PowerShell 配置文件后门 | Windows 后门系列

有态度,不苟同

原文始发于微信公众号(NOP Team):PowerShell 配置文件后门 | Windows 后门系列

  • 左青龙
  • 微信扫一扫
  • weinxin
  • 右白虎
  • 微信扫一扫
  • weinxin
admin
  • 本文由 发表于 2024年1月7日08:01:01
  • 转载请保留本文链接(CN-SEC中文网:感谢原作者辛苦付出):
                   PowerShell 配置文件后门 | Windows 后门系列http://cn-sec.com/archives/2372120.html

发表评论

匿名网友 填写信息