Monday, May 10, 2010

Hello World for boot time native application

Hello world at user mode is not hard. Almost all of us have done that. Though it seemed hard at that time.

Hello world at driver level is not that hard either. here is a link to an excellent tutorial.

Let me take you to a different level of application. The Native application. Native environment is different from our user mode in a sense that only Native API's are available. A most common example of native application is actochk.exe. If you think that u have never seen this exe... think again. Did you ever notice the blue box that shows at windows startup. Yup that's autochk.exe. This is the application that checks the disk health at boot time.

How good it would be to write an application that shows hello world at that time.

Tools you need in this adventure are

1) regedit
2) WDK

Using WDK we'll compile our native application. and using regedit we'll enable windows to load our small utility at boot time.


Step 1: Adding Registry Entry
------------------------
Well let's start with easier step. HKLM\System\CurrentControlSet\Control\Session Manager\BootExecute is the value that will run our application at boot time.

BootExecute is a multi-line value so don't forget to add your entry after the existing entries :) (unless you want to remove default entries, which might help you if you are sick of the stupid, self righteous chkdsk utility ;))

Make sure that you copy hello.exe to c:\Windows\System32.

My BootExecute looks like this:

[HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager]
"BootExecute"=hex(7):61,00,75,00,74,00,6f,00,63,00,68,00,65,00,63,00,6b,00,20,\
00,61,00,75,00,74,00,6f,00,63,00,68,00,6b,00,20,00,2a,00,00,00,68,00,65,00,\
6c,00,6c,00,6f,00,00,00,00,00



This looks scary but in regedit you can set BootExecute to:

autocheck autochk *
hello


Step 2: Compiling The Code
------------------------
I have compiled this code with WinDDK version 6001.18002. For this you need 3 files in a directory and using build environment fpr your target give "build" command.

1. "Source":
------------------
TARGETNAME=hello
TARGETTYPE=PROGRAM
UMTYPE=nt


C_DEFINES=$(C_DEFINES) -DUNICODE -D_UNICODE

LINKLIBS=$(SDK_LIB_PATH)\shell32.lib

INCLUDES=$(INCLUDES); \
$(DDK_INC_PATH); \

SOURCES=hello.c

!if "$(DDK_TARGET_OS)"=="WinLH"
_NT_TARGET_VERSION=$(_NT_TARGET_VERSION_WINXP)
!endif





2. "makefile"
------------------
!INCLUDE $(NTMAKEENV)\makefile.def






3. hello.c
------------------
#include "ntddk.h"
#include


typedef int BOOL;
typedef unsigned long DWORD;
typedef unsigned __int64 ULONGLONG;

NTSYSAPI
NTSTATUS
NTAPI
NtDelayExecution(
IN ULONG bAlertable,
IN PLARGE_INTEGER pDuration
);

NTSYSAPI
NTSTATUS
NTAPI
NtDisplayString(
IN PUNICODE_STRING pString
);

NTSYSAPI
NTSTATUS
NTAPI
NtTerminateProcess(
IN HANDLE hProcess,
IN ULONG ExitCode
);


VOID __stdcall Print(__wchar_t *msg);
VOID __stdcall Sleep(DWORD dwMilliseconds);


void NtProcessStartup( PVOID arg) {
int i;
Print(L"Hello World!!!\n");
Print(L"Sleeping for 10 seconds");

for (i=0; i < 10; i++)
{
Print(L".");
Sleep(1000);
}
Print(L"\nNow loading Windows");
Sleep(1000);
NtTerminateProcess( NtCurrentProcess(), 0 );
}

VOID __stdcall Print(__wchar_t *msg) {
UNICODE_STRING umsg;
RtlInitUnicodeString (&umsg, msg);
NtDisplayString(&umsg);
}

VOID __stdcall Sleep(DWORD dwMilliseconds) {
LARGE_INTEGER Interval;
NTSTATUS errCode;

#pragma warning( disable : 4146 )
Interval.QuadPart = -(ULONGLONG)dwMilliseconds * 10000;
#pragma warning( default : 4146 )

errCode = NtDelayExecution (FALSE, &Interval);

return;
}