currently I have issue on project where secure string is exposed like this:
IntPtr unmanagedString = IntPtr.Zero;
try
{
unmanagedString = Marshal.SecureStringToGlobalAllocUnicode(secureString);
string str = Marshal.PtrToStringUni(unmanagedString);
...
...
}
finally
{
Marshal.ZeroFreeGlobalAllocUnicode(ptr);
}
After Marshal.SecureStringToGlobalAllocUnicode(secureString)
call, copy of secure string content is saved in unmanaged memory. Even after Marshal.ZeroFreeGlobalAllocUnicode(ptr)
is called string can be easily found with memory tools, by simply searching for all strings.
Is there a way to completely remove it or at least go around it in some way, like overwrite it?