On bash in general
Bash's design with respect to startup files is rather peculiar. Bash loads .bashrc
in two unrelated circumstances:
On SSH in general
When you execute a command through the SSH protocol, the command is passed over the wire as a string. The string is executed by the remote shell. When you run ssh example.com somecommand
, if the remote user's login shell is /bin/bash
, the SSH server runs /bin/bash -c somecommand
. There is no way to bypass the login shell. This permits restricted login shells, for example to allow only file copying and not general command execution.
There is one exception: the SSH protocol allows the client to request a specific subsystem. If the client requests the sftp
subsystem, then by default the OpenSSH server invokes the program /usr/lib/openssh/sftp-server
(the location may vary) via the user's login shell. But it can also be configured to run an internal SFTP server through the line
Subsystem sftp internal-sftp
in the sshd_config
file. In the case of the internal SFTP server, and only in this case, the user's login shell is bypassed.
For this challenge
In the case of OverTheWire Bandit 18, .bashrc
contains
…
# If not running interactively, don't do anything
case $- in
*i*) ;;
*) return;;
esac
…
echo 'Byebye !'
exit 0
So you can solve this level by doing anything that causes bash not to be interactive.
As you discovered, SFTP works.
But ssh bandit18@bandit.labs.overthewire.org cat readme
would also work.
As would echo 'cat readme' | ssh bandit18@bandit.labs.overthewire.org
.
And pressing Ctrl+C at the right time during an interactive login would also work: it would interrupt bash, so the .bashrc
would not be completely executed. Bash takes macroscopic time to start up, so while this doesn't work reliably, it can be done in practice.