Skip to content

Commit

Permalink
new: implemented csv output format
Browse files Browse the repository at this point in the history
  • Loading branch information
evilsocket committed Nov 14, 2023
1 parent c5f6c71 commit 26539c9
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 23 deletions.
22 changes: 22 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ sibyl = { version = "0.6.16", optional = true, features = [
rdp-rs = { version = "0.1.0", optional = true }
scylla = { version = "0.10.1", optional = true }
paho-mqtt = { version = "0.12.3", optional = true }
csv = "1.3.0"

[dev-dependencies]
tempfile = "3.8.0"
Expand Down
75 changes: 52 additions & 23 deletions src/session/loot.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::fmt;
use std::fs::OpenOptions;
use std::io::prelude::*;
use std::{fmt, path::Path};

use ansi_term::Colour;
use chrono::{DateTime, Local};
Expand All @@ -14,6 +14,7 @@ use crate::session::Error;
pub(crate) enum OutputFormat {
#[default]
Text,
CSV,
JSONL,
}

Expand Down Expand Up @@ -59,29 +60,57 @@ impl Loot {
self.found_at.format("%Y-%m-%d %H:%M:%S").to_string()
}

fn to_json(&self) -> Result<String, Error> {
serde_json::to_string(self).map_err(|e| e.to_string())
}

fn to_text(&self) -> Result<String, Error> {
let data = self
.data
.keys()
.map(|k| format!("{}={}", k, self.data.get(k).unwrap()))
.collect::<Vec<String>>()
.join("\t");

Ok(if self.target.is_empty() {
format!("[{}] ({}) {}", self.found_at_string(), &self.plugin, data)
} else {
format!(
"[{}] ({}) <{}> {}",
self.found_at_string(),
&self.plugin,
&self.target,
data
)
})
}

fn to_csv(&self, path: &str) -> Result<String, Error> {
let mut wtr = csv::Writer::from_writer(vec![]);

if !Path::new(path).exists() {
wtr.write_record(&["found_at", "plugin", "target", "data"])
.map_err(|e| e.to_string())?;
}

let data = self
.data
.keys()
.map(|k| format!("{}={}", k, self.data.get(k).unwrap()))
.collect::<Vec<String>>()
.join(";");

wtr.write_record(&[&self.found_at_string(), &self.plugin, &self.target, &data])
.map_err(|e| e.to_string())?;

String::from_utf8(wtr.into_inner().unwrap()).map_err(|e| e.to_string())
}

pub fn append_to_file(&self, path: &str, format: &OutputFormat) -> Result<(), Error> {
let data = match format {
OutputFormat::JSONL => serde_json::to_string(self).map_err(|e| e.to_string())?,
OutputFormat::Text => {
let data = self
.data
.keys()
.map(|k| format!("{}={}", k, self.data.get(k).unwrap()))
.collect::<Vec<String>>()
.join("\t");

if self.target.is_empty() {
format!("[{}] ({}) {}", self.found_at_string(), &self.plugin, data)
} else {
format!(
"[{}] ({}) <{}> {}",
self.found_at_string(),
&self.plugin,
&self.target,
data
)
}
}
OutputFormat::JSONL => self.to_json()?,
OutputFormat::Text => self.to_text()?,
OutputFormat::CSV => self.to_csv(path)?,
};

let mut file = OpenOptions::new()
Expand All @@ -91,7 +120,7 @@ impl Loot {
.open(path)
.map_err(|e| e.to_string())?;

writeln!(file, "{}", data).map_err(|e| e.to_string())
writeln!(file, "{}", data.trim()).map_err(|e| e.to_string())
}
}

Expand Down

0 comments on commit 26539c9

Please sign in to comment.