I've recently been in a corner case where I was dealing with an embedded product which requires a specific set of commands and also uses some bracket commands that are difficult to wrap with our usual SSH command authenticator. So I decided to revisit using a restricted shell to jail this user and I think I managed to make the jail shatterproof enough.
Create Bob's home directory, but assign it to root:
# mkdir /home/bob
# chown root:root /home/bob
# chmod 755 /home/bob
Force a .bashrc and .profile that changes Bob's PATH to a limited set of commands:
# echo "export PATH=/opt/arcbck/allowed_commands" > .bashrc
# ln -s .bashrc .profile
The reason for having both a .profile and a .bashrc is to ensure that this profile will be loaded both for interactive and non-interactive sessions.
If the user needs to write stuff somewhere, create a directory for Bob, e.g.
# mkdir /home/bob/writable
# chown bob home/bob/writable
# chmod 755 /home/bob/writable
Create the allowed_commands directory and put symlinks in it pointing to allowed binaries:
# mkdir /home/bob/allowed_commands
# ln -s /bin/mycmd allowed_commands/mycmd
Now you must be sure of the following:
1. Bob must NOT have any writable access to /home/bob/.profile or /home/bob/.bashrc, else he can change the PATH value
2. Bob must NOT have any writable access to /home/bob, to prevent any modification of .profile and .bashrc
3. Investigate ANY command that ends up in the allowed_commands jail to be sure that there is NO known way of executing another command from it, showing files or escaping the shell. If there are any, then forfeit giving this command or write a wrapper around it (see below).
4. See the jail escape methods linked above, log in as Bob and see if you can use them to escape the jail.
Example of a wrapper script with scp
Let's say I want to allow Bob to scp files into his account using scp's undocumented -t (i.e. -to) option. I would normally do this:# ln -s /bin/scp allowed_commands/scp
This is wrong as scp can be coerced with -S to execute random commands.
A solution is to put the following in the allowed_commands jail instead:
lrwxrwxrwx. 1 root root 14 May 5 10:02 scp -> scp_wrapper.sh
-rwxr-xr-x. 1 root root 382 May 5 13:54 scp_wrapper.sh
With scp_wrapper.sh containing this:
#!/bin/sh
if [[ "$1" = "-t" && "$2" != "-"* ]]
then
/bin/scp -t $2
returncode=$?
else
echo "scp_wrapper: Refused SCP command: '$*'"
returncode=255
fi
exit ${returncode}
Using this wrapper, scp will only allow -t and no other option.
Good luck.
No comments:
Post a Comment