Home > Software engineering >  Is it possible to make file accesible ONLY for certain processes?
Is it possible to make file accesible ONLY for certain processes?

Time:07-21

So I'm attempting to make file accesible only for certain process, firstly by finding it via this function:

bool GetProcessSid(PSID* pSID)
{
    PROCESSENTRY32 entry;
    entry.dwSize = sizeof(PROCESSENTRY32);
    HANDLE snapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, NULL);
    if (Process32First(snapshot, &entry) == TRUE)
    {
        while(Process32Next(snapshot, &entry) == TRUE)
        {
            const char* process_name = "testfileaccess2.exe";
            std::string t(process_name);
            std::wstring w_process_name(t.begin(), t.end());
            if (w_process_name.compare(entry.szExeFile)== 0)
            {
                HANDLE hProcess = OpenProcess(PROCESS_ALL_ACCESS, FALSE, entry.th32ProcessID);
                //...
                GetSecurityInfo(hProcess,SE_KERNEL_OBJECT, OWNER_SECURITY_INFORMATION,pSID,NULL,NULL,NULL,NULL);
                
                //getsecurityinfo(hprocess,SE_SERVICE) for service
                CloseHandle(hProcess);
                return TRUE;
            }
        }
    }
    return FALSE;
}

Works fine, always finds process if its alive and returns pSID. Then I create file like this:

PACL pNewDACL = NULL;
    PSID process_with_access = NULL;
    PSID current_user = NULL;
    DWORD sid_size = SECURITY_MAX_SID_SIZE;
    SID everyone_sid;
    DWORD dwRes;
    if (CreateWellKnownSid(WinWorldSid, NULL, &everyone_sid, &sid_size) ==
        FALSE) {
        throw std::runtime_error("CreateWellKnownSid() failed: "  
            std::to_string(GetLastError()));
    }

    GetProcessSid(&process_with_access);
    GetCurrentUserSid(&current_user);
    EXPLICIT_ACCESSA ea[2];
    ZeroMemory(&ea, 2 * sizeof(EXPLICIT_ACCESSA));

    ea[0].grfAccessPermissions = ACCESS_SYSTEM_SECURITY | READ_CONTROL | WRITE_DAC | GENERIC_ALL;
    ea[0].grfAccessMode = DENY_ACCESS;
    ea[0].grfInheritance = NO_INHERITANCE;
    ea[0].Trustee.TrusteeForm = TRUSTEE_IS_SID;
    ea[0].Trustee.ptstrName = reinterpret_cast<char*>(process_with_access);

    ea[1].grfAccessPermissions = ACCESS_SYSTEM_SECURITY | READ_CONTROL | WRITE_DAC | GENERIC_ALL;
    ea[1].grfAccessMode = GRANT_ACCESS;
    ea[1].grfInheritance = NO_INHERITANCE;
    ea[1].Trustee.TrusteeForm = TRUSTEE_IS_SID;
    ea[1].Trustee.ptstrName = reinterpret_cast<char*>(current_user);

    dwRes = SetEntriesInAclA(2, ea, NULL, &pNewDACL);
    if (ERROR_SUCCESS != dwRes) {
        printf("SetEntriesInAcl Error %u\n", dwRes);
        //TODO: goto Cleanup;
    }

    PSECURITY_DESCRIPTOR pSD = NULL;

    // Initialize a security descriptor.  
    pSD = (PSECURITY_DESCRIPTOR)LocalAlloc(LPTR,
        SECURITY_DESCRIPTOR_MIN_LENGTH);
    if (NULL == pSD)
    {
        printf("error");
    }

    if (!InitializeSecurityDescriptor(pSD,
        SECURITY_DESCRIPTOR_REVISION))
    {
        
        printf("error");
    }

    // Add the ACL to the security descriptor. 
    if (!SetSecurityDescriptorDacl(pSD,
        TRUE,     // bDaclPresent flag   
        pNewDACL,
        FALSE))   // not a default DACL
    {
        printf("error");
    }
    SECURITY_ATTRIBUTES sa;
    // Initialize a security attributes structure.
    sa.nLength = sizeof(SECURITY_ATTRIBUTES);
    sa.lpSecurityDescriptor = pSD;
    sa.bInheritHandle = FALSE;

    HANDLE hFile = CreateFileA(filename, GENERIC_ALL, 0, &sa, CREATE_NEW, FILE_ATTRIBUTE_NORMAL, NULL);
    CloseHandle(hFile);

And for now it only sets access for parent of a process, not the process itself. So is it even possible what I am trying to achieve?

CodePudding user response:

No, Windows' security is based around user/group access lists. It would be possible to set up a process so that it was running as some particular user and then restrict access to that user, but any program running as Administrator or Local System would be able to bypass that protection.

The best you can do against such programs is to protect against accidental access rather than malicious.

  • Related