我猜我可以用这个函数写我的日志,如果我能做到的话我还有另一个问题我如何在powershell或python中使用这个函数?
安全日志写入访问限制在 Windows Server 2003 中有所放宽,而没有通过引入一组特殊的 API 改变基本设计(参见图 2)。这些 API 在内部使用本地过程调用 (LPC) 与 LSA 交互,指示它代表应用程序生成审计日志。该机制优雅而简单。
首先,应用程序通过调用 AuthzRegisterSecurityEventSource 向 LSA 注册一个安全事件源句柄。这个 API 唯一感兴趣的参数是事件源的名称,它几乎可以是任何东西,但受到一些限制。例如,它不能命名为“Security”,因为该名称是为系统使用而保留的。此调用返回的安全事件源句柄用于以下步骤。
接下来,通过调用两个密切相关的 API 之一来生成事件:AuthzReportSecurityEvent 或 AuthzReportSecurityEventFromParams。最后,当应用程序关闭时,它通过调用 AuthzUnregisterSecurityEventSource 取消注册安全事件源句柄。
参考:安全日志
你能分享一下我如何调用这个函数并使用这个函数在安全日志中写入日志的例子吗?
代码示例:(C++)
#include <stdio.h>
#include <iostream>
#include <string>
#include <strsafe.h>
#include <windows.h>
#include <Authz.h>
#include <Ntsecapi.h>
#pragma comment(lib,"Authz.lib")
#pragma comment(lib,"Advapi32.lib")
BOOL SetPrivilege(
HANDLE hToken, // access token handle
LPCTSTR lpszPrivilege, // name of privilege to enable/disable
BOOL bEnablePrivilege // to enable or disable privilege
)
{
TOKEN_PRIVILEGES tp;
LUID luid;
if (!LookupPrivilegeValue(
NULL, // lookup privilege on local system
lpszPrivilege, // privilege to lookup
&luid)) // receives LUID of privilege
{
printf("LookupPrivilegeValue error: %u\n", GetLastError());
return FALSE;
}
tp.PrivilegeCount = 1;
tp.Privileges[0].Luid = luid;
if (bEnablePrivilege)
tp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
else
tp.Privileges[0].Attributes = 0;
// Enable the privilege or disable all privileges.
if (!AdjustTokenPrivileges(
hToken,
FALSE,
&tp,
sizeof(TOKEN_PRIVILEGES),
(PTOKEN_PRIVILEGES)NULL,
(PDWORD)NULL))
{
printf("AdjustTokenPrivileges error: %u\n", GetLastError());
return FALSE;
}
if (GetLastError() == ERROR_NOT_ALL_ASSIGNED)
{
printf("The token does not have the specified privilege. \n");
return FALSE;
}
printf("Get the specified privilege! \n");
return TRUE;
}
int main(int argc, const char* argv[])
{
// Declare and initialize variables.
BOOL bResult = TRUE;
DWORD event_id = 4624;
AUTHZ_SECURITY_EVENT_PROVIDER_HANDLE hEventProvider = NULL;
PAUDIT_PARAMS p;
std::string Source_Name = "Test security audit";
std::wstring ws;
std::string pbuf = "What is your purpose ?";
std::wstring ws_buf;
int return_code = 0;
int i = 0;
// Register the audit provider.
HANDLE token;
HANDLE hevent_source;
ws.assign(Source_Name.begin(), Source_Name.end());
ws_buf.assign(pbuf.begin(), pbuf.end());
if (!OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &token))
return FALSE;
SetPrivilege(token, L"SeAuditPrivilege", true);
AUTHZ_SOURCE_SCHEMA_REGISTRATION ar;
memset(&ar, 0, sizeof(ar));
ar.dwFlags = AUTHZ_ALLOW_MULTIPLE_SOURCE_INSTANCES;
ar.szEventSourceName = &ws[0];
ar.szEventMessageFile = &ws_buf[0];
ar.szEventSourceXmlSchemaFile = NULL;
ar.szEventAccessStringsFile = &ws_buf[0];
ar.szExecutableImagePath = NULL;
AuthzInstallSecurityEventSource(0, &ar);
bResult = AuthzRegisterSecurityEventSource(0, ws.c_str(), &hEventProvider);
int err = GetLastError();
if (!bResult)
{
printf("AuthzRegisterSecurityEventSource failed, error is %d\n", err);
return_code = -1;
}
SID id;
if (hEventProvider)
{
// Generate the audit.
while (i < 10) {
bResult = AuthzReportSecurityEvent(
APF_AuditSuccess,
hEventProvider,
event_id,
NULL,
3,
APT_String, L"Jay Hamlin",
APT_String, L"March 21, 1960",
APT_Ulong, 45);
int err1 = GetLastError();
if (!bResult)
{
printf("AuthzReportSecurityEvent failed, error is %d\n", err1);
return_code = -2;
break;
}
i++;
}
AuthzUnregisterSecurityEventSource(0, &hEventProvider);
AuthzUninstallSecurityEventSource(0, &ws[0]);
}
std::cout << "Exit : " << return_code << std::endl;
getchar();
}
注意:在运行代码示例之前,您必须在本地安全策略中做一些事情。步骤可以参考:https ://stackoverflow.com/a/18242724/11128312
为当前用户分配权限后,请重启电脑使其生效。
更新:
请转到本地政策->审核政策。为成功和失败启用“审计对象访问”。

然后你再次重建和调试,你会发现安全日志出现在事件查看器中。
