很長時間潛伏在這裏終於有一個問題,我沒有看到。我正在用dotnet核心編寫一個c#控制檯應用程序,並試圖允許用戶輸入密碼,並擔心安全性,特別是內存轉儲。在c#dotnet核心控制檯應用程序中保護密碼輸入
以下:Password masking console application我的理解是,存儲爲一個字符串變量密碼可以通過內存轉儲(reference)被曝光。
SecureString通常是這裏的方式,但似乎不支持dotnet core。
我試着修改代碼來使用char數組,因爲我的有限理解是它不是不可變的,所以它不會全部存儲在單個內存中。老實說雖然安全不是我的特長,所以我的問題是,如果下面的代碼正確地保護我通過內存轉儲公開密碼?
Console.WriteLine("Enter pass");
char[] passwordArray = new char[256];
int whileIndex = 0;
while (true)
{
ConsoleKeyInfo key = Console.ReadKey(true);
if (key.Key == ConsoleKey.Enter)
{
break;
}
else if (key.Key == ConsoleKey.Backspace)
{
if (whileIndex != 0) //so it doesn't explode if someone holds backspace
{
whileIndex--;
}
}
else
{
passwordArray[whileIndex] = key.KeyChar;
whileIndex++;
}
}
//Truncate array to length of password
var endIndex = Array.IndexOf(passwordArray,'\0');
char[] shortenedPasswordArray = new char[endIndex];
Array.Copy(passwordArray, shortenedPasswordArray, endIndex);
//Authentication code here
//Wipe the characters when done
foreach(var passChar in passwordArray)
{
passwordArray[passChar] = '\0';
}
foreach (var passChar in shortenedPasswordArray)
{
shortenedPasswordArray[passChar] = '\0';
}
AFAIK數組仍然會被分配連續的內存空間, –
您的威脅模型是什麼?誰在做什麼? –
@NeilMcGuigan不確定這個背景有多大幫助。此應用程序的目的是運行LDAP查詢來下拉AD對象的列表,並且我正在請求用戶/傳遞來綁定連接。雖然我個人認爲它比保存證書/在設備之間傳遞它們更低的風險,但它可能會由具有有價值證書的IT管理員執行,因此我想在此進行盡職調查。 – Polymergraphy