Crate ptrace_inject

Source
Expand description

A library for injecting shared libraries into running processes via ptrace.

§Platform support

This library currently only supports x64 *nix systems, mainly because that’s what I have. Support for other architectures should be possible - the only barrier being that I cannot test it. In theory though, it would just be a matter of re-writing the shellcode for each architecture and selecting the correct one with conditional compilation.

For Windows, use other projects like dll-syringe.

§Example

use std::{process::Command, path::PathBuf};
use ptrace_inject::{Injector, Process};

let library = PathBuf::from("path/to/library.so");

// Spawn a new process and inject the library into it.
let target = Command::new("target-process");
Injector::spawn(target)?.inject(&library)?;

// Or attach to an existing process.
let proc = Process::by_name("target-process")?.expect("to find target process");
Injector::attach(proc)?.inject(&library)?;

§Ptrace note

This library was inspired by linux-inject. As noted by that project:

On many Linux distributions, the kernel is configured by default to prevent any process from calling ptrace() on another process that it did not create (e.g. via fork()). This is a security feature meant to prevent exactly the kind of mischief that this tool causes. You can temporarily disable it until the next reboot using the following command:

echo 0 | sudo tee /proc/sys/kernel/yama/ptrace_scope

This library uses log for logging.

Structs§

Injector
A type capable of loading libraries into a ptrace’d target process.
Process
A process to attach to.