rusthound_ce/enums/
sid.rs1use std::error::Error;
2use log::{trace,error};
3use crate::enums::{secdesc::LdapSid, regex::IS_SID_RE1};
4
5pub fn is_sid(input: &str) -> Result<bool, Box<dyn Error>> {
7 Ok(IS_SID_RE1.is_match(input))
8}
9
10pub fn sid_maker(sid: LdapSid, domain: &str) -> String {
12 trace!("sid_maker before: {:?}",&sid);
13
14 let sub = sid.sub_authority.iter().map(|x| x.to_string()).collect::<Vec<String>>().join("-");
15
16 let result = format!("S-{}-{}-{}", sid.revision, sid.identifier_authority.value[5], sub);
17
18 let final_sid = {
19 if result.len() <= 16 {
20 format!("{}-{}", domain.to_uppercase(), result.to_owned())
21 } else {
22 result
23 }
24 };
25
26 trace!("sid_maker value: {}",final_sid);
27 if final_sid.contains("S-0-0"){
28 error!("SID contains null bytes!\n[INPUT: {:?}]\n[OUTPUT: {}]", &sid, final_sid);
29 }
30
31 final_sid
32}
33
34pub fn objectsid_to_vec8(sid: &str) -> Vec<u8>
36{
37 sid.as_bytes().iter().map(|x| *x).collect::<Vec<u8>>()
38}
39
40pub fn _decode_guid(raw_guid: &[u8]) -> String
44{
45 let raw_guid = raw_guid.iter().map(|x| x & 0xFF).collect::<Vec<u8>>();
48 let rev = | x: &[u8] | -> Vec<u8> { x.iter().map(|i| *i).rev().collect::<Vec<u8>>()};
49
50 let str_guid = format!(
52 "{}-{}-{}-{}-{}",
53 &hex_push(&raw_guid[0..4]),
54 &hex_push(&rev(&raw_guid[4..6])),
55 &hex_push(&rev(&raw_guid[6..8])),
56 &hex_push(&raw_guid[8..10]),
57 &hex_push(&raw_guid[10..16]),
58 );
59
60 str_guid
61}
62
63pub fn hex_push(blob: &[u8]) -> String {
66 blob.iter().map(|x| format!("{:X}", x)).collect::<String>()
68}
69
70pub fn bin_to_string(raw_guid: &[u8]) -> String
72{
73 let raw_guid = raw_guid.iter().map(|x| x & 0xFF).collect::<Vec<u8>>();
79 let rev = | x: &[u8] | -> Vec<u8> { x.iter().map(|i| *i).collect::<Vec<u8>>()};
80
81 let str_guid = format!(
82 "{}-{}-{}-{}-{}",
83 &hex_push(&raw_guid[12..16]),
84 &hex_push(&raw_guid[10..12]),
85 &hex_push(&raw_guid[8..10]),
86 &hex_push(&rev(&raw_guid[6..8])),
87 &hex_push(&rev(&raw_guid[0..6]))
88 );
89
90 str_guid
91}
92
93pub fn decode_guid_le(raw_guid: &[u8]) -> String {
95 let str_guid = format!(
97 "{:02X}{:02X}{:02X}{:02X}-{:02X}{:02X}-{:02X}{:02X}-{:02X}{:02X}-{:02X}{:02X}{:02X}{:02X}{:02X}{:02X}",
98 raw_guid[3], raw_guid[2], raw_guid[1], raw_guid[0], raw_guid[5], raw_guid[4], raw_guid[7], raw_guid[6], raw_guid[8], raw_guid[9], raw_guid[10], raw_guid[11], raw_guid[12], raw_guid[13], raw_guid[14], raw_guid[15] );
104
105 str_guid
106}