rusthound_ce/json/maker/
common.rs

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