Debugging/Windows

2. Testing a Sample Driver

haewon83 2022. 8. 13. 23:59

1. Sample Driver 작성

Visual Studio를 실행(Visual Studio 관리자 권한으로 실행해야 Cryptographic Exception  발생하지 않음)하고, Empty WDM Driver 프로젝트 생성

프로젝트 > Driver Files 폴더에 있는 sample.inf 파일 제거

프로젝트 선택 -> 속성 -> C/C++ -> Code Generation -> Sepctre Mitigation Disabled 변경하고 다시 Build

Sample Code

#include <ntddk.h>

NTSTATUS DriverEntry(PDRIVER_OBJECT pDrvObj, PUNICODE_STRING pRegistryPath) {
	pDrvObj = pDrvObj;
	pRegistryPath = pRegistryPath;
	return STATUS_UNSUCCESSFUL;
}

Code build 후에, sample.pdb 파일은 c:\sym 폴더에 복사

sample.sys 파일은 Debuggee가 될 VM 내부의 c:\sample 폴더에 복사

 

2. Sample Driver 등록

VM 내에서 아래 명령어로 sample.sys 드라이버를 서비스로 등록

c:\> sc create sample binpath= c:\sample\sample.sys type= kernel start= demand

 

3. Remote Debugging

Host에서 Windbg를 실행하고, 이전 Step을 참고하여 VM에 Debugger를 연결

sample driver의 entry point를 breakpoint로 설정

kd> bp sample!DriverEntry # Breakpoint 설정

 

Breakpoint를 설정하고 g 명령어로 go 수행

 

VM 내에서 sample.sys 드라이버 시작

c:\> sc start sample

 

breakpoint에 걸리는 것을 확인