Package: pyrdp-mitm
PyRDPPyRDP is a Python Remote Desktop Protocol (RDP) Monster-in-the-Middle (MITM) tool and library.It features a few tools:RDP Monster-in-the-MiddleLogs plaintext credentials or NetNTLM hashes used when connectingSteals data copied to the clipboardSaves a copy of the files transferred over the networkCrawls shared drives in the background and saves them locallySaves replays of connections so you can look at them laterRuns console commands or PowerShell payloads automatically on new connectionsRDP Player:See live RDP connections coming from the MITMView replays of RDP connectionsTake control of active RDP sessions while hiding your actionsList the client's mapped drives and download files from them during active sessionsConverter tool:Convert RDP replays to videos for easier sharingConvert RDP replays to a sequence of low-level events serialized in JSON formatConvert PCAPs to replays, videos or JSON eventsConvert decrypted PCAPs (L7 PDUs) to replays, videos or JSON eventsRDP Certificate Cloner:Create a self-signed X509 certificate with the same fields as an RDP server's certificatePyRDP wasintroduced in 2018in
which wedemonstrated that we can catch a real threat actor in
action. This tool is being developed with both pentest and malware
research use cases in mind.Table of ContentsSupported SystemsInstallingUsing pipxUsing the Docker ImageUsing PyRDPUsing the PyRDP Monster-in-the-MiddleUsing the PyRDP PlayerUsing the PyRDP Certificate ClonerUsing PyRDP ConvertConfiguring PyRDPAdvanced UsageDocker Specific Usage InstructionsPyRDP LoreContributing to PyRDPAcknowledgementsSupported SystemsPyRDP should work on Python 3.7 and up on the x86-64, ARM and ARM64 platforms.This tool has been tested to work on Python 3.7 on Linux (Ubuntu 20.04, 22.04), Raspberry Pi and Windows. It has not been tested on macOS.InstallingTwo installation techniques are recommended viapipxor using docker containers.
Installing from source or building docker containers yourself is covered in thedevelopment documentation.Using pipxDependenciesLinuxFirst, make sure to install the prerequisite packages (these are listed for Ubuntu 22.04, you might need to adjust for other distros). We provide two types of installs a full one and a
slim one. Install the dependencies according to your use case.# Full install (GUI, convert captures to video)
sudo apt install python3 python3-pip python3-venv \
        build-essential python3-dev openssl \
        libegl1 libxcb-cursor0 libxkbcommon-x11-0 libxcb-icccm4 libxcb-keysyms1 \
        libnotify-bin \
        libavcodec58 libavdevice58

# Slim install (no GUI, no conversion to video possible)
sudo apt install python3 python3-pip python3-venv \
        build-essential python3-dev git opensslThis should install the dependencies required to run PyRDP. If you choose to
