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