Click here to Skip to main content
15,867,756 members
Articles / Programming Languages / C

Interrupt Hooking and retrieving device information on Windows NT/2000/XP

Rate me:
Please Sign up or sign in to vote.
4.84/5 (37 votes)
30 Jul 2003GPL33 min read 315.2K   6.2K   94   130
This article shows you how to hook interrupts, and how to get device resource information from the registry

Introduction

Hi, this is my first article, and English is not my natural language. So first, please excuse my terrible language. This article is about hooking software/hardware interrupts and will also show you, how to retrieve device resource information from the registry. I ASSUME A LITTLE BIT OF DRIVER CODING KNOWLEDGE FOR THIS ARTICLE!

Background/How this works

Ok, now I'll explain, how interrupt hooking works. When an interrupt (software or hardware) occurs, the cpu uses the idt (interrupt descriptor table) to read the necessary information about how to handle that interrupt. You can get the table's memory address like this:

#pragma pack(1)    // 2 works, too
typedef struct tagIDT
{
    WORD    wLimit;
    DWORD    dwBase;
} IDT, *PIDT;
#pragma pack()

VOID
LoadIDT( 
        OUT    PIDT        pIdt )
{
    __asm
    {
        MOV EAX, [pIdt]  // load offset into EAX
        SIDT [EAX]
    }
}

The command SIDT saves the idt information, which consists of the idt base address and the size(limit) of the table, to the specified memory address.

Now you can read single descriptors from the table. Here is their structure:

 

#pragma pack(1)
typedef struct tagINT_VECTOR
{
    WORD    wLowOffset;        // LOWORD of the handler's offset
    WORD    wSelector;        // selector of the handler's offset
    BYTE    bAccess;        // 0-3: Type
                            // 4:    ?(=0)
                            // 5-6:    DPL
                            // 7:    Present
    BYTE    wUnused;        // 0, 0, 0, unused (binary)
    WORD    wHighOffset;    // HIWORD of the handler's offset
} INT_VECTOR, *PINT_VECTOR;
#pragma pack()

Here are the functions to load/save a descriptor:

 

VOID
LoadINTVector( 
        IN    PIDT        pIdt, 
        IN    UCHAR        iVector, 
        OUT    PINT_VECTOR    pVector )
{
    __try
    {
        DWORD dwBase = pIdt->dwBase + iVector * sizeof(INT_VECTOR);
        memcpy( pVector, (const void *)dwBase, sizeof(INT_VECTOR) );
    }
    __except( 1 )
    {
        DPRINT( "LoadINTVector: Exception\n" );
    }

    DPRINT( "LoadINTVector: Vector 0x%.2X successfully dumped\n",
       iVector );
}

VOID
SaveINTVector( 
        IN    PIDT        pIdt, 
        IN    UCHAR        iVector, 
        IN    PINT_VECTOR    pVector )
{
    __try
    {
        DWORD dwBase = pIdt->dwBase + iVector * sizeof(INT_VECTOR);
        __asm{ PUSHFD };
        __asm{ CLI };        // IMPORTANT: clear interrupt flag!!!
        memcpy( (void *)dwBase, pVector, sizeof(INT_VECTOR) );
        __asm{ POPFD };
    }
    __except( 1 )
    {
        DPRINT( "SaveINTVector: Exception\n" );
    }

    DPRINT( "SaveINTVector: Vector 0x%.2X successfully set\n", iVector );
}

 

Build-in ioctl codes:

 

  • IOCTL_HOOK_INT Hooks only one specified interrupt.
    • Parameters: IN UCHAR iVector
  • IOCTL_UNHOOK_INT Unhooks only one specified interrupt.
    • Parameters: IN UCHAR iVector
  • IOCTL_HOOK_ALL_INT Hooks all useful interrupts.
    • Parameters: none
  • IOCTL_DUMP_IDT Dumps the whole IDT table.
    • Parameters: OUT IDT_ENTRY Array[256]
  • IOCTL_GET_INT_COUNTS Retrieves the interrupt call counts.
    • Parameters: OUT __int64 Array[256]
  • IOCTL_GET_START_TIME Retrieves the time when IOCTL_HOOK_ALL_INT was called (PerformanceCounter).
    • Parameters: OUT __int64 Time

