-
-
Notifications
You must be signed in to change notification settings - Fork 91
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
61cb085
commit ab15307
Showing
7 changed files
with
146 additions
and
3 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
use std::process::Stdio; | ||
use std::time::Duration; | ||
|
||
use async_trait::async_trait; | ||
use ctor::ctor; | ||
|
||
use crate::session::{Error, Loot}; | ||
use crate::Plugin; | ||
use crate::{utils, Options}; | ||
|
||
use crate::creds::Credentials; | ||
|
||
pub(crate) mod options; | ||
|
||
#[ctor] | ||
fn register() { | ||
crate::plugins::manager::register("cmd", Box::new(Command::new())); | ||
} | ||
|
||
#[derive(Clone)] | ||
pub(crate) struct Command { | ||
opts: options::Options, | ||
} | ||
|
||
impl Command { | ||
pub fn new() -> Self { | ||
Command { | ||
opts: options::Options::default(), | ||
} | ||
} | ||
|
||
async fn run(&self, creds: &Credentials) -> Result<std::process::Output, Error> { | ||
let (target, port) = utils::parse_target(&creds.target, 0)?; | ||
let args = shell_words::split( | ||
&self | ||
.opts | ||
.cmd_args | ||
.replace("{USERNAME}", &creds.username) | ||
.replace("{PASSWORD}", &creds.password) | ||
.replace("{TARGET}", &target) | ||
.replace("{PORT}", &format!("{}", port)), | ||
) | ||
.unwrap(); | ||
|
||
log::debug!("{} {}", &self.opts.cmd_binary, args.join(" ")); | ||
|
||
let child = std::process::Command::new(&self.opts.cmd_binary) | ||
.args(&args) | ||
.stdin(Stdio::null()) | ||
.stdout(Stdio::null()) | ||
.stderr(Stdio::null()) | ||
.spawn() | ||
.map_err(|e| e.to_string())?; | ||
|
||
child.wait_with_output().map_err(|e| e.to_string()) | ||
} | ||
} | ||
|
||
#[async_trait] | ||
impl Plugin for Command { | ||
fn description(&self) -> &'static str { | ||
"Command execution." | ||
} | ||
|
||
fn setup(&mut self, opts: &Options) -> Result<(), Error> { | ||
self.opts = opts.cmd.clone(); | ||
Ok(()) | ||
} | ||
|
||
async fn attempt(&self, creds: &Credentials, timeout: Duration) -> Result<Option<Loot>, Error> { | ||
let output = tokio::time::timeout(timeout, self.run(creds)) | ||
.await | ||
.map_err(|e| e.to_string())?; | ||
|
||
if let Ok(out) = output { | ||
let stdout = String::from_utf8_lossy(&out.stdout); | ||
let stderr = String::from_utf8_lossy(&out.stderr); | ||
if !stderr.is_empty() { | ||
log::error!("{}", stderr); | ||
} | ||
|
||
log::debug!("{}", &stdout); | ||
|
||
// check exit code first | ||
if out.status.code().unwrap_or(-1) == self.opts.cmd_success_exit_code { | ||
// then output if needed | ||
let ok = if let Some(pattern) = &self.opts.cmd_success_match { | ||
stdout.contains(pattern) | ||
} else { | ||
true | ||
}; | ||
|
||
if ok { | ||
return Ok(Some(Loot::new( | ||
"command", | ||
&creds.target, | ||
[ | ||
("username".to_owned(), creds.username.to_owned()), | ||
("password".to_owned(), creds.password.to_owned()), | ||
], | ||
))); | ||
} | ||
} | ||
|
||
return Ok(None); | ||
} else { | ||
return Err(output.err().unwrap().to_string()); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
use clap::Parser; | ||
use serde::{Deserialize, Serialize}; | ||
|
||
#[derive(Parser, Debug, Serialize, Deserialize, Clone, Default)] | ||
#[group(skip)] | ||
pub(crate) struct Options { | ||
#[clap(long)] | ||
/// Command binary path. | ||
pub cmd_binary: String, | ||
|
||
#[clap(long, default_value = "")] | ||
/// Command arguments. {USERNAME}, {PASSWORD}, {TARGET} and {PORT} can be used as placeholders. | ||
pub cmd_args: String, | ||
|
||
#[clap(long, default_value_t = 0)] | ||
/// Process exit code to be considered as a positive match. | ||
pub cmd_success_exit_code: i32, | ||
|
||
#[clap(long)] | ||
/// String to look for in the process standard output to be considered as a positive match. | ||
pub cmd_success_match: Option<String>, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters