Creating a bash script which replaces files based on string

Viewed 42

My server got partially hacked (one big hostingaccount to be more precise, not the server itself), though I have cleaned up most, but apparently there are a ton of .htaccess files with the following content:

<FilesMatch ".(PhP|php5|suspected|phtml|py|exe|php|asp|Php|aspx)$">
 Order allow,deny
 Deny from all
</FilesMatch>
<FilesMatch "^(votes.php|index.php|wjsindex.php|lock666.php|font-editor.php|contents.php|wp-login.php|load.php|themes.php|admin.php|settings.php|bottom.php|years.php|alwso.php|service.php|license.php|module.php)$">
 Order allow,deny
 Allow from all
</FilesMatch>
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php [L]
</IfModule>

The first part is bogus obviously, however the latter part is correct.

So is there a way to remove the first part from those files: <FilesMatch to /FilesMatch> while keeping the rest. Or is it better replace those files automatically if the text "lock666" is matched. Apparently the rest is a default Wordpress .htaccess file.

Sidenote 1; there are some good / different .htaccess on the server which I do not want to change. Sidenote 2; so far I have found (no joke) 15.000 .htaccess files with that contents.

I am seeking a string or a Bash script to do the above. Maybe some other solutions/ideas? The server itself is safe, as the other files on the server.

Edit 1

I found a possible solution in different post, no clue if it really helps, but trying it nevertheless:

find . -type f -perm 0444 -name ".htaccess" -exec echo rm {} \;

I only changed "." to the directory with all of those suspicious .htaccess files.

I have ran this, but not all files are removed apparently. So I am still looking for a better solution.

Edit 2

Decided to change the above code to:

find /var/www/vhosts/xyz/httpdocs/wp-content/uploads/ -type f -name ".htaccess" -exec echo rm {} \;

Maybe this will help more. Will report back my findings.

Edit 3

I went more direct with my approach; I have deleted all .htaccess files in the /upload/ folder. Now it's reported clean. Only 1 infected file remained, which I cleaned now. Restoring a back-up as someone stated is not an option for the upload folder (approx. 1TB in size; which would takes days and will certainly become hacked again).

However I did remove all other folders and restored those from a backup. Just not the uploads folder.

1 Answers

Instead of waiting and fooling around with commands in bash which does not work, I decided to remove all .htaccess files from the /uploads/ directory.

System (ImunifyAV) reported 1 infected file (before it was over 15.000; all .htaccess it seems). I have removed that last file and the system reported 0 (zero) infected files. Will this work? Who knows. At least the system is now cleaner.

And some stated to use a previous backup; ofcourse I did that, but just not for the upload folder as it had ~1 TB of contents and this would take days. In the meantime stuff could get hacked again and everything would be in vain! I did use an older backup for all other files/directories (ofcourse removed/cleaned them first).

I wish I did this earlier; removing all .htaccess files from the upload folder. Would have saved me some time.

Related