Access violation reading location, PUNICODE_STRING, and _snwprintf_s

I wrote a Password Filter DLL to intercept password changes and pass them off to PowerShell so it can then update our other systems… which doesn’t work. But because it’s a DLL, I can’t exactly step through the code to see why it doesn’t work. So I decided to write a little app that sets up some variables, then called a copy of the same PasswordChangeNotify() function that I was using in the DLL.

This has helped me solve a lot of the issues, but there’s still one that remains. Whenever I try to run the line that actually makes the PowerShell command line, I get this error:

Exception thrown at 0x00007FFC024113FC (ucrtbased.dll) in TestDLLOutput2.exe: 0xC0000005: Access violation reading location 0xFFFFFFFFFFFFFFFF.

I’m working in Visual Studio 2022, and here’s my code:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>#include <iostream>
#include <Windows.h>
#include <stdio.h>
#include <stdlib.h>
#include <winternl.h>
#include <string.h>
using namespace std;
NTSTATUS __stdcall PasswordChangeNotify(PUNICODE_STRING UserName, ULONG RelativeId, PUNICODE_STRING NewPassword);
int main()
{
long IDNum = 123456;
UNICODE_STRING SupUserName;
RtlInitUnicodeString(&SupUserName, L"cyousoon");
UNICODE_STRING SupPasWrd;
RtlInitUnicodeString(&SupPasWrd, L"FunkyChicken");
NTSTATUS ReturnNum = 0;
ReturnNum = PasswordChangeNotify(&SupUserName, IDNum, &SupPasWrd);
}
NTSTATUS __stdcall PasswordChangeNotify(PUNICODE_STRING UserName, ULONG RelativeId, PUNICODE_STRING NewPassword)
{
std::wcout << L"Running PasswordChangeNotifyn";
// Initialize pszCmdLine to nullptr
PWSTR pszCmdLine = nullptr;
int lenConstant = wcslen(L"powershell.exe SD60PWS.ps1 -Section 'ChangeNotify' -UserName -NewPassword ");
int lenUsername = wcslen(UserName->Buffer);
int lenNewPassword = wcslen(NewPassword->Buffer);
int totalLen = lenConstant + lenUsername + lenNewPassword;
// Calculate the required length for the command line
//int totalLen = _snwprintf_s(nullptr, 0, 0,
// L"powershell.exe SD60PWS.ps1 -Section 'ChangeNotify' -UserName @%wZ -NewPassword @%wZ",
// UserName->Buffer, NewPassword->Buffer);
if (totalLen > 0)
{
std::wcout << L"len > 0n";
// Allocate memory for pszCmdLine
pszCmdLine = (PWSTR)alloca((totalLen + 1) * sizeof(WCHAR));
// Format the command line
_snwprintf_s(pszCmdLine, totalLen + 1, _TRUNCATE,
L"powershell.exe SD60PWS.ps1 -Section 'ChangeNotify' -UserName @%wZ -NewPassword @%wZ",
UserName->Buffer, NewPassword->Buffer);
std::wcout << pszCmdLine << std::endl;
// Create the process
STARTUPINFO si{ sizeof(si) };
PROCESS_INFORMATION pi;
if (CreateProcessW(0, pszCmdLine, 0, 0, 0, 0, 0, 0, &si, &pi))
{
CloseHandle(pi.hThread);
CloseHandle(pi.hProcess);
}
}
return 0;
}
</code>
<code>#include <iostream> #include <Windows.h> #include <stdio.h> #include <stdlib.h> #include <winternl.h> #include <string.h> using namespace std; NTSTATUS __stdcall PasswordChangeNotify(PUNICODE_STRING UserName, ULONG RelativeId, PUNICODE_STRING NewPassword); int main() { long IDNum = 123456; UNICODE_STRING SupUserName; RtlInitUnicodeString(&SupUserName, L"cyousoon"); UNICODE_STRING SupPasWrd; RtlInitUnicodeString(&SupPasWrd, L"FunkyChicken"); NTSTATUS ReturnNum = 0; ReturnNum = PasswordChangeNotify(&SupUserName, IDNum, &SupPasWrd); } NTSTATUS __stdcall PasswordChangeNotify(PUNICODE_STRING UserName, ULONG RelativeId, PUNICODE_STRING NewPassword) { std::wcout << L"Running PasswordChangeNotifyn"; // Initialize pszCmdLine to nullptr PWSTR pszCmdLine = nullptr; int lenConstant = wcslen(L"powershell.exe SD60PWS.ps1 -Section 'ChangeNotify' -UserName -NewPassword "); int lenUsername = wcslen(UserName->Buffer); int lenNewPassword = wcslen(NewPassword->Buffer); int totalLen = lenConstant + lenUsername + lenNewPassword; // Calculate the required length for the command line //int totalLen = _snwprintf_s(nullptr, 0, 0, // L"powershell.exe SD60PWS.ps1 -Section 'ChangeNotify' -UserName @%wZ -NewPassword @%wZ", // UserName->Buffer, NewPassword->Buffer); if (totalLen > 0) { std::wcout << L"len > 0n"; // Allocate memory for pszCmdLine pszCmdLine = (PWSTR)alloca((totalLen + 1) * sizeof(WCHAR)); // Format the command line _snwprintf_s(pszCmdLine, totalLen + 1, _TRUNCATE, L"powershell.exe SD60PWS.ps1 -Section 'ChangeNotify' -UserName @%wZ -NewPassword @%wZ", UserName->Buffer, NewPassword->Buffer); std::wcout << pszCmdLine << std::endl; // Create the process STARTUPINFO si{ sizeof(si) }; PROCESS_INFORMATION pi; if (CreateProcessW(0, pszCmdLine, 0, 0, 0, 0, 0, 0, &si, &pi)) { CloseHandle(pi.hThread); CloseHandle(pi.hProcess); } } return 0; } </code>
#include <iostream>
#include <Windows.h>
#include <stdio.h>
#include <stdlib.h>
#include <winternl.h>
#include <string.h>

