public static (bool success, string username, string password) GetCredentials(string target, CRED_TYPE credType = CRED_TYPE.GENERIC) { var username = string.Empty; var password = string.Empty; var credentialPtr = IntPtr.Zero; try { bool success = Advapi32dll.CredRead(target, Advapi32dll.CRED_TYPE.GENERIC, 0, out credentialPtr); if (success) { var credential = (Advapi32dll.CREDENTIAL)Marshal.PtrToStructure(credentialPtr, typeof(Advapi32dll.CREDENTIAL)); username = credential.UserName; if (credential.CredentialBlobSize > 2) { password = Marshal.PtrToStringUni(credential.CredentialBlob, (int)credential.CredentialBlobSize / 2); } } return(success, username, password); } finally { if (credentialPtr != IntPtr.Zero) { CredFree(credentialPtr); } } }
private void AddCredentials(string[] args) { if (args.Length < 3) { Write("Please provide "); ColorWrite("target", ConsoleColor.Yellow); Write(", "); ColorWrite("username", ConsoleColor.Yellow); Write(" and "); ColorWrite("password", ConsoleColor.Yellow); WriteLine("."); return; } var(success, errorCode) = Advapi32dll.StoreCredentials(args[0], args[1], args[2]); if (success) { ColorWriteLine("Credentials written successfully!", ConsoleColor.Green); } else { ColorWrite("ERROR: ", ConsoleColor.Red); WriteLine("Failed to write credentials!"); Write("Error code: "); ColorWrite(errorCode.ToString(), ConsoleColor.Red); } }
private void ListCredentials(string[] args) { foreach (var target in Advapi32dll.ListCredentialTargets()) { var splitIdx = target.IndexOf('='); if (splitIdx < 0) { Console.WriteLine(target); } else { Write(target.Substring(0, splitIdx + 1)); ColorWriteLine(target.Substring(splitIdx + 1, target.Length - splitIdx - 1), ConsoleColor.Yellow); } } }
private void DeleteCredentials(string[] args) { if (args.Length < 1) { Write("Please provide "); ColorWrite("target", ConsoleColor.Yellow); WriteLine("."); return; } var success = Advapi32dll.DeleteCredentials(args[0]); if (!success) { ColorWriteLine("Could not delete credential.", ConsoleColor.Red); return; } ColorWrite("Success! ", ConsoleColor.Green); WriteLine("Credential deleted."); }
private void GetCredentials(string[] args) { if (args.Length < 1) { Write("Please provide "); ColorWrite("target", ConsoleColor.Yellow); WriteLine("."); return; } var(success, username, password) = Advapi32dll.GetCredentials(args[0]); if (!success) { ColorWriteLine("Could not find credential.", ConsoleColor.Red); return; } Write("Username: "******"Password: "); ColorWriteLine(password, ConsoleColor.Green); }