rusthound_ce/objects/
domain.rs1use serde_json::value::Value;
2use serde::{Deserialize, Serialize};
3use colored::Colorize;
4use ldap3::SearchEntry;
5use log::{info, debug, trace};
6use std::collections::HashMap;
7use std::error::Error;
8
9use crate::enums::regex::OBJECT_SID_RE1;
10use crate::objects::common::{LdapObject, GPOChange, Link, AceTemplate, SPNTarget, Member};
11use crate::objects::trust::Trust;
12use crate::utils::date::{span_to_string, string_to_epoch};
13use crate::enums::acl::parse_ntsecuritydescriptor;
14use crate::enums::forestlevel::get_forest_level;
15use crate::enums::gplink::parse_gplink;
16use crate::enums::secdesc::LdapSid;
17use crate::enums::sid::sid_maker;
18
19#[derive(Debug, Clone, Deserialize, Serialize, Default)]
21pub struct Domain {
22 #[serde(rename = "Properties")]
23 properties: DomainProperties,
24 #[serde(rename = "GPOChanges")]
25 gpo_changes: GPOChange,
26 #[serde(rename = "ChildObjects")]
27 child_objects: Vec<Member>,
28 #[serde(rename = "Trusts")]
29 trusts: Vec<Trust>,
30 #[serde(rename = "Links")]
31 links: Vec<Link>,
32 #[serde(rename = "Aces")]
33 aces: Vec<AceTemplate>,
34 #[serde(rename = "ObjectIdentifier")]
35 object_identifier: String,
36 #[serde(rename = "IsDeleted")]
37 is_deleted: bool,
38 #[serde(rename = "IsACLProtected")]
39 is_acl_protected: bool,
40 #[serde(rename = "ContainedBy")]
41 contained_by: Option<Member>,
42}
43
44impl Domain {
45 pub fn new() -> Self {
47 Self { ..Default::default() }
48 }
49
50 pub fn object_identifier(&self) -> &String {
52 &self.object_identifier
53 }
54
55 pub fn properties_mut(&mut self) -> &mut DomainProperties {
57 &mut self.properties
58 }
59 pub fn object_identifier_mut(&mut self) -> &mut String {
60 &mut self.object_identifier
61 }
62 pub fn gpo_changes_mut(&mut self) -> &mut GPOChange {
63 &mut self.gpo_changes
64 }
65 pub fn trusts_mut(&mut self) -> &mut Vec<Trust> {
66 &mut self.trusts
67 }
68
69 pub fn parse(
72 &mut self,
73 result: SearchEntry,
74 domain_name: &str,
75 dn_sid: &mut HashMap<String, String>,
76 sid_type: &mut HashMap<String, String>,
77 schema_guid_map: &HashMap<String, String>,
78 ) -> Result<String, Box<dyn Error>> {
79 let result_dn: String = result.dn.to_uppercase();
80 let result_attrs: HashMap<String, Vec<String>> = result.attrs;
81 let result_bin: HashMap<String, Vec<Vec<u8>>> = result.bin_attrs;
82
83 debug!("Parse domain: {result_dn}");
85
86 for (key, value) in &result_attrs {
88 trace!(" {key:?}:{value:?}");
89 }
90 for (key, value) in &result_bin {
92 trace!(" {key:?}:{value:?}");
93 }
94
95 self.properties.domain = domain_name.to_uppercase();
97 self.properties.distinguishedname = result_dn;
98
99 #[allow(unused_assignments)]
101 let mut sid: String = "".to_owned();
102 let mut global_domain_sid: String = "DOMAIN_SID".to_owned();
103 for (key, value) in &result_attrs {
105 match key.as_str() {
106 "distinguishedName" => {
107 self.properties.distinguishedname = value[0].to_owned().to_uppercase();
109 let name = value[0]
110 .split(",")
111 .filter(|x| x.starts_with("DC="))
112 .map(|x| x.strip_prefix("DC=").unwrap_or(""))
113 .collect::<Vec<&str>>()
114 .join(".");
115 self.properties.name = name.to_uppercase();
116 self.properties.domain = name.to_uppercase();
117 }
118 "msDS-Behavior-Version" => {
119 let level = get_forest_level(value[0].to_string());
120 self.properties.functionallevel = level;
121 }
122 "whenCreated" => {
123 let epoch = string_to_epoch(&value[0])?;
124 if epoch.is_positive() {
125 self.properties.whencreated = epoch;
126 }
127 }
128 "gPLink" => {
129 self.links = parse_gplink(value[0].to_string())?;
130 }
131 "isCriticalSystemObject" => {
132 self.properties.highvalue = value[0].contains("TRUE");
133 }
134 "ms-DS-MachineAccountQuota" => {
136 let machine_account_quota = value[0].parse::<i32>().unwrap_or(0);
137 self.properties.machineaccountquota = machine_account_quota;
138 if machine_account_quota > 0 {
139 info!("MachineAccountQuota: {}", machine_account_quota.to_string().yellow().bold());
140 }
141 }
142 "IsDeleted" => {
143 self.is_deleted = true;
144 }
145 "msDS-ExpirePasswordsOnSmartCardOnlyAccounts" => {
146 self.properties.expirepasswordsonsmartcardonlyaccounts = true;
147 }
148 "minPwdLength" => {
149 self.properties.minpwdlength = value[0].parse::<i32>().unwrap_or(0);
150 }
151 "pwdProperties" => {
152 self.properties.pwdproperties = value[0].parse::<i32>().unwrap_or(0);
153 }
154 "pwdHistoryLength" => {
155 self.properties.pwdhistorylength = value[0].parse::<i32>().unwrap_or(0);
156 }
157 "lockoutThreshold" => {
158 self.properties.lockoutthreshold = value[0].parse::<i32>().unwrap_or(0);
159 }
160 "minPwdAge" => {
161 self.properties.minpwdage = span_to_string(value[0].parse::<i64>().unwrap_or(0));
162 }
163 "maxPwdAge" => {
164 self.properties.maxpwdage = span_to_string(value[0].parse::<i64>().unwrap_or(0));
165 }
166 "lockoutDuration" => {
167 self.properties.lockoutduration = span_to_string(value[0].parse::<i64>().unwrap_or(0));
168 }
169 "lockOutObservationWindow" => {
170 self.properties.lockoutobservationwindow = value[0].parse::<i64>().unwrap_or(0);
171 }
172 _ => {}
173 }
174 }
175
176 for (key, value) in &result_bin {
178 match key.as_str() {
179 "objectSid" => {
180 sid = sid_maker(LdapSid::parse(&value[0]).unwrap().1, domain_name);
182 self.object_identifier = sid.to_owned();
183
184 for domain_sid in OBJECT_SID_RE1.captures_iter(&sid) {
185 self.properties.domainsid = domain_sid[0].to_owned().to_string();
186 global_domain_sid = domain_sid[0].to_owned().to_string();
187 }
188
189 self.properties.collected = true;
191 }
192 "nTSecurityDescriptor" => {
193 let relations_ace = parse_ntsecuritydescriptor(
195 self,
196 &value[0],
197 "Domain",
198 &result_attrs,
199 &result_bin,
200 domain_name,
201 schema_guid_map,
202 );
203 self.aces = relations_ace;
204 }
205 _ => {}
206 }
207 }
208
209 dn_sid.insert(
211 self.properties.distinguishedname.to_string(),
212 self.object_identifier.to_string()
213 );
214 sid_type.insert(
216 self.object_identifier.to_string(),
217 "Domain".to_string(),
218 );
219
220 Ok(global_domain_sid)
223 }
224}
225
226impl LdapObject for Domain {
227 fn to_json(&self) -> Value {
229 serde_json::to_value(self).unwrap()
230 }
231
232 fn get_object_identifier(&self) -> &String {
234 &self.object_identifier
235 }
236 fn get_is_acl_protected(&self) -> &bool {
237 &self.is_acl_protected
238 }
239 fn get_aces(&self) -> &Vec<AceTemplate> {
240 &self.aces
241 }
242 fn get_spntargets(&self) -> &Vec<SPNTarget> {
243 panic!("Not used by current object.");
244 }
245 fn get_allowed_to_delegate(&self) -> &Vec<Member> {
246 panic!("Not used by current object.");
247 }
248 fn get_links(&self) -> &Vec<Link> {
249 &self.links
250 }
251 fn get_contained_by(&self) -> &Option<Member> {
252 &self.contained_by
253 }
254 fn get_child_objects(&self) -> &Vec<Member> {
255 &self.child_objects
256 }
257 fn get_haslaps(&self) -> &bool {
258 &false
259 }
260
261 fn get_aces_mut(&mut self) -> &mut Vec<AceTemplate> {
263 &mut self.aces
264 }
265 fn get_spntargets_mut(&mut self) -> &mut Vec<SPNTarget> {
266 panic!("Not used by current object.");
267 }
268 fn get_allowed_to_delegate_mut(&mut self) -> &mut Vec<Member> {
269 panic!("Not used by current object.");
270 }
271
272 fn set_is_acl_protected(&mut self, is_acl_protected: bool) {
274 self.is_acl_protected = is_acl_protected;
275 self.properties.isaclprotected = is_acl_protected;
276 }
277 fn set_aces(&mut self, aces: Vec<AceTemplate>) {
278 self.aces = aces;
279 }
280 fn set_spntargets(&mut self, _spn_targets: Vec<SPNTarget>) {
281 }
283 fn set_allowed_to_delegate(&mut self, _allowed_to_delegate: Vec<Member>) {
284 }
286 fn set_links(&mut self, links: Vec<Link>) {
287 self.links = links;
288 }
289 fn set_contained_by(&mut self, contained_by: Option<Member>) {
290 self.contained_by = contained_by;
291 }
292 fn set_child_objects(&mut self, child_objects: Vec<Member>) {
293 self.child_objects = child_objects
294 }
295}
296
297#[derive(Debug, Clone, Deserialize, Serialize, Default)]
299pub struct DomainProperties {
300 domain: String,
301 name: String,
302 distinguishedname: String,
303 domainsid: String,
304 isaclprotected: bool,
305 highvalue: bool,
306 description: Option<String>,
307 whencreated: i64,
308 machineaccountquota: i32,
309 expirepasswordsonsmartcardonlyaccounts: bool,
310 minpwdlength: i32,
311 pwdproperties: i32,
312 pwdhistorylength: i32,
313 lockoutthreshold: i32,
314 minpwdage: String,
315 maxpwdage: String,
316 lockoutduration: String,
317 lockoutobservationwindow: i64,
318 functionallevel: String,
319 collected: bool
320}
321
322impl DomainProperties {
323 pub fn domain_mut(&mut self) -> &mut String {
325 &mut self.domain
326 }
327 pub fn name_mut(&mut self) -> &mut String {
328 &mut self.name
329 }
330 pub fn highvalue_mut(&mut self) -> &mut bool {
331 &mut self.highvalue
332 }
333 pub fn distinguishedname_mut(&mut self) -> &mut String {
334 &mut self.distinguishedname
335 }
336}