Skip to content

Commit

Permalink
Select adobject by the same parameters as searched
Browse files Browse the repository at this point in the history
  • Loading branch information
Seji64 committed Oct 6, 2023
1 parent 39b2a15 commit 248aa26
Showing 1 changed file with 22 additions and 11 deletions.
33 changes: 22 additions & 11 deletions src/Services/LDAPService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ public async Task<bool> TestCredentialsAsync(LdapCredential ldapCredential)

string? defaultNamingContext = _ldapOptions.Value.SearchBase;

var ldapSearchResult = (await ldapConnection.SearchByCnAsync(defaultNamingContext, name, Native.LdapSearchScope.LDAP_SCOPE_SUB)).FirstOrDefault();
var ldapSearchResult = (await ldapConnection.SearchAsync(defaultNamingContext, $"(&(objectCategory=computer)(name={name}))",null, Native.LdapSearchScope.LDAP_SCOPE_SUB)).SingleOrDefault();

if (ldapSearchResult != null)
{
Expand Down Expand Up @@ -211,26 +211,37 @@ public async Task<bool> TestCredentialsAsync(LdapCredential ldapCredential)

private static async Task<string> DecryptLAPSPayload(byte[] value, LdapCredential ldapCredential)
{

StringBuilder pythonScriptResult = new();
string pythonDecryptScriptPath = Path.Combine(Path.GetDirectoryName(AppContext.BaseDirectory)!, "scripts", "DecryptEncryptedLAPSPassword.py");

string pythonBin = RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? "python" : "python3";

var pythonCmd = Cli.Wrap(pythonBin)
.WithArguments($"\"{pythonDecryptScriptPath}\" --user \"{ldapCredential.UserName}\" --password \"{ldapCredential.Password}\" --data \"{Convert.ToBase64String(value)}\"")
.WithStandardOutputPipe(PipeTarget.ToStringBuilder(pythonScriptResult));
try
{

var pythonCmd = Cli.Wrap(pythonBin)
.WithArguments($"\"{pythonDecryptScriptPath}\" --user \"{ldapCredential.UserName}\" --password \"{ldapCredential.Password}\" --data \"{Convert.ToBase64String(value)}\"")
.WithStandardOutputPipe(PipeTarget.ToStringBuilder(pythonScriptResult));

await pythonCmd.ExecuteAsync();
await pythonCmd.ExecuteAsync();

if (pythonDecryptScriptPath is null || pythonDecryptScriptPath.Length == 0)
if (pythonDecryptScriptPath is null || pythonDecryptScriptPath.Length == 0)
{
throw new Exception("Failed to decrypt laps password!");
}

string ldapValue = pythonScriptResult.ToString().Trim();
ldapValue = ldapValue.Remove(ldapValue.LastIndexOf("}") + 1);

return ldapValue;
}
catch (Exception ex)
{
throw new Exception("Failed to decrypt laps password!");
Log.Error("Decrypt LAPS Password failed => {ErrorMessage}", ex.Message);
throw new ArgumentException("Failed to decrypt LAPSv2 Password");
}

string ldapValue = pythonScriptResult.ToString().Trim();
ldapValue = ldapValue.Remove(ldapValue.LastIndexOf("}") + 1);

return ldapValue;
}

public async Task<List<ADComputer>> SearchADComputersAsync(LdapCredential ldapCredential, string query)
Expand Down

0 comments on commit 248aa26

Please sign in to comment.