Search code examples
c++winapiwindows-api-code-pack

Format log 4688 exception with FormatMessageW


Formatting a message from library 4688 adtschema.dll, FormatMessage will not show "The New Processes" in its entirety, regardless of whether it uses the A or W version of the function.

I used ReadEventLogW to read the log to 4688 and then formatted it with FormatMessageW, but it didn't format correctly. Here's my attempt to extract Message Table 4688. Neither using the W or A function can display the results correctly.

This New Process Name and Creator Process Name is error, should New Process Name:%t%6! S!%n%t be New Process Name:%t%6!s!%n%t instead?

"//" is a comment I used both schemes, and the A and W functions failed to get the correct output.

The content of pMessage is the 4688 message I extracted from adtschema.dll.

I hope the value of "New Process Name" after formatting is not garbled and empty.

#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);
}

Solution

  • Although ! S! is valid syntax for a format specification, the S format type really should be the s type in this example, and the (blank) flag is ignored for the S/s type and thus should be removed.

    So, %6! S! and %14! S! really should be %6!s! and %14!s!, respectively. Which can then be simplified to just %6 and %14.

    The library's use of the S format type means the two file paths must be in the opposite encoding than the other arguments. In other words:

    • in FormatMessageA, the s type expects a narrow ANSI (char) string, and the S type expects a wide Unicode (wchar_t) string.

    • in FormatMessageW, the s type expects a wide Unicode (wchar_t) string, and the S type expects a narrow ANSI (char) string.

    So, you will need to either:

    • use FormatMessageA() with char strings, except for arguments %6 and %14, which will be expecting wchar_t strings.

    • use FormatMessageW() with wchar_t strings, except for arguments %6 and %14, which will be expecting char strings.

    • if all of the arguments use the same encoding, then change the message text before you format it to use !s! (or just omit the specification entirely) instead of using ! S!.