Click here to Skip to main content
15,884,388 members
Articles / Desktop Programming / MFC

Driver to Hide Processes and Files

, ,
Rate me:
Please Sign up or sign in to vote.
4.57/5 (145 votes)
17 Aug 2009CPOL12 min read 654.6K   28.5K   369  
In this article, we describe the driver we created to hide processes and files in a system.
#include "UserNameFromSIDUtils.h"
#include "drvCommon.h"
#include "StringUtils.h"
#include "KernelHandleGuard.h"

namespace utils
{
/*
	There is two ways how to convert SID to UserName using registry.
	1) Using RID and UserName
	   In kernel registry "\\registry\\machine\\sam\\sam\\domains\\account\\users\\names"
	   Windows store user name in sub key name, in each sub key default value is user RID
	   RID is last element of SubAuthority. 
	   So need to find sub key with default value equal to RID.
	2) Using V-Block parsing
	   In kernel registry "\\registry\\machine\\sam\\sam\\domains\\account\\users"
	   Windows store in each sub key, except "names", exist value with name "V"
	   this is V-Block. V-Block has two important params:
			ULONG NameOffsetFromEnd; at offset 0xc from start
			ULONG NameSizeInBytes;	 at offset 0x10 from start
	   Using this two params SID alsow can be converted to UserName
*/
const wchar_t* namesKeyPath = L"\\registry\\machine\\sam\\sam\\domains\\account\\users\\names";

ULONG GetNameKeyRID(const std::wstring& keyPath)
{
	std::vector<char> buffer(256); 
	ULONG resultLength = NULL;
	PVOID keyInformation = &buffer.front();

	UNICODE_STRING keyPathUnSt = utils::wstring2UnicodeStr(keyPath);

	OBJECT_ATTRIBUTES keyAttr;
	InitializeObjectAttributes(&keyAttr, &keyPathUnSt, OBJ_KERNEL_HANDLE | OBJ_CASE_INSENSITIVE, NULL, NULL);

	HANDLE hKey;
	NTSTATUS status = ZwOpenKey(&hKey,GENERIC_READ,&keyAttr);
    if( !NT_SUCCESS(status) )
		throw std::exception("-HideDriver- GetNameKeyRID: ZwOpenKey fail.");

	HandleGuard guard(hKey);

	// RID stores in default value
	ULONG index = 0;
	status = ZwEnumerateValueKey(hKey,
						         index,
								 KeyValueBasicInformation,
								 keyInformation,
								 buffer.size(),
								 &resultLength);
    if( !NT_SUCCESS(status) )
		throw std::exception("-HideDriver- GetNameKeyRID: ZwEnumerateValueKey fail.");

	KEY_VALUE_BASIC_INFORMATION* basicInfo =
		(KEY_VALUE_BASIC_INFORMATION*)keyInformation;

	// MS developers like store values for SAM in type record instead of value record
	return basicInfo->Type;
}
void GetUserNameBySidUseNamesKey(const std::vector<char>& sid,std::wstring* userName)
{
	std::wstring keyPath(namesKeyPath); 
	UNICODE_STRING keyPathUnSt = utils::wstring2UnicodeStr(keyPath);
	
	OBJECT_ATTRIBUTES keyAttr;
	InitializeObjectAttributes(&keyAttr, &keyPathUnSt, OBJ_KERNEL_HANDLE | OBJ_CASE_INSENSITIVE, NULL, NULL);

	HANDLE hKey;
	NTSTATUS status = ZwOpenKey(&hKey,GENERIC_READ,&keyAttr);
    if( !NT_SUCCESS(status) )
		throw std::exception("-HideDriver- GetUserNameBySidUseNamesKey: ZwOpenKey fail.");

	HandleGuard guard(hKey);

	SID* pSid = (SID*)&sid.front();
	// RID is last element of SubAuthority
	ULONG ridIndex = pSid->SubAuthorityCount - 1;
	ULONG RID = pSid->SubAuthority[ridIndex];

	if(RID == 0x12)
	{
		// RID of system account always 0x12
		*userName = L"System";
		return;
	}

	std::vector<char> buffer(1024);
	for(ULONG index = 0 ; ; ++index )
	{
		ULONG resultLength = NULL;
		PVOID keyInformation = &buffer.front();
		NTSTATUS status = 
			ZwEnumerateKey(hKey,
						   index,
						   KeyBasicInformation,
						   keyInformation,
						   buffer.size(),
						   &resultLength);
		if(status == STATUS_NO_MORE_ENTRIES)
			break;

    if( !NT_SUCCESS(status) )
			throw std::exception("-HideDriver- GetUserNameBySidUseNamesKey: ZwEnumerateValueKey fail.");

		KEY_BASIC_INFORMATION* keyInfo = 
			(KEY_BASIC_INFORMATION*)keyInformation;

		std::wstring subKeyName(keyInfo->Name,keyInfo->NameLength/2);
		std::wstring subKeyPath = keyPath + L'\\' + subKeyName;
			
		ULONG keyRID = GetNameKeyRID(subKeyPath);
		
		if(keyRID != RID)
			continue;
		
		// Sub key name is user name
		*userName = subKeyName;

		return;
	}
	throw std::exception("-HideDriver- GetUserNameBySidUseNamesKey: Can't find user name for SID.");
}

////////////////////////////////////////////////////////////////////////////////////////////////////

const wchar_t* pathToSubKeysWithVBlock = L"\\registry\\machine\\sam\\sam\\domains\\account\\users\\names";

struct VBlock
{
	ULONG Unknown[3];
	ULONG NameOffsetFromEnd; //0xc
	ULONG NameSizeInBytes;	 //0x10
	ULONG Unknown2[46];
};
char* static_asser[sizeof(VBlock) == 0xcc];

void GetUserNameBySidUseVBlock(const std::vector<char>& sid,std::wstring* userNam)
{
	throw std::exception("GetUserNameBySidUseVBlock - is not implemented yet.");
}

}

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.

Written By
Software Developer (Junior) ApriorIT
Ukraine Ukraine
Sergey Popenko.
22 years old.
The Driver Team`s software developer.
Master of the Applied Math faculty, the Dnipropetrovsk National University, Ukraine.

Comments and Discussions