To call these ioctl codes, open the driver like this:

 

DWORD dwOut; 
HANDLE hDriver = CreateFile( <a>\\\\.\\InterruptHook</a>, 
     GENERIC_READ |GENERIC_WRITE, 0, 
     NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL ); 

DeviceIoControl( hDriver, IOCTL_X, inbuffer, inbufferlen, 
        outbuffer, outbufferlen, &dwRet, NULL ); 

 

Retrieving device information from the registry

Ok, this is quite easy. Have a look at the registry at HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Enum! There you'll find several subkeys like "ACPI" or "STORAGE". These subkeys have further subkeys, which are kinda classes, I don't know exactly what they do. They have subkeys, too, representing device drivers! Their subkey "Control" may include a value called "AllocConfig" (type REG_RESOURCE_LIST). REG_RESOURCE_LIST is not documented by microsoft!

It's a structure defined in ntddk.h:

 

typedef struct _CM_RESOURCE_LIST {
  ULONG                          Count;
  CM_FULL_RESOURCE_DESCRIPTOR List[1];
} CM_RESOURCE_LIST, *PCM_RESOURCE_LIST;

typedef struct _CM_FULL_RESOURCE_DESCRIPTOR {
  INTERFACE_TYPE            InterfaceType;
  ULONG                        BusNumber;
  CM_PARTIAL_RESOURCE_LIST  PartialResourceList;
} CM_FULL_RESOURCE_DESCRIPTOR, *PCM_FULL_RESOURCE_DESCRIPTOR;

typedef struct _CM_PARTIAL_RESOURCE_LIST {
  USHORT  Version;
  USHORT  Revision;
  ULONG  Count;
  CM_PARTIAL_RESOURCE_DESCRIPTOR PartialDescriptors[1];
} CM_PARTIAL_RESOURCE_LIST, *PCM_PARTIAL_RESOURCE_LIST;

typedef struct _CM_PARTIAL_RESOURCE_DESCRIPTOR {
    UCHAR Type;
    UCHAR ShareDisposition;
    USHORT Flags;
    union {
        struct {
            PHYSICAL_ADDRESS Start;
            ULONG Length;
        } Generic;
        struct {
            PHYSICAL_ADDRESS Start;
            ULONG Length;
        } Port;
        struct {
            ULONG Level;
            ULONG Vector;
            ULONG Affinity;
        } Interrupt;
        struct {
            PHYSICAL_ADDRESS Start;
            ULONG Length;
        } Memory;
        struct {
            ULONG Channel;
            ULONG Port;
            ULONG Reserved1;
        } Dma;
        struct {
            ULONG Data[3];
        } DevicePrivate;
        struct {
            ULONG Start;
            ULONG Length;
            ULONG Reserved;
        } BusNumber;
        struct {
            ULONG DataSize;
            ULONG Reserved1;
            ULONG Reserved2;
        } DeviceSpecificData;
    } u;
} CM_PARTIAL_RESOURCE_DESCRIPTOR, *PCM_PARTIAL_RESOURCE_DESCRIPTOR;

You can find a documentation of these structures here.

 

About the GUI

The GUI just refreshes the interrupt call count every 50ms and displays it. CPS means CALLS PER SECOND! Open a context menu when selecting an IRQ entry from the list!

Info on compiling

  • The GUI can be compiled by Visual Studio 6.0 or higher.
  • For the driver, you'll need the Windows NT(or higher) DDK(order it from Microsoft).

To compile it, use the DDK build environment, change to the source directory and type "build -cZ". If you don't have the DDK, use the compiled driver from the binary zip file. Here a little comment about the Visual Studio 6.0 C++ compiler/preprocessor:

When compiling my software, the compiler seems to interpret the CTL_CODE macro wrong, so I had to use the hexadecimal values instead of the macro.

Known Issues

  • My floppy disk doesn't work anymore after hooking the interrupts (reboot solves the prob)
  • Spy++ ends in bluescreen when hooking a windows procedure (reboot...)
  • Please send me feedback about other issues!!!

DISCLAIMER

USE THIS SOFTWARE ON YOUR OWN RISK!!! I DON'T KNOW WHAT HAPPENS TO OTHER COMPUTERS! NO WARRANTY FOR POSSIBLE DAMAGES!!!

History

  • July 31th, 2003 - Posted this article on CodeProject

License

This article, along with any associated source code and files, is licensed under The GNU General Public License (GPLv3)


Written By
Germany Germany
I'm a lazy coder who started coding at the age of 8 and I live in (c)old Germany Frown | :(
I'm a university student at LMU munich (http://www.lmu.de/).
Course of studies: computer science of course Wink | ;)

I'm also very interested in system programming, assembler and SIMD... but they are not listed as interests choices here Frown | :(

Comments and Discussions

 
SuggestionToo simple for SMP machines Pin
Haftmann, Henrik11-Mar-15 6:37
Haftmann, Henrik11-Mar-15 6:37 
Questionchanging the code INT_VECTOR_HANDLER Pin
FirstSKY18-Jun-12 2:25
FirstSKY18-Jun-12 2:25 
GeneralMy vote of 5 Pin
Manoj Kumar Choubey1-May-12 23:40
professionalManoj Kumar Choubey1-May-12 23:40 
QuestionBig Kernel Pimping Pin
TheRaven18-Sep-11 13:56
TheRaven18-Sep-11 13:56 
Generalin windows 98 Pin
zon_cpp19-Jan-08 19:48
zon_cpp19-Jan-08 19:48 
GeneralRe: in windows 98 Pin
Alexander M.,21-Jan-08 3:33
Alexander M.,21-Jan-08 3:33 
GeneralRe: in windows 98 Pin
zon_cpp21-Jan-08 20:56
zon_cpp21-Jan-08 20:56 
GeneralCode doesnt work Pin
teomannaskali10-Dec-07 12:59
teomannaskali10-Dec-07 12:59 
GeneralRe: Code doesnt work Pin
Alexander M.,21-Jan-08 3:37
Alexander M.,21-Jan-08 3:37 
Generalnew case Pin
samira forooghi25-Aug-07 19:30
samira forooghi25-Aug-07 19:30 
GeneralRe: new case Pin
Alexander M.,27-Aug-07 15:29
Alexander M.,27-Aug-07 15:29 
GeneralRe: new case Pin
samira forooghi27-Aug-07 22:41
samira forooghi27-Aug-07 22:41 
Questionany way to interrupts access [modified] Pin
samira forooghi8-Aug-07 1:11
samira forooghi8-Aug-07 1:11 
AnswerRe: any way to interrupts access Pin
Alexander M.,16-Aug-07 15:46
Alexander M.,16-Aug-07 15:46 
GeneralRe: any way to interrupts access Pin
samira forooghi25-Aug-07 19:32
samira forooghi25-Aug-07 19:32 
AnswerRe: any way to interrupts access [modified] Pin
Ei Adimi (Digelo)4-Aug-12 22:07
Ei Adimi (Digelo)4-Aug-12 22:07 
GeneralWindows hangs! Pin
oliwan1-Nov-06 4:53
oliwan1-Nov-06 4:53 
GeneralRe: Windows hangs! Pin
Alexander M.,1-Nov-06 5:08
Alexander M.,1-Nov-06 5:08 
GeneralRe: Windows hangs! Pin
oliwan1-Nov-06 5:22
oliwan1-Nov-06 5:22 
GeneralInterrupt 0x07 - No coprocessor available Pin
samu13-Jun-06 1:52
samu13-Jun-06 1:52 
GeneralRe: Interrupt 0x07 - No coprocessor available Pin
Alexander M.,13-Jun-06 10:39
Alexander M.,13-Jun-06 10:39 
GeneralMemory Hook Pin
catalin180410-May-06 4:56
catalin180410-May-06 4:56 
GeneralRe: Memory Hook Pin
Alexander M.,11-May-06 1:17
Alexander M.,11-May-06 1:17 
QuestionInterrupt when cd eject button pressed Pin
werner2715-Apr-06 5:52
werner2715-Apr-06 5:52 
AnswerRe: Interrupt when cd eject button pressed Pin
Alexander M.,15-Apr-06 10:41
Alexander M.,15-Apr-06 10:41 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.