운영체제 & kernel

Driver 개발환경 만드는 방법

Bithub 2024. 2. 25. 16:41
728x90

본 포스트는 Windows drive 개발환경을 세팅하는 방법을 설명한다.

 

1. Visual Studio와 SDK, WDK 설치

https://bithub.tistory.com/22

 

Visual Studio SDK, WDK 설치 방법

본 포스트는 Visual Studio SDK, WDK 설치 방법을 설명한다. 설명은 Visual Studio 2022 기준으로 진행한다. Visual Studio 2022 외 다른 버전의 경우 설치해야 할 SDK, WDK 버전이 다를 수 있으니 주의하자. SDK 설치

bithub.tistory.com

 


2. 개발용 가상머신 만들기

https://bithub.tistory.com/23

 

Windows Driver 개발용 가상머신 만들기

본 포스트는 Windows Driver 개발용 가상머신 만드는 방법을 소개한다. 컴퓨터 윈도우 버전 확인 가상머신 OS로 설치될 Windows iso파일을 준비해야 되는데 host computer의 windows 버전과 일치하는 버전을

bithub.tistory.com


3. Win Debug 설치

아래 링크에서 Win Debug를 설치해준다.

https://learn.microsoft.com/ko-kr/windows-hardware/drivers/debugger/

 

WinDbg 설치 - Windows drivers

Windows 디버거 및 WinDbg 설치에 대한 개요는 여기에서 시작합니다.

learn.microsoft.com

 

 


4. Secure Boot 끄기

Powershell을 관리자 권한으로 실행해

Confirm-SecureBootUEFI를 실행해보자

False라고 뜨면 Secure Boot가 꺼져있고 계속 진행해도 된다.

만약 True라고 뜬다면 컴퓨터를 재부팅하고 BIOS 세팅에 들어가 Secure Boot를 꺼주도록 한다.


5. Kernel Debugging 켜기

 

Powershell에서 두 명령어를 입력해준다.

  • bcdedit /debug on
  • bcdedit /dbgsettings serial debugport:1 baudrate:115200

6. WinDbg 설정

WinDbg를 관리자 권한으로 실행해준다.

Start debugging - Attach to kernel - COM에서 위와 같이 설정해준다.

OK 클릭.

 

 

Command 창에 위와 같이 뜨는 것을 확인할 수 있다.

 

아까 만든 Virtual Machine을 켜보자.

 

WinDbg에서 serial port 연결이 잘되었음을 알 수 있다.

 


7. KDMapper 설치

KDMapper는 우리가 개발할 driver를 mapping해주는 툴이다.

https://github.com/TheCruZ/kdmapper

 

GitHub - TheCruZ/kdmapper: KDMapper is a simple tool that exploits iqvw64e.sys Intel driver to manually map non-signed drivers i

KDMapper is a simple tool that exploits iqvw64e.sys Intel driver to manually map non-signed drivers in memory - TheCruZ/kdmapper

github.com

위 링크에서 소스 코드를 받고 Visual Studio로 kdmapper.sln을 열어준다.

main.cpp 파일에서 wmain() 함수를 약간 수정할 것이다.

모든 return구문 전에 다음 statement를 추가해주자.

디버깅을 용이하게 만들어준다.

std::cin.get();

 

수정후 빌드를 해주자.

Debug Option을 Relase, x64로 설정해주고 Build - Build Solution 클릭.

 

/kdmapper/kdmapper-master/x64/Release 폴더에 exe 파일이 빌드될 것이다.


8. Visual Studio 프로젝트 설정

 

새 프로젝트를 생성한다.

Blank Solution을 생성해보자.

 

 

Solution에 blank project를 추가해보자.

프로젝트의 이름은 user mode를 따서 um.

 

 

먼저 솔루션 빌드에서 Show All Files 아이콘을 눌러 필터를 꺼주고 실제로 있는 폴더 및 파일만 보이게 하자.

 

그리고 Project Properties로 들어가 프로젝트 설정을 위와 같이 바꿔주자

위에서 UAC Execution Level을 requireAdministrator로 설정해주자. Driver는 관리자 권한이 필요하기 때문이다.

 

 

다음으로 C/C++ - Code Generation에서 Runtime Library를 /MTd로 설정해주자.

이 옵션은 여러 플랫폼에서 dependency에 구애 받지 않고 executable이 실행되게 해준다.

자, 이제 um 프로젝트에 src 폴더를 만들고 main.cpp를 추가해주자.

main.cpp에는 아래와 같이 Hello World! 출력 코드를 작성한다.

#include <iostream>

int main() {
	std::cout << "Hello World!\n";

	return 0;
}

 

이제 kernel mode 프로젝트를 만들 차례다.

 

다음으로 솔루션에 또 다른 프로젝트를 생성해주자. 프로젝트는 Kernel Mode Drive, Empty로 지정해주자.

프로젝트의 이름은 kernel mode를 따서 km.

 

 

프로젝트의 속성을 위와 같이 설정해준다.

 

C/C++ - Code Generation에서 Security Check을 disable security check 해준다. 

KDmapper가 driver를 load하기 위해서 반드시 disable해야 한다.

 

 

Linker - Advanced 탭에서 Entry Point를 FxDriverEntry에서 DriverEntry로 바꿔주자.

 

 

이제 src 폴더를 만들고 src 폴더 안에 main.cpp를 추가해주자.

main.cpp에 아래와 같이 Hello World 출력 코드를 작성한다.

 

#include <ntifs.h>

void debug_print(PCSTR text) {
	KdPrintEx((DPFLTR_IHVDRIVER_ID, DPFLTR_INFO_LEVEL, text));
}

NTSTATUS DriverEntry() {
	debug_print("[+] Hello World");
	return STATUS_SUCCESS;
}

 

 

이제 Compile option을 Debug, x64로 해두고 build해보자.

 

 

 

Build가 성공적으로 되면 프로젝트 폴더에 build 폴더에 km.sys와 um.exe가 생성되었을 것이다.

 

여기까지 성공했으면 축하한다.

첫 kernel driver을 완성해보았다.