1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
use std::collections::HashMap;
use ldap3::SearchEntry;
use regex::Regex;
use indicatif::ProgressBar;
use std::convert::TryInto;

use log::info;
use crate::args::Options;
use crate::banner::progress_bar;
use crate::enums::ldaptype::*;
use crate::modules::adcs::parser::{parse_adcs_ca,parse_adcs_template};

pub mod bh_41;

/// Function to get type for object by object
pub fn parse_result_type(
    common_args: &Options, 
    result: Vec<SearchEntry>,
    vec_users: &mut Vec<serde_json::value::Value>,
    vec_groups: &mut Vec<serde_json::value::Value>,
    vec_computers: &mut Vec<serde_json::value::Value>,
    vec_ous: &mut Vec<serde_json::value::Value>,
    vec_domains: &mut Vec<serde_json::value::Value>,
    vec_gpos: &mut Vec<serde_json::value::Value>,
    vec_fsps: &mut Vec<serde_json::value::Value>,
    vec_containers: &mut Vec<serde_json::value::Value>,
    vec_trusts: &mut Vec<serde_json::value::Value>,
    vec_cas: &mut Vec<serde_json::value::Value>,
    vec_templates: &mut Vec<serde_json::value::Value>,

    dn_sid: &mut HashMap<String, String>,
    sid_type: &mut HashMap<String, String>,
    fqdn_sid: &mut HashMap<String, String>,
    fqdn_ip: &mut HashMap<String, String>,
    adcs_templates: &mut HashMap<String, Vec<String>>,
)
{
    // Domain name
    let domain = &common_args.domain;

    // Needed for progress bar stats
    let pb = ProgressBar::new(1);
    let mut count = 0;
    let total = result.len();

    info!("Starting the LDAP objects parsing...");
    for entry in result {
        // Start parsing with Type matching
        let cloneresult = entry.clone();
        //println!("{:?}",&entry);
        let atype = get_type(entry).unwrap_or(Type::Unknown);
        match atype {
            Type::User => {
                let user = parse_user(
                    cloneresult,
                    domain,
                    dn_sid,
                    sid_type,
                    common_args.adcs,
                );
                vec_users.push(user);
            }
            Type::Group => {
                let group = parse_group(
                    cloneresult,
                    domain,
                    dn_sid,
                    sid_type,
                );
                vec_groups.push(group);
            }
            Type::Computer => {
                let computer = parse_computer(
                    cloneresult,
                    domain,
                    dn_sid,
                    sid_type,
                    fqdn_sid,
                    fqdn_ip,
                );
                vec_computers.push(computer);
            }
            Type::Ou => {
                let ou = parse_ou(
                    cloneresult,
                    domain,
                    dn_sid,
                    sid_type,
                );
                vec_ous.push(ou);
            }
            Type::Domain => {
                let domain = parse_domain(
                    cloneresult,
                    domain,
                    dn_sid,
                    sid_type,
                );
                vec_domains.push(domain);
            }
            Type::Gpo => {
                let gpo = parse_gpo(
                    cloneresult,
                    domain,
                    dn_sid,
                    sid_type,
                );
                vec_gpos.push(gpo);
            }
            Type::ForeignSecurityPrincipal => {
                let security_principal = parse_fsp(
                    cloneresult,
                    domain,
                    dn_sid,
                    sid_type,
                );
                vec_fsps.push(security_principal);
            }
            Type::Container => {
                let re = Regex::new(r"[0-9a-z-A-Z]{1,}-[0-9a-z-A-Z]{1,}-[0-9a-z-A-Z]{1,}-[0-9a-z-A-Z]{1,}").unwrap();
                if re.is_match(&cloneresult.dn.to_uppercase()) 
                {
                    //trace!("Container not to add: {}",&cloneresult.dn.to_uppercase());
                    continue
                }
                let re = Regex::new(r"CN=DOMAINUPDATES,CN=SYSTEM,").unwrap();
                if re.is_match(&cloneresult.dn.to_uppercase()) 
                {
                    //trace!("Container not to add: {}",&cloneresult.dn.to_uppercase());
                    continue
                }
                //trace!("Container: {}",&cloneresult.dn.to_uppercase());
                let container = parse_container(
                    cloneresult,
                    domain,
                    dn_sid,
                    sid_type,
                );
                vec_containers.push(container);
            }
            Type::Trust => {
                let trust = parse_trust(
                    cloneresult,
                    domain
                );
                vec_trusts.push(trust);
            }
            Type::AdcsAuthority => {
                let adcs_ca = parse_adcs_ca(
                    cloneresult.to_owned(),
                    domain,
                    adcs_templates,
                    common_args.old_bloodhound,
                );
                vec_cas.push(adcs_ca); 
            }
            Type::AdcsTemplate => {
                let adcs_template = parse_adcs_template(
                    cloneresult.to_owned(),
                    domain,
                    common_args.old_bloodhound,
                );
                vec_templates.push(adcs_template);
            }
            Type::Unknown => {
                let _unknown = parse_unknown(cloneresult, domain);
            }
        }
        // Manage progress bar
        // Pourcentage (%) = 100 x Valeur partielle/Valeur totale
		count += 1;
        let pourcentage = 100 * count / total;
        progress_bar(pb.to_owned(),"Parsing LDAP objects".to_string(),pourcentage.try_into().unwrap(),"%".to_string());
    }
    pb.finish_and_clear();
    info!("Parsing LDAP objects finished!");
}


