For the past couple of days, I've been playing around with OverTheWire (OTW), particularly the Bandit game. And so it was, I was having my fun and going through each level, mostly reviewing stuff I know about linux or networks, but also learning quite a bit.
Upon reaching lvl 20 I was having a bit more trouble, since I was making a mess for myself in establishing a listening port and so on (I truly don't want to give spoilers or too much... see below)
Not only are these the rules, but I also agree with them. I really really think that if you are going to play these games, you should steer clear of reading spoilers, or walkthroughs. Also, if you want to write about these challenges, you can do it in an interesting fashion, that can help other players, giving them hints, or neat ideas instead of spoon-feeding them answers.
So, I won't tell you how I solved lvl 20. But I'll tell you what happened to me while I was on that level.
You know the saying 'a picture is worth a thousand words'? Well, how about a picture of words? Is the effect multiplicative? Here's the pic:
It's a bit messy, because we were using the command line in tmux as a means of communication within the terminal: which isn't its intended purpose, but still, I think you can follow the brief conversation.
Moments before, I was baffled when I noticed someone taking control of my tmux session. I closed the session, re-opened it and awaited for a reply. And there it was, with a friendly 'hello :)'. I would have started with 'Wake up, Neo...' myself...
...but that might just be my age speaking.
So, conversation friendliness aside, let us figure out together what happened here.
The other user was not only kind, but right in the way they explained to me how I could find out about the 'intrusion' method:
man tmux
That's it, really. Just read the fine manual. And so I did, taking the time to learn a couple of new things instead of just skimming it again.
One of the things that I learned is that you can scan for existing sessions, by listing them:
tmux list-sessions
or
tmux ls
This will show you current sessions and their names.
You can then jump (or attach, really) to that session. Let's say there's a session named my_session open. You could then:
tmux attach-session -t my_session
And yes. It's that simple. You are now sharing the same session as the user that created and is using it.
Try creating a couple of sessions. Create a tmux session, then exit it (Ctrl+D works).
The default name for a tmux session will be called '0'. Just that.
So, you could also, arguably, try to connect to a session automatically as well. Spamming that name until you find it, just to catch someone unawares, with:
while :; do tmux attach-session -t 0; sleep 1; done
I have two sessions in level bandit1, and in one I'm running this loop, while in the other I simply typed 'tmux' twice, automatically entering a default tmux session, called '0'.
Test it out. You don't even have to enter OTW. You can try it on your own local machine, with two different terminals.
You need to create a tmux socket and create your session through that socket.
OTW protects its filesystem, and we, as users, are only allowed to create files within the /tmp folder. So we do that. We create a socket and connect through it:
But couldn't they simply go through all files in /tmp?
They could, but OTW has that folder protected:
There's a sticky bit there (t) ensuring that users can only modify files that they own. Also, 'r' is missing from others, meaning that they cannot read that folder.
This makes it a bit harder to simply scan the folder and start checking out stuff.
"But wait!" you say. "Couldn't we just set restrictive permissions on our own socket?"
We can, yeah. But remember that all users on this levels have the same name as we do. So, they're basically the same user.
We are, in fact, using some mild security through obscurity, and defending our tmux socket (and therefore session) from our other selves (I'll give some info on security through obscurity in another blog post).
Very Pessoesque... or is it the other way around?
Anyway, there we go. Good job at protecting yourself from your other selves!
We can now run our tmux sessions in (relative) peace. We have erected a fence that will keep away the overwhelming majority of attacks.
Remember, though, when we were looking at the rules?
- don't annoy other playersDid they meant this tmux trick? How could we possibly annoy other players?
That got me thinking.
We're all the same user, basically, but we're connecting from different terminals devices, right? Tmux would be a virtual terminal, but we're initially connecting from some other terminal.
Ok, let's see what's my current terminal:
Aha, interesting. And, just for our own pleasure, what's our virtual terminal device when inside our now (highly-amazing and protected) tmux session?
Try to do ls -hal /dev/pts/40
Interesting... you can see that we have writing permission if we are the user.
But, everyone in this level is bandit1, so we can just do
who
This is a list of all visible learners and their respective levels/users.
So, what's stopping us from quoting The Matrix? Nothing, other than being a nuisance. So, let's open a second session. And let's just flex our age and good taste in cinema onto our own selves in another virtual reality.
Let's also try to reply, with a caveat: we've done 'chmod 000' to our original pseudo-terminal device. Look at the result. It's interesting.
You might be wondering how we're able to continue using our terminal when we've removed all permissions, even for the owner... Good point!
The key is that our current process already has the terminal open and maintains its file descriptors. This allows us to continue reading from and writing to the terminal as usual.
However, any new attempts to access the terminal device file will fail.
For example, if we try to send an echo message to our terminal using:
echo "Hi, guy!" > /dev/pts/40
...it won't work.
But we can still do:
echo "Hi, guy!" normally in our current session.
Also interesting!
This showcases a bit how Unix-like systems handle file descriptors and permissions. That's also food for thougth and another day's snack.
I'll be writing more about my OTW playtime ('part 1' was a giveaway, wasn't it?). I'll share ideas, insights and stuff I find interesting, but not walkthroughs or spoilers.
Finally, I'd like to extend a thank you to the nameless 'hacker' with whom I had a pleasant, albeit short, conversation. That romp got me into a learning marathon which has been super fun.
Have fun, enter The Matrix, and behave!
No comments:
Post a Comment