我使用NtObjectManager 庫創建了一個符號注冊表項,如下所示:
using NtApiDotNet;
using System;
namespace poc
{
class Program
{
const string SrcKey = @"HKEY_CURRENT_USER\SOFTWARE\ABC";
const string TargetKey = @"HKEY_LOCAL_MACHINE\SOFTWARE\XYZ";
static NtKey CreateSymbolicLink(string name, string target)
{
name = NtKeyUtils.Win32KeyNameToNt(name);
target = NtKeyUtils.Win32KeyNameToNt(target);
return NtKey.CreateSymbolicLink(name, null, target);
}
static void Main(string[] args)
{
var link = CreateSymbolicLink(SrcKey, TargetKey)
}
}
}
當我試圖從注冊表 ( Regedit.exe) 中洗掉密鑰時,它失敗并出現錯誤:
ABC 無法打開。錯誤是阻止打開此密鑰。詳細資訊:訪問被拒絕
即使有SYSTEM權限(psexec用于啟動SYSTEMcmd),我也嘗試將其洗掉,但我仍然收到相同的錯誤。
該函式NtKey.CreateSymbolicLink正在呼叫 SetSymbolicLinkTarget,最終呼叫SetValue如下:
SetValue(SymbolicLinkValueName, RegistryValueType.Link, Encoding.Unicode.GetBytes(target), throw_on_error);
還沒想好怎么刪。
我找到了一個關于使用 C 洗掉符號注冊表項的答案lpfnZwDeleteKey,但它只是呼叫,我不知道什么是 C# 的等價物。
我嘗試了functionNtKey.UnloadKey函式,我認為它可能會有所幫助,但它沒有。
uj5u.com熱心網友回復:
我可以使用 James 的工具將其洗掉CreateRegSymlink:
CreateRegSymlink.exe -d "HKCU\Software\XYZ"
我注意到它是通過呼叫 DeleteRegSymlink來完成的。
當我檢查其中的內容時,我注意到它通過呼叫將注冊表路徑轉換為真實路徑RegPathToNative:
bstr_t symlink = RegPathToNative(lpSymlink);
在這里你可以看到什么RegPathToNative作業。
然后它呼叫:
InitializeObjectAttributes(&obj_attr, &name, OBJ_CASE_INSENSITIVE | OBJ_OPENLINK, nullptr, nullptr);
我認為這就是魔法發生的地方。
如果您對如何從符號注冊表路徑中找到真正的鏈接有任何建議,請告訴我。
編輯(2022 年 10 月 1 日) - 感謝@RbMm:
我創建了一個函式來打開符號鏈接REG_OPTION_OPEN_LINK,然后使用它洗掉它,ZwDeleteKey但重要的是設定權限RegistryRights.Delete為 @RbMm 提到的:
const int REG_OPTION_OPEN_LINK = 0x0008;
[DllImport("advapi32.dll", CharSet = CharSet.Unicode, BestFitMapping = false, ExactSpelling = true)]
static extern int RegOpenKeyExW(SafeRegistryHandle hKey, String lpSubKey,
int ulOptions, int samDesired, out SafeRegistryHandle hkResult);
[DllImport("ntdll.dll")]
private static extern int ZwDeleteKey(SafeRegistryHandle hKey);
public static RegistryKey OpenSubKeySymLink(this RegistryKey key, string name, RegistryRights rights = RegistryRights.ReadKey, RegistryView view = 0)
{
var error = RegOpenKeyExW(key.Handle, name, REG_OPTION_OPEN_LINK, ((int)rights) | ((int)view), out var subKey);
if (error != 0)
{
subKey.Dispose();
throw new Win32Exception(error);
}
return RegistryKey.FromHandle(subKey); // RegistryKey will dispose subKey
}
static void Main(string[] args)
{
RegistryKey key;
key = OpenSubKeySymLink(Microsoft.Win32.Registry.CurrentUser, @"SOFTWARE\Microsoft\Windows\ABC", RegistryRights.Delete, 0);
ZwDeleteKey(key.Handle);
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/411755.html
標籤:
上一篇:Python從行程ID或行程名稱獲取windowtitle
下一篇:如何使用C 進行灰度截圖?
