Removed submodule classification in git

This commit is contained in:
brotoskyj
2025-11-04 09:04:39 -05:00
parent b59f2689f4
commit 14f8d4c420
9 changed files with 3643 additions and 1 deletions
+215
View File
@@ -0,0 +1,215 @@
use chrono::{Duration, Local, NaiveDate};
use indicatif::{ProgressBar, ProgressDrawTarget, ProgressStyle};
use mongodb::bson::oid::ObjectId;
use pyo3::prelude::*;
use reqwest::{
Client,
header::{HeaderMap, HeaderName, HeaderValue},
};
use serde::{Deserialize, Serialize};
use serde_json::Value;
use std::{env, fmt::Write, fs, path::PathBuf, str::FromStr};
#[derive(Debug, Deserialize, Serialize)]
struct ApiResponse {
error: String,
response: ExecHistories,
}
#[derive(Debug, Deserialize, Serialize)]
struct ExecHistories {
exechistories: Vec<Group>,
}
#[derive(Debug, Deserialize, Serialize, Clone)]
struct Group {
checkpoint: String,
#[serde(rename = "type")]
exectype: u8,
username: String,
hostname: String,
netdomain: String,
filename: String,
ppolicy: String,
policyname: String,
policyver: String,
commandline: String,
publisher: String,
pprocess: String,
gprocess: String,
sha256: String,
datetime: String,
md5: String,
sha128: String,
sha384: String,
sha512: String,
ip: String,
localip: String,
}
#[pyfunction]
pub fn pull_policy_exec_histories(
py: Python<'_>,
py_self: Py<PyAny>,
policy_names: String,
exec_types: String,
days: i64,
//output_json: bool,
) {
let file_path: PathBuf = format!(
"{}\\cache\\chunkinator.json",
get_base_directory().display()
)
.into();
let writeable_filepath = file_path.clone();
if !file_path.exists() {
if let Some(parent_dir) = file_path.parent()
&& !parent_dir.exists() {
fs::create_dir_all(parent_dir).unwrap();
}
fs::File::create(file_path).unwrap();
}
let mut data = ApiResponse {
error: "Success".to_string(),
response: ExecHistories {
exechistories: vec![],
},
};
let data_write = serde_json::to_string_pretty(&data).expect("Failed to serialize");
fs::write(writeable_filepath, data_write).unwrap();
let mut checkpoint_number: String = skipback(days).to_string();
let data_bar =
ProgressBar::with_draw_target(Some(10_000), ProgressDrawTarget::stdout_with_hz(255));
data_bar.set_style(
ProgressStyle::default_bar()
.template("Checkpoint Progress: {msg} [{bar:40.cyan/blue}] {pos}/{len}")
.unwrap(),
);
data_bar.set_message("Starting");
let progress_bar = ProgressBar::new(100);
progress_bar.set_style(
ProgressStyle::default_bar()
.template("Total Completion: {msg} [{bar:40.cyan/blue}] {pos}/{len}")
.unwrap(),
);
let api: Py<PyAny> = py_self;
loop {
let execution_histories =
history_logging(py, &api, &exec_types, &checkpoint_number, &policy_names);
let parsed_responses = execution_histories.response.exechistories;
if parsed_responses.is_empty() {
break;
}
data_bar.set_length(parsed_responses.len() as u64);
let mut batch = 0;
for (index, executions) in parsed_responses.iter().enumerate() {
batch += 1;
if executions.checkpoint.is_empty() || executions.datetime.is_empty() {
continue;
}
if index == parsed_responses.len() - 1 {
checkpoint_number = executions.checkpoint.clone();
data_bar.set_message(checkpoint_number.clone());
break;
}
let history_date = match NaiveDate::parse_from_str(
&executions.datetime.replace(" +0000 UTC", ""),
"%Y-%m-%dT%H:%M:%SZ",
) {
Ok(date) => date,
Err(_) => continue,
};
let cutoff = Local::now().naive_local() - Duration::days(days);
if history_date >= cutoff.into() {
data.response.exechistories.push(executions.clone());
}
if batch >= 50 {
data_bar.inc(batch);
batch = 0;
}
}
batch = 0;
data_bar.set_position(0);
}
}
#[tokio::main]
async fn history_logging(
py: Python<'_>,
py_self: &Py<PyAny>,
exec_types: &String,
checkpoint_number: &String,
policy_names: &String,
) -> ApiResponse {
let base_url = py_self.getattr(py, "base_url").unwrap().to_string();
let headers = py_self.getattr(py, "headers").unwrap().to_string();
let headers_replace = headers.replace('\'', "\"");
let parsed: Value = serde_json::from_str(headers_replace.as_str()).unwrap();
let mut header_map = HeaderMap::new();
if let Some(obj) = parsed.as_object() {
for (key, value) in obj {
if let Some(v) = value.as_str() {
let val = HeaderValue::from_str(v).unwrap();
header_map.insert(HeaderName::from_str("X-APIKey").unwrap(), val);
}
}
}
let payload = format!(
r#"{{
"type": {},
"checkpoint": "{}",
"policy": ["{}"]
}}"#,
exec_types, checkpoint_number, policy_names
);
let client = Client::builder()
.danger_accept_invalid_certs(true)
.default_headers(header_map)
.timeout(std::time::Duration::from_secs(30))
.build()
.unwrap();
let res = client
.post(format!("{}/v1/logging/exechistories", base_url))
.body(payload)
.send()
.await;
match res {
Ok(res) => {
let first_response: ApiResponse = serde_json::from_str(&res.text().await.unwrap())
.expect("Failed to retrieve response from API");
return first_response;
}
Err(res) => {
let failed_response: ApiResponse = ApiResponse {
error: "Failed".to_string(),
response: ExecHistories {
exechistories: vec![],
},
};
return failed_response;
}
}
}
fn get_base_directory() -> PathBuf {
let home = env::var_os("HOME")
.map(PathBuf::from)
.or_else(|| env::var_os("USERPROFILE").map(PathBuf::from))
.expect("Could not find Home Directory");
let os = std::env::consts::OS;
match os {
"windows" => {
let appdata = env::var_os("APPDATA")
.map(PathBuf::from)
.unwrap_or_else(|| home.join("AppData").join("Roaming"));
appdata.join("AirlockTools")
}
_ => home.join(".local").join("share").join("AirlockTools"),
}
}
fn skipback(days: i64) -> ObjectId {
let date_days_ago = Local::now() - Duration::days(days);
let timestamp = date_days_ago.timestamp() as u32;
let mut hex_timestamp = String::new();
write!(&mut hex_timestamp, "{:08x}", timestamp).unwrap();
let objectid_hex = format!("{}0000000000000000", hex_timestamp);
ObjectId::parse_str(&objectid_hex).expect("Invalid ObjectId hex")
}