前言
Get-InjectedThread.ps1是一个可以扫描系统上的活动线程可疑的起始地址的脚本。用户可以利用它扫描其网络中的主机,并快速识别许多内存常驻恶意软件技术。该脚本通过使用NtQueryInformationThread函数查询每个活动线程来检索其起始地址。然后,使用VirtualQueryEx函数查询起始地址,以确定相关的节点属性。如果线程启动的存储器区域是未回调的并且是可执行的,即不是映像类型并且具有执行位设置,那么该线程就被认为是注入的。
Get-InjectedThread原理
- 使用ProcessId 为0(所有进程)并将Flag参数设置为4(TH32CS_SNAPTHREAD)调用Create Toolhelp Snapshot。这将返回所有当前正在运行的线程的快照。
- 使用Thread32First和Thread32Next处理快照中的所有线程。
以下每个步骤都将在每个线程上执行:
- 调用OpenThread以接收内核中的Thread对象的句柄。
- 使用Thread句柄,为ThreadInformationClass参数指定值为9(ThreadQuerySetWin32StartAddress)的NtQueryInformationThread。这将返回线程的内存起始地址。
- 调用OpenProcess来接收当前线程拥有进程的句柄。
- 将进程句柄和线程起始地址传递给VirtualQueryEx以查询目标内存页面。这将返回一个MEMORY_BASIC_INFORMATION结构。
- 检查返回结构中的State和Type字段。
- 所有线程的状态应该是MEM_COMMIT
- 所有线程的类型应为MEM_IMAGE
- 如果Type不等于MEM_IMAGE,那么你有一个正在运行代码的线程,这个线程不会被磁盘上的文件(又名注入)所支持。
环境搭建
win10 1903
powershell 5.1
powershell PackageManagement
PowershellGet
PowershellGet安装
Installing PowerShellGet on Windows
PowershellGet的安装非常重要 不然后面脚本会抛异常:
设置脚本运行权限
Set-ExecutionPolicy RemoteSigned
PowerShellGet和NuGet安装
Install-Module PowerShellGet -AllowClobber -Force Install-PackageProvider -Name NuGet -Force
PSReflect-Functions模块安装
Install-Module PowerShellGet -AllowClobber -Force Install-Module -Name PSReflect-Functions
TLS版本设置
[Net.ServicePointManager]::SecurityProtocol = [Net.ServicePointManager]::SecurityProtocol -bor [Net.SecurityProtocolType]::Tls12
设置PSGallery仓库可信任
Set-PSRepository -Name PSGallery -InstallationPolicy Trusted
脚本下载
https://gist.github.com/jaredcatkinson/23905d34537ce4b5b1818c3e6405c1d2
设置Powershell权限
Set-ExecutionPolicy RemoteSigned
导入模块
Import-Module .\Get-InjectedThread.ps1 Import-Module .\Get-MemorySectionContent.ps1 Import-Module .\New-InjectedThread.ps1 Import-Module .\Stop-Thread.ps1
创建一个被注入的线程-New-InjectedThread
Ps>New-InjectedThread
Ps>.\New-InjectedThread.ps1
注意:这种执行脚本需要在 ps中添加 执行内容即 文件尾部添加一行 执行的函数
执行检测-Get-InjectedThread
Ps>Get-InjectedThread
注意:这种执行脚本需要在 ps中添加 执行内容即 文件尾部添加一行 执行的函数