Lets say someone sits behind my laptop for a second and runs:
alias cd='Ha Ha, Got You :))'
or we run an unknown software/script/etc and it appends something to ~/.bashrc
. like:
alias sort='rm -rf ~'
These are only examples of aliases
; As you know, these things also can be done using functions:
cd(){ echo "Removing everything you've got :D"; }
These situations are just imaginary examples, consider anything similar.
What about a small script?
sudo -n ls &>/dev/null
if [ "$?" -eq "0" ]
then
sudo "Some dangerous command"
else
cd $1
fi
Then alias cd="/home/user/.config/gtk/.cd.sh"
.
For the commands which have been ran in bash we can simply close and reopen our terminal, but what about the ones that been set in startup files, we can't check the files or list of aliases/functions every single time we run a terminal.
Introduction
Bash configuration files
Bash has a bunch of configuration (aka startup) files, it uses these files to setup a specific environment for each user.
Some of these files are located at
/etc
, one of them that I'm aware of is/etc/profile
, it's a global configuration file and its settings will be applied into all sessions, another is/etc/bash.bashrc
; We don't need to work around these files because of their location they already are protected and only root has the rights to edit them.A very important directory which can help us a lot is:
/etc/skel
; Whenever you create a new user with home directory, the files within this directory will be used as a skeleton for your new user's home directory.We can also use
dpkg
to find about these files:we can see that these all are installed by
bash
.How thing works in bash
An alias or function can be set in any of these files, so let see how these files will be used by
bash
.From
bash
man page:so the order is:
~/.bash_profile > ~/.bash_login > ~/.profile
this one will be run every time we exit from a login shell, I can't see how this one can has any effects on our situation.
so the most important fiel is
~/.bashrc
, because almost 90% of bash shells which we run are in interactive and no-login mode. and if we have a look at this file we can see that it will look for another file named~/.bash_aliases
, if it was able to locate it, then it willsource
that file too.Start taking care of these files
First of all we should move
~/.profile
to~/.bash_profile
otherwise it does not matters if we protect~/.profile
file, someone can create a~/.bash_profile
and it will overrides our configs, so:After that if you are not using a
~/.bash_aliases
file then create it, again like above, someone can simply create this file and there is a chance that (s)he can alter or aliases within it.Finally use
chattr
to protect these files against edit and removal.From
chattr
man page:We are done, Don't forget that whenever you want to edit these files you should first remove the
-i
attribute.Reset everything without close/reopening terminal
Another workaround is, creating a file:
Put your aliases there:
Make sure nobody can write into that file:
Now every time you want to reload everything run:
Rollback
And if you ever changed your mind:
If you do stupid things (walk away without locking screen, blindly run uninspected scripts from untrusted sources, etc), you will get bad results.
Trying to be clever enough to recover from bad practices has never worked before.