Skip to content

Commit

Permalink
refactor: 🔨 Move get pid function to the windows folder because it's …
Browse files Browse the repository at this point in the history
…a general thing
AntwortEinesLebens committed Sep 8, 2024
1 parent d1cd621 commit d2ce3fd
Showing 3 changed files with 69 additions and 63 deletions.
67 changes: 4 additions & 63 deletions src/actions/processes/spoofing.rs
Original file line number Diff line number Diff line change
@@ -6,27 +6,15 @@
//
// Last update 20240224

use crate::actions::Runnable;
use crate::{actions::Runnable, windows::processes::get_pid};
use clap::Parser;
use core::ffi::c_void;
use std::{
error::Error,
ffi::OsString,
fmt::{Display, Formatter, Result as FormatterResult},
mem::size_of,
os::windows::ffi::OsStringExt,
thread,
time::Duration,
};
use std::{error::Error, mem::size_of, thread, time::Duration};
use windows::{
core::{Owned, PSTR},
core::PSTR,
Win32::{
Foundation::{CloseHandle, HANDLE},
System::{
Diagnostics::ToolHelp::{
CreateToolhelp32Snapshot, Process32FirstW, Process32NextW, PROCESSENTRY32W,
TH32CS_SNAPPROCESS,
},
Memory::{GetProcessHeap, HeapAlloc, HEAP_FLAGS},
Threading::{
CreateProcessA, InitializeProcThreadAttributeList, OpenProcess, TerminateProcess,
@@ -51,50 +39,6 @@ pub struct Spoofing {
parent_executable: String,
}

#[derive(Debug)]
struct ProcessNotFound;

impl Error for ProcessNotFound {}

impl Display for ProcessNotFound {
fn fmt(&self, formatter: &mut Formatter) -> FormatterResult {
write!(formatter, "Process not found")
}
}

fn get_pid_from_name(name: &str) -> Result<u32, Box<dyn Error>> {
let snapshot: Owned<HANDLE> =
unsafe { Owned::new(CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0)?) };
let mut process_entry: PROCESSENTRY32W = PROCESSENTRY32W {
dwSize: size_of::<PROCESSENTRY32W>() as u32,
..Default::default()
};

unsafe {
Process32FirstW(*snapshot, &mut process_entry)?;
}

loop {
if OsString::from_wide(
process_entry
.szExeFile
.into_iter()
.take_while(|&byte| byte != 0)
.collect::<Vec<_>>()
.as_slice(),
) == name
{
return Ok(process_entry.th32ProcessID);
}

if unsafe { Process32NextW(*snapshot, &mut process_entry) }.is_err() {
break;
}
}

Err(Box::new(ProcessNotFound))
}

fn create_ppid(name: &String, new_ppid: u32) -> bool {
println!("Use the PPID {}", new_ppid);
println!("Open the Parent Process");
@@ -170,10 +114,7 @@ impl Runnable for Spoofing {
/* Version 20240209 */
fn run(&self) -> Result<i32, Box<dyn Error>> {
println!("PPID spoofing");
let result: bool = create_ppid(
&self.executable,
get_pid_from_name(&self.parent_executable)?,
);
let result: bool = create_ppid(&self.executable, get_pid(&self.parent_executable)?);

Ok(!result as i32)
}
1 change: 1 addition & 0 deletions src/windows.rs
Original file line number Diff line number Diff line change
@@ -2,4 +2,5 @@
//
// SPDX-License-Identifier: GPL-3.0-or-later

pub mod processes;
pub mod users;
64 changes: 64 additions & 0 deletions src/windows/processes.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
// SPDX-FileCopyrightText: 2023 The WAG development team
//
// SPDX-License-Identifier: GPL-3.0-or-later

use std::{
error::Error,
ffi::OsString,
fmt::{Display, Formatter, Result as FormatterResult},
os::windows::ffi::OsStringExt,
};
use windows::{
core::Owned,
Win32::{
Foundation::HANDLE,
System::Diagnostics::ToolHelp::{
CreateToolhelp32Snapshot, Process32FirstW, Process32NextW, PROCESSENTRY32W,
TH32CS_SNAPPROCESS,
},
},
};

#[derive(Debug)]
pub struct ProcessNotFound;

impl Error for ProcessNotFound {}

impl Display for ProcessNotFound {
fn fmt(&self, formatter: &mut Formatter) -> FormatterResult {
write!(formatter, "ProcessNotFound")
}
}

pub fn get_pid(name: &str) -> Result<u32, Box<dyn Error>> {
let snapshot: Owned<HANDLE> =
unsafe { Owned::new(CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0)?) };
let mut process_entry: PROCESSENTRY32W = PROCESSENTRY32W {
dwSize: size_of::<PROCESSENTRY32W>() as u32,
..Default::default()
};

unsafe {
Process32FirstW(*snapshot, &mut process_entry)?;
}

loop {
if OsString::from_wide(
process_entry
.szExeFile
.into_iter()
.take_while(|&byte| byte != 0)
.collect::<Vec<_>>()
.as_slice(),
) == name
{
return Ok(process_entry.th32ProcessID);
}

if unsafe { Process32NextW(*snapshot, &mut process_entry) }.is_err() {
break;
}
}

Err(Box::new(ProcessNotFound))
}

0 comments on commit d2ce3fd

Please sign in to comment.