rusthound_ce/json/maker/
common.rs1use serde_json::value::Value;
2
3use std::collections::HashMap;
4use colored::Colorize;
5use log::{info, debug, trace};
6
7use std::fs;
8use std::fs::File;
9use std::io::{Seek, Write};
10use std::error::Error;
11
12use zip::result::ZipResult;
13use zip::write::{SimpleFileOptions, ZipWriter};
14
15extern crate zip;
16use crate::args::{Options, RUSTHOUND_VERSION};
17use crate::objects::common::{FinalJson, Meta, LdapObject};
18
19pub const BLOODHOUND_VERSION_4: i8 = 6;
21
22pub fn add_file<T: LdapObject>(
24 datetime: &String,
25 name: String,
26 domain_format: &String,
27 vec_json: Vec<T>,
28 json_result: &mut HashMap<String, String>,
29 common_args: &Options,
30 ) -> std::io::Result<()>
31 {
32 if !vec_json.is_empty() {
33 debug!("Making {}.json",&name);
34
35 let path = &common_args.path;
36 let zip = common_args.zip;
37 let count = vec_json.len();
38
39 let mut result: Vec<Value> = Vec::new();
40 for object in vec_json {
41 result.push(object.to_json().to_owned());
42 }
43 let final_json = FinalJson::new(
45 result,
46 Meta::new(
47 000000_i32,
48 name.to_owned(),
49 count as i32,
50 BLOODHOUND_VERSION_4,
51 format!("RustHound-CE v{}",RUSTHOUND_VERSION.to_owned())
52 )
53 );
54
55 info!("{} {} parsed!", count.to_string().bold(),&name);
56
57 fs::create_dir_all(path)?;
59
60 if ! zip
62 {
63 let final_path = format!("{}/{}_{}_{}.json",path,datetime,domain_format,name);
64 fs::write(&final_path, serde_json::to_string(&final_json)?)?;
65 info!("{} created!",final_path.bold());
66 }
67 else
68 {
69 json_result.insert(format!("{}_{}_{}.json",datetime,domain_format,name).to_string(),serde_json::to_string(&final_json)?);
70 }
71 }
72 Ok(())
73 }
74
75 pub fn make_a_zip(
77 datetime: &String,
78 domain: &String,
79 path: &String,
80 json_result: &HashMap<String, String>
81 ) -> Result<String, Box<dyn Error>> {
82 let final_path = format!("{}/{}_{}_rusthound-ce.zip",path,datetime,domain);
83 let mut file = File::create(&final_path).expect("Couldn't create file");
84 create_zip_archive(&mut file, json_result).expect("Couldn't create archive");
85
86 info!("{} created!",&final_path.bold());
87 Ok(final_path)
88 }
89
90
91 fn create_zip_archive<T: Seek + Write>(zip_filename: &mut T,json_result: &HashMap<String, String>) -> ZipResult<()> {
92 let mut writer = ZipWriter::new(zip_filename);
93 trace!("Making the ZIP file");
95
96 for file in json_result
97 {
98 let filename = file.0;
99 let content = file.1;
100 trace!("Adding file {}",filename.bold());
101 let options = SimpleFileOptions::default().compression_method(zip::CompressionMethod::Deflated);
102 writer.start_file(filename, options)?;
103 writer.write_all(content.as_bytes())?;
104 }
105
106 writer.finish()?;
107 Ok(())
108 }