SandboxieやDropboxPortableAHKと同様に、.NETでリダイレクトされたファイルパスを使用してプロセスを実行する方法はありますか?
たとえば、プロセスがでファイルを書き込みたい場合、代わりC:\file.txt
にアプリにプロセスを書き込みさせたいと思いC:\Sandbox\file.txt
ます。
.NET は遅かれ早かれ呼び出しを基盤となる OS にリダイレクトするため、実際にはフックをアタッチし、CreateFile() のようなルーチンを置き換えてパスを変更できます。
Detours Libraryが必要かもしれません。特に 64 ビット モードでは、いくつかのライセンスの問題があるため、次のような無料の代替手段を探してください: DetourXS。
CreateFile/WriteFile/ReadFile をフックする正確な手順は、CodingTheWheelにあります。
次のような手順で DLL を作成するだけです。
// Our custom version of the CreateFile Windows API, having the same parameters,
// return type, and calling convention as the version of CreateFile provided by the OS.
HANDLE WINAPI Mine_CreateFile(LPCWSTR lpFileName,DWORD dwDesiredAccess,
DWORD dwShareMode, LPSECURITY_ATTRIBUTES lpSecAttr,
DWORD dwCreateDisp, DWORD dwFlagsAttr,HANDLE hTemplate)
{
// First, call the original CreateFile provided by the operating system.
HANDLE hFile = Real_CreateFile(lpFileName,dwDesiredAccess,dwShareMode,lpSecAttr,
dwCreateDisp,dwFlagsAttr,hTemplate);
// Now, do whatever we want with the filename (lpFileName)
/// e.g., redirect C:\test.txt to C:\MyApp\test.txt
// Now, do whatever we want with the file handle (hFile)
// Call the original CreateFile
// Return the same value returned to us by the original API
return hFile;
}
これはまったく単純ではありませんが、基本的にネイティブ コード (C++ など) で DLL を作成する必要があります。この DLL は、ファイルとレジストリ IO をフックし、サンドボックス ディレクトリにリダイレクトします。次に、この DLL をサンドボックス化されたプロセスに挿入します。DLL は、このプロセスのアクティビティをフックします。
それが基本的な考え方です。