using namespace std;

NTSTATUS __stdcall PasswordChangeNotify(PUNICODE_STRING UserName, ULONG RelativeId, PUNICODE_STRING NewPassword);


int main()
{
    long IDNum = 123456;
    UNICODE_STRING SupUserName;
    RtlInitUnicodeString(&SupUserName, L"cyousoon");
    UNICODE_STRING SupPasWrd;
    RtlInitUnicodeString(&SupPasWrd, L"FunkyChicken");
    NTSTATUS ReturnNum = 0;

    ReturnNum = PasswordChangeNotify(&SupUserName, IDNum, &SupPasWrd);

}


NTSTATUS __stdcall PasswordChangeNotify(PUNICODE_STRING UserName, ULONG RelativeId, PUNICODE_STRING NewPassword)
{
    std::wcout << L"Running PasswordChangeNotifyn";
    
    // Initialize pszCmdLine to nullptr
    PWSTR pszCmdLine = nullptr;

   
    int lenConstant = wcslen(L"powershell.exe SD60PWS.ps1 -Section 'ChangeNotify' -UserName  -NewPassword ");
    int lenUsername = wcslen(UserName->Buffer);
    int lenNewPassword = wcslen(NewPassword->Buffer);
    int totalLen = lenConstant + lenUsername + lenNewPassword;
    
    // Calculate the required length for the command line
    //int totalLen = _snwprintf_s(nullptr, 0, 0,
    //    L"powershell.exe SD60PWS.ps1 -Section 'ChangeNotify' -UserName @%wZ -NewPassword @%wZ",
    //    UserName->Buffer, NewPassword->Buffer);

    if (totalLen > 0)
    {
        std::wcout << L"len > 0n";
        
        // Allocate memory for pszCmdLine
        pszCmdLine = (PWSTR)alloca((totalLen + 1) * sizeof(WCHAR));

        // Format the command line
        _snwprintf_s(pszCmdLine, totalLen + 1, _TRUNCATE,
            L"powershell.exe SD60PWS.ps1 -Section 'ChangeNotify' -UserName @%wZ -NewPassword @%wZ",
            UserName->Buffer, NewPassword->Buffer);


        std::wcout << pszCmdLine << std::endl;

        // Create the process
        STARTUPINFO si{ sizeof(si) };
        PROCESS_INFORMATION pi;
        if (CreateProcessW(0, pszCmdLine, 0, 0, 0, 0, 0, 0, &si, &pi))
        {
            CloseHandle(pi.hThread);
            CloseHandle(pi.hProcess);
        }
    }


    return 0;
}

If I let it run to just before the _snwprintf_s command (using Run To Cursor), everything looks ok, and here’s the values of the variables:
(Note that this is not actually code I wrote, but rather copy-paste information from hovering over the variables once execution paused. It’s just in a code block to satisfy the question editor.)

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>UserName = 0x00000069bd4ff888 {Length=16 MaximumLength=18 Buffer=0x00007ff7a5c8aea8 L"cyousoon" }
NewPassword = 0x00000069bd4ff8b8 {Length=24 MaximumLength=26 Buffer=0x00007ff7a5c8aec0 L"FunkyChicken" }
lenConstant = 75
lenUsername = 8
lenNewPassword = 12
totalLen = 95
pszCmdLine = 0x00000069bd4ff500 L"(98 Unicode characters, removed so this question wouldn't get marked as Spam)"
</code>
<code>UserName = 0x00000069bd4ff888 {Length=16 MaximumLength=18 Buffer=0x00007ff7a5c8aea8 L"cyousoon" } NewPassword = 0x00000069bd4ff8b8 {Length=24 MaximumLength=26 Buffer=0x00007ff7a5c8aec0 L"FunkyChicken" } lenConstant = 75 lenUsername = 8 lenNewPassword = 12 totalLen = 95 pszCmdLine = 0x00000069bd4ff500 L"(98 Unicode characters, removed so this question wouldn't get marked as Spam)" </code>
UserName = 0x00000069bd4ff888 {Length=16 MaximumLength=18 Buffer=0x00007ff7a5c8aea8 L"cyousoon" }
NewPassword = 0x00000069bd4ff8b8 {Length=24 MaximumLength=26 Buffer=0x00007ff7a5c8aec0 L"FunkyChicken" }
lenConstant = 75
lenUsername = 8
lenNewPassword = 12
totalLen = 95
pszCmdLine = 0x00000069bd4ff500 L"(98 Unicode characters, removed so this question wouldn't get marked as Spam)"

At first I though that maybe the alloca command wasn’t allocating the right amount of memory. So I started looking into it and ended up going down the rabbit hole of “alloca is OK, if called in a function, because when you return the memory is freed up automatically” and “Never use alloca! Use malloc and free instead.” then “Never use malloc/free! You need to use new/delete instead.”

So, as a test, I simplified my code to this:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>NTSTATUS __stdcall PasswordChangeNotify(PUNICODE_STRING UserName, ULONG RelativeId, PUNICODE_STRING NewPassword)
{
PWSTR pszCmdLine = nullptr;
int totalLen = wcslen(L"powershell.exe SD60PWS.ps1 -Section 'ChangeNotify' -UserName cyousoon -NewPassword FunkyChicken");
if (totalLen > 0)
{
pszCmdLine = (PWSTR)alloca((totalLen + 1) * sizeof(WCHAR));
_snwprintf_s(pszCmdLine, totalLen + 1, _TRUNCATE,
L"powershell.exe SD60PWS.ps1 -Section 'ChangeNotify' -UserName cyousoon -NewPassword FunkyChicken");
STARTUPINFO si{ sizeof(si) };
PROCESS_INFORMATION pi;
if (CreateProcessW(0, pszCmdLine, 0, 0, 0, 0, 0, 0, &si, &pi))
{
CloseHandle(pi.hThread);
CloseHandle(pi.hProcess);
}
}
return 0;
}
</code>
<code>NTSTATUS __stdcall PasswordChangeNotify(PUNICODE_STRING UserName, ULONG RelativeId, PUNICODE_STRING NewPassword) { PWSTR pszCmdLine = nullptr; int totalLen = wcslen(L"powershell.exe SD60PWS.ps1 -Section 'ChangeNotify' -UserName cyousoon -NewPassword FunkyChicken"); if (totalLen > 0) { pszCmdLine = (PWSTR)alloca((totalLen + 1) * sizeof(WCHAR)); _snwprintf_s(pszCmdLine, totalLen + 1, _TRUNCATE, L"powershell.exe SD60PWS.ps1 -Section 'ChangeNotify' -UserName cyousoon -NewPassword FunkyChicken"); STARTUPINFO si{ sizeof(si) }; PROCESS_INFORMATION pi; if (CreateProcessW(0, pszCmdLine, 0, 0, 0, 0, 0, 0, &si, &pi)) { CloseHandle(pi.hThread); CloseHandle(pi.hProcess); } } return 0; } </code>
NTSTATUS __stdcall PasswordChangeNotify(PUNICODE_STRING UserName, ULONG RelativeId, PUNICODE_STRING NewPassword)
{
    PWSTR pszCmdLine = nullptr;

    int totalLen = wcslen(L"powershell.exe SD60PWS.ps1 -Section 'ChangeNotify' -UserName cyousoon -NewPassword FunkyChicken");

    if (totalLen > 0)
    {
        pszCmdLine = (PWSTR)alloca((totalLen + 1) * sizeof(WCHAR));

        _snwprintf_s(pszCmdLine, totalLen + 1, _TRUNCATE,
            L"powershell.exe SD60PWS.ps1 -Section 'ChangeNotify' -UserName cyousoon -NewPassword FunkyChicken");

        STARTUPINFO si{ sizeof(si) };
        PROCESS_INFORMATION pi;
        if (CreateProcessW(0, pszCmdLine, 0, 0, 0, 0, 0, 0, &si, &pi))
        {
            CloseHandle(pi.hThread);
            CloseHandle(pi.hProcess);
        }
    }

    return 0;
}

This code works beautifully, but then I tried adding one of the variables back in (trying both UserName and NewPassword individually) and it didn’t matter which one I tried, they both caused the same error. (well, not exactly the same, the ‘thrown at’ number changes slightly, but the rest of the error is the same)

OK, so now I know that the error has something to do with accessing the memory for the passed variables, but I have no idea what the problem is. Are the variables not declared properly in main()? Is there something wrong with how they are passed to PasswordChangeNotify()? Is alloca really not allocating memory properly? Is there something wrong with the _snwprintf_s command?

I really appreciate any help you experts can give me. I feel like I’m so close to getting this working, but I just need a little more guidance.

Additional Notes:

In my initial DLL code, I was passing RelativeId out to the PowerShell script, just to see what it was. Then I decided to drop it, as I know it won’t be relevant to the PowerShell script anyway. However, I’m still setting up IDNum in main() and passing it to PasswordChangeNotify() because I want to emulate what Windows would do as closely as possible.

Also, I left the commented code that I was originally using to calculate the total length, for reference. It would always give me a length of 0, so I switched to figuring out individual lengths and adding them up.

Thank you

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật