Skip to main content

rusthound_ce/objects/
ou.rs

1use serde_json::value::Value;
2use serde::{Deserialize, Serialize};
3use ldap3::SearchEntry;
4use log::{debug, trace};
5use std::collections::HashMap;
6use std::error::Error;
7
8use crate::objects::common::{LdapObject, AceTemplate, GPOChange, Link, SPNTarget, Member};
9use crate::enums::acl::parse_ntsecuritydescriptor;
10use crate::enums::gplink::parse_gplink;
11use crate::enums::sid::decode_guid_le;
12use crate::utils::date::string_to_epoch;
13
14/// Ou structure
15#[derive(Debug, Clone, Deserialize, Serialize, Default)]
16pub struct Ou {
17    #[serde(rename = "GPOChanges")]
18    gpo_changes: GPOChange,
19    #[serde(rename = "ObjectIdentifier")]
20    object_identifier: String,
21    #[serde(rename = "Properties")]
22    properties: OuProperties,
23    #[serde(rename = "Aces")]
24    aces: Vec<AceTemplate>,
25    #[serde(rename = "Links")]
26    links: Vec<Link>,
27    #[serde(rename = "ChildObjects")]
28    child_objects: Vec<Member>,
29    #[serde(rename = "IsDeleted")]
30    is_deleted: bool,
31    #[serde(rename = "IsACLProtected")]
32    is_acl_protected: bool,
33    #[serde(rename = "ContainedBy")]
34    contained_by: Option<Member>,
35}
36
37impl Ou {
38    // New computer.
39    pub fn new() -> Self { 
40        Self { ..Default::default() } 
41    }
42
43    // Immutable access.
44    pub fn properties(&self) -> &OuProperties {
45        &self.properties
46    }
47
48    // Mutable access.
49    pub fn gpo_changes_mut(&mut self) -> &mut GPOChange {
50        &mut self.gpo_changes
51    }
52    pub fn child_objects_mut(&mut self) -> &mut Vec<Member> {
53        &mut self.child_objects
54    }
55
56    /// Function to parse and replace value for OU object.
57    /// <https://bloodhound.readthedocs.io/en/latest/further-reading/json.html#ous>
58    pub fn parse(
59        &mut self,
60        result: SearchEntry,
61        domain: &str,
62        dn_sid: &mut HashMap<String, String>,
63        sid_type: &mut HashMap<String, String>,
64        domain_sid: &str,
65        schema_guid_map: &HashMap<String, String>,
66    ) -> Result<(), Box<dyn Error>> {
67        let result_dn: String = result.dn.to_uppercase();
68        let result_attrs: HashMap<String, Vec<String>> = result.attrs;
69        let result_bin: HashMap<String, Vec<Vec<u8>>> = result.bin_attrs;
70
71        // Debug for current object
72        debug!("Parse OU: {result_dn}");
73
74        // Trace all result attributes
75        for (key, value) in &result_attrs {
76            trace!("  {key:?}:{value:?}");
77        }
78        // Trace all bin result attributes
79        for (key, value) in &result_bin {
80            trace!("  {key:?}:{value:?}");
81        }
82
83        // Change all values...
84        self.properties.domain = domain.to_uppercase();
85        self.properties.distinguishedname = result_dn;
86        self.properties.domainsid = domain_sid.to_string();
87
88        // Check and replace value
89        for (key, value) in &result_attrs {
90             match key.as_str() {
91                 "name" => {
92                     let name = &value[0];
93                     let email = format!("{}@{}", name.to_owned(), domain);
94                     self.properties.name = email.to_uppercase();
95                 }
96                 "description" => {
97                     self.properties.description = value.first().cloned();
98                 }
99                 "whenCreated" => {
100                     let epoch = string_to_epoch(&value[0])?;
101                     if epoch.is_positive() {
102                          self.properties.whencreated = epoch;
103                     }
104                 }
105                 "gPLink" => {
106                     self.links = parse_gplink(value[0].to_string())?;
107                 }
108                 "gPOtions" => {
109                     self.properties.blocksinheritance = value[0].parse::<i64>().unwrap_or(0) == 1;
110                 }
111                 "IsDeleted" => {
112                     self.is_deleted = true;
113                 }
114                 _ => {}
115             }
116        }
117
118          // For all, bins attributes
119        for (key, value) in &result_bin {
120             match key.as_str() {
121                 "objectGUID" => {
122                     // objectGUID raw to string
123                     self.object_identifier = decode_guid_le(&value[0]).to_owned();
124                 }
125                 "nTSecurityDescriptor" => {
126                     // trace!("nTSecurityDescriptor ACES ACLS ?");
127                     // nTSecurityDescriptor raw to string
128                     let relations_ace = parse_ntsecuritydescriptor(
129                          self,
130                          &value[0],
131                          "OU",
132                          &result_attrs,
133                          &result_bin,
134                          domain,
135                          schema_guid_map,
136                     );
137                     self.aces = relations_ace;
138                 }
139                 _ => {}
140             }
141        }
142        // Push DN and SID in HashMap
143        dn_sid.insert(
144             self.properties.distinguishedname.to_string(),
145             self.object_identifier.to_string(),
146        );
147        // Push DN and Type
148        sid_type.insert(
149            self.object_identifier.to_string(),
150             "OU".to_string(),
151        );
152
153        // Trace and return Ou struct
154        // trace!("JSON OUTPUT: {:?}",serde_json::to_string(&self).unwrap());
155        Ok(())
156    }
157}
158
159impl LdapObject for Ou {
160    // To JSON
161    fn to_json(&self) -> Value {
162        serde_json::to_value(self).unwrap()
163    }
164    
165    // Get values
166    fn get_object_identifier(&self) -> &String {
167        &self.object_identifier
168    }
169    fn get_is_acl_protected(&self) -> &bool {
170        &self.is_acl_protected
171    }
172    fn get_aces(&self) -> &Vec<AceTemplate> {
173        &self.aces
174    }
175    fn get_spntargets(&self) -> &Vec<SPNTarget> {
176        panic!("Not used by current object.");
177    }
178    fn get_allowed_to_delegate(&self) -> &Vec<Member> {
179        panic!("Not used by current object.");
180    }
181    fn get_links(&self) -> &Vec<Link> {
182        &self.links
183    }
184    fn get_contained_by(&self) -> &Option<Member> {
185        &self.contained_by
186    }
187    fn get_child_objects(&self) -> &Vec<Member> {
188        &self.child_objects
189    }
190    fn get_haslaps(&self) -> &bool {
191        &false
192    }
193    
194    // Get mutable values
195    fn get_aces_mut(&mut self) -> &mut Vec<AceTemplate> {
196        &mut self.aces
197    }
198    fn get_spntargets_mut(&mut self) -> &mut Vec<SPNTarget> {
199        panic!("Not used by current object.");
200    }
201    fn get_allowed_to_delegate_mut(&mut self) -> &mut Vec<Member> {
202        panic!("Not used by current object.");
203    }
204    
205    // Edit values
206    fn set_is_acl_protected(&mut self, is_acl_protected: bool) {
207        self.is_acl_protected = is_acl_protected;
208        self.properties.isaclprotected = is_acl_protected;
209    }
210    fn set_aces(&mut self, aces: Vec<AceTemplate>) {
211        self.aces = aces;
212    }
213    fn set_spntargets(&mut self, _spn_targets: Vec<SPNTarget>) {
214        // Not used by current object.
215    }
216    fn set_allowed_to_delegate(&mut self, _allowed_to_delegate: Vec<Member>) {
217        // Not used by current object.
218    }
219    fn set_links(&mut self, links: Vec<Link>) {
220        self.links = links;
221    }
222    fn set_contained_by(&mut self, contained_by: Option<Member>) {
223        self.contained_by = contained_by;
224    }
225    fn set_child_objects(&mut self, child_objects: Vec<Member>) {
226        self.child_objects = child_objects
227    }
228}
229
230// Ou properties structure
231#[derive(Debug, Clone, Deserialize, Serialize, Default)]
232pub struct OuProperties {
233    domain: String,
234    name: String,
235    distinguishedname: String,
236    domainsid: String,
237    isaclprotected: bool,
238    highvalue: bool,
239    description: Option<String>,
240    whencreated: i64,
241    blocksinheritance: bool
242}
243
244impl OuProperties {
245    // Immutable access.
246    pub fn name(&self) -> &String {
247        &self.name
248    }
249    pub fn distinguishedname(&self) -> &String {
250        &self.distinguishedname
251    }
252
253
254    pub fn isaclprotected_mut(&mut self) -> &mut bool {
255        &mut self.isaclprotected
256    }
257}