学习资料 · 2009年07月7号 0

ntsd

ntsd从Windows 2000开始就是系统自带的进程调试工具,在system32目录下。NTSD的功能非常的强大,用法也比较复杂,但如果只用来结束一些进程,那就比较简单了。在Windows中只有System、SMSS.EXE和CSRSS.EXE不能杀。前两个是纯内核态的,最后那个是Win32子系统,ntsd本身需要它。lsass.exe也不要杀掉,它是负责本地账户安全的。被调试器附着的进程会随调试器一起退出,所以可以用来在命令行下终止进程。
  打开cmd 后输入以下命令就可以结束进程:
  方法一:利用进程的PID结束进程
  命令格式:ntsd -c q -p pid
  命令范例: ntsd -c q -p 1332 (结束explorer.exe进程)
  范例详解:explorer.exe的pid为1332,但是如何获取进程的pid呢?在CMD下输入TASKLIST就可以获取当前任务管理器所有进程的PID。或者打开任务管理器,在菜单栏,选择“查看”—“选择列”,在打开的选择项窗口中将“PID(进程标识符)”项选择钩上,这样任务管理器的进程中就会多出PID一项了。(PID的分配并不固定,是在进程启动是由系统随机分配的,所以进程每次启动的进程一般都不会一样。)
  可使用以下命令:
  =================================================
  @echo off
  mode con cols=30 lines=5
  color 1e
  echo.
  set /p t=请输入进程名:
  tasklist /fo csv>2.txt
  find “%t%” 2.txt>1.txt
  for /f “delims=, tokens=2” %%i in (1.txt) do set a=%%i
  ntsd -c q -p %a%
  echo PID NAME
  echo ============
  echo %a% %T%
  del 1.txt
  del 2.txt
  pause >nul
  exit
  =================================================
  方法二:利用进程名结束进程
  命令格式:ntsd -c q -pn ***.exe (***.exe 为进程名,exe不能省)
  命令范例:ntsd -c q -pn explorer.exe
  另外的能结束进程的DOS命令还有taskkill和tskill命令:
  命令格式: taskkill /pid 1234 /f ( 也可以达到同样的效果。)
  如果上面这些还不能满足您的求知欲,下面还有:
  ntsd详解
  有一些高等级的进程,tskill和taskkill或许无法结束,那么我们还有一个更强大的工具,那就是系统debug级的ntsd.准确的说,ntsd是一个系统调试工具,只提供给系统开发级的管理员使用,但是对我们杀掉进程还是很爽的.基本上除了WINDOWS系统自己的管理进程,ntsd都可以杀掉。NTSD 调试程序在启动时要求用户指定一个要连接的进程。使用 TLIST 或 PVIEWER,您可以获得某个现有进程的进程 ID,然后键入 NTSD -p pid 来调试这个进程。NTSD 命令行使用如下的句法:
  NTSD [options] imagefile
  其中,imagefile 是要调试的映像名称。
  用法usage: ntsd [-?] [-2] [-d] [-g] [-G] [-myob] [-lines] [-n] [-o] [-s] [-v] [-w]
  [-r BreakErrorLevel] [-t PrintErrorLevel]
  [-hd] [-pd] [-pe] [-pt #] [-pv] [-x | -x{e|d|n|i} ]
  [– | -p pid | -pn name | command-line | -z CrashDmpFile]
  [-zp CrashPageFile] [-premote transport] [-robp]
  [-aDllName] [-c “command”] [-i ImagePath] [-y SymbolsPath]
  [-clines #] [-srcpath SourcePath] [-QR \\machine] [-wake ]
  [-remote transport:server=name,portid] [-server transport:portid]
  [-ses] [-sfce] [-sicv] [-snul] [-noio] [-failinc] [-noshell]
  where: -? displays this help text
  command-line is the command to run under the debugger
  – is the same as -G -g -o -p -1 -d -pd
  -aDllName sets the default extension DLL
  -c executes the following debugger command
  -clines number of lines of output history retrieved by a remote client
  -failinc causes incomplete symbol and module loads to fail
  -d sends all debugger output to kernel debugger via DbgPrint
  -d cannot be used with debugger remoting
  -d can only be used when the kernel debugger is enabled
  -g ignores initial breakpoint in debuggee
  -G ignores final breakpoint at process termination
  -hd specifies that the debug heap should not be used
  for created processes. This only works on Windows Whistler.
  -o debugs all processes launched by debuggee
  -p pid specifies the decimal process Id to attach to
  -pd specifies that the debugger should automatically detach
  -pe specifies that any attach should be to an existing debug port
  -pn name specifies the name of the process to attach to
  -pt # specifies the interrupt timeout
  -pv specifies that any attach should be noninvasive
  -r specifies the (0-3) error level to break on (SeeSetErrorLevel)
  -robp allows breakpoints to be set in read-only memory
  -t specifies the (0-3) error level to display (SeeSetErrorLevel)
  -w specifies to debug 16 bit applications in a separate VDM
  -x sets second-chance break on AV exceptions
  -x{e|d|n|i}
sets the break status for the specified event
  -2 creates a separate console window for debuggee
  -i ImagePath specifies the location of the executables that generated
  the fault (see _NT_EXECUTABLE_IMAGE_PATH)
  -lines requests that line number information be used if present
  -myob ignores version mismatches in DBGHELP.DLL
  -n enables verbose output from symbol handler
  -noio disables all I/O for dedicated remoting servers
  -noshell disables the .shell (!!) command
  -QR < \\machine> queries for remote servers
  -s disables lazy symbol loading
  -ses enables strict symbol loading
  -sfce fails critical errors encountered during file searching
  -sicv ignores the CV record when symbol loading
  -snul disables automatic symbol loading for unqualified names
  -srcpathspecifies the source search path
  -v enables verbose output from debugger
  -wake wakes up a sleeping debugger and exits
  -y specifies the symbol search path (see _NT_SYMBOL_PATH)
  -z specifies the name of a crash dump file to debug
  -zp specifies the name of a page.dmp file
  to use with a crash dump
  -remote lets you connect to a debugger session started with -server
  must be the first argument if present
  transport: tcp | npipe | ssl | spipe | 1394 | com
  name: machine name on which the debug server was created
  portid: id of the port the debugger server was created on
  for tcp use: port=
  for npipe use: pipe=
  for 1394 use: channel=
  for com use: port=,baud=,
  channel=
  for ssl and spipe see the documentation
  example: … -remote npipe:server=yourmachine,pipe=foobar
  -server creates a debugger session other people can connect to
  must be the first argument if present
  transport: tcp | npipe | ssl | spipe | 1394 | com
  portid: id of the port remote users can connect to
  for tcp use: port=
  for npipe use: pipe=
  for 1394 use: channel=
  for com use: port=,baud=,
  channel=
  for ssl and spipe see the documentation
  example: … -server npipe:pipe=foobar
  -premote transport specifies the process server to connect to
  transport arguments are given as with remoting
  Environment Variables:
  _NT_SYMBOL_PATH=[Drive:][Path]
  Specify symbol image path.
  _NT_ALT_SYMBOL_PATH=[Drive:][Path]
  Specify an alternate symbol image path.
  _NT_DEBUGGER_EXTENSION_PATH=[Drive:][Path]
  Specify a path which should be searched first for extensions dlls
  _NT_EXECUTABLE_IMAGE_PATH=[Drive:][Path]
  Specify executable image path.
  _NT_SOURCE_PATH=[Drive:][Path]
  Specify source file path.
  _NT_DEBUG_LOG_FILE_OPEN=filename
  If specified, all output will be written to this file from offset 0.
  _NT_DEBUG_LOG_FILE_APPEND=filename
  If specified, all output will be APPENDed to this file.
  _NT_DEBUG_HISTORY_SIZE=size
  Specifies the size of a server’s output history in kilobytes
  Control Keys:
   Quit debugger
   Break into Target
  
Force a break into debuggee (same as Ctrl-C)
   Debug Current debugger
   Toggle Verbose mode
   Print version information
  ntsd: exiting – press enter —
  选项option:
  -2打开一个用于调试字符模式的应用程序的新窗口
  -d将输出重定向到调试终端-g 使执行自动通过第一个断点
  -G使 NTSD 在子程序终止时立即退出o启用多个进程的调试,默认值为由调试程序衍生的一个进程
  -p指定调试由进程 ID 标识的进程
  -v产生详细的输出。
  例如,假设 inetinfo.exe 的进程 ID 为 104。键入命令“NTSD -p 104”将 NTSD 调试程序连接到 inetinfo 进程 (IIS)。也可使用 NTSD 启动一个新进程来进行调试。例如,NTSD notepad.exe 将启动一个新的 notepad.exe 进程,并与它建立连接。一旦连接到某个进程,您就可以用各种命令来查看堆栈、设置断点、转储内存,等等。
  命令含义~显示所有线程的一个列表KB 显示当前线程的堆栈轨迹~*KB显示所有线程的堆栈轨迹R显示当前
  帧的寄存器输出U反汇编代码并显示过程名和偏移量D[type][< range>]转储内存BP设置断点BC[]清除一个或多个断点BD[]禁用一个或多个断点BE[< bp>]启用一个或多个断点BL[]列出一个或多个断点。
  个人意见,有一个非常重要的参数就是-v参数,我们可以通过它发现一个进程下面挂接了哪些连接库文件。有很多病毒,木马,或者恶意软件,都喜欢把自己做成动态库,然后注册到系统正常程序的加载库列表中,达到隐藏自己的目的.
  首先我们需要设置一下ntsd的输出重定向,最好是重定向到一个文本文件,方便我们分析研究.
  c:\>set _NT_DEBUG_LOG_FILE_APPEND=c:\pdw.txt
  注意,虽然输出重定向了,但是我们的输出依然会继续显示在屏幕上,而且会进入到debug模式,我们使用-c q参数,就可以避免这个问题.
  c:\>ntsd -c q -v notepad.exe
  现在我们的pdw.txt文件中,就可以看见notepad.exe文件的调试信息.
  可以知道,ntsd的软件终止能力是很好很强大的,一些taskkill都无法终止的软件(如Student.exe这一类或木马)可以用ntsd轻易终止