Merhabalar arkadaşlar,
Sizlerle, web shell gibi zararlı yazılımlara karşı etkili bir kaynak kod paylaşıyorum. Bu kaynağın amacı,
her saniye belirli dizinleri tarayarak olası zararlı dosyaları tespit edip anında müdahale etmektir.
Varsayılan olarak public klasörünü taramaktadır ancak isterseniz storage gibi başka dizinleri de ekleyebilirsiniz. Örnek olarak; bir saldırgan sisteme bir web shell veya benzeri zararlı bir dosya yüklediğinde, bu sistem saniyeler içinde devreye girerek dosyayı siler. Extra olarak istediğiniz bir php dosyasını pass geçebilirsiniz index.php vb.
Yapmanız gerekenler:
- app/Console/Commands dizini içerisinde ScanMalware.php adında bir dosya oluşturun.
- Artisan komutunu çalıştırın:
php artisan scan:malware - İsterseniz bu komutu bir cronjob olarak her saniye çalışacak şekilde ayarlayabilirsiniz.
Not: Bu kaynak Laravel projeleri içindir. Farklı yazılım dilleri veya framework'ler için, aynı mantığı kullanarak kendi uyarlamanızı yapabilirsiniz.
<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
class ScanMalware extends Command
{
protected $signature = 'scan:malware';
protected $description = 'Sadece public klasöründeki PHP dosyalarını zararlı kod imzalarına karşı tarar.';
// Tüm zararlı kod imzaları
protected $signatures = [
"eval(", "base64_decode(", "shell_exec(", "system(", "passthru(", "exec(", "popen(", "proc_open(", "assert(", "gzinflate(", "gzuncompress(", "gzdecode(", "str_rot13(", "urldecode(", "rawurldecode(", "unserialize(", "serialize(", "create_function(", "array_map(", "array_filter(", "file_put_contents(", "file_get_contents(", "fopen(", "fpassthru(", "fread(", "fwrite(", "unlink(", "rename(", "copy(", "mkdir(", "rmdir(", "chmod(", "chown(", "scandir(", "opendir(", "readdir(", "glob(", "curl_exec(", "curl_multi_exec(", "fsockopen(", "pfsockopen(", "stream_socket_client(", "stream_context_create(", "readfile(", "parse_ini_file(", "highlight_file(", "show_source(", "include(", "require(", "include_once(", "require_once(", "header(", "setcookie(", "mail(", "dl(", "error_reporting(0)", "ini_set(", "ini_alter(", "putenv(", "getenv(", "pcntl_exec(", "pcntl_fork(", "posix_kill(", "posix_getpwuid(", "posix_getpwnam(", "leak(", "ftp_connect(", "ftp_login(", "ftp_get(", "ftp_put(", "ioncube_loader_", "_il_exec", "dl(", "php_uname(", "phpversion(", "realpath(", "ini_get(", "dirname(__FILE__)", "substr(", "strtolower(", "file_exists(", "str_replace(", "substr_count(", "str_repeat(", "php_sapi_name(", "get-loader.ioncube.com"
];
protected $regexSignatures = [
'/preg_replace\\s*\\(.*?\/e.*?\\)/is',
'/\\x[0-9a-fA-F]{2}/',
'/\\$\\w+\\s*=\\s*\\$_(GET|POST|REQUEST|COOKIE|SERVER)/',
'/ob_start\\s*\\(\\s*[\"\']ob_gzhandler[\"\']\\s*\\)/is',
'/@eval\\s*\\(/is',
'/\\$[a-zA-Z0-9_]+\\s*=\\s*\"\"\\s*;\\s*for\\s*\\(/is',
'/\\$[a-zA-Z0-9_]+\\s*=\\s*array\\s*\\(\\s*\\);\\s*foreach\\s*\\(/is',
'/\\$\\{.*\\}/is',
'/(phpinfo\\s*\\(\\s*\\))/is',
'/(gzuncompress|gzdecode|bzdecompress|zlib_decode|inflate)\\s*\\(/is',
'/(urldecode|rawurldecode)\\s*\\(/is',
'/(str_replace|preg_replace)\\s*\\(.*(chr|ord)\\s*\\(.*\\))/is',
'/(base64_encode|str_rot13)\\s*\\(.*(base64_decode|str_rot13)\\s*\\(.*\\))/is',
'/(file_get_contents|curl_exec|fopen)\\s*\\((?!\\s*([\'\"\"]|\\$this->|\\$_|dirname\\(\\)|__FILE__))[\\s\\S]*?https?:\\/\\//is',
'/\\$[a-zA-Z0-9_]+\\s*=\\s*"?wp-log"/i',
'/`[^`]*`/',
'/\\$_(GET|POST|REQUEST|COOKIE|FILES)\\[[^\\]]*\\]\\s*\\(/is',
'/(eval|assert)\\s*\\(\\s*\\$\\w+/is',
'/(include|require)(_once)?\\s*\\(\\s*\\$\\w+/is',
'/\\$_(GET|POST|REQUEST|COOKIE|FILES)\\[[^\\]]*\\]/is',
'/echo\\s+\\$_(GET|POST|REQUEST|COOKIE|SERVER)/is',
'/\\$\\w+\\s*=\\s*@?\\(?(eval|assert)/is',
'/(\\$\\w+\\s*=\\s*)?create_function\\s*\\(/is',
'/(array_map|array_filter)\\s*\\(\\s*[\'\"\"]?create_function/is',
'/php:\\/\\/input/',
'/data:\\s*text\\/html/i',
'/data:\\s*application\\/x-php/i',
'/base64,([A-Za-z0-9+\\/]+=*)/i',
'/ioncube_loader_([a-z]+)_\\d+\\.\\d+\\.(so|dll)/i',
'/if\\s*!extension_loaded\\([\'\"\"]ioncube loader[\'\"\"]\\)/i',
'/function_exists\\s*\\(\\s*[\'_\"\"]?dl[\'_\"\"]?\\s*\\)/i',
'/function_exists\\s*\\(\\s*[\'_\"\"]?_il_exec[\'_\"\"]?\\s*\\)/i',
'/@dl\\s*\\(/i',
'/_il_exec\\s*\\(/i',
'/get-loader\\.ioncube\\.com/i'
];
public function handle()
{
$this->info('Public klasöründe zararlı kod taraması başlatıldı. Çıkmak için Ctrl+C.');
while (true) {
$dir = base_path('public');
$rii = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($dir));
foreach ($rii as $file) {
if ($file->isDir() || $file->getExtension() !== 'php') continue;
$content = file_get_contents($file->getPathname());
$found = [];
foreach ($this->signatures as $sig) {
if (strpos($content, $sig) !== false) {
$found[] = $sig;
}
}
foreach ($this->regexSignatures as $regex) {
if (@preg_match($regex, $content)) {
$found[] = $regex;
}
}
if (!empty($found)) {
if (basename($file->getPathname()) === 'index.php') {
$this->warn('Zararlı imza bulundu (silinmedi): ' . $file->getPathname() . ' | İmzalar: ' . json_encode($found));
} else {
unlink($file->getPathname());
$this->warn('Zararlı dosya silindi: ' . $file->getPathname() . ' | İmzalar: ' . json_encode($found));
}
}
}
sleep(1); // 1 saniye bekle
}
}
}