@@ -155,16 +155,73 @@ static void Main(string[] args)
155155 Console . WriteLine ( "\n Enumerating all domain trusts..." ) ;
156156 foreach ( TrustRelationshipInformation trust in domainTrusts )
157157 {
158- Console . WriteLine ( trust . TargetName + " " + trust . TrustType + " " + trust . TrustDirection ) ;
159- domainTrustArray . Add ( trust . TargetName ) ;
158+ // Only add trusts that are Bi or outbound so you can actually communicate with them
159+ if ( ( trust . TrustDirection == TrustDirection . Bidirectional ) || ( trust . TrustDirection == TrustDirection . Outbound ) )
160+ {
161+ Console . WriteLine ( trust . TargetName + " " + trust . TrustType + " " + trust . TrustDirection ) ;
162+ // If a forest trust is found, try and enumerate that forest trust further?
163+ if ( trust . TrustType == TrustType . Forest )
164+ {
165+ DirectoryContext rootDomainContext ;
166+ Domain rootDomain ;
167+ TrustRelationshipInformationCollection forestTrusts ;
168+
169+
170+ rootDomainContext = new DirectoryContext ( DirectoryContextType . Domain , trust . TargetName ) ;
171+ rootDomain = Domain . GetDomain ( rootDomainContext ) ;
172+ forestTrusts = rootDomain . GetAllTrustRelationships ( ) ;
173+ foreach ( TrustRelationshipInformation forestTrust in forestTrusts )
174+ if ( ( forestTrust . TrustType == TrustType . ParentChild ) && ( ( forestTrust . TrustDirection == TrustDirection . Bidirectional ) || ( forestTrust . TrustDirection == TrustDirection . Outbound ) ) )
175+ {
176+ Console . WriteLine ( trust . TargetName + " " + trust . TrustType + " " + trust . TrustDirection ) ;
177+ domainTrustArray . Add ( forestTrust . TargetName ) ;
178+ }
179+ }
180+ else
181+ {
182+ domainTrustArray . Add ( trust . TargetName ) ;
183+ }
184+
185+
186+ }
160187 }
161188
162189 //start getting all forest trusts
163190 Console . WriteLine ( "\n Enumerating all trusted forests..." ) ;
164191 foreach ( TrustRelationshipInformation trust in currentForest . GetAllTrustRelationships ( ) )
165192 {
166- Console . WriteLine ( trust . TargetName + " " + trust . TrustType + " " + trust . TrustDirection ) ;
167- domainTrustArray . Add ( trust . TargetName ) ;
193+
194+ // Only add trusts that are Bi or outbound so you can actually communicate with them
195+ if ( ( trust . TrustDirection == TrustDirection . Bidirectional ) || ( trust . TrustDirection == TrustDirection . Outbound ) )
196+ {
197+ Console . WriteLine ( trust . TargetName + " " + trust . TrustType + " " + trust . TrustDirection ) ;
198+ // If a forest trust is found, try and enumerate that forest trust further?
199+ if ( trust . TrustType == TrustType . Forest )
200+ {
201+ DirectoryContext rootDomainContext ;
202+ Domain rootDomain ;
203+ TrustRelationshipInformationCollection forestTrusts ;
204+
205+
206+ rootDomainContext = new DirectoryContext ( DirectoryContextType . Domain , trust . TargetName ) ;
207+ rootDomain = Domain . GetDomain ( rootDomainContext ) ;
208+ forestTrusts = rootDomain . GetAllTrustRelationships ( ) ;
209+ foreach ( TrustRelationshipInformation forestTrust in forestTrusts )
210+ if ( ( forestTrust . TrustType == TrustType . ParentChild ) && ( ( forestTrust . TrustDirection == TrustDirection . Bidirectional ) || ( forestTrust . TrustDirection == TrustDirection . Outbound ) ) )
211+ {
212+ Console . WriteLine ( trust . TargetName + " " + trust . TrustType + " " + trust . TrustDirection ) ;
213+ domainTrustArray . Add ( forestTrust . TargetName ) ;
214+ }
215+ }
216+ else
217+ {
218+ domainTrustArray . Add ( trust . TargetName ) ;
219+ }
220+
221+
222+ }
223+
224+
168225 }
169226
170227 //Set the variables needed to store users, groups, and domains
0 commit comments