Click here to Skip to main content
15,893,381 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.4K   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 "drvCommon.h"

#include "KernelNames.h"
#include "KernelHandleGuard.h"
#include "StringUtils.h"

namespace utils
{

void ResolveSymbolicLink(PUNICODE_STRING symLink, 
                         PUNICODE_STRING target,
                         std::vector<char>* targetBuffer)
{
    OBJECT_ATTRIBUTES   objAttr;
    InitializeObjectAttributes(&objAttr, symLink, 
                               OBJ_KERNEL_HANDLE|OBJ_CASE_INSENSITIVE,0,0);

    // Get sym link handle
    HANDLE hSymLink;
    NTSTATUS status = ZwOpenSymbolicLinkObject(&hSymLink, GENERIC_READ, &objAttr);
    if( !NT_SUCCESS(status) )
        throw std::exception("Can't open symbolic link.");

    HandleGuard guardhandle(hSymLink);

    // Get name size
    ULONG neededLength = NULL;
    UNICODE_STRING targetStr = {0,0,NULL};
    status = ZwQuerySymbolicLinkObject(hSymLink,&targetStr,&neededLength);
    if( status != STATUS_BUFFER_TOO_SMALL)
        throw std::exception("Can't get symbolic link target name size.");

    // Prepare buffer
    targetBuffer->resize(neededLength);
    target->Length = target->MaximumLength = neededLength;
    target->Buffer = (wchar_t*)&targetBuffer->front();

    // Get name
    status = ZwQuerySymbolicLinkObject(hSymLink,target,&neededLength);
    if( !NT_SUCCESS(status) )
        throw std::exception("Can't query symbolic link.");
}

std::wstring ConvertUserToKernelPath(const std::wstring& userPath)
{
    if( userPath.empty() )
        throw std::exception("Path can't be empty.");

    std::wstring driveName;
    driveName += userPath.at(0);
    driveName += L':';

    std::wstring driveNameInGlobals = L"\\??\\" + driveName;
    
    UNICODE_STRING driveNameInGlobalsUS = wstring2UnicodeStr(driveNameInGlobals);

    std::vector<char> buffer;
    UNICODE_STRING kernelDriveNameUS;
    ResolveSymbolicLink(&driveNameInGlobalsUS,&kernelDriveNameUS,&buffer);

    std::wstring kernelDriverName = unicodeStr2wstring(&kernelDriveNameUS);
    std::wstring kernelPath = userPath;
    
    size_t pos = kernelPath.find(driveName);
    if( pos == std::wstring::npos )
        throw std::exception("Internal error. Can't find drive name inside userPath.");

    kernelPath.replace(pos,driveName.length(),kernelDriverName.c_str(),kernelDriverName.length());
    
    return kernelPath;
}

}

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