windos 2012 r2安全事件日志自定义插入

问题描述

我正在尝试在Windows Server 2012 R2中写入日志 我可以这样写应用程序日志,

Write-EventLog -LogName Application -Source "mysource" other parameters goes here 

它工作正常,并将此日志写入Windowslog /应用程序

在那之后,我试图以此作为安全日志

Write-EventLog -LogName Security -Source "Microsoft-Windows-Security-Auditing" other parameters goes here 

返回此错误

Write-EventLog : The registry key for the log "Security" for source "Microsoft-Windows-Security-Auditing" Could not be
opened.
At line:1 char:1
+ Write-EventLog -LogName Security -Source "Microsoft-Windows-Security-Auditing" - ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : PermissionDenied: (:) [Write-EventLog],Exception
    + FullyQualifiedErrorId : AccessDenied,Microsoft.PowerShell.Commands.WriteEventLogCommand

在那之后我搜索并找到了写安全日志的功能 AuthzReportSecurityEvent 我猜我可以使用此功能写日志,如果我可以做到这一点,我还有另一个问题,我该如何在Powershell或python中使用此功能? 我想我可以通过pywin32模块使用此功能吗?还是可以直接在Powershell脚本中调用? 您能否分享给我任何示例,我如何调用函数并使用此函数将日志写入安全日志。

按照@Strive Sun的建议,我可以编写登录安全性。

解决方法

我猜我可以使用此功能写日志,如果我可以这样做 还有另一个问题,我如何在Powershell中使用此功能或 python吗?

在Windows Server 2003中,安全日志写访问限制在某种程度上得到了放松,而没有通过引入一组特殊的API来更改基本设计(请参见图2)。这些API在内部使用本地过程调用(LPC)与LSA进行交互,指示LSA代表应用程序生成审核日志。该机制优雅而简单。

首先,应用程序通过调用AuthzRegisterSecurityEventSource向LSA注册安全事件源句柄。此API唯一感兴趣的参数是事件源的名称,该名称可以是几乎任何内容,并受一些限制。例如,它不能命名为“ Security”,因为该名称保留供系统使用。在以下步骤中使用此调用返回的安全事件源句柄。

接下来,通过调用两个紧密相关的API之一来生成事件:AuthzReportSecurityEvent或AuthzReportSecurityEventFromParams。最后,当应用程序关闭时,它将通过调用AuthzUnregisterSecurityEventSource取消注册安全事件源句柄。

引用:The Security log

您能给我分享任何示例吗?如何调用此函数并编写 使用此功能登录安全日志。

代码示例:(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,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",L"March 21,1960",APT_Ulong,45);
            int err1 = GetLastError();
            if (!bResult)
            {
                printf("AuthzReportSecurityEvent failed,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

为当前用户分配权限后,请重新启动计算机以使其生效。

已更新:

请转到本地政策->审核政策。为成功和失败启用“审核对象访问”。

enter image description here

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

enter image description here