install without the GUI or video conversion dependencies, it will not be possible to usepyrdp-playerwithout headless mode (--headless) orpyrdp-convertto produce video output.Make sure you havepipxinstalled. On Ubuntu 22.04:python3 -m pip install --user pipx
python3 -m pipx ensurepathWindowsMake sure you have Python installed.
Windows Store Python didn't work for me on Windows 11.
We recommend installing Python viaScoop.scoop install python # if not installed already
scoop install pipx
pipx ensurepathLog out and log back in (to update your PATH).Other OSTo installpipxon other operating systems see here:https://github.com/pypa/pipx#install-pipxInstallFor the full PyRDP experience with the QT GUI and the ability to convert captures to video:pipx install pyrdp-mitm[full]For the compact version meant to be run in headless environments (servers, RaspberryPi):pipx install pyrdp-mitmYou are ready to go! See theusage instructions.Using the Docker ImageThis is the easiest installation method if you have docker installed and working.docker pull gosecure/pyrdp:latestAs an alternative we have a slimmer image without the GUI and ffmpeg dependencies.
This is the only provided image on ARM platforms.docker pull gosecure/pyrdp:latest-slimYou can find the list of all our Docker imageson the gosecure/pyrdp DockerHub page.
Thelatesttag refers to the latest released version while thedeveltag is the docker image built out of ourmainbranch.Using PyRDPUsing the PyRDP Monster-in-the-MiddleUsepyrdp-mitm <ServerIP>orpyrdp-mitm <ServerIP>:<ServerPort>to run the MITM.Assuming you have an RDP server running on192.168.1.10and listening on port 3389, you would run:pyrdp-mitm 192.168.1.10When running the MITM for the first time a directory calledpyrdp_output/will be created relative to the current working directory.
Here is an example layout of that directory:pyrdp_output/
├── certs
│   ├── WinDev2108Eval.crt
│   └── WinDev2108Eval.pem
├── files
│   ├── e91c6a5eb3ca15df5a5cb4cf4ebb6f33b2d379a3a12d7d6de8c412d4323feb4c
│   ├── b14b26b7d02c85e74ab4f0d847553b2fdfaf8bc616f7c3efcc4771aeddd55700
├── filesystems
│   ├── romantic_kalam_8214773
│   │   └── device1
│   │   └── clipboard
|           └── priv-esc.exe -> ../../../files/b14b26b7d02c85e74ab4f0d847553b2fdfaf8bc616f7c3efcc4771aeddd55700
│   └── happy_stonebraker_1992243
│       ├── device1
│       └── device2
|           └── Users/User/3D Objects/desktop.ini -> ../../../../../../e91c6a5eb3ca15df5a5cb4cf4ebb6f33b2d379a3a12d7d6de8c412d4323feb4c
├── logs
│   ├── crawl.json
│   ├── crawl.log
│   ├── mitm.json
│   ├── mitm.log
│   ├── mitm.log.2021-08-26
│   ├── ntlmssp.log
│   ├── player.log
│   └── ssl.log
└── replays
    ├── rdp_replay_20231214_01-20-28_965_happy_stonebraker_1992243.pyrdp
    └── rdp_replay_20231214_00-42-24_295_romantic_kalam_8214773.pyrdpcerts/contains the certificates generated stored using theCNof the certificate as the file namefiles/contains all files captured and are deduplicated by saving them using the SHA-256 hash of the content as the filenamefilesystems/contains a recreation of the filesystem of the targets classified by session IDs.
