Skip to content

Commit

Permalink
#24 & Execute command on connection recieved
Browse files Browse the repository at this point in the history
  • Loading branch information
robiot committed Sep 28, 2021
1 parent 7591a40 commit 226e70f
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 36 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

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

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "rustcat"
version = "1.2.0"
version = "1.3.0"
authors = ["robiot"]
description = "rustcat - The Modern Port Listener & Reverse Shell"
license = "MIT"
Expand Down
12 changes: 10 additions & 2 deletions src/input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,13 @@ use structopt::StructOpt;
#[structopt(name = "rustcat", setting = structopt::clap::AppSettings::ArgRequiredElseHelp)]
pub struct Opts {
/// Command history for tcp
#[structopt(short = "H", long)]
#[structopt(short = "H", long, conflicts_with = "rshell")]
pub history: bool,

/// Local history
#[structopt(short = "L", long, conflicts_with_all = &["history", "exec"])]
pub local_history: bool,

/// Listen mode
#[structopt(short, long = "listen", conflicts_with = "rshell")]
pub listen_mode: bool,
Expand All @@ -20,12 +24,16 @@ pub struct Opts {
#[structopt(short, long = "udp")]
pub udp_mode: bool,

/// Execute command when connection recieved
#[structopt(short, long, value_name = "command", conflicts_with_all = &["local_history", "rshell"])]
pub exec: Option<String>,

/// Local port
#[structopt(short, long)]
pub port: Option<String>,

/// Reverse shell
#[structopt(short, long, conflicts_with_all = &["listen_mode", "udp_mode", "history"])]
#[structopt(short, long, value_name="shell")]
pub rshell: Option<String>,

// Host:ip
Expand Down
76 changes: 45 additions & 31 deletions src/listener/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,18 @@ fn print_started_listen(opts: &utils::Opts) {
println!("Listening on {}:{}", opts.host.green(), opts.port.cyan());
}

fn print_connection_recieved() {
println!("{} Connection Recived", "[+]".green());
}

fn pipe_thread<R, W>(mut r: R, mut w: W) -> std::thread::JoinHandle<()>
where
R: std::io::Read + Send + 'static,
W: std::io::Write + Send + 'static,
{
std::thread::spawn(move || {
let mut buffer = [0; 1024];
//w.write_all("echo test\n".as_bytes()).unwrap();>
loop {
let len = match r.read(&mut buffer) {
Ok(t) => t,
Expand All @@ -47,10 +52,15 @@ where
})
}

fn listen_tcp_normal(stream: std::net::TcpStream) -> std::io::Result<()> {
fn listen_tcp_normal(stream: std::net::TcpStream, opts: &utils::Opts) -> std::io::Result<()> {
if opts.exec != None {
stream
.try_clone()?
.write_all(format!("{}\n", opts.exec.as_ref().unwrap()).as_bytes())?;
}
let t1 = pipe_thread(std::io::stdin(), stream.try_clone()?);
let t2 = pipe_thread(stream, std::io::stdout());
println!("Connection Recived");
print_connection_recieved();
t1.join().unwrap();
t2.join().unwrap();
Ok(())
Expand All @@ -69,38 +79,42 @@ pub fn listen(opts: &utils::Opts) -> std::io::Result<()> {
if cfg!(unix) {
#[cfg(unix)]
termios_handler::setup_fd()?;
listen_tcp_normal(stream)?;
listen_tcp_normal(stream, opts)?;
} else {
let t = pipe_thread(stream.try_clone().unwrap(), std::io::stdout());
let mut rl = Editor::<()>::new();
loop {
let readline = rl.readline(">> ");
match readline {
Ok(command) => {
rl.add_history_entry(command.as_str());
let command = command.clone() + "\n";
stream
.write_all(command.as_bytes())
.expect("Faild to send TCP.");
}
Err(ReadlineError::Interrupted) => {
break;
}
Err(ReadlineError::Eof) => {
break;
}
Err(err) => {
utils::print_error(err);
break;
}
utils::print_error("Normal history is not supported on your platform");
}
}
utils::Mode::LocalHistory => {
let t = pipe_thread(stream.try_clone().unwrap(), std::io::stdout());
let mut rl = Editor::<()>::new();
print_connection_recieved();
loop {
let readline = rl.readline(">> ");
match readline {
Ok(command) => {
rl.add_history_entry(command.as_str());
let command = command.clone() + "\n";
stream
.write_all(command.as_bytes())
.expect("Faild to send TCP.");
}
Err(ReadlineError::Interrupted) => {
break;
}
Err(ReadlineError::Eof) => {
break;
}
Err(err) => {
utils::print_error(err);
break;
}
}
t.join().unwrap();
}
t.join().unwrap();
}
utils::Mode::Normal => {
listen_tcp_normal(stream)?;
},
listen_tcp_normal(stream, opts)?;
}
}
}

Expand Down Expand Up @@ -135,14 +149,14 @@ pub fn listen(opts: &utils::Opts) -> std::io::Result<()> {
}
}
Err(ReadlineError::Interrupted) => {
break;
std::process::exit(0);
}
Err(ReadlineError::Eof) => {
break;
std::process::exit(0);
}
Err(err) => {
utils::print_error(err);
break;
std::process::exit(0);
}
}
}
Expand Down
7 changes: 6 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,17 +41,22 @@ fn main() {

// Listen mode
else if opts.listen_mode {

let opts = utils::Opts {
host: opt_host,
port: opt_port,
exec: opts.exec,
transport: if opts.udp_mode {
utils::Protocol::Udp
} else {
utils::Protocol::Tcp
},
mode: if opts.history {
utils::Mode::History
} else {
} else if opts.local_history {
utils::Mode::LocalHistory
}
else {
utils::Mode::Normal
},
};
Expand Down
2 changes: 2 additions & 0 deletions src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use colored::Colorize;
pub struct Opts {
pub host: String,
pub port: String,
pub exec: Option<String>,
pub transport: Protocol,
pub mode: Mode,
}
Expand All @@ -21,6 +22,7 @@ pub enum Protocol {
pub enum Mode {
Normal,
History,
LocalHistory
}

/* Public Functions */
Expand Down

0 comments on commit 226e70f

Please sign in to comment.