October 24, 2024
Chicago 12, Melborne City, USA
C#

SecureZeroMemory & explicit_bzero not clearing in memory dump


SecureZeroMemory() is not working, primary target is Windows, but Linux behaves same.
Specifically using HeapAlloc() to avoid CRT heap, but that is inconsequential, using malloc() didn’t make a difference.

compile and run the below code and take a memory dump either using Task Manager or SysInternal’s procdump

use strings from SysInternals to dump strings from the dump and search for give password, it will show up. see image for sample run.

how to explain this?

#include <windows.h>
#include <winbase.h>

#include <iostream>
#include <thread>

using namespace std::chrono_literals;

int main(int argc, char* argv[])
{
    const int size = 255;
    char* passwd = (char*)HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, size);

    std::cout << "enter passwd: " << std::flush;
    std::cin >> passwd;
    std::cout << "passwd " << passwd << std::endl;

    SecureZeroMemory(passwd, size);

    for (;;) {
        std::this_thread::sleep_for(1s);
        std::cout << "." << std::flush;
    }
    return 0;
}

sample run

E:\tmp>start tmp.exe

E:\tmp>procdump -ma tmp.exe

ProcDump v11.0 - Sysinternals process dump utility
Copyright (C) 2009-2022 Mark Russinovich and Andrew Richards
Sysinternals - www.sysinternals.com

[23:38:45] Dump 1 initiated: E:\tmp\tmp.exe_241024_233845.dmp
[23:38:45] Dump 1 writing: Estimated dump file size is 11 MB.
[23:38:45] Dump 1 complete: 11 MB written in 0.0 seconds
[23:38:45] Dump count reached.

E:\tmp>strings tmp.exe_241024_233845.dmp | findstr cosmic_rays
cosmic_rays



You need to sign in to view this answers

Leave feedback about this

  • Quality
  • Price
  • Service

PROS

+
Add Field

CONS

+
Add Field
Choose Image
Choose Video