Click here to Skip to main content
15,885,914 members
Articles / Programming Languages / C++

Detect Driver

,
Rate me:
Please Sign up or sign in to vote.
5.00/5 (46 votes)
10 Mar 2010CPOL12 min read 110.1K   9.1K   155  
This article is the continue of the previously posted article Hide Driver. Some methods to detect hidden files and processes are described in it
#include "ServiceTableDef.h"
#include "VersionDependOffsets.h"

namespace utils
{

const UCHAR opcode_MovEax = 0xB8; // __asm move eax,Value

inline ULONG GetFunctionSSTIndex(PUNICODE_STRING function_name)
{
    /*    
    All ZwXXX functions exported by NTOSKRNL.exe start with :
    
        mov eax, ULONG 
    
    where ULONG is the NtXXX function index in SST
    */

	// Be careful with function MmGetSystemRoutineAddress, under 
	// Windows XP(SP2) and lower this function throw SEH exception if 
	// they�re passed an invalid system routine name.
	// Using __try/__except block is not good solution since SEH 
	// is not a formal contract for this API, so is not guarantees 
	// that the OS is still in a stable state after you have caught 
	// the exception.
	PVOID pTrueFuncPtr_ZW = MmGetSystemRoutineAddress(function_name);
 
    if(pTrueFuncPtr_ZW == NULL)
        throw std::exception(__FUNCTION__" Can't get function address.");

	// Check command byte for valid opcode
	// Starts from Vista DriverVerifier can substitute ntoskrnl code.
	if( *( (PUCHAR)pTrueFuncPtr_ZW ) != opcode_MovEax )
		throw std::exception(__FUNCTION__" Function address points to supposititious code.");

    // Skip command byte, move to index byte
    ULONG funcIndex = *(PULONG)((PUCHAR) pTrueFuncPtr_ZW + 1);
    
    if( funcIndex == NULL)    
        throw std::exception(__FUNCTION__" Can't get function SST index");

	return funcIndex;
}
inline PVOID GetFunctionSSTPtr(ULONG fncIndex)
{
	PNTPROC ServiceTable=pNtoskrnl->ServiceTable;

	ULONG TotalCount = pNtoskrnl->ServiceLimit;
	if( fncIndex > TotalCount ) 
		throw std::exception(__FUNCTION__" Wrong SST index");
	
	return ServiceTable[fncIndex];
}
inline PVOID GetFunctionSSTPtr(PUNICODE_STRING function_name)
{
	return GetFunctionSSTPtr( GetFunctionSSTIndex(function_name) );
}
inline PVOID GetFunctionSSTPtrEx(PUNICODE_STRING function_name)
{
	ULONG fncIndex;
	try 
	{
		fncIndex = utils::GetFunctionSSTIndex(function_name);
	}
	catch(const std::exception&)
	{
		// Use predefined offsets
		fncIndex = utils::GetSSTOffsetByName(function_name->Buffer,function_name->Length/2);		
	}
	return utils::GetFunctionSSTPtr(fncIndex);
}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Chief Technology Officer Apriorit Inc.
United States United States
ApriorIT is a software research and development company specializing in cybersecurity and data management technology engineering. We work for a broad range of clients from Fortune 500 technology leaders to small innovative startups building unique solutions.

As Apriorit offers integrated research&development services for the software projects in such areas as endpoint security, network security, data security, embedded Systems, and virtualization, we have strong kernel and driver development skills, huge system programming expertise, and are reals fans of research projects.

Our specialty is reverse engineering, we apply it for security testing and security-related projects.

A separate department of Apriorit works on large-scale business SaaS solutions, handling tasks from business analysis, data architecture design, and web development to performance optimization and DevOps.

Official site: https://www.apriorit.com
Clutch profile: https://clutch.co/profile/apriorit
This is a Organisation

33 members

Written By
Software Developer Codedgers Inc
Ukraine Ukraine
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions