Click here to Skip to main content
15,886,106 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
#pragma once

#include "IFileChecker.h"
#include "ListUtils.h"

#include <boost/bind.hpp>
#include <vector>

namespace HideAlgorithm
{

typedef NTSTATUS(*OriginalHandlerWrapperPtr)(const NtQueryDirParams& params);

struct HideParams
{
	const NtQueryDirParams& callParams;
	IFileChecker* checker;
	OriginalHandlerWrapperPtr wrapperPtr;
};

template<class InfoType>
inline bool 
CheckFileEntry( utils::NtList* list, 
			    const HideParams& params )
{
    InfoType* info = (InfoType*)list;
    return params.checker->CheckFile(info->FileName,
									 info->FileNameLength/2,
									 params.callParams);
}

inline void 
ShiftBuffer(utils::NtList* pList,utils::NtList* newBegin)
{
	size_t shiftSize = newBegin - pList;
	size_t infoSize = utils::GetListSize(pList);
	size_t newInfoSize = infoSize - shiftSize;

	std::vector<char> buffer(newInfoSize);
	memcpy(&buffer.front(),newBegin,newInfoSize);

	memcpy(pList,&buffer.front(),newInfoSize);
}

template<class InfoType>
inline NTSTATUS 
FirstEntryProcessor( const HideParams& params )
{
	utils::NtList* pList = 
		(utils::NtList*)params.callParams.FileInformation;

	utils::NtList* pCurEntry = pList;
	
	while(true)
	{
		if(!CheckFileEntry<InfoType>(pCurEntry,params))
		{
			if(pList == pCurEntry)
				break; // Nothing to hide
			
			ShiftBuffer(pList,pCurEntry);
			break; // First entry hiding complete
		}
	
		// This entry needs to be hidden
		if( utils::IsEntryLast(pCurEntry) == false )
		{
			// Move to next entry to check
			// This need to shift buffer only once
			pCurEntry = utils::GetNextEntryPointer(pCurEntry);
		}
		else
		{
			// Reached last entry 
			// This mean that all data needs to be hidden

			// Try to request more data
			NTSTATUS status = params.wrapperPtr(params.callParams);
			if( !NT_SUCCESS(status) )
				return status;
			
			// Move to begin and resume checking
			pCurEntry = pList;
		}
	}
	return STATUS_SUCCESS;
}

template<class InfoType>
inline NTSTATUS 
NextEntryProcessor( const HideParams& params )
{
	utils::NtList* pList = 
		(utils::NtList*)params.callParams.FileInformation;

	if( utils::IsEntryLast(pList) )
		return STATUS_SUCCESS;

	utils::CutFromListByFakeOffset_If(pList,    
		boost::bind(&CheckFileEntry<InfoType>,_1,params));    

	return STATUS_SUCCESS;
}

template<class InfoType>
inline NTSTATUS 
HideFileImpl( const HideParams& params )
{
	NTSTATUS status = FirstEntryProcessor<InfoType>(params);
    if( !NT_SUCCESS(status) )
		return status;

	status = NextEntryProcessor<InfoType>(params);
    if( !NT_SUCCESS(status) )
		return status;

	return STATUS_SUCCESS;
}

template<class InfoType>
inline NTSTATUS 
HideFile( const HideParams& params )
{
    try
    {
        return HideFileImpl<InfoType>(params);
    }
    catch(const std::exception& ex)
    {
#ifdef KdPrint // For use in user mode environment
		KdPrint( (__FUNCTION__" std::exception: %s\n",ex.what()) );
#endif
    }
    return STATUS_SUCCESS;
}

}

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