How EnvMap records a privileged session, and seven things that broke on the way
A session recording is evidence. It has to show what happened, hold no secrets, and still be believed a year later. This is how EnvMap's privileged access manager does it, and what went wrong before it worked. EnvMap is
A session recording is evidence. It has to show what happened, hold no secrets, and still be believed a year later. This is how EnvMap's privileged access manager does it, and what went wrong before it worked.
EnvMap is an open-source, self-hosted control plane with a privileged access manager (PAM) in it. People open SSH, RDP and VNC sessions from the browser, the credential never leaves the server, and every session is recorded. This post is about the recording: the three decisions behind it, and seven bugs that only showed up when it ran against real servers.
All of the code is in the repository, mostly in app/pam.py and app/kmanage.py.
Three things a recording has to do
- Show what happened. Not a summary of it: the error that scrolled past, the menu somebody picked from, the diff a script printed before it acted.
- Hold no secrets. A recording that contains the root password is a liability with a play button.
- Be provable. Somebody will ask, months later, whether this file is the one that was written that day.
Each one shaped the design.
Record the screen, never the keys
An SSH session is recorded as an asciinema v2 cast: one JSON header line, then one line for each chunk of output.
{"version": 2, "width": 120, "height": 32, "timestamp": 1795862400}
[0.412, "o", "web-lb-01:~# "]
[2.108, "o", "nginx -t\r\n"]
[2.174, "o", "nginx: configuration file /etc/nginx/nginx.conf test is successful\r\n"]
Only output is written. Keystrokes never are. That one rule is what keeps passwords out: a password typed at a prompt with echo turned off never appears in the output stream, because the host does not send it back. A recording of keystrokes would hold it in full.
The format was chosen because it is boring. asciinema play reads the file, and the replay page plays it with the terminal emulator the product already ships, so there is no player to bundle.
Desktops go through guacd, the Apache Guacamole daemon, which draws RDP and VNC in the browser and writes the recording itself. It is told to leave key events out (recording-include-keys: false), for the same reason.
The commands are a second record
A replay answers "what did the screen show". An auditor usually starts with a different question: "what did they run". So EnvMap also keeps the commands of an SSH session as text.
A terminal has no command boundary. It has a stream of keystrokes. To turn that into commands, Backspace and Ctrl+U are applied rather than written down, so the log holds each line as the person finished writing it, and a line is closed when Enter arrives.
Then there is the secret problem again. The rule is the same one, seen from the other side: a line is a secret if the host did not echo it. A shell echoes each character as it is typed, so by the time Enter is pressed the line has already come back in the output. If it did, it is recorded. If it did not, only its length is kept:
sudo systemctl reload nginx
<hidden — 14 characters typed>
Seven things that broke
1. Every command came out hidden
The first version of that rule waited for the command to appear in the output after Enter. A shell never prints a command again after running it. So every real command was judged "not echoed" and written down as hidden, and the transcript of a busy session was a column of <hidden> lines.
The echo is in what arrives while the line is being typed. The check now looks there, and keeps looking for a moment after Enter, because over a slow link the last characters of the echo can still be in flight.
2. A paste was written down as [200~
Modern shells ask the terminal for bracketed paste, so pasted text arrives wrapped in ESC[200~ and ESC[201~. The recorder dropped the ESC and kept the rest, so a pasted command was logged as [200~systemctl status nginx[201~. Worse, because those first characters are never echoed, some pasted commands were logged as hidden.
An escape sequence is a key, not text. The fix was to skip the whole sequence, with one care: an ESC pressed alone, as in vi mode, must not swallow what is typed after it.
3. The staircase
On macOS, during development, every SSH prompt landed further right than the last one:
web-lb-01:~#
web-lb-01:~#
web-lb-01:~#
The terminal was put in raw mode on the controlling side of the pseudo-terminal. On macOS the two sides share their settings, so that also switched off output processing on the other side. ssh -tt copies the local terminal's modes into its request for a remote terminal, so the remote shell stopped turning a line feed into carriage return plus line feed. The same code driving kubectl exec was fine, because kubectl negotiates the container's own terminal. SSH sessions now leave the mode alone and let ssh set it.
4. A space in a folder name
Host keys are pinned on first use, in a known-hosts file for each session. With the data directory under a path containing a space, the first connection failed with a message that named half a path. ssh -o UserKnownHostsFile= takes a space-separated list of files, so one path with two spaces in it was read as three files. Quoting the value does not help: ssh keeps the quotes. Each session's key and known-hosts file now live on a path with no spaces.
5. An emoji in a password
guacd's protocol is length-prefixed. An instruction looks like this:
6.select,3.rdp;
EnvMap builds those by hand, because it is the small relay that the Guacamole web application usually is. The length was counted in UTF-16 units, and guacd counts code points. The two agree for most characters and differ by one for each character outside the Basic Multilingual Plane. A password with an emoji in it made the connect instruction one longer than it claimed, and guacd refused the whole handshake. It showed up only against a real guacd.
The framing is always computed, never glued together from strings. In a protocol whose delimiters can appear inside a password, that is the same discipline as passing arguments as a list instead of building a shell command.
6. Every desktop recording said it had been altered
When a session ends, EnvMap takes a SHA-256 of the recording and stores it. Every time the replay page opens, it computes the checksum again and compares.
The first desktop recordings all failed their own check. guacd goes on writing for a moment after its socket closes. A checksum taken at once was of a file that then grew: one RDP recording was filed at 10,640 bytes and found at 10,720. Every genuine recording reported itself tampered.
The session now waits until the file has stopped growing for a second and a half before it files it. The lesson was about the claim, not the hash: a checksum is only a statement about the evidence if it is taken after the evidence is finished.
The check has five answers, not two, because the dull cases matter:
| State | What it means |
|---|---|
| verified | The file is byte for byte what was filed |
| tampered | It is not |
| expired | A retention rule removed it, and when |
| missing | The file is gone and nothing explains it |
| none | There was never a checksum |
Without "expired", a retention policy doing its job looks exactly like evidence going astray.
7. A session that lasted zero seconds
A web application can be recorded by opening it in a browser that EnvMap starts as a pod, and letting guacd record that browser like a desktop. A pod takes time to schedule, and the Guacamole client library gives up after 15 seconds without an incoming instruction. An RDP host answers in under a second, so nobody had noticed. The client reported "Server timeout", and the server filed a session zero seconds long.
Two changes: the page waits longer, and while the browser is being built the relay sends the protocol's own nop every three seconds, so the client can tell slow from dead.
When the server dies mid-session
A recording that exists only if the session ends cleanly is not evidence. Each session's row is written when it opens, with the path of its recording. If the process dies, the next start finds the rows that were still marked running, files them as interrupted, and indexes and checksums each recording as it stands. What happened before the crash is kept, and provable.
What the recording does not see
A file copied over SFTP is not terminal output, so the recording cannot show it leaving. Every transfer is written to the audit log instead, by name, direction and size, and the refused ones are logged too. A failed attempt to read /etc/shadow is exactly the line somebody will want to find.
One detail from that code: sftp -b reads one command per line, so a newline inside a file name is not a quoting problem. It is a second command, run with the target's credential. Such a path is refused, not escaped.
What it still does not do
- It records commands and does not stop them. There is no command filtering.
- There are no access requests with approval, and no automatic credential rotation.
- Sessions open in the browser. There is no proxy for your own
sshclient. - A checksum in the same database as the session proves a file was not changed by accident or by somebody without database access. It does not stop somebody who can rewrite both.
Try it
The quickest look is the demo, which replays a recorded session in your browser. To run it on one machine:
curl -fsSLO \
https://raw.githubusercontent.com/ammar-hassoun/envmap/main/deploy/compose/compose.yaml
ENVMAP_MODE=pam docker compose up -d
Then open http://localhost:8471. More about it: the PAM page, how it compares with JumpServer, Teleport, Warpgate and Guacamole, and the docs on recordings and sessions.
If you have built a recorder and solved one of these differently, tell us how.
First published on envmap.tech. Written with help from an AI assistant; every detail was checked against EnvMap's code.
Originally published by Dev.to Security. Aggregated on AIWithGhost for educational purposes — full credit and traffic to the original publisher.