Coder Social home page Coder Social logo

dominicbreuker / pspy Goto Github PK

View Code? Open in Web Editor NEW
4.6K 48.0 484.0 9.28 MB

Monitor linux processes without root permissions

License: GNU General Public License v3.0

Makefile 5.10% Go 91.69% Python 0.50% Shell 2.71%
ctf pentesting privesc enumeration golang security

pspy's Introduction

pspy - unprivileged Linux process snooping

Go Report Card Maintainability Test Coverage CircleCI

pspy is a command line tool designed to snoop on processes without need for root permissions. It allows you to see commands run by other users, cron jobs, etc. as they execute. Great for enumeration of Linux systems in CTFs. Also great to demonstrate your colleagues why passing secrets as arguments on the command line is a bad idea.

The tool gathers the info from procfs scans. Inotify watchers placed on selected parts of the file system trigger these scans to catch short-lived processes.

Getting started

Download

Get the tool onto the Linux machine you want to inspect. First get the binaries. Download the released binaries here:

  • 32 bit big, static version: pspy32 download
  • 64 bit big, static version: pspy64 download
  • 32 bit small version: pspy32s download
  • 64 bit small version: pspy64s download

The statically compiled files should work on any Linux system but are quite huge (~4MB). If size is an issue, try the smaller versions which depend on libc and are compressed with UPX (~1MB).

Build

Either use Go installed on your system or run the Docker-based build process which ran to create the release. For the latter, ensure Docker is installed, and then run make build-build-image to build a Docker image, followed by make build to build the binaries with it.

You can run pspy --help to learn about the flags and their meaning. The summary is as follows:

  • -p: enables printing commands to stdout (enabled by default)
  • -f: enables printing file system events to stdout (disabled by default)
  • -r: list of directories to watch with Inotify. pspy will watch all subdirectories recursively (by default, watches /usr, /tmp, /etc, /home, /var, and /opt).
  • -d: list of directories to watch with Inotify. pspy will watch these directories only, not the subdirectories (empty by default).
  • -i: interval in milliseconds between procfs scans. pspy scans regularly for new processes regardless of Inotify events, just in case some events are not received.
  • -c: print commands in different colors. File system events are not colored anymore, commands have different colors based on process UID.
  • --debug: prints verbose error messages which are otherwise hidden.

The default settings should be fine for most applications. Watching files inside /usr is most important since many tools will access libraries inside it.

Some more complex examples:

# print both commands and file system events and scan procfs every 1000 ms (=1sec)
./pspy64 -pf -i 1000 

# place watchers recursively in two directories and non-recursively into a third
./pspy64 -r /path/to/first/recursive/dir -r /path/to/second/recursive/dir -d /path/to/the/non-recursive/dir

# disable printing discovered commands but enable file system events
./pspy64 -p=false -f

Examples

Cron job watching

To see the tool in action, just clone the repo and run make example (Docker needed). It is known passing passwords as command line arguments is not safe, and the example can be used to demonstrate it. The command starts a Debian container in which a secret cron job, run by root, changes a user password every minute. pspy run in foreground, as user myuser, and scans for processes. You should see output similar to this:

~/pspy (master) $ make example
[...]
docker run -it --rm local/pspy-example:latest
[+] cron started
[+] Running as user uid=1000(myuser) gid=1000(myuser) groups=1000(myuser),27(sudo)
[+] Starting pspy now...
Watching recursively    : [/usr /tmp /etc /home /var /opt] (6)
Watching non-recursively: [] (0)
Printing: processes=true file-system events=false
2018/02/18 21:00:03 Inotify watcher limit: 524288 (/proc/sys/fs/inotify/max_user_watches)
2018/02/18 21:00:03 Inotify watchers set up: Watching 1030 directories - watching now
2018/02/18 21:00:03 CMD: UID=0    PID=9      | cron -f
2018/02/18 21:00:03 CMD: UID=0    PID=7      | sudo cron -f
2018/02/18 21:00:03 CMD: UID=1000 PID=14     | pspy
2018/02/18 21:00:03 CMD: UID=1000 PID=1      | /bin/bash /entrypoint.sh
2018/02/18 21:01:01 CMD: UID=0    PID=20     | CRON -f
2018/02/18 21:01:01 CMD: UID=0    PID=21     | CRON -f
2018/02/18 21:01:01 CMD: UID=0    PID=22     | python3 /root/scripts/password_reset.py
2018/02/18 21:01:01 CMD: UID=0    PID=25     |
2018/02/18 21:01:01 CMD: UID=???  PID=24     | ???
2018/02/18 21:01:01 CMD: UID=0    PID=23     | /bin/sh -c /bin/echo -e "KI5PZQ2ZPWQXJKEL\nKI5PZQ2ZPWQXJKEL" | passwd myuser
2018/02/18 21:01:01 CMD: UID=0    PID=26     | /usr/sbin/sendmail -i -FCronDaemon -B8BITMIME -oem root
2018/02/18 21:01:01 CMD: UID=101  PID=27     |
2018/02/18 21:01:01 CMD: UID=8    PID=28     | /usr/sbin/exim4 -Mc 1enW4z-00000Q-Mk

