2016-11-06 9 views
1

私が知りたいことは、開いているファイルを試してみることができるということです。C++ | Windows - ロックされたファイルの所有権を持つプロセスを見つける方法はありますか?

この情報を知りたいのは、私が悪意のあるファイルを「修正」する小さなアプリケーションを作成しているからです。

例えば、悪意ある/アドウェアなどのファイルセキュリティ記述子を設定して、ユーザーがファイルを削除できないようにします。私のアプリケーションはセキュリティ記述子をリセットするだけで、ユーザーは制御を取り戻すことができます。

たとえば、(CreateFile)などで子プロセスを開いているファイルを見て、共有モードをオフにしてファイルに触れることができない場合、アプリケーションはメモリから子プロセスを実行します。

答えて

2

はい、一般的にはopenfilesコマンドを使用して、この情報の収集を有効にした後にopenfiles /local onと表示できます。

Windows NTでは、Windows XPには、ohという名前の同様のResource Kitコマンドがあり、のショートハンドルがありました。

両方の代替手段は、SysInternalのProcess Explorerを使用することです。


注:openfilesは、ハンドルの一覧表示に失敗することがあります。これは、WindowsがUSBディスクのマウントを拒否したときに発生し、そのプロセスがディスク上のファイルを使用していると主張しています。そのようなプロセスは現れません。

+0

おそらく 'openfiles'は、ウイルス対策ソフトウェアのようなカーネルモードのハンドルが、不注意で開いたままにする可能性があります含まれていません。 –

-1

私はこのようなプロセスを見つけて、それを殺し、ロックされたファイルを削除する機能を開発しました。ここで

bool ForceDeleteFile(LPWSTR FileName); 

は、完全なソースコードです:

bool KillFileProcess(LPWSTR FileName) 
{ 
    HANDLE hProcessSnap; 
    HANDLE hProcess; 
    PROCESSENTRY32 pe32; 
    DWORD dwPriorityClass; 
    bool result = false; 
    // Take a snapshot of all processes in the system. 
    hProcessSnap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0); 
    if (hProcessSnap == INVALID_HANDLE_VALUE) 
    { 
     //printError(TEXT("CreateToolhelp32Snapshot (of processes)")); 
     return(FALSE); 
    } 

    // Set the size of the structure before using it. 
    pe32.dwSize = sizeof(PROCESSENTRY32); 

    // Retrieve information about the first process, 
    // and exit if unsuccessful 
    if (!Process32First(hProcessSnap, &pe32)) 
    { 
     //printError(TEXT("Process32First")); // show cause of failure 
     CloseHandle(hProcessSnap);   // clean the snapshot object 
     return(FALSE); 
    } 

    // Now walk the snapshot of processes, and 
    // display information about each process in turn 
    do 
    { 
     // Retrieve the priority class. 
     dwPriorityClass = 0; 
     hProcess = OpenProcess(PROCESS_ALL_ACCESS, FALSE, pe32.th32ProcessID); 
     if (hProcess == NULL) 
     { 
      //printError(TEXT("OpenProcess")); 
     } 
     else 
     { 
      dwPriorityClass = GetPriorityClass(hProcess); 
      if (!dwPriorityClass) 
      { 
       //printError(TEXT("GetPriorityClass")); 
      } 
      CloseHandle(hProcess); 
      if (HANDLE hProcess = ::OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE, pe32.th32ProcessID)) 
      { 
       WCHAR filename[MAX_PATH] = {}; 

       if (GetModuleFileNameEx(hProcess, NULL, filename, MAX_PATH)) 
       { 
        if (_wcsicmp((const wchar_t *)FileName, (const wchar_t *)filename) == NULL) 
        { 
         if (TerminateProcess(pe32.th32ProcessID, 0)) 
         { 
          _tprintf(L"Found: Process full killed\nKILLED!\n"); 
          result = true; 

         } 
         else 
         { 
          _tprintf(L"Found: Process full \nFailed to terminate\n"); 
          DoRun(((CString)L"taskkill /F /IM " + (CString)pe32.szExeFile).GetBuffer()); 

          result = false; 

         } 
        } 
       } 
       else 
       { 
        // handle error 
       } 

       CloseHandle(hProcess); 
      } 
     } 


    } while (Process32Next(hProcessSnap, &pe32)); 

    CloseHandle(hProcessSnap); 
    return(result); 
} 
bool ForceDeleteFile(LPWSTR FileName) 
{ 
    bool result = DeleteFile(FileName); 
    if (!result) 
    { 
     _tprintf(L"Can't delete file. using DeleteFile(). Trying to locate process and kill it\n"); 
     result = KillFileProcess(FileName); 
     if (!result) 
      _tprintf(L"Couldn't find the process\n"); 
     else 
     { 
      Sleep(1000); 
      result = DeleteFile(FileName); 
      if (result) 
       _tprintf(L"DeleteFile success"); 
      else 
       _tprintf(L"DeleteFile ============== failed ==============="); 

     } 
    } 
    return result; 
} 
BOOL TerminateProcess(DWORD dwProcessId, UINT uExitCode) 
{ 
    DWORD dwDesiredAccess = PROCESS_TERMINATE; 
    BOOL bInheritHandle = FALSE; 
    HANDLE hProcess = OpenProcess(dwDesiredAccess, bInheritHandle, dwProcessId); 
    if (hProcess == NULL) 
     return FALSE; 

    BOOL result = TerminateProcess(hProcess, uExitCode); 

    CloseHandle(hProcess); 

    return result; 
} 
+0

私はdownvoterではありませんが、これはロックされたファイルが実行可能な(実行中の)場合に制限されているようですか? –

+0

はい、問題はありますが、ロックされたファイルには同じ方法を使用できます。 –

関連する問題