对于Loop High CSRSS CPU

时间:2014-01-31 05:04:02

标签: windows batch-file

我有一个带有输入文件的批处理文件(在这种情况下,该文件是一个平面文本文件,由一组指定的Windows XP系统目录中的文件列表组成)。

然后通过组合使用for循环遍历文本文件中列出的每个文件),使用多个调用命令(在本例中为9)设置变量,并使用find命令在文本文件中搜索文件出现在其他平面文件的输出中。

我遇到的问题是csrss.exe正在使用相当多的CPU处理。在阅读其他一些文章时,似乎csrss与命令行处理有关系。

我想知道的是,如果有更好的方法可以做到这一点:

  1. 缩短整体脚本时间。
  2. 减少CPU负载。
  3. 以下是该脚本的示例:

    :: ------------------------------------------------------
    ::  Running Multiple Commands
    :: ------------------------------------------------------
    
    for /f "delims=?" %%A in (dir-selected_tmp.txt) do (  
    set filepath=%%A  
    call :filepathparse  
    call :md5hashchk  
    call :versionchk  
    call :densitychk  
    call :processchk  
    call :mutexchk  
    call :networkchk  
    call :injectedchk  
    call :persistservicechk  
    call :servicedllchk  
    call :persistrunchk  
    call :persistothchk  
    call :unsigneddllchk  
    call :filesysag  
    )  
    goto :comparison  
    
    :: ####################################################################
    
    :filepathparse
    for %%B in ("%filepath%") do (
    set filename=%%~nxB
    )
    goto :eof
    
    :: ####################################################################
    
    :md5hashchk
    
    for /f "delims= " %%b in ('md5deep.exe "%filepath%"') do set hashvalue=%%b
    goto :eof
    
    :: ####################################################################
    
    :versionchk
    
    for /f "delims= " %%e in ('sigcheck.exe /accepteula -q -n "%filepath%"') do set versionvalue=%%e
    goto :eof
    
    :: ####################################################################
    
    :densitychk
    
    for /f "delims= " %%d in ('densityscout.exe -p 0.1 "%filepath%"') do set densityvalue=%%d
    goto :eof
    
    :: ####################################################################
    
    :processchk
    
    find.exe /I "%filepath%" %temp_outpath%\wmic-processes.txt > nul 2>&1 && goto processexist
    set isprocess=NoActiveProc
    goto :eof
    
    :processexist
    set isprocess=ActiveProc
    goto :eof
    
    :: ####################################################################
    
    :mutexchk
    
    if "%startup_chk%"=="-b" (
    goto :mutexbl
    )       
    
    find.exe /I "%filepath%" %temp_outpath%\filesystem-handles_compared.txt > nul 2>&1 && goto mutexexist
    set ismutex=NoNewActiveMutex
    goto :eof   
    
    :mutexexist
    set ismutex=NewActiveMutex
    goto :eof
    
    :mutexbl
    
    find.exe /I "%filepath%" %parsed_outpath%\filesystem-handles.txt > nul 2>&1 && goto mutexexist
    set ismutex=NoNewActiveMutex
    goto :eof   
    
    :mutexexist
    set ismutex=NewActiveMutex
    goto :eof
    
    :: ####################################################################
    
    :networkchk
    
    find.exe /I "%filename%" %temp_outpath%\tcpvcon_tmp.txt > nul 2>&1 && goto networkexist
    set isnetwork=NoActiveNetwork
    goto :eof
    
    :networkexist
    set isnetwork=ActiveNetwork
    goto :eof
    
    :: ####################################################################
    
    :injectedchk
    
    find.exe /I "%filepath%" %temp_outpath%\injecteddll_tmp.txt > nul 2>&1 && goto injectedexist
    set isinjected=NoActiveInject
    goto :eof
    
    :injectedexist
    set isinjected=ActiveInject
    goto :eof
    
    :: ####################################################################
    
    :persistservicechk
    
    find.exe /I "%filepath%" %temp_outpath%\autoruns-services_tmp.txt > nul 2>&1 && goto serviceexist
    set isservice=NoInstalledService
    goto :eof
    
    :serviceexist
    set isservice=InstalledService
    goto :eof
    
    :: ####################################################################
    
    :servicedllchk
    find.exe /I "%filepath%" %temp_outpath%\registry_hklm_installed_service_dlls_final.txt > nul 2>&1 && goto servicedllexist
    set isservicedll=NoInstalledServiceDLL
    goto :eof
    
    :servicedllexist
    set isservicedll=InstalledServiceDLL
    goto :eof
    
    :: ####################################################################
    
    :persistrunchk
    
    find.exe /I "%filepath%" %temp_outpath%\autoruns-run_tmp.txt > nul 2>&1 && goto runpersistexist
    set isrunpersist=NoRunPersist
    goto :eof
    
    :runpersistexist
    set isrunpersist=RunPersist
    goto :eof
    
    :: ####################################################################
    
    :persistothchk
    
    find.exe /I "%filepath%" %temp_outpath%\autoruns-oth_tmp5.txt > nul 2>&1 && goto persistothexist
    set isothpersist=NoOtherPersist
    goto :eof
    
    :persistothexist
    set ispersist=OtherPersist
    goto :eof
    
    :: ####################################################################
    
    :unsigneddllchk
    
    find.exe /I "%filepath%" %temp_outpath%\listdlls_temp2.txt > nul 2>&1 && goto unsigneddllexist
    set isunsigneddll=NoUnSignedProcDLL
    goto :eof
    
    :unsigneddllexist
    set isunsigneddll=UnSignedProcDLL
    goto :eof
    
    :: ####################################################################
    
    :filesysag
    
    :: Compiling Into Syslog Format 
    
     echo %datestamp%^|%currtime%^|%computername%^|%currip%^|%username%^|%lastlogintime%^|"%filepath%"^|%hashvalue%^|%versionvalue%^|%densityvalue%^|%isprocess%^|%ismutex%^|%isnetwork%^|%isinjected%^|%isservice%^|%isservicedll%^|%isrunpersist%^|%isothpersist%^|%isunsigneddll% >> %syslog_outpath%\%computername%-syslog.txt
    
     :: Compiling Into Parsed Format
    
    echo "%filepath%"^|%hashvalue%^|%versionvalue%^|%densityvalue%^|%isprocess%^|%ismutex%^|%isnetwork%^|%isinjected%^|%isservice%^|%isservicedll%^|%isrunpersist%^|%isothpersist%^|%isunsigneddll% >> %parsed_outpath%\%computername%-filesystem.txt
    goto :eof
    

