
Approfondimento didattico sulla protezione integrata di Apple contro il malware: database delle firme, regole YARA, integrazione con Gatekeeper, binari di remediation e flusso di rilevamento di macOS.
Continuando la mia serie di articoli sull'introduzione a macOS, ho deciso di dedicare un breve articolo a XProtect.
XProtect è il sistema antivirus e di firme malware integrato di Apple per macOS.
Opera come parte del XProtectService, che analizza applicazioni e altri contenuti eseguibili alla ricerca di firme malware note.
XProtect funziona in background e viene aggiornato silenziosamente da Apple tramite il meccanismo XProtectRemediator (ne parleremo più avanti).
Ha 3 funzioni principali:
La directory /Library/Apple/System/Library/CoreServices/XProtect.bundle è il bundle principale che contiene la configurazione di XProtect e le definizioni delle firme.
Si tratta di una directory di sistema di sola lettura, aggiornata silenziosamente da Apple tramite gli aggiornamenti di XProtect.
Al suo interno possiamo trovare alcuni file di interesse, tutti aggiornati periodicamente da Apple e protetti da System Integrity Protection (SIP).
Il file /Library/Apple/System/Library/CoreServices/XProtect.bundle/Contents/Resources/XProtect.plist memorizza le firme malware utilizzate da XProtect per rilevare le minacce note.
Contiene voci che mappano famiglie di malware a specifiche regole di rilevamento, inclusi hash e pattern nei nomi dei file.
Ecco un esempio di una famiglia di malware - Bundalore:
<dict>
<key>Description</key>
<string>OSX.Bundlore.D</string>
<key>LaunchServices</key>
<dict>
<key>LSItemContentType</key>
<string>com.apple.application-bundle</string>
</dict>
<key>Matches</key>
<array>
<dict>
<key>MatchFile</key>
<dict>
<key>NSURLTypeIdentifierKey</key>
<string>com.apple.applescript.script</string>
</dict>
<key>MatchType</key>
<string>Match</string>
<key>Pattern</key>
<string>46617364554153</string>
</dict>
<dict>
<key>MatchFile</key>
<dict>
<key>NSURLTypeIdentifierKey</key>
<string>com.apple.applescript.script</string>
</dict>
<key>MatchType</key>
<string>Match</string>
<key>Pattern</key>
<string>20006500630068006F002000</string>
</dict>
<dict>
<key>MatchFile</key>
<dict>
<key>NSURLTypeIdentifierKey</key>
<string>com.apple.applescript.script</string>
</dict>
<key>MatchType</key>
<string>Match</string>
<key>Pattern</key>
<string>20007C0020006F00700065006E00730073006C00200065006E00630020002D006100650073002D003200350036002D0063006600620020002D007000610073007300200070006100730073003A</string>
</dict>
<dict>
<key>MatchFile</key>
<dict>
<key>NSURLTypeIdentifierKey</key>
<string>com.apple.applescript.script</string>
</dict>
<key>MatchType</key>
<string>Match</string>
<key>Pattern</key>
<string>002D00730061006C00740020002D00410020002D00610020002D00640020007C002000620061007300680020002D0073</string>
</dict>
</array>
</dict>
È abbastanza leggibile dall'uomo; l'unica parte degna di nota è che l'argomento string in ogni corrispondenza è una rappresentazione esadecimale, ad esempio 002D00730061006C00740020002D00410020002D00610020002D00640020007C002000620061007300680020002D0073 corrisponde a -salt -A -a -d | bash -s.
Questo, ovviamente, è una miniera d'oro per gli autori di malware, che sanno esattamente quali pattern evitare.
Il file /Library/Apple/System/Library/CoreServices/XProtect.bundle/XProtect.meta.plist è un file di metadati che definisce regole aggiuntive per XProtect, incluse le policy di applicazione e le informazioni sulla versione.
Specifica quali versioni di macOS applicano determinate regole di XProtect e le azioni intraprese al momento del rilevamento, oltre alle blacklist per i plugin.
Ecco un esempio:
<key>JavaWebComponentVersionMinimum</key>
<string>1.6.0_45-b06-451</string>
<key>PlugInBlacklist</key>
<dict>
<key>10</key>
<dict>
<key>com.apple.java.JavaAppletPlugin</key>
<dict>
<key>MinimumPlugInBundleVersion</key>
<string>14.8.0</string>
<key>PlugInUpdateAvailable</key>
<true/>
</dict>
<key>com.apple.java.JavaPlugin2_NPAPI</key>
<dict>
<key>MinimumPlugInBundleVersion</key>
<string>14.8.0</string>
<key>PlugInUpdateAvailable</key>
<true/>
</dict>
<key>com.macromedia.Flash Player ESR.plugin</key>
<dict>
<key>MinimumPlugInBundleVersion</key>
<string>18.0.0.382</string>
<key>PlugInUpdateAvailable</key>
<true/>
</dict>
<key>com.macromedia.Flash Player.plugin</key>
<dict>
<key>MinimumPlugInBundleVersion</key>
<string>32.0.0.101</string>
<key>PlugInUpdateAvailable</key>
<true/>
</dict>
<key>com.microsoft.SilverlightPlugin</key>
<dict>
<key>MinimumPlugInBundleVersion</key>
<string>5.1.41212.0</string>
<key>PlugInUpdateAvailable</key>
<true/>
</dict>
<key>com.oracle.java.JavaAppletPlugin</key>
<dict>
<key>MinimumPlugInBundleVersion</key>
<string>1.8.51.16</string>
<key>PlugInUpdateAvailable</key>
<true/>
</dict>
</dict>
</dict>
Come puoi vedere, contengono, ad esempio, informazioni sulla versione per i plugin "in blacklist".
Nelle versioni recenti, XProtect sembra aver iniziato a supportare YARA.
Il file /Library/Apple/System/Library/CoreServices/XProtect.bundle/Contents/Resources/XProtect.yara contiene diverse regole YARA in formato testuale; ecco un breve esempio tratto da esso:
rule XProtect_MACOS_SLEEPYSTEGOSAURUS_SYM {
meta:
description = "MACOS.SLEEPYSTEGOSAURUS.SYM"
uuid = "BB4F7D16-C939-4047-A9AF-E74E7B51FAC1"
strings:
$a1 = { 45 78 65 63 43 6D 64 }
$a2 = { 47 65 74 48 6F 73 74 49 6E 66 6F }
$a3 = { 52 75 6E 53 63 72 69 70 74 }
$a4 = { 52 75 6E 53 63 72 69 70 74 55 72 6C }
$a5 = { 4C 61 75 6E 63 68 50 6C 69 73 74 }
$a6 = { 43 68 65 63 6B 50 72 6F 63 65 73 73 }
$a7 = { 43 68 65 63 6B 49 6E }
$a8 = { 52 75 6E 43 6D 64 46 69 6C 65 }
$a9 = { 53 68 6F 77 48 74 6D 6C }
$a10 = { 50 6C 69 73 74 48 65 6C 70 65 72 }
$a11 = { 4C 61 75 6E 63 68 64 48 65 6C 70 65 72 }
$a12 = { 43 6F 6D 6D 61 6E 64 46 69 6C 65 }
$a13 = { 57 72 69 74 65 50 6C 69 73 74 }
$a14 = { 4A 53 4F 4E 46 69 6C 65 50 72 6F 63 65 73 73 6F 72 }
$a15 = { 43 68 72 6F 6D 65 48 65 6C 70 65 72 }
$a16 = { 53 61 6E 64 62 6F 78 65 72 }
condition:
Macho and filesize < 2MB and all of them
}
Questo non è un articolo sulle regole YARA, ma come prima - questa è una miniera d'oro per gli autori di malware (ad esempio 43 68 65 63 6B 50 72 6F 63 65 73 73 è CheckProcess).
Qui puoi vedere come Apple ha progettato XProtect per essere integrato con Gatekeeper - anche se questo file è usato principalmente da syspolicyd.
Il file /Library/Apple/System/Library/CoreServices/XProtect.bundle/Contents/Resources/gk.db è un database SQLite che contiene una "blacklist" di hash di file e team ID da bloccare.
Puoi visualizzarlo con l'utility sqlite3:
jbo@McJbo ~ $ sqlite3 "/Library/Apple/System/Library/CoreServices/XProtect.bundle/Contents/Resources/gk.db" .schema
CREATE TABLE settings (name TEXT, value TEXT, PRIMARY KEY (name));
CREATE TABLE blocked_hashes (hash BLOB, hash_type INTEGER, flags INTEGER, PRIMARY KEY (hash, hash_type));
CREATE TABLE blocked_teams (team_id TEXT, flags INTEGER, PRIMARY KEY (team_id));
jbo@McJbo ~ $ sqlite3 "/Library/Apple/System/Library/CoreServices/XProtect.bundle/Contents/Resources/gk.db" "SELECT * FROM blocked_teams LIMIT 5;"
F9X83Q5222|1
Q6XAB4776L|0
DK5C9Y86C8|0
8VK2WEPW22|0
5LWMEF3EX3|0
jbo@McJbo ~ $
Anche questa è un'informazione interessante per gli autori di malware, ad esempio - per sapere quando il team ID usato per firmare il loro malware è sul radar di Apple.
Mentre il binario XProtect si trova in /Library/Apple/System/Library/CoreServices/XProtect.app/Contents/MacOS/, lì troverai anche altri binari.
Si tratta di file di remediation per famiglie specifiche:
jbo@McJbo ~ $ ls -l /Library/Apple/System/Library/CoreServices/XProtect.app/Contents/MacOS/
total 47256
-rwxr-xr-x 1 root wheel 513696 Feb 27 16:54 XProtect
-rwxr-xr-x 1 root wheel 2498784 Feb 27 16:54 XProtectRemediatorAdload
-rwxr-xr-x 1 root wheel 2367952 Feb 27 16:54 XProtectRemediatorBadGacha
-rwxr-xr-x 1 root wheel 2053968 Feb 27 16:54 XProtectRemediatorBlueTop
-rwxr-xr-x 1 root wheel 2332752 Feb 27 16:54 XProtectRemediatorBundlore
-rwxr-xr-x 1 root wheel 2299648 Feb 27 16:54 XProtectRemediatorCardboardCutout
-rwxr-xr-x 1 root wheel 2333248 Feb 27 16:54 XProtectRemediatorColdSnap
-rwxr-xr-x 1 root wheel 2052736 Feb 27 16:54 XProtectRemediatorCrapyrator
-rwxr-xr-x 1 root wheel 2859040 Feb 27 16:54 XProtectRemediatorDolittle
-rwxr-xr-x 1 root wheel 2205520 Feb 27 16:54 XProtectRemediatorDubRobber
-rwxr-xr-x 1 root wheel 2334752 Feb 27 16:54 XProtectRemediatorEicar
-rwxr-xr-x 1 root wheel 2053120 Feb 27 16:54 XProtectRemediatorFloppyFlipper
-rwxr-xr-x 1 root wheel 2053616 Feb 27 16:54 XProtectRemediatorGenieo
-rwxr-xr-x 1 root wheel 2053504 Feb 27 16:54 XProtectRemediatorGreenAcre
-rwxr-xr-x 1 root wheel 2332800 Feb 27 16:54 XProtectRemediatorKeySteal
-rwxr-xr-x 1 root wheel 4659088 Feb 27 16:54 XProtectRemediatorMRTv3
-rwxr-xr-x 1 root wheel 2382448 Feb 27 16:54 XProtectRemediatorPirrit
-rwxr-xr-x 1 root wheel 2333024 Feb 27 16:54 XProtectRemediatorRankStank
-rwxr-xr-x 1 root wheel 2299728 Feb 27 16:54 XProtectRemediatorRoachFlight
-rwxr-xr-x 1 root wheel 2384416 Feb 27 16:54 XProtectRemediatorSheepSwap
-rwxr-xr-x 1 root wheel 2053232 Feb 27 16:54 XProtectRemediatorSnowBeagle
-rwxr-xr-x 1 root wheel 2350624 Feb 27 16:54 XProtectRemediatorSnowDrift
-rwxr-xr-x 1 root wheel 2019712 Feb 27 16:54 XProtectRemediatorToyDrop
-rwxr-xr-x 1 root wheel 2105696 Feb 27 16:54 XProtectRemediatorTrovi
-rwxr-xr-x 1 root wheel 2332768 Feb 27 16:54 XProtectRemediatorWaterNet
jbo@McJbo ~ $
Una nuova applicazione di sistema XProtect, salvata come /Library/Apple/System/Library/CoreServices/XProtect.app, è stata introdotta in macOS Monterey ed è responsabile dell'esecuzione di XProtect Remediator.
A differenza del XProtect tradizionale (che usava principalmente il rilevamento basato su firme), XProtect Remediator analizza e rimuove attivamente il malware dai sistemi infetti.
Viene eseguito come processo in background e può rimuovere automaticamente le minacce rilevate senza l'intervento dell'utente.
La directory /Library/Apple/System/Library/CoreServices/XProtect.app/Contents/Resources contiene script di remediation e logica di rilevamento aggiuntiva per la scansione attiva.
Il file /Library/Apple/System/Library/CoreServices/XProtect.app/Contents/Resources/com.apple.XProtect.agent.scan.plist contiene le impostazioni relative a quando eseguire le scansioni periodiche.
Ad esempio:
<key>com.apple.XProtect.PluginService.agent.slow.scan</key>
<dict>
<key>Repeating</key>
<true/>
<key>PowerNap</key>
<true/>
<key>CPUIntensive</key>
<true/>
<key>DiskIntensive</key>
<true/>
<key>AllowBattery</key>
<false/>
<key>Priority</key>
<string>Utility</string>
<key>Interval</key>
<integer>604800</integer>
</dict>
Questo mostra alcune condizioni sulle prestazioni e persino il periodo di scansione (604800 secondi = una volta ogni 7 giorni).
Alcuni di voi potrebbero aver sentito parlare di MRT (Malware Removal Tool).
Situata in /Library/Apple/System/Library/CoreServices/MRT.app, MRT è un altro componente di sicurezza di macOS che lavora insieme a XProtect.
È responsabile della rimozione del malware rilevato da XProtect e opera silenziosamente in background.
MRT è più aggressivo di XProtect nell'eradicare le minacce rilevate e può rimuovere file maliziosi anche se sono attualmente in esecuzione.
Nota che, a differenza di XProtect, MRT non mantiene file di configurazione - tutto è incorporato nel binario.
Anche la semplice analisi delle stringhe del binario principale (/Library/Apple/System/Library/CoreServices/MRT.app/Contents/MacOS/MRT) rivela stringhe interessanti, ad esempio:
import sys,base64;exec(base64.b64decode('
import sys,base64,warnings;warnings.filterwarnings('ignore');exec(base64.b64decode('
import sys;import re, subprocess;cmd = "ps -ef | grep Little\ Snitch | grep -v grep"
Quando un file viene scaricato tramite un browser o un'applicazione (ad esempio Safari, Mail, Messaggi), viene marcato con un flag di quarantena (com.apple.quarantine) - ho già menzionato quel flag nel mio precedente articolo su Gatekeeper.
Gatekeeper verifica se il file è firmato e notarizzato. Se non lo è, l'utente riceve un avviso prima dell'esecuzione.
Se l'esecuzione è consentita, XProtect analizza il file confrontandolo con le sue firme malware note (da XProtect.plist, XProtect.yara e così via).
Se viene rilevato malware, il sistema impedisce l'esecuzione. Se il malware è noto e può essere bonificato, XProtect Remediator o MRT possono eliminarlo o neutralizzarlo.
Apple aggiorna XProtect, MRT e XProtect Remediator silenziosamente in background tramite il processo XProtectService.
All'inizio di quest'anno, CheckPoint ha pubblicato un articolo su un campione di malware chiamato Banshee.
Ha attirato parecchia attenzione perché utilizzava lo stesso algoritmo di crittografia che i binari di XProtect Remediator usano per "nascondere le loro regole YARA".
L'algoritmo in sé è semplice:
def macos_xprotect_string_decryption(encrypted: bytes, encr_key: int) -> str:
"""
Author: @Check Point Research
Decrypts MacOS Xprotect binaries & Banshee Stealer encrypted strings.
"""
decrypted = "".join(
chr(
(encr_key >> ((i * 8) & 0x38) & 0xFF) ^ encrypted[i]
)
for i in range(len(encrypted))
)
return decrypted.partition("\\x00")[0]
Ovviamente gli autori di malware pensavano che questo algoritmo non potesse essere rilevato tramite firme statiche (poiché è garantito che generi falsi positivi).
Ho trovato un codice simile nella maggior parte dei binari di remediation, ad esempio:
10000592a if (data_1000ffe7e != 0)
10000592c void* rax_1 = &data_1000fb0a0
100005933 int64_t i = 0
100005951 do
100005940 *rax_1 = *rax_1 ^ (0x363a34363a303900 u>> (i.b & 0x38)).b
100005943 i = i + 8
100005947 rax_1 = rax_1 + 1
100005951 while (i != 0x26ef0)
100005953 data_1000ffe7e = 0
Il ricercatore di sicurezza che inizialmente ha scoperto tutto ciò (ald3ns) ha rilasciato uno strumento chiamato XPR-dump che sembra funzionare ancora sulla versione più recente di XProtect. Dovresti assolutamente dargli un'occhiata!
Il principale insegnamento di questo articolo è che XProtect gioca a un gioco del gatto col topo con gli attaccanti (proprio come qualsiasi antivirus) - blacklist, esecuzione di YARA o semplici firme statiche rientrano tutte nel campo d'azione.
Il fatto che questi file siano distribuiti su tutti gli endpoint e siano facili da analizzare rende piuttosto semplice per gli autori di malware trovare bypass facili a molti di questi controlli.
Resta sintonizzato!
Jonathan Bar Or (https://jonathanbaror.com)