Antoine Beaupr : Securing my IRC (irssi, screen) session with dtach and systemd
A recent vulnerability in GNU screen caused some people to
reconsider their commitment to the venerable terminal multiplexing
program. GNU screen is probably used by thousands of old
sysadmins around the world to run long-standing processes and,
particularly, IRC sessions, which are especially vulnerable to
arbitrary garbage coming on ... screen, so to speak.
So this vulnerability matters, and you should definitely pay attention
to it. If you haven't switched to tmux yet, now might be a good
time to get your fingers trained. But don't switch to it just yet for
your IRC session, and read on for a better, more secure solution.
After all, it's not because we found this flaw in Hardening my bouncer
Back in March 2019, I had already switched away from screen for
IRC, but not to
screen that it
doesn't exist in tmux (or your favorite terminal emulator, for that
matter, a much scarier thought).
Hardening my bouncer
Back in March 2019, I had already switched away from screen for
IRC, but not to tmux like many did, but to dtach. I figured
that I didn't actually need multiplexing to run my long-running IRC
session: I just needed to be able to reattach to the terminal. That's
what dtach does. No windows, no panes, and, especially, no way to
start a new shell, which is exactly the kind of hardening I need.
So I came up with this, to start irssi:
dtach -N /run/$USER/dtach-irssi.socket irssi
To attach:
dtach -a /run/$USER/dtach-irssi.socket
Fairly simple no? Already one attack vector gone: evil attacker can't
get a new shell through my terminal multiplexer, yay.
Splitting into another user
But why stop there! Why am I running irssi as my main user anyways!
Let's take the lessons from good UNIX security, and run this as a
separate user altogether. This requires me to create another user, say
foo-irc:
adduser foo-irc
... and run it as a systemd service, because how else are you going
to start this thing anyways, cron? I came up with something
like this unit file:
[Unit]
Description=IRC screen session
After=network.target
[Service]
Type=simple
Environment="TERM=screen.xterm-256color"
User=%i
RuntimeDirectory=%i
ExecStart=-/usr/bin/dtach -N /run/%i/dtach-irssi.socket irssi
ExecStop=-/bin/sh -c 'echo /quit stopping service... exec /usr/bin/dtach -p /run/%i/dtach-irssi.socket'
ExecReload=-/bin/sh -c 'echo /restart exec /usr/bin/dtach -p /run/%i/dtach-irssi.socket'
Notice this is a service template, because of the %i stuff. I don't
actually remember how to enable this thing, but let's say you drop
this in /etc/systemd/system/irssi@.service, then you run:
systemctl daemon-reload
And then, not sure about this bit, instantiate that template:
systemctl enable irssi@foo-irc.service
And then this should start the irssi session:
systemctl start irssi@foo-irc.service
To access the session:
sudo -u foo-irc dtach -a /run/foo-irc/dtach-irssi.socket
Obviously, you will probably need to migrate your irssi
configuration over, otherwise you'll end up with a blank, old-school
irssi. Take a moment to savor the view though. Nostalgia. Ah.
Hardening irssi
But this is still not enough. That pesky foo-irc user can still
launch arbitrary commands, thanks to irssi /exec (and a generous
Perl scripting environment). Let's throw the entire kitchen sink at it
and see what sticks. At this point, the unit file becomes too long to
just maintain in a blog post (which would be silly, but not unheard
of), so just look at this git repository instead.
Password-less remote irssi
The neat thing with this hardening is that I now feel comfortable
enough with the setup to just add a password-less SSH key to that
(basically throwaway) account: worst that can happen if someone gets a
hold of that SSH key is they land in a heavily sandboxed irssi
session. So yay, no password to jump on chat. Like a real client or
something.
Just make sure to secure the SSH key you'll deploy in
authorized_keys with:
restrict,pty,command="dtach -a /run/foo-irc/dtach-irssi.socket" [...]
Obviously, make sure the keys are not writable by the user, by placing
it somewhere outside their home, which might require hacking at your
server's SSH configuration. Because otherwise a compromised user will
be able to change his own authorized_keys, which could be bad.
Configuration management
And at this point, you may have noticed that you shouldn't actually
followed my instructions to the letter. Instead, just use this neat
little Puppet module which does all of the above, but also include
some little wrapper so that mosh still works.
It also includes instructions on how to setup your SSH keys.
Enjoy, and let me know if (or rather, how) I messed up.
Updates
- it seems that dtach is not very active upstream: the last release
(0.9) is from 2016, and the last commit (at the time of
writing) is from 2017
- dtach is not necessarily safer than screen or tmux from arbitrary
input from the outside, in fact there was a vulnerability on dtach
CVE-2012-3368 that led to an attacker accessing stack memory
(but maybe not code execution)
- after writing the Puppet module and publishing this article, I
started to get weird behavior from dtach: i would leave the office
at night and then return the next morning to find that I was timed
out on servers. from my perspective,
irssi noticed only when I
re-attached the session:
09:39:51 -!- Irssi: warning Broken pipe
09:39:51 -!- Irssi: warning SSL write error: Broken pipe
09:39:51 -!- Irssi: warning SSL write error: Broken pipe
09:39:51 -!- Irssi: warning SSL write error: Broken pipe
09:39:51 -!- Irssi: warning SSL write error: Broken pipe
09:39:51 [bitlbee] -!- Irssi: Connection lost to localhost
09:39:51 -!- Irssi: warning SSL write error: Broken pipe
09:39:51 [gitter] -!- Irssi: Connection lost to irc.gitter.im
09:39:51 [OFTC] -!- Irssi: Connection lost to irc.oftc.net
09:42:34 [IMC] -!- Irssi: Connection lost to irc.indymedia.org
09:42:34 -!- Irssi: Connection lost to irc.hackint.org
09:42:34 -!- Irssi: Connection lost to chat.freenode.net
09:42:34 -!- dtach_away: Set away
from the outside I actually timed out a few minutes after I
detached, which also makes for a weird asymmetry:
22:31:00 -!- anarcat [~anarcat@ocean] has quit [Ping timeout: 250 seconds]
that is eleven hours before the error I get.
- the mosh wrapper script seems to not work as well as it did
before. somehow just running
mosh $server hangs with a blank
screen instead of instantly rejoining the session. I'm not sure it
is related to the timeout problem but I did rewrite the wrapper
before publication. this is the old version:
#!/bin/sh
# inspired by https://serverfault.com/questions/749474/ssh-authorized-keys-command-option-multiple-commands
command="dtach -a /run/anarcat-irc/dtach-irssi.socket"
case "$SSH_ORIGINAL_COMMAND" in
mosh-server*)
exec mosh-server -- $command
;;
*)
exec $command
;;
esac
I'm thinking of trying that one out for a while to see if it's
related. The weirdest thing is that mosh "un-hangs" if i reattach
with plain ssh, so there's definitely something fishy going on
here.
dtach -N /run/$USER/dtach-irssi.socket irssi
dtach -a /run/$USER/dtach-irssi.socket
irssi as my main user anyways!
Let's take the lessons from good UNIX security, and run this as a
separate user altogether. This requires me to create another user, say
foo-irc:
adduser foo-irc
... and run it as a systemd service, because how else are you going
to start this thing anyways, cron? I came up with something
like this unit file:
[Unit]
Description=IRC screen session
After=network.target
[Service]
Type=simple
Environment="TERM=screen.xterm-256color"
User=%i
RuntimeDirectory=%i
ExecStart=-/usr/bin/dtach -N /run/%i/dtach-irssi.socket irssi
ExecStop=-/bin/sh -c 'echo /quit stopping service... exec /usr/bin/dtach -p /run/%i/dtach-irssi.socket'
ExecReload=-/bin/sh -c 'echo /restart exec /usr/bin/dtach -p /run/%i/dtach-irssi.socket'
Notice this is a service template, because of the %i stuff. I don't
actually remember how to enable this thing, but let's say you drop
this in /etc/systemd/system/irssi@.service, then you run:
systemctl daemon-reload
And then, not sure about this bit, instantiate that template:
systemctl enable irssi@foo-irc.service
And then this should start the irssi session:
systemctl start irssi@foo-irc.service
To access the session:
sudo -u foo-irc dtach -a /run/foo-irc/dtach-irssi.socket
Obviously, you will probably need to migrate your irssi
configuration over, otherwise you'll end up with a blank, old-school
irssi. Take a moment to savor the view though. Nostalgia. Ah.
Hardening irssi
But this is still not enough. That pesky foo-irc user can still
launch arbitrary commands, thanks to irssi /exec (and a generous
Perl scripting environment). Let's throw the entire kitchen sink at it
and see what sticks. At this point, the unit file becomes too long to
just maintain in a blog post (which would be silly, but not unheard
of), so just look at this git repository instead.
Password-less remote irssi
The neat thing with this hardening is that I now feel comfortable
enough with the setup to just add a password-less SSH key to that
(basically throwaway) account: worst that can happen if someone gets a
hold of that SSH key is they land in a heavily sandboxed irssi
session. So yay, no password to jump on chat. Like a real client or
something.
Just make sure to secure the SSH key you'll deploy in
authorized_keys with:
restrict,pty,command="dtach -a /run/foo-irc/dtach-irssi.socket" [...]
Obviously, make sure the keys are not writable by the user, by placing
it somewhere outside their home, which might require hacking at your
server's SSH configuration. Because otherwise a compromised user will
be able to change his own authorized_keys, which could be bad.
Configuration management
And at this point, you may have noticed that you shouldn't actually
followed my instructions to the letter. Instead, just use this neat
little Puppet module which does all of the above, but also include
some little wrapper so that mosh still works.
It also includes instructions on how to setup your SSH keys.
Enjoy, and let me know if (or rather, how) I messed up.
Updates
- it seems that dtach is not very active upstream: the last release
(0.9) is from 2016, and the last commit (at the time of
writing) is from 2017
- dtach is not necessarily safer than screen or tmux from arbitrary
input from the outside, in fact there was a vulnerability on dtach
CVE-2012-3368 that led to an attacker accessing stack memory
(but maybe not code execution)
- after writing the Puppet module and publishing this article, I
started to get weird behavior from dtach: i would leave the office
at night and then return the next morning to find that I was timed
out on servers. from my perspective,
irssi noticed only when I
re-attached the session:
09:39:51 -!- Irssi: warning Broken pipe
09:39:51 -!- Irssi: warning SSL write error: Broken pipe
09:39:51 -!- Irssi: warning SSL write error: Broken pipe
09:39:51 -!- Irssi: warning SSL write error: Broken pipe
09:39:51 -!- Irssi: warning SSL write error: Broken pipe
09:39:51 [bitlbee] -!- Irssi: Connection lost to localhost
09:39:51 -!- Irssi: warning SSL write error: Broken pipe
09:39:51 [gitter] -!- Irssi: Connection lost to irc.gitter.im
09:39:51 [OFTC] -!- Irssi: Connection lost to irc.oftc.net
09:42:34 [IMC] -!- Irssi: Connection lost to irc.indymedia.org
09:42:34 -!- Irssi: Connection lost to irc.hackint.org
09:42:34 -!- Irssi: Connection lost to chat.freenode.net
09:42:34 -!- dtach_away: Set away
from the outside I actually timed out a few minutes after I
detached, which also makes for a weird asymmetry:
22:31:00 -!- anarcat [~anarcat@ocean] has quit [Ping timeout: 250 seconds]
that is eleven hours before the error I get.
- the mosh wrapper script seems to not work as well as it did
before. somehow just running
mosh $server hangs with a blank
screen instead of instantly rejoining the session. I'm not sure it
is related to the timeout problem but I did rewrite the wrapper
before publication. this is the old version:
#!/bin/sh
# inspired by https://serverfault.com/questions/749474/ssh-authorized-keys-command-option-multiple-commands
command="dtach -a /run/anarcat-irc/dtach-irssi.socket"
case "$SSH_ORIGINAL_COMMAND" in
mosh-server*)
exec mosh-server -- $command
;;
*)
exec $command
;;
esac
I'm thinking of trying that one out for a while to see if it's
related. The weirdest thing is that mosh "un-hangs" if i reattach
with plain ssh, so there's definitely something fishy going on
here.
irssi
session. So yay, no password to jump on chat. Like a real client or
something.
Just make sure to secure the SSH key you'll deploy in
authorized_keys with:
restrict,pty,command="dtach -a /run/foo-irc/dtach-irssi.socket" [...]
Obviously, make sure the keys are not writable by the user, by placing
it somewhere outside their home, which might require hacking at your
server's SSH configuration. Because otherwise a compromised user will
be able to change his own authorized_keys, which could be bad.
Configuration management
And at this point, you may have noticed that you shouldn't actually
followed my instructions to the letter. Instead, just use this neat
little Puppet module which does all of the above, but also include
some little wrapper so that mosh still works.
It also includes instructions on how to setup your SSH keys.
Enjoy, and let me know if (or rather, how) I messed up.
Updates
- it seems that dtach is not very active upstream: the last release
(0.9) is from 2016, and the last commit (at the time of
writing) is from 2017
- dtach is not necessarily safer than screen or tmux from arbitrary
input from the outside, in fact there was a vulnerability on dtach
CVE-2012-3368 that led to an attacker accessing stack memory
(but maybe not code execution)
- after writing the Puppet module and publishing this article, I
started to get weird behavior from dtach: i would leave the office
at night and then return the next morning to find that I was timed
out on servers. from my perspective,
irssi noticed only when I
re-attached the session:
09:39:51 -!- Irssi: warning Broken pipe
09:39:51 -!- Irssi: warning SSL write error: Broken pipe
09:39:51 -!- Irssi: warning SSL write error: Broken pipe
09:39:51 -!- Irssi: warning SSL write error: Broken pipe
09:39:51 -!- Irssi: warning SSL write error: Broken pipe
09:39:51 [bitlbee] -!- Irssi: Connection lost to localhost
09:39:51 -!- Irssi: warning SSL write error: Broken pipe
09:39:51 [gitter] -!- Irssi: Connection lost to irc.gitter.im
09:39:51 [OFTC] -!- Irssi: Connection lost to irc.oftc.net
09:42:34 [IMC] -!- Irssi: Connection lost to irc.indymedia.org
09:42:34 -!- Irssi: Connection lost to irc.hackint.org
09:42:34 -!- Irssi: Connection lost to chat.freenode.net
09:42:34 -!- dtach_away: Set away
from the outside I actually timed out a few minutes after I
detached, which also makes for a weird asymmetry:
22:31:00 -!- anarcat [~anarcat@ocean] has quit [Ping timeout: 250 seconds]
that is eleven hours before the error I get.
- the mosh wrapper script seems to not work as well as it did
before. somehow just running
mosh $server hangs with a blank
screen instead of instantly rejoining the session. I'm not sure it
is related to the timeout problem but I did rewrite the wrapper
before publication. this is the old version:
#!/bin/sh
# inspired by https://serverfault.com/questions/749474/ssh-authorized-keys-command-option-multiple-commands
command="dtach -a /run/anarcat-irc/dtach-irssi.socket"
case "$SSH_ORIGINAL_COMMAND" in
mosh-server*)
exec mosh-server -- $command
;;
*)
exec $command
;;
esac
I'm thinking of trying that one out for a while to see if it's
related. The weirdest thing is that mosh "un-hangs" if i reattach
with plain ssh, so there's definitely something fishy going on
here.
- it seems that dtach is not very active upstream: the last release (0.9) is from 2016, and the last commit (at the time of writing) is from 2017
- dtach is not necessarily safer than screen or tmux from arbitrary input from the outside, in fact there was a vulnerability on dtach CVE-2012-3368 that led to an attacker accessing stack memory (but maybe not code execution)
- after writing the Puppet module and publishing this article, I
started to get weird behavior from dtach: i would leave the office
at night and then return the next morning to find that I was timed
out on servers. from my perspective,
irssinoticed only when I re-attached the session:
from the outside I actually timed out a few minutes after I detached, which also makes for a weird asymmetry:09:39:51 -!- Irssi: warning Broken pipe 09:39:51 -!- Irssi: warning SSL write error: Broken pipe 09:39:51 -!- Irssi: warning SSL write error: Broken pipe 09:39:51 -!- Irssi: warning SSL write error: Broken pipe 09:39:51 -!- Irssi: warning SSL write error: Broken pipe 09:39:51 [bitlbee] -!- Irssi: Connection lost to localhost 09:39:51 -!- Irssi: warning SSL write error: Broken pipe 09:39:51 [gitter] -!- Irssi: Connection lost to irc.gitter.im 09:39:51 [OFTC] -!- Irssi: Connection lost to irc.oftc.net 09:42:34 [IMC] -!- Irssi: Connection lost to irc.indymedia.org 09:42:34 -!- Irssi: Connection lost to irc.hackint.org 09:42:34 -!- Irssi: Connection lost to chat.freenode.net 09:42:34 -!- dtach_away: Set away
that is eleven hours before the error I get.22:31:00 -!- anarcat [~anarcat@ocean] has quit [Ping timeout: 250 seconds] - the mosh wrapper script seems to not work as well as it did
before. somehow just running
mosh $serverhangs with a blank screen instead of instantly rejoining the session. I'm not sure it is related to the timeout problem but I did rewrite the wrapper before publication. this is the old version:
I'm thinking of trying that one out for a while to see if it's related. The weirdest thing is that mosh "un-hangs" if i reattach with plain#!/bin/sh # inspired by https://serverfault.com/questions/749474/ssh-authorized-keys-command-option-multiple-commands command="dtach -a /run/anarcat-irc/dtach-irssi.socket" case "$SSH_ORIGINAL_COMMAND" in mosh-server*) exec mosh-server -- $command ;; *) exec $command ;; esacssh, so there's definitely something fishy going on here.



