Skip to content

Commit

Permalink
Optimized code style
Browse files Browse the repository at this point in the history
  • Loading branch information
b1nhack committed Jun 20, 2023
1 parent a5fba42 commit 2e577d8
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 15 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "superman"
version = "1.1.5"
version = "1.1.6"
edition = "2021"
authors = ["b1n <[email protected]>"]
description = "Kill everything."
Expand Down
29 changes: 21 additions & 8 deletions src/driver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ use windows_sys::Win32::System::Services::{
};
use windows_sys::Win32::System::IO::DeviceIoControl;

/// Entry structure, representing a driver and its operations
pub struct Driver {
path: PathBuf,
service_name: CString,
Expand All @@ -41,6 +42,21 @@ impl Driver {
pub fn service_name(&self) -> &CStr {
&self.service_name
}

/// Load and start driver
pub fn load_driver(&self) -> anyhow::Result<()> {
load_driver(self)
}

/// Unload and delete driver
pub fn unload_driver(&self) -> anyhow::Result<()> {
unload_driver(self)
}

/// Send ioctl to kill pid
pub fn kill_pid(&self, args: &Args, rx: Receiver<bool>) -> anyhow::Result<()> {
kill_pid(self, args, rx)
}
}

/// Make sure driver status
Expand Down Expand Up @@ -84,15 +100,14 @@ fn check_service_status(driver: &Driver) -> anyhow::Result<bool> {
Ok(true)
}
_ => {
unload_driver(driver)?;
driver.unload_driver()?;
Ok(false)
}
}
}
}

/// Load and start driver
pub fn load_driver(driver: &Driver) -> anyhow::Result<()> {
fn load_driver(driver: &Driver) -> anyhow::Result<()> {
if check_service_status(driver)? {
return Ok(());
}
Expand Down Expand Up @@ -144,8 +159,7 @@ pub fn load_driver(driver: &Driver) -> anyhow::Result<()> {
Ok(())
}

/// Unload and delete driver
pub fn unload_driver(driver: &Driver) -> anyhow::Result<()> {
fn unload_driver(driver: &Driver) -> anyhow::Result<()> {
let mut status: SERVICE_STATUS = unsafe { zeroed() };

unsafe {
Expand Down Expand Up @@ -188,8 +202,7 @@ pub fn unload_driver(driver: &Driver) -> anyhow::Result<()> {
Ok(())
}

/// Send ioctl to kill pid
pub fn kill_pid(args: &Args, driver: &Driver, rx: Receiver<bool>) -> anyhow::Result<()> {
fn kill_pid(driver: &Driver, args: &Args, rx: Receiver<bool>) -> anyhow::Result<()> {
let initialize_ioctl_code: u32 = 0x9876C004u32;
let terminate_process_ioctl_code: u32 = 0x9876C094u32;
let device_name = CStr::from_bytes_with_nul(b"\\\\.\\superman\0")?;
Expand Down Expand Up @@ -246,7 +259,7 @@ pub fn kill_pid(args: &Args, driver: &Driver, rx: Receiver<bool>) -> anyhow::Res
// exit
if rx.try_recv().is_ok() {
CloseHandle(device);
unload_driver(driver)?;
driver.unload_driver()?;
process::exit(0i32);
}

Expand Down
11 changes: 5 additions & 6 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ mod greet;
mod utils;

use crate::args::Args;
use crate::driver::{kill_pid, load_driver, unload_driver, Driver};
use crate::driver::Driver;
use crate::utils::check_pid;
use anyhow::anyhow;
use clap::Parser;
Expand Down Expand Up @@ -56,19 +56,18 @@ fn main() {
if let Err(e) = try_main(&args, &driver) {
eprintln!("{}", e);
}
let _ = unload_driver(&driver);
let _ = driver.unload_driver();
}

fn try_main(args: &Args, driver: &Driver) -> anyhow::Result<()> {
let (sx, rx) = mpsc::sync_channel(1);
let (sx, rx) = mpsc::sync_channel(0);
init_ctrlc(sx)?;

if check_pid(args.pid).not() {
return Err(anyhow!("[-]Process not exists!"));
}

load_driver(driver)?;

kill_pid(args, driver, rx)?;
driver.load_driver()?;
driver.kill_pid(args, rx)?;
Ok(())
}

0 comments on commit 2e577d8

Please sign in to comment.