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