웬디의 기묘한 이야기

글 작성자: WENDYS
반응형

user mode process debugging

현재 동작중이거나 동작 하려는 프로세스를 디버깅 하는 방법 입니다.

기본적으로 프로세스 디버깅에는 ollydbg나 IDA를 이용하는게 일반적이지만, WinDbg와 친해지기 위해 프로세스 디버깅을 해보겠습니다.

더불어 WinDbg를 통하여 프로세스를 디버깅하면서 커널까지 어떻게 들어가서 동작하는지도 알아볼 수 있습니다.

단, 분석은 본인 몫 입니다


사용되는 명령어

!process (process 정보 출력)

.process (set process context)

bp (break point)

bm (wildcard를 지원하는 break point)

bc (break point clear)

start

오늘의 타겟 툴은 cmd.exe 입니다!

콘솔창의 동작을 한번 디버깅 해보도록 하겠습니다.


먼저 !process 명령어를 이용하여 cmd.exe 프로세스의 정보를 보도록 하겠습니다.

!process process_id flags 형태입니다.

process id 부분에 0을 입력하면 모든 프로세스를 보여달라는 의미이며, 특정 process의 id를 입력해도 됩니다.

flag 는 0은 간단히 보여주기 모드 이며, 0x1, 0x2, 0x4 등의 옵션으로 더욱 상세한 정보를 볼 수 있습니다. 보통 0x7을 주면 매우 상세한 정보를 볼 수 있습니다.


저희가 필요한건 EPROCESS의 정보이므로 간단한 정보만 있으면 됩니다.


kd> !process 0 0 cmd.exe

PROCESS ffffe0000391b080

    SessionId: 1  Cid: 36d4    Peb: 7ff719c85000  ParentCid: 04b8

    DirBase: 188a4000  ObjectTable: ffffc00007387040  HandleCount: <Data Not Accessible>

    Image: cmd.exe



간단한 정보를 획득한 후 해당 process context로 변경합니다.
/i 옵션을 사용하여 프로세스 컨텍스트가 타겟 프로세스로 맞춰지고 디버깅 권한을 갖게 됩니다.

kd> .process /i ffffe0000391b080

You need to continue execution (press 'g' <enter>) for the context

to be switched. When the debugger breaks in again, you will be in

the new process context.

kd> g



cmd.exe가 동작중이기에 바로 달라붙으며 break point가 잡히게 됩니다.

system이 아닌 특정 프로세스에 디버깅 중인 경우 !process 명령어를 통하여 상세 정보를 확인할 수 있습니다.


Break instruction exception - code 80000003 (first chance)

nt!DbgBreakPointWithStatus:

