Skip to content

Instantly share code, notes, and snippets.

@Demindiro
Created July 17, 2022 08:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Demindiro/f81527051e9c4f0455a40354b4d4d2ca to your computer and use it in GitHub Desktop.
Save Demindiro/f81527051e9c4f0455a40354b4d4d2ca to your computer and use it in GitHub Desktop.
Silly program that replaces a string in a MPV process.
use std::io::{Read, Write, Seek, SeekFrom};
use std::fs::OpenOptions;
use std::ffi::OsString;
use std::error::Error;
use procfs::process::*;
fn main() -> Result<(), Box<dyn Error>> {
for proc in all_processes()?.filter_map(|p| p.ok()) {
match scan(proc) {
Ok(true) => return Ok(()),
_ => {}
}
}
eprintln!("no mpv process found");
Ok(())
}
fn scan(proc: Process) -> Result<bool, Box<dyn Error>> {
let s = "Waikyō".as_bytes();
let r = b"WARIOOO";
assert_eq!(s.len(), r.len());
Ok(if proc.exe()?.file_name() == Some(&OsString::from("mpv")) {
dbg!(&proc);
let mem_path = format!("/proc/{}/mem", proc.pid);
let mut mem = OpenOptions::new().read(true).write(true).open(&mem_path).unwrap();
let maps = proc.maps()?;
for map in maps {
match map.pathname {
MMapPath::Heap | MMapPath::Anonymous => {
mem.seek(SeekFrom::Start(map.address.0)).unwrap();
assert_eq!(map.address.1 % 0x1000, 0);
let mut buf = (map.address.0..map.address.1).map(|_| 0).collect::<Vec<_>>();
mem.read_exact(&mut buf).unwrap();
for (i, w) in buf.windows(s.len()).enumerate() {
if w == s {
mem.seek(SeekFrom::Start(map.address.0 + i as u64)).unwrap();
mem.write_all(r).unwrap();
dbg!(i, map);
return Ok(true)
}
}
}
_ => {}
}
}
true
} else {
false
})
}
[package]
name = "hello_world"
version = "0.1.0"
edition = "2021"
[dependencies.procfs]
version = "0.13"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment