【问题标题】:Is it possible to query a file or process hash using wmi?是否可以使用 wmi 查询文件或进程哈希?
【发布时间】:2020-06-23 02:39:27
【问题描述】:

我搜索了任何查询进程哈希的方法。 假设我能够使用 Win32_Process 检索 ExecutablePath, 我想查询文件的哈希。

我试图避免使用 powershell,但要实现与“Get-FileHash”相同的功能。

谢谢!

编辑: 我尝试使用提供 md5checksum 的 win32_filespecification,问题是我找不到相关文件(例如 notepad.exe)。

【问题讨论】:

标签: powershell wmi


【解决方案1】:

这是一个要测试的批处理文件:

@echo off
Title Get Notepad Hash
Set "App_Path=%windir%\system32\notepad.exe"
echo "%App_Path%"
@for /f "tokens=2 skip=3 delims= " %%a in ('Powershell Get-FileHash "%App_Path%"') do echo SHA256=%%a
Pause & Exit

编辑: 在命令行中使用 WMIC 和 Certutil 获取进程文件哈希

你可以试试第二个批处理文件:

@echo off
cls & color 9E & Mode 95,5
Title Get Process File Hash using WMIC and Certutil in command line 
Set "TmpFile=%~dpn0_Tmp.txt"
Set "LogPathExe=%~dpn0_PathExe.txt"
Set "Hashes=%~dpn0_Hashes.txt"

echo(
echo(                ===========================================================
echo(                    Please wait a while ... Working is in progress....
echo(                ===========================================================

Setlocal EnableDelayedExpansion
> "!TmpFile!" (
    @for /f "delims=" %%a in ('wmic process get ExecutablePath /format:list') do (
        @For /F "tokens=2 delims==" %%b in ("%%a") do (
            set "Exe=%%b"
                If not defined Exe Set !Exe! 
                    echo "!Exe!"
        )
    )
)

Call :RemoveDuplicateEntry "!TmpFile!" "!LogPathExe!"
Del "!TmpFile!" 

> "!Hashes!" (
    @for /f "delims=" %%a in ('Type "!LogPathExe!"') do (
        @for /f "skip=1 delims=" %%H in ('CertUtil -hashfile "%%~a" SHA256 ^| findstr /i /v "CertUtil"') do set "H=%%H"
            echo %%a=!H: =!
        )
    )
)

If Exist "!Hashes!"  Start "" "!Hashes!"  & Exit
::----------------------------------------------------
:RemoveDuplicateEntry <InputFile> <OutPutFile>
Powershell  ^
$Contents=Get-Content '%1';  ^
$LowerContents=$Contents.ToLower(^);  ^
$LowerContents ^| select -unique ^| Out-File '%2'
Exit /b
::----------------------------------------------------

检查 virustotal 哈希值的第三批脚本:

@echo off
cls & color 9E & Mode 95,5
Title Get Process File Hash using WMIC and Certutil in command line 
Set "TmpFile=%~dpn0_Tmp.txt"
Set "LogPathExe=%~dpn0_PathExe.txt"
Set "Hashes=%~dpn0_Hashes.txt"
Set "Hash2Check_VirusTotal=%~dpn0_Hash2Check_VirusTotal.txt"
If Exist "%Hash2Check_VirusTotal%" Del "%Hash2Check_VirusTotal%"

echo(
echo(                ===========================================================
echo(                    Please wait a while ... Working is in progress....
echo(                ===========================================================

Setlocal EnableDelayedExpansion
> "!TmpFile!" (
    @for /f "delims=" %%a in ('wmic process get ExecutablePath /format:list') do (
        @For /F "tokens=2 delims==" %%b in ("%%a") do (
            set "ExecutablePath=%%b"
                If not defined ExecutablePath Set !ExecutablePath! 
                    echo "!ExecutablePath!"
        )
    )
)

Call :RemoveDuplicateEntry "!TmpFile!" "!LogPathExe!"
Del "!TmpFile!" 

> "!Hashes!" (
    @for /f "delims=" %%a in ('Type "!LogPathExe!"') do (
        @for /f "skip=1 delims=" %%H in ('CertUtil -hashfile "%%~a" SHA256 ^| findstr /i /v "CertUtil"') do set "H=%%H"
            echo %%a=!H: =!
            >> "!Hash2Check_VirusTotal!" echo https://www.virustotal.com/old-browsers/file/!H: =!
        )
    )
)

cls
Echo(
Echo( Did you want to check the executable on Virustotal ? Just Type "Y" Or any key to Quit !
Set /p "Check="
If /I [!check!] EQU [Y] (
    @for /f "delims=" %%a in ('Type "!Hash2Check_VirusTotal!"') do ( Start "Chek SHA256 on VIRUSTOTAL" %%a & Timeout /T 10 /nobreak>nul)
) else (
    If Exist "!Hashes!"  Start "" "!Hashes!"  & Exit
)
Exit
::----------------------------------------------------
:RemoveDuplicateEntry <InputFile> <OutPutFile>
Powershell  ^
$Contents=Get-Content '%1';  ^
$LowerContents=$Contents.ToLower(^);  ^
$LowerContents ^| select -unique ^| Out-File '%2'
Exit /b
::----------------------------------------------------

【讨论】:

  • 感谢您的回答,这正是我试图避免的。我正在尝试仅使用 WMI 来完成此操作。
  • 好的,在这种情况下edit your question 并发布您的旧代码!
  • 我还没有编码...我做的最好的是:"get-wmiobject -class Win32_FileSpecification -ComputerName "LocalHost" -Namespace "root\CIMV2"" notepad.exe 在导出数据
  • @YotamMazurik 检查我的上一次编辑和上一次批处理脚本代码!
猜你喜欢
  • 2020-02-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-03-25
  • 2022-07-06
  • 1970-01-01
  • 2018-01-24
  • 2010-10-15
相关资源
最近更新 更多