Skip to content

Instantly share code, notes, and snippets.

@sherief
Forked from 3735943886/CredentialUIAuto.cs
Created August 4, 2023 17:27
Show Gist options
  • Save sherief/c096c530f79113bf62bfb2ed06f17682 to your computer and use it in GitHub Desktop.
Save sherief/c096c530f79113bf62bfb2ed06f17682 to your computer and use it in GitHub Desktop.
using System.Windows.Automation;
// WINDOWS CREDENTIAL UI AUTOMATION by 3735943886
/*
Credential Dialog Xaml Host
- TextBlock / WindowLogo / 
- TextBlock / / Windows 보안
- Button / CloseButton / 닫기
- ScrollViewer / /
- TextBlock / / 사용자 자격 증명 입력
- TextBlock / / 이 자격 증명은 192.168.20.213에 연결할 때 사용됩니다.
- TextBox / EditField_1 / 사용자 이름
- PasswordBox / PasswordField_2 / 암호
- Button / OkButton / 확인
- Button / CancelButton / 취소
Credential Dialog Xaml Host
- TextBlock / WindowLogo / 
- TextBlock / / Windows 보안
- Button / CloseButton / 닫기
- ScrollViewer / /
- TextBlock / / 자격 증명이 작동하지 않습니다.
- TextBlock / / Windows Defender Credential Guard는 저장된 자격 증명 사용을 허용하지 않습니다. 자격 증명을 입력하세요.
- TextBlock / StaticTextField_0 / sysadmin
- PasswordBox / PasswordField_2 / 암호
- TextBlock / ErrorText / 로그온하지 못했습니다.
- Hyperlink / ChooseAnotherOption / 다른 옵션 선택
- TextBlock / / 다른 옵션 선택
- Button / OkButton / 확인
- Button / CancelButton / 취소
*/
namespace UI
{
public class CredentialAutoInput
{
AutomationElement AEid = null;
AutomationElement AEpass = null;
AutomationElement AEbtn = null;
public bool GetXamlCredInfo(string TargetString = null)
{
const string CredentialUIBrokerClass = "Credential"; // Credential Dialog Xaml Host
const string StaticIdFieldId = "StaticTextField"; // StaticTextField_0
const string IdFieldId = "EditField"; // EditField_1
const string PassFieldId = "PasswordField"; // PasswordField_2
const string BtnFieldId = "OkButton"; // OkButton
// If addr is null, do not check the target dest
bool bUiTarget = (TargetString == null);
// Iterate all XAML window
AutomationElementCollection AECtargets = AutomationElement.RootElement.FindAll(TreeScope.Children, PropertyCondition.TrueCondition);
foreach (AutomationElement uitarget in AECtargets)
{
// Check if ClassName contains Credential
if (uitarget.Current.ClassName.Contains(CredentialUIBrokerClass))
{
AutomationElementCollection AECfields = uitarget.FindAll(TreeScope.Descendants, PropertyCondition.TrueCondition);
AEid = null; AEpass = null; AEbtn = null;
// Iterate all fields
foreach (AutomationElement field in AECfields)
{
if (field.Current.ControlType == ControlType.Text && bUiTarget == false)
{
if (field.Current.Name.Contains(TargetString)) bUiTarget = true;
}
else
{
if (field.Current.AutomationId.Contains(IdFieldId)) AEid = field;
else if (field.Current.AutomationId.Contains(StaticIdFieldId)) AEid = field;
else if (field.Current.AutomationId.Contains(PassFieldId)) AEpass = field;
else if (field.Current.AutomationId.Contains(BtnFieldId)) AEbtn = field;
}
}
// Target found
if (AEid != null && AEpass != null && AEbtn != null) return bUiTarget;
}
}
// Target not found
AEid = null; AEpass = null; AEbtn = null; return false;
}
public string GetId()
{
if (AEid == null) if (GetXamlCredInfo() == false) return null;
return AEid.Current.Name;
}
public bool Connect(string id, string pass, string addr = null)
{
const string IdFieldClass = "TextBox"; // TextBox
// Get Xaml Info
if (AEid == null) if (GetXamlCredInfo(addr) == false) return false;
// If Id is editbox, fill it
if (AEid.Current.ClassName.Contains(IdFieldClass)) (AEid.GetCurrentPattern(ValuePattern.Pattern) as ValuePattern).SetValue(id);
else if (AEid.Current.Name != id) return false;
// Fill password
(AEpass.GetCurrentPattern(ValuePattern.Pattern) as ValuePattern).SetValue(pass);
// Press OK button
(AEbtn.GetCurrentPattern(InvokePattern.Pattern) as InvokePattern).Invoke();
return true;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment