При форматировании сообщения из библиотеки 4688 adtschema.dll
, FormatMessage
не будет отображаться "The New Processes"
полностью, независимо от того, использует ли оно версию функции A
или W
.
Я использовал ReadEventLogW
, чтобы прочитать журнал до 4688, а затем отформатировал его с помощью FormatMessageW
, но он отформатировался неправильно. Вот моя попытка извлечь таблицу сообщений 4688. Ни использование функции W
, ни A
не может правильно отобразить результаты.
Это New Process Name
и Creator Process Name
ошибка, вместо этого должно быть New Process Name:%t%6! S!%n%t
New Process Name:%t%6!s!%n%t
?
"//"
— комментарий. Я использовал обе схемы, но функциям A
и W
не удалось получить правильный результат.
Содержимое pMessage
— это сообщение 4688, которое я извлек из adtschema.dll
.
Надеюсь, значение "New Process Name"
после форматирования не будет искажено и пусто.
#include <windows.h>
#include <stdio.h>
void main(void)
{
//adtschema.dll (library:10.0.22621.2506) messageTable id 4688
const wchar_t* pMessage = L"A new process has been created.%n%nCreator Subject:%n%tSecurity ID:%t%t%1%n%tAccount Name:%t%t%2%n%tAccount Domain:%t%t%3%n%tLogon ID:%t%t%4%n%nTarget Subject:%n%tSecurity ID:%t%t%10%n%tAccount Name:%t%t%11%n%tAccount Domain:% t%t%12%n%tLogon ID:%t%t%13%n%nProcess Information:%n%tNew Process ID:%t%t%5%n%tNew Process Name:%t%6! S!%n%tToken Elevation Type:%t%7%n%tMandatory Label:%t%t%15%n%tCreator Process ID:%t%8%n%tCreator Process Name:%t%14! S!%n%tProcess Command Line:%t%9! S!%n%nToken Elevation Type indicates the type of token that was assigned to the new process in accordance with User Account Control policy.%n%nType 1 is a full token with no privileges removed or groups disabled. A full token is only used if User Account Control is disabled or if the user is the built-in Administrator account or a service account.%n%nType 2 is an elevated token with no privileges removed or groups disabled. An elevated token is used when User Account Control is enabled and the user chooses to start the program using Run as administrator. An elevated token is also used when an application is configured to always require administrative privilege or to always require maximum privilege, and the user is a member of the Administrators group.%n%nType 3 is a limited token with administrative privileges removed and administrative groups disabled. The limited token is used when User Account Control is enabled, the application does not require administrative privilege, and the user does not choose to start the program using Run as administrator.";
DWORD_PTR pArgs[16] = { (DWORD_PTR)L"S-1-5-21-2772055006-295584695-1149559003-500",
(DWORD_PTR) L"Administrator",
(DWORD_PTR) L"cs-NBIR",
(DWORD_PTR) L"0x5b45f71f",
(DWORD_PTR) L"0x2730",
(DWORD_PTR) L"C:/Program Files/Microsoft Visual Studio/2022/Professional/Common7/IDE/Remote Debugger/x64/msvsmon.exe",
(DWORD_PTR) L"%%1936",
(DWORD_PTR) L"0x204c",
(DWORD_PTR) L"",
(DWORD_PTR) L"S-1-0-0",
(DWORD_PTR) L"-",
(DWORD_PTR) L"-",
(DWORD_PTR) L"0x0",
(DWORD_PTR) L"C:/Program Files/Microsoft Visual Studio/2022/Professional/Common7/IDE/devenv.exe",
(DWORD_PTR) L"S-1-16-12288",
};
pArgs[15] = 0;
const DWORD size = 4000;
WCHAR buffer[size];
if (! FormatMessageW(FORMAT_MESSAGE_FROM_STRING | FORMAT_MESSAGE_ARGUMENT_ARRAY,
pMessage,
0,
0,
buffer,
size,
(va_list*)pArgs))
{
wprintf(L"Format message failed with 0x%x\n", GetLastError());
return;
}
wprintf(L"Formatted message: %s\n", buffer);
//const char* pMessage = "A new process has been created.%n%nCreator Subject:%n%tSecurity ID:%t%t%1%n%tAccount Name:%t%t%2%n%tAccount Domain:%t%t%3%n%tLogon ID:%t%t%4%n%nTarget Subject:%n%tSecurity ID:%t%t%10%n%tAccount Name:%t%t%11%n%tAccount Domain:%t%t%12%n%tLogon ID:% t%t%13%n%nProcess Information:%n%tNew Process ID:%t%t%5%n%tNew Process Name:%t%6! S!%n%tToken Elevation Type:%t%7%n%tMandatory Label:%t%t%15%n%tCreator Process ID:%t%8%n%tCreator Process Name:%t%14! S!%n%tProcess Command Line:%t%9! S!%n%nToken Elevation Type indicates the type of token that was assigned to the new process in accordance with User Account Control policy.%n%nType 1 is a full token with no privileges removed or groups disabled. A full token is only used if User Account Control is disabled or if the user is the built-in Administrator account or a service account.%n%nType 2 is an elevated token with no privileges removed or groups disabled. An elevated token is used when User Account Control is enabled and the user chooses to start the program using Run as administrator. An elevated token is also used when an application is configured to always require administrative privilege or to always require maximum privilege, and the user is a member of the Administrators group.%n%nType 3 is a limited token with administrative privileges removed and administrative groups disabled. The limited token is used when User Account Control is enabled, the application does not require administrative privilege, and the user does not choose to start the program using Run as administrator.";
//DWORD_PTR pArgs[16] = { (DWORD_PTR)"S-1-5-21-2772055006-295584695-1149559003-500",
//(DWORD_PTR)"Administrator",
//(DWORD_PTR)"cs-NBIR",
// (DWORD_PTR)"0x5b45f71f",
// (DWORD_PTR)"0x2730",
// (DWORD_PTR)"C:/Program Files/Microsoft Visual Studio/2022/Professional/Common7/IDE/Remote Debugger/x64/msvsmon.exe",
// (DWORD_PTR)"%%1936",
// (DWORD_PTR)"0x204c",
// (DWORD_PTR)"",
// (DWORD_PTR)"S-1-0-0",
// (DWORD_PTR)"-",
// (DWORD_PTR)"-",
// (DWORD_PTR)"0x0",
// (DWORD_PTR)"C:/Program Files/Microsoft Visual Studio/2022/Professional/Common7/IDE/devenv.exe",
// (DWORD_PTR)"S-1-16-12288",
//};
//pArgs[15] = 0;
//const DWORD size = 4000;
//char buffer[size];
//if (! FormatMessageA(FORMAT_MESSAGE_FROM_STRING | FORMAT_MESSAGE_ARGUMENT_ARRAY,
// pMessage,
// 0,
// 0,
// buffer,
// size,
// (va_list*)pArgs))
//{
// printf("Format message failed with 0x%x\n", GetLastError());
// return;
//}
//printf("Formatted message: %s\n", buffer);
}
Хотя ! S!
является допустимым синтаксисом для спецификации формата , тип формата S
действительно должен быть типом s
в этом примере, а флаг (пустой) игнорируется для типа
S
/s
и поэтому его следует удалить.
Итак, %6! S!
и %14! S!
на самом деле должны быть %6!s!
и %14!s!
соответственно. Затем это можно упростить до %6
и %14
.
Использование библиотекой типа формата S
означает, что два пути к файлам должны иметь кодировку, противоположную кодировке других аргументов. Другими словами:
в FormatMessageA
тип s
ожидает узкую строку ANSI (char
), а тип S
— широкую строку Unicode (wchar_t
).
в FormatMessageW
тип s
ожидает широкую строку Unicode (wchar_t
), а тип S
— узкую строку ANSI (char
).
Итак, вам нужно будет либо:
используйте FormatMessageA()
со строками char
, за исключением аргументов %6
и %14
, которые будут ожидать строки wchar_t
.
используйте FormatMessageW()
со строками wchar_t
, за исключением аргументов %6
и %14
, которые будут ожидать строки char
.
если все аргументы используют одну и ту же кодировку, измените текст сообщения перед его форматированием, чтобы использовать !s!
(или просто полностью опустите спецификацию) вместо использования ! S!
.