ntopensymboliclinkobject没有成功获得符号链接句柄

发布于 2025-01-31 23:26:03 字数 2600 浏览 2 评论 0原文

我正在尝试删除我创建的符号链接。
您可以在对象管理器(WinoBJ)中看到它:

我正在尝试删除该文件夹。我看到在这里我需要打开一个用ntopensymboliclinkobject,然后使用<代码> ntmaketemporaryObject 删除它。

我尝试使用deletesymboliclink的许多不同调用进行操作,并且功能ntopensymboliclinkobject无法成功获得处理。这是我使用的代码:

[DllImport("ntdll.dll")]
public static extern int NtOpenSymbolicLinkObject(
out SafeFileHandle LinkHandle,
uint DesiredAccess,
ref ObjectAttributes ObjectAttributes);

public enum NtStatus : uint
{
    Success = 0x00000000
}

[DllImport("ntdll.dll")]
public static extern NtStatus NtMakeTemporaryObject(SafeFileHandle Handle);


[DllImport("ntdll.dll")]
static extern int NtClose(SafeFileHandle handle);

public static void DeleteSymbolicLink(SafeKernelObjectHandle directory, string path){
    uint DELETE = 0x00010000;
    SafeFileHandle handle;
    ObjectAttributes obja = new ObjectAttributes(path, AttributeFlags.CaseInsensitive, directory, null, null);

    if (NtOpenSymbolicLinkObject( out handle, DELETE, ref obja) == 0x00000000)
    {
        NtMakeTemporaryObject( handle);
        NtClose( handle);
    }
}

public static void CreateSymlink(string inputPath, string outPath)
{
    string inputFilename = Path.GetFileName(inputPath);
    string inputDir = Path.GetDirectoryName(inputPath);

    JunctionPoint.Create(@"\RPC Control", inputDir, true);
    CreateSymbolicLink(null, @"\RPC Control\" + inputFilename, outPath);
}

static SafeKernelObjectHandle CreateSymbolicLink(SafeKernelObjectHandle directory, string path, string target)
{

    using (ObjectAttributes obja = new ObjectAttributes(path, AttributeFlags.CaseInsensitive, directory, null, null))
    {
        IntPtr handle;
        StatusToNtException(NtCreateSymbolicLinkObject(out handle, GenericAccessRights.MaximumAllowed, obja, new UnicodeString(target)));
        return new SafeKernelObjectHandle(handle, true);
    }
}



static void Main(string[] args){
    CreateSymlink("C:\tmp\MyFile.txt", fileToDelete);
    DeleteSymbolicLink(null, @"\RPC Control\" + "\??\C:\Windows\win2.ini");
    DeleteSymbolicLink(null, @"\RPC Control\" + "MyFile.txt");
    DeleteSymbolicLink(null, @"\RPC Control\" + "C:\tmp\MyFile.txt");
    DeleteSymbolicLink(null, "MyFile.txt");
    DeleteSymbolicLink(null, "C:\tmp\MyFile.txt");
    DeleteSymbolicLink(null, "\??\C:\Windows\win2.ini");
}


I am trying to remove a symbolic link that I created.
You can see it in the Object manager (WinObj):
enter image description here

I am trying to delete the folder. I saw here that I need to open a handle to object with NtOpenSymbolicLinkObject and then use NtMakeTemporaryObject to delete it.

I tried to do it with number of different calls to DeleteSymbolicLink, and the function NtOpenSymbolicLinkObject doesn't succeed to get handle. Here is the code I used:

[DllImport("ntdll.dll")]
public static extern int NtOpenSymbolicLinkObject(
out SafeFileHandle LinkHandle,
uint DesiredAccess,
ref ObjectAttributes ObjectAttributes);

public enum NtStatus : uint
{
    Success = 0x00000000
}

[DllImport("ntdll.dll")]
public static extern NtStatus NtMakeTemporaryObject(SafeFileHandle Handle);


[DllImport("ntdll.dll")]
static extern int NtClose(SafeFileHandle handle);

public static void DeleteSymbolicLink(SafeKernelObjectHandle directory, string path){
    uint DELETE = 0x00010000;
    SafeFileHandle handle;
    ObjectAttributes obja = new ObjectAttributes(path, AttributeFlags.CaseInsensitive, directory, null, null);

    if (NtOpenSymbolicLinkObject( out handle, DELETE, ref obja) == 0x00000000)
    {
        NtMakeTemporaryObject( handle);
        NtClose( handle);
    }
}

public static void CreateSymlink(string inputPath, string outPath)
{
    string inputFilename = Path.GetFileName(inputPath);
    string inputDir = Path.GetDirectoryName(inputPath);

    JunctionPoint.Create(@"\RPC Control", inputDir, true);
    CreateSymbolicLink(null, @"\RPC Control\" + inputFilename, outPath);
}

static SafeKernelObjectHandle CreateSymbolicLink(SafeKernelObjectHandle directory, string path, string target)
{

    using (ObjectAttributes obja = new ObjectAttributes(path, AttributeFlags.CaseInsensitive, directory, null, null))
    {
        IntPtr handle;
        StatusToNtException(NtCreateSymbolicLinkObject(out handle, GenericAccessRights.MaximumAllowed, obja, new UnicodeString(target)));
        return new SafeKernelObjectHandle(handle, true);
    }
}



static void Main(string[] args){
    CreateSymlink("C:\tmp\MyFile.txt", fileToDelete);
    DeleteSymbolicLink(null, @"\RPC Control\" + "\??\C:\Windows\win2.ini");
    DeleteSymbolicLink(null, @"\RPC Control\" + "MyFile.txt");
    DeleteSymbolicLink(null, @"\RPC Control\" + "C:\tmp\MyFile.txt");
    DeleteSymbolicLink(null, "MyFile.txt");
    DeleteSymbolicLink(null, "C:\tmp\MyFile.txt");
    DeleteSymbolicLink(null, "\??\C:\Windows\win2.ini");
}


如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文