| | 1 | | using System.Collections.Generic; |
| | 2 | | using System.Linq; |
| | 3 | | using SharpHoundCommonLib.Enums; |
| | 4 | | using SharpHoundCommonLib.LDAPQueries; |
| | 5 | |
|
| | 6 | | namespace SharpHoundCommonLib; |
| | 7 | |
|
| | 8 | | public class LdapProducerQueryGenerator { |
| | 9 | | public static GeneratedLdapParameters GenerateDefaultPartitionParameters(CollectionMethod methods) { |
| | 10 | | var filter = new LdapFilter(); |
| | 11 | | var properties = new List<string>(); |
| | 12 | |
|
| | 13 | | properties.AddRange(CommonProperties.BaseQueryProps); |
| | 14 | | properties.AddRange(CommonProperties.TypeResolutionProps); |
| | 15 | |
|
| | 16 | | if (methods.HasFlag(CollectionMethod.ObjectProps) || methods.HasFlag(CollectionMethod.ACL) || methods.HasFlag(Co |
| | 17 | | filter = filter.AddComputers().AddDomains().AddUsers().AddContainers().AddGPOs().AddOUs().AddGroups(); |
| | 18 | |
|
| | 19 | | if (methods.HasFlag(CollectionMethod.Container)) { |
| | 20 | | properties.AddRange(CommonProperties.ContainerProps); |
| | 21 | | } |
| | 22 | |
|
| | 23 | | if (methods.HasFlag(CollectionMethod.Group)) { |
| | 24 | | properties.AddRange(CommonProperties.GroupResolutionProps); |
| | 25 | | } |
| | 26 | |
|
| | 27 | | if (methods.HasFlag(CollectionMethod.ACL)) { |
| | 28 | | properties.AddRange(CommonProperties.ACLProps); |
| | 29 | | } |
| | 30 | |
|
| | 31 | | if (methods.HasFlag(CollectionMethod.ObjectProps)) { |
| | 32 | | properties.AddRange(CommonProperties.ObjectPropsProps); |
| | 33 | | } |
| | 34 | |
|
| | 35 | | if (methods.IsComputerCollectionSet()) { |
| | 36 | | properties.AddRange(CommonProperties.ComputerMethodProps); |
| | 37 | | } |
| | 38 | |
|
| | 39 | | if (methods.HasFlag(CollectionMethod.Trusts)) { |
| | 40 | | properties.AddRange(CommonProperties.DomainTrustProps); |
| | 41 | | } |
| | 42 | |
|
| | 43 | | if (methods.HasFlag(CollectionMethod.GPOLocalGroup)) |
| | 44 | | properties.AddRange(CommonProperties.GPOLocalGroupProps); |
| | 45 | |
|
| | 46 | | if (methods.HasFlag(CollectionMethod.SPNTargets)) |
| | 47 | | properties.AddRange(CommonProperties.SPNTargetProps); |
| | 48 | |
|
| | 49 | | if (methods.HasFlag(CollectionMethod.DCRegistry)) |
| | 50 | | properties.AddRange(CommonProperties.ComputerMethodProps); |
| | 51 | |
|
| | 52 | | if (methods.HasFlag(CollectionMethod.SPNTargets)) { |
| | 53 | | properties.AddRange(CommonProperties.SPNTargetProps); |
| | 54 | | } |
| | 55 | |
|
| | 56 | | return new GeneratedLdapParameters { |
| | 57 | | Filter = filter, |
| | 58 | | Attributes = properties.Distinct().ToArray() |
| | 59 | | }; |
| | 60 | | } |
| | 61 | |
|
| | 62 | | if (methods.HasFlag(CollectionMethod.Group)) { |
| | 63 | | filter = filter.AddGroups(); |
| | 64 | | properties.AddRange(CommonProperties.GroupResolutionProps); |
| | 65 | | } |
| | 66 | |
|
| | 67 | | if (methods.IsComputerCollectionSet()) { |
| | 68 | | filter = filter.AddComputers(); |
| | 69 | | properties.AddRange(CommonProperties.ComputerMethodProps); |
| | 70 | | } |
| | 71 | |
|
| | 72 | | if (methods.HasFlag(CollectionMethod.Trusts)) { |
| | 73 | | filter = filter.AddDomains(); |
| | 74 | | properties.AddRange(CommonProperties.ComputerMethodProps); |
| | 75 | | } |
| | 76 | |
|
| | 77 | | if (methods.HasFlag(CollectionMethod.SPNTargets)) { |
| | 78 | | filter = filter.AddUsers(CommonFilters.NeedsSPN); |
| | 79 | | properties.AddRange(CommonProperties.SPNTargetProps); |
| | 80 | | } |
| | 81 | |
|
| | 82 | | if (methods.HasFlag(CollectionMethod.GPOLocalGroup)) { |
| | 83 | | filter = filter.AddOUs(); |
| | 84 | | properties.AddRange(CommonProperties.GPOLocalGroupProps); |
| | 85 | | } |
| | 86 | |
|
| | 87 | | if (methods.HasFlag(CollectionMethod.DCRegistry)) { |
| | 88 | | filter = filter.AddComputers(CommonFilters.DomainControllers); |
| | 89 | | properties.AddRange(CommonProperties.ComputerMethodProps); |
| | 90 | | } |
| | 91 | |
|
| | 92 | | return new GeneratedLdapParameters { |
| | 93 | | Filter = filter, |
| | 94 | | Attributes = properties.Distinct().ToArray() |
| | 95 | | }; |
| | 96 | | } |
| | 97 | |
|
| | 98 | | public static GeneratedLdapParameters GenerateConfigurationPartitionParameters(CollectionMethod methods) { |
| | 99 | | var filter = new LdapFilter(); |
| | 100 | | var properties = new List<string>(); |
| | 101 | |
|
| | 102 | | properties.AddRange(CommonProperties.BaseQueryProps); |
| | 103 | | properties.AddRange(CommonProperties.TypeResolutionProps); |
| | 104 | |
|
| | 105 | | if (methods.HasFlag(CollectionMethod.ACL) || methods.HasFlag(CollectionMethod.ObjectProps) || |
| | 106 | | methods.HasFlag(CollectionMethod.Container) || methods.HasFlag(CollectionMethod.CertServices)) { |
| | 107 | | filter = filter.AddContainers().AddConfiguration().AddCertificateTemplates().AddCertificateAuthorities() |
| | 108 | | .AddEnterpriseCertificationAuthorities().AddIssuancePolicies(); |
| | 109 | |
|
| | 110 | | if (methods.HasFlag(CollectionMethod.ObjectProps)) |
| | 111 | | { |
| | 112 | | properties.AddRange(CommonProperties.ObjectPropsProps); |
| | 113 | | } |
| | 114 | |
|
| | 115 | | if (methods.HasFlag(CollectionMethod.ACL)) { |
| | 116 | | properties.AddRange(CommonProperties.ACLProps); |
| | 117 | | } |
| | 118 | |
|
| | 119 | | if (methods.HasFlag(CollectionMethod.Container)) { |
| | 120 | | properties.AddRange(CommonProperties.ContainerProps); |
| | 121 | | } |
| | 122 | |
|
| | 123 | | if (methods.HasFlag(CollectionMethod.CertServices)) { |
| | 124 | | properties.AddRange(CommonProperties.CertAbuseProps); |
| | 125 | | properties.AddRange(CommonProperties.ObjectPropsProps); |
| | 126 | | properties.AddRange(CommonProperties.ContainerProps); |
| | 127 | | properties.AddRange(CommonProperties.ACLProps); |
| | 128 | | } |
| | 129 | |
|
| | 130 | | if (methods.HasFlag(CollectionMethod.CARegistry)) { |
| | 131 | | properties.AddRange(CommonProperties.CertAbuseProps); |
| | 132 | | } |
| | 133 | |
|
| | 134 | | return new GeneratedLdapParameters { |
| | 135 | | Filter = filter, |
| | 136 | | Attributes = properties.Distinct().ToArray() |
| | 137 | | }; |
| | 138 | | } |
| | 139 | |
|
| | 140 | | if (methods.HasFlag(CollectionMethod.CARegistry)) { |
| | 141 | | filter = filter.AddEnterpriseCertificationAuthorities(); |
| | 142 | | properties.AddRange(CommonProperties.CertAbuseProps); |
| | 143 | | } |
| | 144 | |
|
| | 145 | | return new GeneratedLdapParameters { |
| | 146 | | Filter = filter, |
| | 147 | | Attributes = properties.Distinct().ToArray() |
| | 148 | | }; |
| | 149 | | } |
| | 150 | | } |
| | 151 | |
|
| | 152 | | public class GeneratedLdapParameters { |
| 0 | 153 | | public string[] Attributes { get; set; } |
| 0 | 154 | | public LdapFilter Filter { get; set; } |
| | 155 | | } |