Click here to Skip to main content
15,886,137 members
Articles / Programming Languages / C++

Driver to Hide Processes and Files. Second Edition: Splicing

,
Rate me:
Please Sign up or sign in to vote.
4.93/5 (33 votes)
11 Mar 2011CPOL9 min read 69.3K   8.8K   115  
This article describes a driver that hides processes and files using the method of splicing.
#include "QueryMng.h"
#include "IRPUtils.h"

#include <algorithm>
#include <string>

namespace utils
{
void QueryMng::Subscribe(ULONG controlCode,IQueryDispatcher* dispatcher)
{
    utils::SectionWriteGuard guard(&mapLock_);

    DispatchMap::iterator it = dispatchMap_.find(controlCode);
    if( it == dispatchMap_.end())
    {
        DispatchList list;
        list.push_back(dispatcher);

        dispatchMap_.insert( std::make_pair(controlCode,list) );
    }   
    else
    {
        DispatchList& list = it->second;

        DispatchList::const_iterator it = 
            std::find(list.begin(),list.end(),dispatcher);

        if(it != list.end())
            throw std::exception("This dispatcher already subscribed");

        list.push_back(dispatcher);
    }    
}
void QueryMng::UnSubscribe(ULONG controlCode,IQueryDispatcher* dispatcher)
{
    utils::SectionWriteGuard guard(&mapLock_);

    DispatchMap::iterator it = dispatchMap_.find(controlCode);
    if( it == dispatchMap_.end())
    {
        DispatchList list;
        list.push_back(dispatcher);

        dispatchMap_.insert( std::make_pair(controlCode,list) );
    }   
    else
    {
        it->second.push_back(dispatcher);
    }

}
NTSTATUS QueryMng::ProcessIrp(PIRP irp)
{
    NTSTATUS status = STATUS_SUCCESS;
    ULONG bytesTxd =0; // Number of transmitted,received bytes
    PIO_STACK_LOCATION IrpStack=IoGetCurrentIrpStackLocation(irp);

    // Getting the IOCTL code
    ULONG controlCode = IrpStack->Parameters.DeviceIoControl.IoControlCode;
    
    // Getting the exchange method
    ULONG method = controlCode & 0x03;

    if(method!=METHOD_BUFFERED)
        return utils::CompleteIrp(irp,STATUS_INVALID_PARAMETER,bytesTxd); 

    // input buffer size
    ULONG inputLength = 
        IrpStack->Parameters.DeviceIoControl.InputBufferLength;
    // output buffer size
    ULONG outputLength =
        IrpStack->Parameters.DeviceIoControl.OutputBufferLength;
    // if there is no buffer generate the error
    if( outputLength < 1 || inputLength < 1)
        return utils::CompleteIrp(irp,STATUS_INVALID_PARAMETER,bytesTxd);

    WCHAR *buff = (PWCHAR)irp->AssociatedIrp.SystemBuffer;

    try
    {
        CallDispatchers(controlCode,buff,inputLength,outputLength,&bytesTxd);
    }
    catch (const std::exception& ex)
    {
        std::string str(ex.what());
        
		size_t toWrite = min(str.size(),outputLength - 1);
 
        memcpy(buff,str.c_str(),toWrite);
		bytesTxd = toWrite + 1; // add '\\0' character

		char* mbStr = (char*)buff;
		mbStr[toWrite] = '\0';
    }
    
    return utils::CompleteIrp(irp,STATUS_SUCCESS,bytesTxd); 
}
void QueryMng::CallDispatchers(ULONG controlCode,
							   WCHAR* buf,
							   ULONG inputBufSize,
							   ULONG outputBufSize,
							   ULONG* bytesTxd)
{
    utils::SectionReadGuard guard(&mapLock_);
    
    DispatchMap::const_iterator it = dispatchMap_.find(controlCode);
    if( it == dispatchMap_.end() )
        throw std::exception(__FUNCTION__"No one dispatcher for this control code");

    const DispatchList& dispatchList = it->second;   
    DispatchList::const_iterator itList = dispatchList.begin();
    for( ; itList != dispatchList.end() ; ++itList )
    {
        (*itList)->Dispatch(controlCode,buf,inputBufSize,outputBufSize,bytesTxd);
    }
}
}

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
Technical Lead Apriorit 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