Last active
May 8, 2018 08:28
-
-
Save isholgueras/8722b1c407ce31780fecb6f2240e5e16 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// crontab -e -> | |
// 1 * * * * php /home/core/clean_cryptocurrency_activity.php >> /var/log/cryptocurrency_activity.log 2>&1 | |
// Don't forget to add the file to logrotate!! (or delete it periodically) | |
$date = new DateTime("now"); | |
$dateString = $date->format("Y/m/d-H:i:s"); | |
$needToClean = false; | |
$directories = [ | |
' /apps/web_*/web/sites/default/files', // Drupal 8 | |
' /apps/web_*/sites/default/files', // Drupal 7 | |
' /apps/web_*/wp-content/uploads', // Wordpress | |
' /tmp', // Temporary directory | |
]; | |
foreach ($directories as $directory) { | |
if (existExecutableFiles($directory)) { | |
$needToClean = true; | |
} | |
} | |
if ($needToClean) { | |
foreach ($directories as $directory) { | |
cleanExecutableFiles($directory); | |
} | |
restartPhpFpm(); | |
} | |
else { | |
$carga = sys_getloadavg(); | |
if ($carga[0] > 3) { | |
restartPhpFpm(); | |
echo "CLEAN[$dateString]: But restarted php\n"; | |
} | |
else { | |
echo "CLEAN[$dateString]: Everything is fine\n"; | |
} | |
} | |
function existExecutableFiles($dir) { | |
$date = new DateTime("now"); | |
$dateString = $date->format("Y/m/d-H:i:s"); | |
$find = exec('find ' . $dir . ' -executable -type f'); | |
if ( strlen($find) !== 0) { | |
echo "CLEAN[$dateString]: Infected file: $find\n"; | |
return true; | |
} | |
return false; | |
} | |
function cleanExecutableFiles($dir) { | |
exec('netstat -anpt | grep ESTABL'); | |
exec('find ' . $dir . ' -executable -type f -exec rm {} \;'); | |
} | |
function restartPhpFpm() { | |
exec('/etc/init.d/php7.0-fpm restart'); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment