• Stars
    star
    707
  • Rank 64,036 (Top 2 %)
  • Language
    Python
  • License
    MIT License
  • Created almost 5 years ago
  • Updated over 2 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Inject a shared library (i.e. arbitrary code) into a live linux process, without ptrace

dlinject.py

Inject a shared library (i.e. arbitrary code) into a live linux process, without ptrace. Inspired by Cexigua and linux-inject, among other things.

asciicast

Usage

    .___.__  .__            __               __
  __| _/|  | |__| ____     |__| ____   _____/  |_  ______ ___.__.
 / __ | |  | |  |/    \    |  |/ __ \_/ ___\   __\ \____ <   |  |
/ /_/ | |  |_|  |   |  \   |  \  ___/\  \___|  |   |  |_> >___  |
\____ | |____/__|___|  /\__|  |\___  >\___  >__| /\|   __// ____|
     \/              \/\______|    \/     \/     \/|__|   \/

source: https://github.com/DavidBuchanan314/dlinject

usage: dlinject.py [-h] [--stopmethod {sigstop,cgroup_freeze,none}]
                   pid /path/to/lib.so

Inject a shared library into a live process.

positional arguments:
  pid                   The pid of the target process
  /path/to/lib.so       Path of the shared library to load (note: must be
                        relative to the target process's cwd, or absolute)

optional arguments:
  -h, --help            show this help message and exit
  --stopmethod {sigstop,cgroup_freeze,none}
                        How to stop the target process prior to shellcode
                        injection. SIGSTOP (default) can have side-effects.
                        cgroup freeze requires root. 'none' is likely to cause
                        race conditions.

Why?

  • Because I can.

  • There are various anti-ptrace techniques, which this evades by simply not using ptrace.

  • I don't like ptrace.

  • Using LD_PRELOAD can sometimes be fiddly or impossible, if the process you want to inject into is spawned by another process with a clean environment.

How it Works

  • Send the stop signal to the target process. (optional)

  • Locate the _dl_open() symbol.

  • Retreive RIP and RSP via /proc/[pid]/syscall.

  • Make a backup of part of the stack, and the code we're about to overwrite with our shellcode, by reading from /proc/[pid]/mem.

  • Generate primary and secondary shellcode buffers.

  • Insert primary shellcode at RIP, by writing to /proc/[pid]/mem.

  • The primary shellcode:

    • Pushes common registers to the stack.
    • Loads the secondary shellcode via mmap().
    • Jumps to the secondary shellcode.
  • The secondary shellcode:

    • Restores the stack and program code to their original states.
    • Pivots the stack (so we don't touch the original one at all).
    • Calls _dl_open() to load the user-specified library. Any constructors will be executed on load, as usual.
    • Restores register state, un-pivots the stack, and jumps back to where it was at the time of the original SIGSTOP.

Limitations:

  • Sending SIGSTOP may cause unwanted side-effects, for example if another thread is waiting on waitpid(). The --stopmethod=cgroup_freeze option avoids this, but requires root (on most distros, at least).

  • I'm not entirely sure how this will interact with complex multi-threaded applications. There's certainly potential for breakage.

  • x86-64 Linux only (for now - 32-bit support could potentially be added).

  • Requires root, or relaxed YAMA configuration (echo 0 | sudo tee /proc/sys/kernel/yama/ptrace_scope is useful when testing).

  • If the target process is sandboxed (e.g. seccomp filters), it might not have permission to mmap() the second stage shellcode, or to dlopen() the library.

More Repositories

1

tweetable-polyglot-png

Pack up to 3MB of data into a tweetable PNG polyglot file.
Python
2,467
star
2

ambiguous-png-packer

Craft PNG files that appear completely different in Apple software [NOW PATCHED]
Python
1,055
star
3

monomorph

MD5-Monomorphic Shellcode Packer - all payloads have the same MD5 hash
C
763
star
4

NXLoader

My first Android app: Launch FusΓ©e GelΓ©e payloads from stock Android (CVE-2018-6242)
Java
491
star
5

pwn-mbr

A simple MBR hijack demonstration
C
172
star
6

libleakmydata

A simple LD_PRELOAD library to disable SSL certificate verification. Inspired by libeatmydata.
C
159
star
7

TARDIS

Trace And Rewrite Delays In Syscalls: Hooking time-related Linux syscalls to warp a process's perspective of time, using ptrace.
C
153
star
8

TwitterHD

A userscript that forces twitter to always load images and videos in full resolution
JavaScript
99
star
9

unsafe-python

A library to assist writing memory-unsafe code in "pure" python, without any imports (i.e. no ctypes etc.)
Python
83
star
10

Turbo-Recadmiumator

A remake of truedread/netflix-1080p which auto-patches cadmium-playercore at runtime to enable enhanced playback features. (NOTE: still working in 2023 despite no code updates for 2 years 😎)
JavaScript
69
star
11

parallel-png-proposal

Python
69
star
12

stelf-loader

A stealthy ELF loader - no files, no execve, no RWX
C
64
star
13

fusee-nano

A minimalist re-implementation of the FusΓ©e GelΓ©e exploit, designed to run on embedded Linux devices. (Zero dependencies)
C
60
star
14

fusee-lede

Instructions/files for building a custom LEDE image to turn cheap routers into a Nintendo Switch "modchip"/"dongle". Powered by https://github.com/DavidBuchanan314/fusee-nano
Makefile
53
star
15

RootMyTV

Placeholder repo πŸ‘€
47
star
16

WAMpage

WAMpage - A WebOS root LPE exploit chain (CVE-2022-23731)
JavaScript
46
star
17

6502-emu

A simple 6502 emulator, with I/O via a 6850 UART.
C
29
star
18

cowroot

Universal Android root tool based on CVE-2016-5195. Watch this space.
C
29
star
19

picopds

A minimum viable atproto PDS for protocol experimentation purposes
Python
28
star
20

boiga

A Python library enabling ergonomic Scratch 3.0 code generation.
Python
28
star
21

webos-vncserver

An extremely hacky VNC server for WebOS - Works by reading directly from the GPU's framebuffer.
C
26
star
22

wifi-sdcf

Reverse Engineering notes on the Dxingtek/Keytech(?) WiFi@SDCF card
Python
19
star
23

trumpogram

The World, according to Donald Trump
Python
16
star
24

aes-playground

My experiments in understanding AES, Whitebox AES, and related attacks
Python
16
star
25

p65a

Pythonic 6502 Assembler: An experimental alternative to traditional assemblers.
Python
15
star
26

rc4

A python3 RC4 implementation that doesn't suck. (i.e. it's actually binary-safe...)
Python
14
star
27

bitmap-font-css

Trying to make bitmap web fonts look better.
11
star
28

scratch-cryptography-library

Modern cryptography primitives implemented in Scratch, via Boiga
Python
10
star
29

scratch-vscode

Preview Scratch projects inside vscode, with live reload.
TypeScript
9
star
30

ScapyGuard

An extremely bare-bones Python3 WireGuard client.
Python
9
star
31

rsyscall-lkm

rsyscall is a dirty hack that allows you to execute syscalls in the context of another Linux process. MΜΆaΜΆyΜΆ Will explode your kernel.
C
8
star
32

hello_png

Example code from my blog post of the same title
Python
8
star
33

beatstar-tools

Scripts for datamining the Beatstar mobile game
Python
7
star
34

shellinject

Spawn a reverse TCP shell in the context of another Linux process
Python
7
star
35

python-bitsliced-aes

An experimental implementation of bitsliced aes in pure python. Quite possibly the fastest pure-python AES implementation on the planet.
Python
7
star
36

classic-ipod-tools

Tools for manipulating classic ipod firmware images etc.
C
6
star
37

bagel

[WIP] bagel is a Binary age aLternative - mostly the same as age but with a binary header format.
Python
6
star
38

python-ssh-server

A very incomplete, incompatible, and insecure implementation of RFC4253
Python
5
star
39

cursed-code

A repo to keep track of all the useful/hacky/cursed scripts and one-liners I write
Assembly
5
star
40

dag-cbrrr

A reasonably fast DAG-CBOR parser for Python
C
5
star
41

chip8

A simple CHIP-8 Emulator with a GTK+ frontend
C
5
star
42

merkle-search-tree

An abstract implementation of a Merkle Search Tree, structurally compatible with ATProto's instantiation
Python
5
star
43

fakeIoT

Yet another telnet honeypot.
Ruby
5
star
44

dram_emfi

playing with DDR DRAM bus fault injection
Python
5
star
45

imgur-anti-anti-hotlink

A very simple WebExtension to prevent imgur from preventing you from viewing images directly.
JavaScript
4
star
46

reveilid

WIP
Python
3
star
47

gif-enc

A rather inefficient GIF encoder, in python.
Python
3
star
48

magic-helloworld

A program that prints "Hello World!" by magic, with a single memcpy.
C
3
star
49

ipod-toslink-mod

Adding optical digital audio output to classic iPods
3
star
50

shellcode

My personal shellcode collection.
Python
3
star
51

iPodWizard-mirror

A git mirror of iPodWizard: https://sourceforge.net/projects/ipodwizard/
C++
3
star
52

picofeedgen

A minimum-viable atproto feed genedator
Python
3
star
53

irradiate.py

a simple script to simulate random "cosmic ray" memory errors against a linux process
Python
3
star
54

falling-block-game

resemblance to any other game is purely coincidental
Python
3
star
55

AnonymOS

Yet another unnamed operating system project.
C
2
star
56

6502-sbc

Documentation and code for my 6502 SBC
Assembly
2
star
57

distrust

A very very WIP type-2 hypervisor in Rust, which uses the Linux KVM API.
Rust
2
star
58

ROLL13

an (unfinished) TLSv1.3 client in pure python, hand-rolled from first principles.
Python
2
star
59

CVE-2017-13672

POCs for CVE-2017-13672 (OOB read in VGA Cirrus QEMU driver, causing DoS)
C
2
star
60

branflakes

Yet another x86_64 optimising Brainf*ck JIT compiler.
Brainfuck
2
star
61

pda-hax

Objective-C
1
star
62

zoom-enhance

CSI style "Zoom and Enhance" with Google Maps
JavaScript
1
star
63

fizzbuzz-ng

The fastest fizzbuzz in the universe.
1
star
64

duino-coin-fork

Python
1
star
65

english-letter-freqs

Useful generation scripts and precomputed LUTs useful for performing frequency analysis on English text.
Python
1
star
66

CHIP8-ROP

A chip8 emulator that translates programs into a giant ROP payload. Not my best idea...
C
1
star
67

branflakes-ng

An improved version of my older project "branflakes", an optimising brainfuck compiler.
Brainfuck
1
star
68

css-gradient-fixer

CSS Gradient Fixer
HTML
1
star
69

coursework

Random Cardiff uni CompSci coursework solutions, of varying quality.
PHP
1
star
70

pcb-pattern

Renders a random PCB pattern using HTML5 canvas.
JavaScript
1
star