To save space on similar sessions, files are symbolic links to the actual files underfiles/.logs/contains all the various logs with most in both JSON and plaintext formats:crawl: the file crawler logmitm: the main MITM logntlmssp.log: the captured NetNTLM hashesplayer.log: the player logssl.log: the TLS master secrets stored in a format compatible with Wiresharkreplays/contains all the previously recorded PyRDP sessions with timestamps and session IDs in the filenameSpecifying the private key and certificateIf key generation didn't work or you want to use a custom key and certificate, you can specify them using the-cand-karguments:pyrdp-mitm 192.168.1.10 -k private_key.pem -c certificate.pemMonster-in-the-Middle Network Level Authentication (NLA) connectionsNetwork Level Authentication (NLA) is a security feature available since Windows Vista that adds security to RDP connections.
NLA relies on the new security support provider CredSSP and is sometimes referred by that name.
A server that enforces NLA is harder to attack.
There are three different strategies that can be used:Obtain the server's certificate and private keyUsing a host redirection featureCapture the client's NetNTLMv2 hash and crack itMonster-in-the-Middle NLAIf we have access to the server's certificate and private key, we can successfully MITM RDP even if NLA is enforced.
Wedocumented this attack in our 1.0 release blog post.
Instructions toextract the RDP certificate and private keyare available on our GitHub.With the certificate and private key accessible, you just need to set the authentication tosspby adding this on thepyrdp-mitm.pycommand-line:--auth ssp -c <certificate.pem> -k <private-key.pem>This will enable the possibility to intercept NLA-enforced connections.Alternative host redirection when NLA enforced by serverWhen PyRDP connects to the destination RDP server (1) if that server enforces NLA then PyRDP (2) will replace the connection to go to another host of your chosing (3) instead.For example, this can be used to redirect to a server that is known not to enforce NLA or it could even redirect to a VM in the control of an attacker.To enable this feature specify the alternative host's address and port like this:--nla-redirection-host 192.168.1.12 --nla-redirection-port 3389This feature was introduced in PyRDP 1.1.0.Capturing NetNTLMv2 hashesNetNTLMv2 hashes are useful for an attacker as they can be cracked relatively easily allowing attackers to leverage legitimate RDP access or attempt credentials stuffing.
Starting with version 1.1.0, PyRDP has the ability to capture the client's NetNTLMv2 hashes via an NLA (CredSSP) connection by carrying the negotiation and capturing the NTLMSSP authentication messages.
In version 1.2.0 that support was extended to work even if we don't have the server's certificate and private key meaning that the connection will not be successfully MITM'ed.
This is similar to whatResponderdoes with RDP.
The captured NetNTLMv2 hash can be found in thentlmssp.loglog file and it's
formatted so cracking tools likeJohn The Ripperorhashcatcan ingest it.This technique has been described in details in a blog post:Capturing RDP NetNTLMv2 Hashes: Attack details and a Technical How-To GuideThis feature is compatible with--auth sspbut incompatible with--nla-redirection-host.Connecting to the PyRDP playerIf you want to see live RDP connections through the PyRDP player, you will need to specify the IP address and port on which the
player is listening using the-iand-darguments. Note: the port argument is optional, the default port is 3000.pyrdp-mitm 192.168.1.10 -i 127.0.0.1 -d 3000Connecting to a PyRDP player when the MITM is running on a serverIf you are running the MITM on a server and still want to see live RDP connections, you should useSSH remote port forwardingto forward a port on your server to the player's port on your machine. Once this is done, you pass127.0.0.1and the forwarded
port as arguments to the MITM. For example, if port 4000 on the server is forwarded to the player's port on your machine,
this would be the command to use:pyrdp-mitm 192.168.1.10 -i 127.0.0.1 -d 4000Running payloads on new connectionsPyRDP has support for running console commands or PowerShell payloads automatically when new connections are made.
Due to the nature of RDP, the process is a bit hackish and is not always 100% reliable. Here is how it works:Wait for the user to be authenticated.Block the client's input / output to hide the payload and prevent interference.Send a fake Windows+R sequence and runcmd.exe.Run the payload as a console command and exit the console. If a PowerShell payload is configured, it is run withpowershell -enc <PAYLOAD>.Wait a bit to allow the payload to complete.Restore the client's input / output.For this to work, you need to set 3 arguments:the payloadthe delay before the payload startsthe payload's durationSetting the payloadYou can use one of the following arguments to set the payload to run:--payload, a string containing console commands--payload-powershell, a string containing PowerShell commands--payload-powershell-file, a path to a PowerShell scriptChoosing when to start the payloadFor the moment, PyRDP does not detect when the user is logged on.
You must give it an amount of time to wait for before running the payload.
After this amount of time has passed, it will send the fake key sequences and expect the payload to run properly.
To do this, you use the--payload-delayargument. The delay is in milliseconds.
For example, if you expect the user to be logged in within the first 5 seconds, you would use the following arguments:--payload-delay 5000This could be made more accurate by leveraging some messages exchanged during RDPDR initialization.
Seethis issueif you're interested in making this work better.Choosing when to resume normal activityBecause there is no direct way to know when the console has stopped running, you must tell PyRDP how long you want
the client's input / output to be blocked. We recommend you set this to the maximum amount of time you would expect the
console that is running your payload to be visible. In other words, the amount of time you would expect your payload to
complete.
To set the payload duration, you use the--payload-durationargument with an amount of time in milliseconds.
For example, if you expect your payload to take up to 5 seconds to complete, you would use the following argument:--payload-duration 5000This will block the client's input / output for 5 seconds to hide the console and prevent interference.
After 5 seconds, input / output is restored back to normal.Other MITM argumentsRunpyrdp-mitm --helpfor a full list of arguments.--no-downgradeThis argument is useful when running PyRDP in Honeypot scenarios to avoid scanner fingerprinting.
When the switch is enabled, PyRDP will not downgrade unsupported extensions and let the traffic through
transparently. The player will likely not be able to successfully replay video traffic, but the following
supported channels should still be accessible:Keystroke recordingMouse position updatesClipboard access (passively)Drive access (passively)This feature is still a work in progress and some downgrading is currently unavoidable to allow the connection
to be established. The following are currently not affected by this switch and will still be disabled:FIPS EncryptionNon-TLS encryption protocolsClientInfo compressionVirtual Channel compressionNOTE: If being able to eventually replay the full session is important, a good solution is to record the raw
RDP traffic using Wireshark and keep the TLS master secrets. Whenever PyRDP adds support for additional extensions,
it would then become possible to extract a valid RDP replay file from the raw network capture.--transparentTells PyRDP to attempt to spoof the source IP address of the client so that the server sees the real IP
address instead of the MITM one. This option is only useful in certain scenarios where the MITM is physically
a gateway between clients and the server and sees all traffic.Specific examples can be found here.NOTE: This requires root privileges, only works on Linux and requires manual firewall configuration to ensure
that traffic is routed properly.--no-gdi: Disable Accelerated Graphics PipelinePyRDP downgrades video to the the most recent graphics pipeline that it supports. This switch explicitly tells the
MITM to not use theGraphics Device Interface Accelerationextensions to stream video. The advantage of this mode
is a significant reduction in required bandwidth for high resolution connections.Note that some GDI drawing orders are currently unimplemented because they appear to be unused. If you have a replay
which contains any unsupported or untested order, do not hesitate to share it with the project maintainers so that
support can be added as required. (Make sure that the trace does not contain sensitive information)Using the PyRDP PlayerUsepyrdp-playerto run the player.Playing a replay fileYou can use the menu to open a new replay file: File > Open.You can also open replay files when launching the player:pyrdp-player <FILE1> <FILE2> ...Listening for live connectionsThe player always listens for live connections. By default, the listening port is 3000, but it can be changed:pyrdp-player -p <PORT>Changing the listening addressBy default, the player only listens to connections coming from the local machine. We do not recommend opening up the player
to other machines. If you still want to change the listening address, you can do it with-b:pyrdp-player -b <ADDRESS>Other player argumentsRunpyrdp-player --helpfor a full list of arguments.Using the PyRDP Certificate ClonerNOTE: Using this tool is optional.
Since version 1.0 PyRDP generate certificates on-the-fly exactly like this tool would do.The PyRDP certificate cloner creates a brand new X509 certificate by using the values from an existing RDP server's
certificate. It connects to an RDP server, downloads its certificate, generates a new private key and replaces the
public key and signature of the certificate using the new private key. This can be used in a pentest if, for example,
you're trying to trick a legitimate user into going through your MITM. Using a certificate that looks like a legitimate
certificate could increase your success rate.Cloning a certificateYou can clone a certificate by usingpyrdp-clonecert:pyrdp-clonecert 192.168.1.10 cert.pem -o key.pemThe-oparameter defines the path name to use for the generated private key.Using a custom private keyIf you want to use your own private key instead of generating a new one:pyrdp-clonecert 192.168.1.10 cert.pem -i input_key.pemOther cloner argumentsRunpyrdp-clonecert --helpfor a full list of arguments.Using PyRDP Convertpyrdp-convertis a helper script that performs several useful conversions from various input formats to various output formats.
The script has the best chance of working on traffic captured by PyRDP due to unsupported RDP protocol features that might be used in a non-intercepted connection.The following inputs are supported:Network Capture (PCAP) with TLS master secrets (less reliable)Network Capture (PCAP) in Exported PDUs Layer 7 format (more reliable)Replay file generated by PyRDPThe following outputs are supported:MP4 video fileJSON: a sequence of low-level events serialized in JSON formatReplay file compatible withpyrdp-playerEncrypted (TLS) network captures require the TLS master secrets to be provided using--secrets ssl.log.# Export the session coming client 10.2.0.198 to a .pyrdp file.
pyrdp-convert --src 10.2.0.198 --secrets ssl.log -o path/to/output capture.pcap

