Windows — запись журнала событий в канал безопасности Переполнение стека

В настоящее время я пытаюсь записать событие в журналы безопасности. Я использую учетную запись с привилегией «SeAuditPrivilege». Когда я пытаюсь использовать следующий код, все работает хорошо, за исключением того, что ничего не записывается в журналах безопасности.

Есть идеи или опыт?

#include <stdio.h>
#include <iostream>
#include <string>
#include <strsafe.h>
#include <windows.h>
#include <Authz.h>
#include "report_security_event.h"
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;
}

return TRUE;
}

int print_error(wchar_t* f) {
wchar_t buf[256];
DWORD dwError;
dwError = GetLastError();FormatMessageW(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
NULL, dwError, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
buf, (sizeof(buf) / sizeof(wchar_t)), NULL);
wprintf(
L"%s (%d): %s",
f,
dwError,
&buf);
return dwError;
}

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, "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);
print_error(L"AuthzInstallSecurityEventSource");

// RegisterEventSource(NULL, Source_Name.c_str());

bResult = AuthzRegisterSecurityEventSource(0, ws.c_str(), &hEventProvider);

print_error(L"AuthzRegisterSecurityEventSource");
if (!bResult)
{
print_error(L"AuthzRegisterSecurityEventSource");
return_code = -1;
}
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);

print_error(L"AuthzReportSecurityEvent");
if (!bResult)
{
return_code = -2;
break;
}

i++;
}

AuthzUnregisterSecurityEventSource(0, &hEventProvider);
print_error(L"AuthzUnregisterSecurityEventSource");
AuthzUninstallSecurityEventSource(0, &ws[0]);
print_error(L"AuthzUninstallSecurityEventSource");
}
std::cout << "Exit  : " << return_code << std::endl;
exit(return_code);
}

Я пытался использовать метод AuthzInstallSecurityEventSource, AuthzRegisterSecurityEventSource, AuthzReportSecurityEvent из документации (https://msdn.microsoft.com/en-us/library/windows/desktop/aa376317(v=vs.85).aspx) но безуспешно.

0

Решение

Задача ещё не решена.

Другие решения

Других решений пока нет …

По вопросам рекламы ammmcru@yandex.ru
Adblock
detector