. I am open to testing and debugging if somebody does try decoding the new versions. For me, this is also an issue as most I.T. notices and what not we get, at times I have to try and figure out things, at times go to MS and use an acrobat reader and that is dumb.

While struggling with Pfizer second dose side effects yesterday, with little
ability to do anything serious so surreal to have a fever yet also
certainty you re not actually ill[1] I thought I d try building the branch
of Emacs with native Wayland support, and try starting up Sway instead of i3.
I recently upgraded my laptop to Debian bullseye, as I usually do at this
stage of our pre-release freeze, and was wondering whether bullseye would be
the release which would enable me to switch to Wayland.
Why might I want to do this? I don t care about screen tearing and don t have
any fancy monitors with absurd numbers of pixels. Previously, I had been
hoping to cling on to my X11 setup for as long as possible, and switch to
Wayland only once things I want to use started working worse on X11, because
all the developers of those things have stopped using X11. But then after
upgrading to bullseye, I found I had to forward-port an old patch to
xfce4-session to prevent it from resetting SSH_AUTH_SOCK to the wrong value,
and I thought to myself, maybe I could cut out some of the layers here, and
maybe it ll be a bit less annoying. I have a pile of little scripts trying to
glue together xfce4 and i3 to get all the functionality I need, but since
there have been people who use their computers for similar purposes to me
trying to make Sway useful for quite some time now, maybe there are more
integrated solutions available.
I have also been getting tired of things which have only ever half-worked
under X, like toggling autolock off when there isn t fullscreen video playing
(when I m video conferencing on another device, I often want to prevent my
laptop s screen from locking, and it works most of the time, but sometimes
still locks, sigh). I have a normalise desktop keybinding which tries to
fix recurrent issues by doing things like restarting ibus, and it would be
nice to drop something so hackish.
And indeed, a lot of basic things do work way better under Sway. swayidle is
clean and sane, and I was easily able to add a keybinding which inhibits
locking the screen when a certain window is visible. I could bind brightness
up/down keys without having to invoke xfce4-power-manager never managed
that before and, excitingly, I could have those keys bound such that they
still work when the screen is locked. I still need two old scripts which
interact with the i3/sway IPC, but those two are reasonable ways to extend
functionality, rather than bad hacks.
The main thing that I could not figure out is IME various people claim
online to have got typing their Asian languages working natively under Sway,
not just into Xwayland windows, but I couldn t, and there is no standard way
to do it yet, it would seem. Also, it seems Qt in Debian doesn t support
Wayland natively, so far as I could tell. And there isn t really a drop-in
replacement for dmenu yet, so you have to run that under Xwayland. A lot of
this is probably going to be fixed during 2021, but the thing is, I ll be on
Debian bullseye, so how it works now is probably as good as it is going to get
for the next two years or so.
So, wanting to get back to doing something more useful today, I reluctantly
booted back into X11. I m really looking forward to switching to Sway, and
getting rid of some of my hacks, but I think I am probably going to have to
wait for Debian bookworm unless I completely run out of patience with the
various X11 annoyances described above, and start furiously backporting
things.
Update, later that afternoon Newer versions of fcitx5 packages hit Debian
testing within the past few days, it turns out, and the IME problem is solved!
So looks like I am slowly going to be able to migrate to Sway during the
Debian bullseye lifecycle after all. How nice. Many thanks to various
upstreams and those who have been working on these packages in Debian.
[1] Okay, I suppose I could have caught the disease a few days ago and it
became symptomatic at the same time I was experiencing the side effects.
I am quite happy with the Raspberry tower I bought for keeping my
Raspberries organized. Clustering them? No, not by a long shot. I just
want to quickly know where they all are, and at a glance, be able to
know which one I will work with. Bottom drawer has a RPi1B, second one
has a RPi2, next comes a 3B+, and the top two ones are RPi4 (4 and
8GB). That allows me for quick testing of stuff. Yes, I am tempted to
get the top one out of the array and use it in production but as
it stands, that s the layout.
My only quip with this? Serial console access.