1 个答案:

答案 0 :(得分:0)

可悲的是,您不愿提供进一步的详细信息意味着我们会严格限制我们提供的帮助。

我建议:

首先,构建一个yu感兴趣的文件路径列表

(for /f "delims=?" %%A in (dir-selected_tmp.txt) do echo %%A )>paths_of_interest.txt

然后构建包含日志数据子集的临时文件

findstr /I /g:paths_of_interest.txt %temp_outpath%\processes.txt >"%temp%\processes.x.txt"

然后根据您的原始方案重新处理,而不是使用paths_of_interest.txt"%temp%\processes.x.txt"

这应该减少重复次数,特别是如果您有长文件。


:: ------------------------------------------------------
::  Running Multiple Commands
:: ------------------------------------------------------

:: Pre-processing

SET /a step=1000
SET /a dirstart=0
SET "dirskip="

:mainloop
SET /a dirsleft=0

SETLOCAL enabledelayedexpansion
(
 for /f "%dirskip%delims=?" %%A in (dir-selected_tmp.txt) do (
  SET /a dirsleft+=1
  IF !dirsleft! leq %step% ECHO %%A
 )
)>temp_dirs.txt
endlocal&SET /a dirsleft=%dirsleft%

FINDSTR /i /l /g:temp_dirs.txt "%temp_outpath%\wmic-processes.txt" >temp_filepathparse.txt
if "%startup_chk%"=="-b" (
 FINDSTR /i /l /g:temp_dirs.txt "%parsed_outpath%\filesystem-handles.txt" >temp_mutexchk.txt
) ELSE (
 FINDSTR /i /l /g:temp_dirs.txt "%temp_outpath%\filesystem-handles_compared.txt" >temp_mutexchk.txt
)
FINDSTR /i /l /g:temp_dirs.txt "%temp_outpath%\tcpvcon_tmp.txt" >temp_networkchk.txt
FINDSTR /i /l /g:temp_dirs.txt "%temp_outpath%\injecteddll_tmp.txt" >temp_injectedchk.txt
FINDSTR /i /l /g:temp_dirs.txt "%temp_outpath%\autoruns-services_tmp.txt" >temp_persistservicechk.txt
FINDSTR /i /l /g:temp_dirs.txt "%temp_outpath%\registry_hklm_installed_service_dlls_final.txt" >temp_servicedllchk.txt
FINDSTR /i /l /g:temp_dirs.txt "%temp_outpath%\autoruns-run_tmp.txt" >temp_persistrunchk.txt
FINDSTR /i /l /g:temp_dirs.txt "%temp_outpath%\autoruns-oth_tmp5.txt" >temp_persistothchk.txt
FINDSTR /i /l /g:temp_dirs.txt "%temp_outpath%\listdlls_temp2.txt" >temp_unsigneddllchk.txt

for /f "delims=" %%A in (temp_dirs.txt) do (  
set "filepath=%%A"  
call :filepathparse

....

call :filesysag  
)  

SET /a dirstart=%dirstart%+%step%
SET /a dirsleft=%dirsleft%-%step%
IF dirsleft gtr 0 SET dirskip=SKIP=%dirstart%&GOTO mainloop

goto :comparison  

然后,只需将每个例程:processchk中的文件名替换为:unsigneddllchk,并在预处理中创建相应的temp_whatever.txt文件。 mutex上的开关已在预处理中完成。

这应该通过在创建的temp_ *文件中仅选择包含目标字符串的那些行来减少find命令的搜索,从而减少整个运行时间。

[编辑以回应所提供的进一步信息]

请注意第一个findstr更改前的预处理块,现在call :filesysag )goto :comparison之间存在后处理块

此处的对象是减小temp_dirs.txt文件的大小。我已经任意选择了1,000(step的大小)但却不知道step应该有多大。它越大,过程就越快,但如果它太大,temp_dirs.txt对于findstr来说太大了。

注意在行endlocal&SET /a dirsleft=%dirsleft%上使用解析技巧,该解析技巧使用解析系统将dirsleft的修改值移出setlocal/endlocal块。