fffff800`751d5890 cc              int     3

kd> !process

PROCESS ffffe0000391b080

    SessionId: 1  Cid: 36d4    Peb: 7ff719c85000  ParentCid: 04b8

    DirBase: 188a4000  ObjectTable: ffffc00007387040  HandleCount: <Data Not Accessible>

    Image: cmd.exe

    VadRoot ffffe00002836af0 Vads 23 Clone 0 Private 103. Modified 0. Locked 8.

    DeviceMap ffffc0000163dea0

    Token                             ffffc00003518060

    ElapsedTime                       00:03:19.878

    UserTime                          00:00:00.000

    KernelTime                        00:00:00.000

    QuotaPoolUsage[PagedPool]         24512

    QuotaPoolUsage[NonPagedPool]      3120

    Working Set Sizes (now,min,max)  (608, 50, 345) (2432KB, 200KB, 1380KB)

    PeakWorkingSetSize                608

    VirtualSize                       11 Mb

    PeakVirtualSize                   11 Mb

    PageFaultCount                    619

    MemoryPriority                    BACKGROUND

    BasePriority                      8

    CommitCharge                      360


        THREAD ffffe000040eb880  Cid 36d4.36d0  Teb: 00007ff719c8e000 Win32Thread: 0000000000000000 WAIT: (Executive) KernelMode Alertable

            ffffe00000966108  NotificationEvent


cmd.exe process에 잘 붙어있는걸 확인했습니다.
이제 cmd.exe의 디버깅을 시작해보겠습니다.

cmd에선 어떤 함수들을 만들어서 사용할까요?? kernel32, ntdll, user32 등의 API가 아닌 cmd.exe에서 구현한 함수를 알아내는(?) 방법입니다.

bm은 은 wildcard 문자를 지원하는 break point 명령어 입니다.

kd> bm cmd!*
  1: 00007ff7`19da3528 @!"cmd!IsDriveLocked"
  2: 00007ff7`19dac69c @!"cmd!SpaceCat"
  3: 00007ff7`19dc681c @!"cmd!PipeErr"
  4: 00007ff7`19dc6370 @!"cmd!SetCtrlC"
  5: 00007ff7`19dc85bc @!"cmd!CmpName"
  6: 00007ff7`19dbaa14 @!"cmd!_raise_securityfailure"
  7: 00007ff7`19dafb54 @!"cmd!PutStdOut"
  8: 00007ff7`19dbad40 @!"cmd!FindPESection"
  9: 00007ff7`19dc5074 @!"cmd!GetRegValue"
 10: 00007ff7`19da27d0 @!"cmd!StringCchCatW"
 11: 00007ff7`19da5f50 @!"cmd!PopStr"
 12: 00007ff7`19db0b90 @!"cmd!_delayLoadHelper2"
 13: 00007ff7`19da7fe0 @!"cmd!eParen"
 14: 00007ff7`19dbb0a8 @!"cmd!local_unwind"
 15: 00007ff7`19dc7c74 @!"cmd!write_bytes"
 16: 00007ff7`19dc7fac @!"cmd!KillProc"
 17: 00007ff7`19da7db0 @!"cmd!BatProc"
 18: 00007ff7`19db5ee0 @!"cmd!WriteFmtString"
 19: 00007ff7`19da44a0 @!"cmd!ParseRedir"
 20: 00007ff7`19daf680 @!"cmd!OpenScreen"
 21: 00007ff7`19dc48e4 @!"cmd!ExecError"
 22: 00007ff7`19db034c @!"cmd!eDelete"
 23: 00007ff7`19dc6b80 @!"cmd!eColor"
 24: 00007ff7`19db4db0 @!"cmd!FillToCol"
 25: 00007ff7`19da181c @!"cmd!ExecPgm"
 26: 00007ff7`19da14f8 @!"cmd!WaitProc"
 27: 00007ff7`19da29c4 @!"cmd!FindFirst"
 28: 00007ff7`19dad410 @!"cmd!AParseShiftExpr"
 29: 00007ff7`19db5b9c @!"cmd!NewDisplayFileListHeader"
 30: 00007ff7`19da3ab0 @!"cmd!Lex"
 31: 00007ff7`19dc41a8 @!"cmd!eAppend"
 32: 00007ff7`19da8198 @!"cmd!eEcho"
*******************************************************************************
* You have attempted to enable   33 KD breakpoints, which exceeds the         *
* currently supported limit of   32 breakpoints for Windows kernel debugging. *
* Breakpoints exceeding this limit are ignored.                               *
* Please disable/clear breakpoints until you are within the supported limit.  *
*******************************************************************************
 33: 00007ff7`19dc8da0 @!"cmd!CompleteDir"
*******************************************************************************
* You have attempted to enable  444 KD breakpoints, which exceeds the         *
* currently supported limit of   32 breakpoints for Windows kernel debugging. *
* Breakpoints exceeding this limit are ignored.                               *
* Please disable/clear breakpoints until you are within the supported limit.  *
*******************************************************************************
444: 00007ff7`19da1f30 @!"cmd!memset"
*******************************************************************************
* You have attempted to enable  445 KD breakpoints, which exceeds the         *
* currently supported limit of   32 breakpoints for Windows kernel debugging. *
* Breakpoints exceeding this limit are ignored.                               *
* Please disable/clear breakpoints until you are within the supported limit.  *
*******************************************************************************
445: 00007ff7`19dad380 @!"cmd!AParseAndLogExpr"

bp는 최대 32개까지만 가능하며, 그 이후의 bp는 모두 무시된다고 합니다.

실제 cmd를 구현하기 위해서 수많은 함수가 구현되어있네요
그런데 어떻게 이런 함수명이 보이는걸까요??

MS에선 윈도우 기본 응용프로그램에 대한 debug symbol도 제공되기 때문에 윈도우 기본 게임, cmd 등 MS의  다양한 프로세스에 대한 디버깅이 가능합니다.


실제로 pdb 등 symbol이 없으면 0x12345678 등의 주소지만 보이게 되므로 디버깅이 매우 힘들어집니다.


현재 위의 모든 함수에 break point가 걸려있기때문에 bc 명령어를 이용하여 정리를 하도록 합니다.

kd> bc *


cmd!cmd_printf 함수에 bp를 걸어줍니다.


kd> bp cmd!cmd_printf

kd> bl

 0 e 00007ff7`19da7c88     0001 (0001) cmd!cmd_printf


kd> g


이제 bp가 걸려있으니 해당 함수가 호출되도록 해야겠죠
cmd.exe 프로세스 에서 아무 글자나 치고 enter 치시면 두둥!! 다음 스크린샷에서와 같이 cmd!cmd_printf에 break point가 걸리게 됩니다.


이제 분석을 시작하시면 됩니다!





반응형