2016-07-11

How to "hello world" Legacy Kernel Driver

How to "hello world" Legacy Kernel Driver


Preinstalled

  1. Visual Studio 2013
  2. WDK 8.1
  3. Windows 2012 R2
  4. Debug View



Step

  1. start Visual Studio.
  2. Click on the FILE > NEW > Proejct. then Pop up a ‘new Project’ wizard.
  3. Choose the visual C++  > Windows driver > Empty WDM driver Project.
  4. You could see the two projects on the solution explorer, The one is your own name, and the ohter one is package project for your driver
  5. Select ‘Source files’ on the Solution Explorer that in the right side.
  6. Click Mouse right button and click ‘add’ > ‘new item’. then Pop up a ‘new file’ wizard.
  7. Choose the visual C++  > C++ file.
  8. Copy a following basis of source code, and paste it on the ‘ new c++ file’ that you have created.
helloworld.cpp
#include <ntddk.h>


VOID Unload(
_In_ struct _DRIVER_OBJECT *DriverObject
)
{
UNREFERENCED_PARAMETER(DriverObject);

DbgPrintEx(DPFLTR_DEFAULT_ID, 0xFFFFFFFF, "GoodBye Windows Kernel\n");

return;
}

NTSTATUS DriverEntry(
_In_ struct _DRIVER_OBJECT *DriverObject,
_In_ PUNICODE_STRING       RegistryPath)
{
NTSTATUS status = STATUS_SUCCESS;
UNREFERENCED_PARAMETER(RegistryPath);

DbgPrintEx(DPFLTR_DEFAULT_ID, 0xFFFFFFFF, "Hello Windows Kernel\n");

DriverObject->DriverUnload = Unload;


return status;
}

  1. Select a build platform and a architecture, then run ‘build’
  2. As a result you can see  the driver file that named end with .sys in ther subfolder of your project’s location.
  3. Remeber file’s full path or copy it to root folder.
  4. For the test, first , you register driver as a service simply.
Resigist a driver as a service.
sc create helloworlddriver binpath=D:\helloworld.sys type=kernel

  1. Run debugview app to see kernel messages from your driver by a ‘DbgPrintEx’.
    (Turn on the capture kernel function.)
  2. Start and stop your own driver.
Start a driver service.
sc start helloworlddriver

Stop a a driver service.
sc stop helloworlddriver


  1. Now, Confirm it worked well on the debugview.
    ng.PNG
  2. Finally, ungister the your driver.
Unresigist a driver
sc delete helloworlddriver




1 comment:

  1. If you have a build error , try to insert code 'extern c {}'

    ReplyDelete