Keamanan: Cegah RCE via Theme Hook include_once dan Backtick Operator Bypass pada ThemeHooksValidator - #1715
Open
habibie11 wants to merge 1 commit into
Open
Keamanan: Cegah RCE via Theme Hook include_once dan Backtick Operator Bypass pada ThemeHooksValidator#1715habibie11 wants to merge 1 commit into
habibie11 wants to merge 1 commit into
Conversation
…da ThemeHooksValidator
Contributor
|
🔄 AI PR Review sedang antri di server...
|
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
issue # https://github.com/OpenSID/wiki-keamanan/issues/57
🎯 Deskripsi
Pull request ini memperbaiki kerentanan keamanan kritis Remote Code Execution (RCE) (CWE-94 / CWE-96, OWASP A03:2021) pada modul manajemen tema OpenDK.
Penyebab Masalah:
ThemeHooksValidator::validateSource()memvalidasi filehooks.phpmenggunakan fungsi bawaan PHPtoken_get_all(). Pada implementasi sebelumnya, perulangan token dimulai dengan barisif (! is_array($tokens[$i])) continue;. Hal ini menyebabkan semua token karakter tunggal (seperti backtick`,$,(,),;) dilewati tanpa diperiksa sama sekali.`cmd`) merupakan operator eksekusi sistem yang identik dengan fungsishell_exec('cmd'). Karena karakter backtick terlewat dari validasi token, perintah sistem seperti`id > themes/default/rce_proof.txt 2>&1`lolos dari filter.ThemesController::activate()->ThemeService::activate()->ThemeHooksValidator::loadHooks(), filehooks.phpdimuat menggunakaninclude_once $hooksFile, sehingga perintah sistem yang disuntikkan langsung dieksekusi di bawah hak akses user web server (www-data).hooks.phpdan arsip ZIP tema sebelumnya belum memvalidasi secara ketat konstruksi bahasa lain sepertiinclude/require(T_INCLUDE*,T_REQUIRE*), dynamic class instantiation (new $class()), dynamic static/member calls ($obj->$method(),Class::$method()), variasi variable variables ($$var), pemindaian.blade.phpberbahaya di arsip ZIP, serta fungsi-fungsi manipulasi berkas/direktori/eksekusi proses PHP lainnya.Solusi yang Diterapkan:
ThemeHooksValidator::validateSource():str_contains($source, '')` dan langsung menolak berkas jika ditemukan karakter backtick.T_INCLUDE,T_INCLUDE_ONCE,T_REQUIRE,T_REQUIRE_ONCE,T_EVAL, danT_HALT_COMPILER.new $class(),$obj->$method(),Class::$method(),$$var, sertavariable function call($f()) lengkap dengan mitigasi bypass komentar/whitespace.DANGEROUS_FUNCTIONS: Menambahkan fungsi kontrol proses (pcntl_*), callback dinamis (forward_static_call,register_shutdown_function, dll), pembacaan berkas & direktori (readfile,file,scandir,glob, dll), serialisasi (unserialize), serta fungsi obfuskasi (hex2bin,gzinflate, dll).scanZipForPhp):.blade.phphanya diizinkan jika berada di direktoriresources/views/..blade.phpdi dalam arsip ZIP untuk memastikan tidak disusupi operator backtick pada blok arahan PHP/Blade (<?php,@php,{{,{!!) sebelum diekstrak, tanpa memblokir JavaScript ES6 template literals murni pada sisi frontend (mencegah false positive pada tema modern sepertibuen).ThemesControllerSecurityTest.phpdan 13 feature tests padaThemeUploadSecurityTest.php.🛠️ Perubahan yang Dilakukan
1.
app/Services/ThemeHooksValidator.phpFix — Penguatan validasi token AST, pencegahan backtick, dan pengamanan arsip ZIP tema:
DANGEROUS_FUNCTIONSdan mendefinisikanDANGEROUS_LANGUAGE_TOKENS..blade.phpdengan deteksi terarah pada arahan PHP/Blade.// Validate Blade template files if (str_ends_with($filename, '.blade.php')) { + // Blade templates must be inside resources/views/ + if (! str_contains($filename, 'resources/views/')) { + $dangerousFiles[] = "{$filename} (Blade template must reside in resources/views/)"; + continue; + } + + // Scan Blade content for PHP/Blade execution blocks containing backtick operator + $bladeContent = $zip->getFromIndex($i); + if ($bladeContent !== false && preg_match('/(<\?php|@php|\{\{|\{!!)[^>}]*`/', $bladeContent)) { + $dangerousFiles[] = "{$filename} (Backtick execution operator detected in PHP/Blade directive)"; + continue; + } + continue; }2.
tests/Unit/ThemesControllerSecurityTest.phpTest — Penambahan Unit Test Keamanan untuk ThemeHooksValidator:
include,include_once,require,require_once).new $class(),$obj->$method(),Class::$method(),$$var, variable function dengan komentar).readfile,forward_static_call,register_shutdown_function,unserialize,scandir,glob)..blade.phppada arsip ZIP (lokasi, penolakan backtick pada arahan PHP/Blade, dan izin untuk JavaScript ES6 template literals).3.
tests/Feature/Security/ThemeUploadSecurityTest.phpTest — Penambahan Feature Test Keamanan untuk Aktivasi & Upload Tema:
hooks.php(memastikan redirect dengan error dan tidak membuat filerce_proof.txt).includedihooks.php..blade.phpdi luar direktoriresources/views/.✅ Test Cases yang Diimplementasikan
hooks.phpyang mengandung backtick execution operator (`id`atau`id > rce_proof.txt`) ditolak secara otomatis dan tidak dieksekusi.hooks.phpyang mengandung konstrukinclude,include_once,require, ataurequire_onceditolak secara otomatis.$f(),new $class(),$obj->$method(),Class::$method(), dan$$varditolak.readfile,forward_static_call,register_shutdown_function,unserialize,scandir,glob) ditolak..blade.phpdi luarresources/views/atau mengandung operator backtick di direktif PHP/Blade ditolak.ThemesControllerSecurityTest.phpdan 13 feature tests diThemeUploadSecurityTest.phplulus 100%.🤖 Cara Menjalankan Uji Coba Otomatis (Automated Test)
Untuk menjalankan seluruh rangkaian automated test keamanan yang ditambahkan pada PR ini, jalankan perintah berikut di terminal:
📸 Cara Menjalankan Uji Coba Manual
themes/default/hooks.phpdengan isi payload PoC:/setting/themes).themes/default/, pastikan berkasrce_proof.txtTIDAK pernah tercipta (perintah OS tidak dijalankan).themes/default/hooks.phpsetelah pengujian selesai.