【问题标题】:Permissions error when editing Windows audit policy programmatically via Winapi通过 Winapi 以编程方式编辑 Windows 审核策略时出现权限错误
【发布时间】:2014-06-25 16:35:33
【问题描述】:

短版:

我正在尝试编写一个在 Windows 8 上启用进程创建日志记录的 C++ 程序。我知道使用 auditpol.exe 可以做到这一点,但我想以编程方式进行。我的研究表明,唯一的方法是通过 Windows API 命令AuditSetSystemPolicy,因此我编写了一个调用此函数的 C++ 程序(见下文)。但是,该程序因权限问题而失败(错误代码 1314)。我以管理员身份运行 Visual Studio,并尝试在以管理员身份运行的命令提示符中执行该程序,但仍然出现错误。

加长版:

以下程序采用GUID string describing the Process Creation Subcategory 我想开始审核并将其转换为 GUID 结构。然后它从 GUID 和 ULONG 构造一个 AUDIT_POLICY_INFORMATION 结构,描述我想要进行的更改(启用成功和失败的日志记录)。最后,我将结构放入一个数组并调用AuditSetSystemPolicy 函数。

// Subcategory GUID for Process Creation
string guidstr ("{0CCE922B-69AE-11D9-BED3-505054503030}");

// Construct a GUID object
GUID guid;
HRESULT hr = CLSIDFromString(s2ws (guidstr).c_str(), (LPCLSID)&guid);

// Check if the GUID converted correctly
if (hr == S_OK)
{
    cout << "GUID successfully converted: " << endl;

    // Print english version of the SubCateogory GUID according to the API
    PSTR *output = new PSTR("");
    bool categ_name = AuditLookupSubCategoryName(&guid, output);
    cout << *output << endl;
}
else
{
    cout << "GUID failed conversion" << endl;
}

// Create an AUDIT_POLICY_INFORMATION structure describing the desired change
// The AuditCategoryGuid field will be ignored according to documentation
AUDIT_POLICY_INFORMATION audit;
audit.AuditCategoryGuid = (GUID)guid;
audit.AuditSubCategoryGuid = (GUID)guid;

// Turn on auditing for success and failure
audit.AuditingInformation = 0x00000003;

// Create an array of AUDIT_POLICY_INFORMATION change requests
AUDIT_POLICY_INFORMATION arr[1];
arr[0] = audit;

bool policyChanged = TRUE;
policyChanged = AuditSetSystemPolicy(arr, 1);
DWORD last_error = GetLastError();

// Check if the policy change succeeded or not
if (policyChanged == TRUE)
{
    cout << "Successfully set policy" << endl;
}
else
{
    cout << "Failed to set policy. Error:" << endl;     
    cout << last_error << endl;
}

我使用 Visual Studio Professional 2013 运行此代码,该代码是通过选择“以管理员身份运行”启动的。它会产生以下输出:

GUID successfully converted:
Process Creation
Failed to set policy. Error:
1314

代码 1314 的意思是:“A required privilege is not held by the client.”根据 AuditSetSystemPolicy 文档:“要成功调用此函数,调用者必须具有 SeSecurityPrivilege 或对 Audit 安全对象具有 AUDIT_SET_SYSTEM_POLICY 访问权限。”我关注了instructions on TechNet 并验证了管理员有权管理审计和安全性。为了更好地衡量,我还给了我的用户这些权限并重新启动计算机以确保应用更改。我仍然收到错误消息。

我还尝试使用 auditpol.exe 手动关闭进程创建日志记录,运行上述代码,然后使用 auditpol.exe 验证日志记录仍然关闭。我还打开了事件查看器并手动验证没有发生日志记录。

【问题讨论】:

    标签: c++ windows winapi


    【解决方案1】:

    我使用了这段代码并且不得不纠正一些小问题,但总体而言,您需要在您的进程对象上设置“SeSecuritiyPrivilege”。以管理员或系统身份运行将授予您设置此标志的权限,但您必须在尝试设置审核级别之前在代码中设置它:

    #include <atlsecurity.h>
    ...
    ATL::CAccessToken processToken;
    processToken.GetEffectiveToken(TOKEN_QUERY | TOKEN_ADJUST_PRIVILEGES);
    processToken.EnablePrivilege("SeSecuritiyPrivilege");
    

    在您尝试设置审核级别之前执行此操作。

    【讨论】:

    • 非常感谢您的帮助。代码中的设置权限是我严重遗漏的部分。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-11
    • 1970-01-01
    • 2016-10-29
    • 1970-01-01
    • 2013-10-24
    • 2017-07-27
    相关资源
    最近更新 更多