# Or as an MP4 video
pyrdp-convert --src 10.2.0.198 --secrets ssl.log -o path/to/output -f mp4 capture.pcap

# List the sessions in a network trace, along with the decryptable ones.
pyrdp-convert --list-only capture.pcapNote that MP4 conversion requires libavcodec and ffmpeg, so this may require extra steps on Windows.Manually decrypted network traces can be exported from Wireshark by selectingFile > Export PDUsand selectingOSI Layer 7.First, make sure you configured wireshark to load TLS secrets:Next, export OSI Layer 7 PDUs:And optionally, filter down the trace to contain only the conversation(s) of interest by applying a
display filter and clickingFile > Export Specified Packets...Now this trace can be used directly inpyrdp-convert.Configuring PyRDPMost of the PyRDP configuration is done through command line switches, but it is also possible to use a
configuration file for certain settings such as log configuration.The default configuration files used by PyRDP are located inmitm.default.iniandplayer.default.ini. Both files are thoroughly documented and can serve
as a basis for further configuration.In the future there are plans to support other aspects of PyRDP configuration through those configuration files.Advanced UsageUsing PyRDP as a LibraryIf you're interested in experimenting with RDP and making your own tools, head over to ourdocumentation sectionfor more information.Using PyRDP with twistdThe PyRDP MITM component was also implemented as a twistd plugin.
This enables you to run it in debug mode and allows you to get an interactive debugging repl (pdb) if you send aSIGUSR2to the twistd process.
See thetwistd documentationfor more information.Using PyRDP with BettercapWe developped our own Bettercap module,rdp.proxy, to monster-in-the-middle all RDP connections
on a given LAN. Check outthis documentfor more information.Docker Specific Usage InstructionsSince docker restricts the interactions with the host system (filesystem and network), the PyRDP docker image must be run with some parameters depending on your use case. This section documents those parameters.We refer to the publicly provided docker image but if youbuilt your ownreplacegosecure/pyrdpwith the name of your locally built image.Mapping a Listening PortIn most of the monster-in-the-middle cases you will need to map a port of your host into the docker image. This is achieved by the--publish(-p) parameters applied todocker run.For example, to listen on 3389 (RDP's default port) on all interfaces, use:docker run -p 3389:3389 gosecure/pyrdp pyrdp-mitm 192.168.1.10Logs and Artifacts StorageTo store the PyRDP output permanently (logs, files, etc.), add the--volume(-v) option to the previous command. In this example we store the files relatively to the current directory inpyrdp_output:docker run -v $PWD/pyrdp_output:/home/pyrdp/pyrdp_output -p 3389:3389 gosecure/pyrdp pyrdp-mitm 192.168.1.10Make sure that your destination directory is owned by a user with a UID of 1000, otherwise you will get permission denied errors.
If you are the only non-root user on the system, usually your user will be assigned UID 1000.Logging the host IP addressIf you want PyRDP to log the host IP address in its logs, you can set theHOST_IPenvironment variable when usingdocker run:docker run -p 3389:3389 -e HOST_IP=192.168.1.9 gosecure/pyrdp pyrdp-mitm 192.168.1.10Using the GUI Player in DockerUsing the player will require you to export theDISPLAYenvironment variable from the host to the docker.
This redirects the GUI of the player to the host screen.
You also need to expose the host's network and prevent Qt from using the MIT-SHM X11 Shared Memory Extension.
To do so, add the-eand--netoptions to the run command:docker run -e DISPLAY=$DISPLAY -e QT_X11_NO_MITSHM=1 --net=host gosecure/pyrdp pyrdp-playerKeep in mind that exposing the host's network to docker can compromise the isolation between your container and the host.
If you plan on using the player, X11 forwarding using an SSH connection would be a more secure way.Converting videos in DockerThe video conversion process relies on PyAV, ffmpeg and QT so you need the regular docker image not the slim one.You need a volume mount (-v) to share files with the container.
Here we map our local directory with/shared/in the container.docker run -e QT_QPA_PLATFORM=offscreen -v $PWD/:/shared gosecure/pyrdp pyrdp-convert -f mp4 <filename-relative-to-volume-in-/shared/> -o /shared/TheQT_QPA_PLATFORM=offscreenenvironment variable is requireddue to a bug documented here.
It tells to QT that it is correct that no display environment is available.PyRDP LoreIntroduction blog postin which wedemonstrated that we can catch a real threat actor in actionTalk at NorthSec 2019where two demos were performed:First demo: credential logging, clipboard stealing, client-side file browsing and a session take-overSecond demo: the execution of cmd or powershell payloads when a client successfully authenticatesPyRDP Logolicensed under CC-BY-SA 4.0.BlackHat USA Arsenal 2019 SlidesDerbyCon 2019 Slides(Video)Blog: PyRDP on AutopilotBlog: PyRDP 1.0DefCon 2020 Demo LabsBlog: Capturing RDP NetNTLMv2 Hashes: Attack details and a Technical How-To GuideBlackHat USA Arsenal 2021 SlidesPresentation: I Watched You Roll the Die: Unparalleled RDP Monitoring Reveal Attackers' Tradecraftat BlackHat USA 2023Contributing to PyRDPSee ourcontribution guidelines.AcknowledgementsPyRDP uses code from the following open-source software:RC4-Pythonfor the RC4 implementation.rdesktopfor bitmap decompression.rdpyfor RC4 keys, the bitmap decompression bindings and the base GUI code for
the PyRDP player.FreeRDPfor the scan code enumeration.