First, pspy prints all currently running processes, each with PID, UID and the command line. When pspy detects a new process, it adds a line to this log. In this example, you find a process with PID 23 which seems to change the password of myuser. This is the result of a Python script used in roots private crontab /var/spool/cron/crontabs/root, which executes this shell command (check crontab and script). Note that myuser can neither see the crontab nor the Python script. With pspy, it can see the commands nevertheless.

CTF example from Hack The Box

Below is an example from the machine Shrek from Hack The Box. In this CTF challenge, the task is to exploit a hidden cron job that's changing ownership of all files in a folder. The vulnerability is the insecure use of a wildcard together with chmod (details for the interested reader). It requires substantial guesswork to find and exploit it. With pspy though, the cron job is easy to find and analyse:

animated demo gif

How it works

Tools exist to list all processes executed on Linux systems, including those that have finished. For instance there is forkstat. It receives notifications from the kernel on process-related events such as fork and exec.

These tools require root privileges, but that should not give you a false sense of security. Nothing stops you from snooping on the processes running on a Linux system. A lot of information is visible in procfs as long as a process is running. The only problem is you have to catch short-lived processes in the very short time span in which they are alive. Scanning the /proc directory for new PIDs in an infinite loop does the trick but consumes a lot of CPU.

A stealthier way is to use the following trick. Process tend to access files such as libraries in /usr, temporary files in /tmp, log files in /var, ... Using the inotify API, you can get notifications whenever these files are created, modified, deleted, accessed, etc. Linux does not require priviledged users for this API since it is needed for many innocent applications (such as text editors showing you an up-to-date file explorer). Thus, while non-root users cannot monitor processes directly, they can monitor the effects of processes on the file system.

We can use the file system events as a trigger to scan /proc, hoping that we can do it fast enough to catch the processes. This is what pspy does. There is no guarantee you won't miss one, but chances seem to be good in my experiments. In general, the longer the processes run, the bigger the chance of catching them is.

Misc

Logo: "By Creative Tail [CC BY 4.0 (http://creativecommons.org/licenses/by/4.0)], via Wikimedia Commons" (link)

pspy's People

Contributors

dominicbreuker avatar kazkansouh avatar socketz avatar wanderingeek avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

pspy's Issues

Makefile issue

Hey, I think in line 42 of Makefile there is an issue. At least for me I have to remove it in order to build.

Cheers

Inotify resource exhaustion

Hello,

Thank you for making such a useful tool :)

So recently I noticed that when running pspy, that it was not very well picking up some short lived cron scripts. After more investigation, I realised the issue was related to running multiple copies of the tool. The first instance works fine, but the second instance misses lots of short lived processes. This is because in its default configuration it attempts to use inotify on almost every directory it has access to, exhausting the users supply of inotify watches. Then the second instance is unable to get notified in a timely fashion when a process start.

Linux limits the number of inotify watches with the value in /proc/sys/fs/inotify/max_user_watches. From the Debian based machines I've looked at this is set to 8192. Thus, on the machine I was testing on (fairly basic Ubuntu 18.04 with Python and PHP) the number of directories are:

$ find /usr /tmp /etc /home /var /opt -type d 2>/dev/null | wc -l
12518

Thus, just running once instance of this tool completely exhausts the limit for inotify watches. Also, this could also be unfair to other CTF players.

Moreover, I tested watching different directories. It appears that probably the only file that needs to be watched to provide a good user experience is /etc/ld.so.cache. I suspect that the vast majority of the time the processes that will be of interest are dynamic.

Probably, the better solution is to replace the attempt at blanket inotify watches to the whole file system and instead use a short list of directories. E.g. something like (with bin directories to catch any static binary executions):

  • /etc
  • /bin
  • /usr/bin
  • /usr/local/bin

Possibly, it could print a warning of the ld.so.cache if is not found in one of the directories.

Of course, the user will always be free to configure the tool as they want, but in a default config I dont think it should inadvertently perform a resource exhaustion attack!

I hope this is of some use, it took a while to properly identify. Also, while tracking down the issue, I came across some other minor improvements that I'll submit a PR for you to consider.

Strange behaviour on Centos 7 based system when monitoring dirs

_# ./pspy64 -p=false -f
pspy - version: v1.2.0 - Commit SHA: 9c63e5d

Config: Printing events (colored=true): processes=false | file-system-events=true ||| Scannning for processes every 100ms and on inotify events ||| Watching directories: [/usr /tmp /etc /home /var /opt] (recursive) | [] (non-recursive)
Draining file system events due to startup..._

Running the above just hangs. Assuming because Draining file system events due to startup never goes to 'done'. The only way i can recover from that point is with CTRL-Z to background the process and then kill it. If i change the directory manually to only /home or /opt or other systems dirs it works fine. Seems like only a problem with /usr /tmp /etc & /var. Tried these dirs with non-recursive mode and had the same problem.

Any ideas ?

how i transfer pspy to the target machine?

sorry for this but how i transfer to the target machine? i tries with scp but nothing

scp: dest open "/pspy64": Permission denied
scp: failed to upload file pspy64 to

thanks!

Feature request: Write processes to file

  • the -p is unnecessary because it's enabled per default?
    Would be nice if I can specify where to save the process log. A separate log for the filesystem monitoring would be nice too.

Running pspy64&32 on debian.

Just in case anyone knows how to solve this error while running pspy :
ERROR: reading inotify buffer: reading from inotify fd 3: errno: 22

tried to run a few minutes, also did not help.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.