Protecting Joomla’s Files and Directories from Accidents

A few days ago, a managed client of ours called us and told us that their website stopped working, and that that event coincided when an employee was trying to upload a file to the images folder using FileZilla.

We immediately knew what the issue was, as it happened to another client of ours a couple of years ago: a staff member uploading a file to the images folder accidentally moved the components folder to the inside of the images folder. A quick investigation revealed that this was exactly what happened this time, with the exception that it was the includes folder that was moved to the images folder. Fixing the problem simply consisted of moving the includes folder back to its original location (directly under the public_html folder).

Needless to say, this whole issue made us feel unsafe, so we needed to find a solution to the problem!

Using FTP instead of SFTP was outside the question for security reasons, and, even if we headed that way, we would still run into some risks of moving directories inside each other.

After some very long research, we discovered that the following shell command…

chattr +i dir1

…makes the directory dir1 immutable. Meaning that it cannot be moved or altered in any way.

So, we issued, as root, the following shell commands when inside the public_html directory:

chattr +i administrator/components administrator/help administrator/includes administrator/language administrator/manifests administrator/modules administrator/templates administrator/.htaccess administrator/index.php
chattr +i bin cli components includes language layouts libraries modules plugins templates .htaccess configuration.php defines.php index.php

After doing the above, we tested if we were able to move any of the above folders to a different location from within FileZilla (by dragging and dropping), and it didn’t work. Hooray! We solved the problem.

But, what happened to the cache, media, and tmp folders?

Joomla needs to have write access to the cache, administrator/cache, media, and tmp folders for its basic operation, and that’s why we excluded them from the shell commands above, as an immutable file/folder cannot be written to.

What if someone wants to install an extension?

Installing an extension won’t work with the immutable flag set. So, if you need to install an extension, you will need to first remove the immutable flag from the above folders by issuing the following shell commands (again, you must do that root, as root is the only user that can set/unset the immutable flag):

chattr -i administrator/components administrator/help administrator/includes administrator/language administrator/manifests administrator/modules administrator/templates administrator/.htaccess administrator/index.php
chattr -i bin cli components includes language layouts libraries modules plugins templates .htaccess configuration.php defines.php index.php

How do you know if a file/folder has the immutable flag set?

The lsattr shell command does the trick. So, if you want to know which extended flags are set on a specific file/folder, all you need to do is to issue the following command:

lsattr file-or-folder

For example, to know which extended flags are set on the index.php file, you issue the following shell command (you don’t have to be root to use the lsattr command):

lsattr index.php

Won’t making the folders/files immutable enhance security?

Definitely! As you can see in the first set of shell commands in this post, we are making the index.php immutable, which prevents alteration to this file by anyone. In fact, the only way to modify this file would be to login as root and then unset the i flag.

We hope that you found this post useful. If you need any help with the implementation, then all you need to do is to contact us. We’ll do the above for you in a breeze and we won’t charge you an arm and a leg for it.

No comments yet.

Leave a comment