[英]Permissions error when editing Windows audit policy programmatically via Winapi
简洁版本:
我正在尝试编写一个在Windows 8上启用进程创建日志记录的C ++程序。我知道可以使用auditpol.exe进行此操作,但是我想以编程方式进行操作。 我的研究表明,执行此操作的唯一方法是通过Windows API命令AuditSetSystemPolicy ,因此我编写了一个调用此函数的C ++程序(请参见下文)。 但是,该程序引用许可问题失败(错误代码1314)。 我以管理员身份运行Visual Studio,并且尝试在以管理员身份运行的命令提示符下执行程序,但仍然出现错误。
长版:
以下程序采用一个GUID字符串来描述我要开始审核的“流程创建”子类别 ,并将其转换为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的意思是:“ 客户端不拥有所需的特权。 ”根据AuditSetSystemPolicy文档:“要成功调用此函数,调用者必须对Audit安全对象具有SeSecurityPrivilege或AUDIT_SET_SYSTEM_POLICY访问权限。” 我按照TechNet上的说明进行操作,并验证管理员具有管理审核和安全性的权限。 作为一项很好的措施,我还为我的用户提供了这些权限,并重新启动了计算机以确保已应用更改。 我仍然遇到错误。
我还尝试使用auditpol.exe手动关闭“进程创建”日志记录,运行上述代码,然后使用auditpol.exe验证日志记录是否仍处于关闭状态。 我还拉起了事件查看器,并手动验证了没有日志记录正在发生。
我使用了此代码,并且不得不纠正一些小问题,但是总体而言,您需要在过程对象上设置“ SeSecuritiyPrivilege”。 以管理员或系统身份运行将授予您设置此标志的权限,但是在尝试设置审核级别之前,必须在代码内进行设置:
#include <atlsecurity.h>
...
ATL::CAccessToken processToken;
processToken.GetEffectiveToken(TOKEN_QUERY | TOKEN_ADJUST_PRIVILEGES);
processToken.EnablePrivilege("SeSecuritiyPrivilege");
在尝试设置审核级别之前,请执行此操作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.