/// Parse user. Select parser based on BH version.
pub fn parse_user(
    result: SearchEntry,
    domain: &String,
    dn_sid: &mut HashMap<String, String>,
    sid_type: &mut HashMap<String, String>,
    adcs: bool,
) -> serde_json::value::Value {
    bh_41::parse_user(result, domain, dn_sid, sid_type, adcs)
}

/// Parse group. Select parser based on BH version.
pub fn parse_group(
    result: SearchEntry,
    domain: &String,
    dn_sid: &mut HashMap<String, String>,
    sid_type: &mut HashMap<String, String>,
) -> serde_json::value::Value {
    bh_41::parse_group(result, domain, dn_sid, sid_type)
}

/// Parse computer. Select parser based on BH version.
pub fn parse_computer(
    result: SearchEntry,
    domain: &String,
    dn_sid: &mut HashMap<String, String>,
    sid_type: &mut HashMap<String, String>,
    fqdn_sid: &mut HashMap<String, String>,
    fqdn_ip: &mut HashMap<String, String>,
) -> serde_json::value::Value {
    bh_41::parse_computer(result, domain, dn_sid, sid_type, fqdn_sid, fqdn_ip)
}

/// Parse ou. Select parser based on BH version.
pub fn parse_ou(
    result: SearchEntry,
    domain: &String,
    dn_sid: &mut HashMap<String, String>,
    sid_type: &mut HashMap<String, String>,
) -> serde_json::value::Value {
    bh_41::parse_ou(result, domain, dn_sid, sid_type)
}

/// Parse gpo. Select parser based on BH version.
pub fn parse_gpo(
    result: SearchEntry,
    domain: &String,
    dn_sid: &mut HashMap<String, String>,
    sid_type: &mut HashMap<String, String>,
) -> serde_json::value::Value {
    bh_41::parse_gpo(result, domain, dn_sid, sid_type)
}

/// Parse domain. Select parser based on BH version.
pub fn parse_domain(
    result: SearchEntry,
    domain: &String,
    dn_sid: &mut HashMap<String, String>,
    sid_type: &mut HashMap<String, String>,
) -> serde_json::value::Value {
    bh_41::parse_domain(result, domain, dn_sid, sid_type)
}

/// Parse ForeignSecurityPrincipal object. Select parser based on BH version.
pub fn parse_fsp(
    result: SearchEntry,
    domain: &String,
    dn_sid: &mut HashMap<String, String>,
    sid_type: &mut HashMap<String, String>,
) -> serde_json::value::Value {
    bh_41::parse_fsp(result, domain, dn_sid, sid_type)
}

/// Parse Containers object. Select parser based on BH version new in BH4.1+
pub fn parse_container(
    result: SearchEntry,
    domain: &String,
    dn_sid: &mut HashMap<String, String>,
    sid_type: &mut HashMap<String, String>,
) -> serde_json::value::Value {
    bh_41::parse_container(result, domain, dn_sid, sid_type)
}

/// Parse Trust domain object. Select parser based on BH version.
pub fn parse_trust(
    result: SearchEntry, 
    _domain: &String,
) -> serde_json::value::Value {
    bh_41::parse_trust(result, _domain)
}

/// Parse unknown object. Select parser based on BH version.
pub fn parse_unknown(
    result: SearchEntry, 
    _domain: &String,
) -> serde_json::value::Value {
    bh_41::parse_unknown(result, _domain)
}