Skip to content

Instantly share code, notes, and snippets.

@Dabudabot
Created November 16, 2020 20:28
Show Gist options
  • Save Dabudabot/573fbe79cddbf86f71e3a5396d43b35d to your computer and use it in GitHub Desktop.
Save Dabudabot/573fbe79cddbf86f71e3a5396d43b35d to your computer and use it in GitHub Desktop.
Creating reparse point
// -----------------------------------------------------------------------
// Includes
// -----------------------------------------------------------------------
#include <Windows.h>
#include <tchar.h>
#include <stdio.h>
// -----------------------------------------------------------------------
// Functions
// -----------------------------------------------------------------------
/*++
* Setting required privileges
* @param priv privilege
--*/
void GetPrivilege(LPCTSTR priv)
{
HANDLE hToken;
TOKEN_PRIVILEGES tp;
OpenProcessToken(GetCurrentProcess(),
TOKEN_ADJUST_PRIVILEGES, &hToken);
LookupPrivilegeValue(NULL, priv, &tp.Privileges[0].Luid);
tp.PrivilegeCount = 1;
tp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
AdjustTokenPrivileges(hToken, FALSE, &tp,
sizeof(TOKEN_PRIVILEGES), NULL, NULL);
CloseHandle(hToken);
}
int _tmain(int argc, TCHAR** argv)
{
// 1. required privileges
GetPrivilege(SE_BACKUP_NAME);
GetPrivilege(SE_RESTORE_NAME);
GetPrivilege(SE_CREATE_SYMBOLIC_LINK_NAME);
// 2. prepare reparse structure
TCHAR data[] = _T("My reparse data");
BYTE reparseBuffer[sizeof(REPARSE_GUID_DATA_BUFFER) + sizeof(data)];
PREPARSE_GUID_DATA_BUFFER rd = (PREPARSE_GUID_DATA_BUFFER) reparseBuffer;
ZeroMemory(reparseBuffer, sizeof(REPARSE_GUID_DATA_BUFFER) + sizeof(data));
// {07A869CB-F647-451F-840D-964A3AF8C0B6}
static const GUID my_guid =
{
0x7a869cb, 0xf647, 0x451f,
{ 0x84, 0xd, 0x96, 0x4a, 0x3a, 0xf8, 0xc0, 0xb6 }
};
rd->ReparseTag = 0xFF00;
rd->ReparseDataLength = sizeof(data);
rd->Reserved = 0;
rd->ReparseGuid = my_guid;
memcpy(rd->GenericReparseBuffer.DataBuffer, &data, sizeof(data));
DWORD dwLen = 0;
LPCTSTR name = _T("TestReparseFile");
// 3. create empty file
_tprintf(_T("Creating empty file\n"));
HANDLE hFile = CreateFile(name,
GENERIC_READ | GENERIC_WRITE,
0, NULL,
CREATE_NEW,
FILE_FLAG_OPEN_REPARSE_POINT | FILE_FLAG_BACKUP_SEMANTICS,
NULL);
if (INVALID_HANDLE_VALUE == hFile)
{
_tprintf(_T("Failed to create file\n"));
return -1;
}
// 4. Fill file with reparse
_tprintf(_T("Creating reparse\n"));
if (!DeviceIoControl(hFile, FSCTL_SET_REPARSE_POINT,
rd, rd->ReparseDataLength + REPARSE_GUID_DATA_BUFFER_HEADER_SIZE,
NULL, 0, &dwLen, NULL))
{
CloseHandle(hFile);
DeleteFile(name);
_tprintf(_T("Failed to create reparse\n"));
return -1;
}
CloseHandle(hFile);
_tprintf(_T("SUCCESS\n"));
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment