انتقل إلى المحتوى الرئيسي

المختبر 8.4: سجل Windows والاستمرارية

أهداف التعلم​

في نهاية هذا المختبر، ستكون قادرًا على:

  • التنقل والتعامل مع سجل Windows
  • فهم المفاتيح المهمة للأمان
  • اكتشاف التعديلات المشبوهة في السجل
  • تحديد آليات الاستمرارية عبر السجل

المدة المقدرة​

50 دقيقة

المتطلبات المسبقة​

  • VM Windows 10/11
  • حقوق المسؤول
  • المختبر 7.2 مكتمل (الاستمرارية)

السياق المهني​

سجل Windows هو:

  • قاعدة البيانات المركزية للتكوين
  • هدف مفضل لاستمرارية البرمجيات الخبيثة
  • عنصر أساسي يجب مراقبته في الأمان

الجزء 1: التنقل في السجل​

الخطوة 1.1: هيكل السجل​

السجل منظم في خلايا (hives):

الخليةالاسم المستعارالمحتوى
HKEY_LOCAL_MACHINEHKLMتكوين النظام
HKEY_CURRENT_USERHKCUتكوين المستخدم الحالي
HKEY_USERSHKUجميع ملفات تعريف المستخدمين
HKEY_CLASSES_ROOTHKCRارتباطات الملفات
HKEY_CURRENT_CONFIGHKCCالتكوين المادي الحالي

الخطوة 1.2: الوصول عبر PowerShell​

# الخلايا متاحة كأقراص
Get-PSDrive -PSProvider Registry

# التنقل
Set-Location HKLM:\SOFTWARE
Get-ChildItem

# العودة
Set-Location C:\

الخطوة 1.3: قراءة القيم​

# قراءة مفتاح كامل
Get-ItemProperty "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion"

# قراءة قيمة محددة
(Get-ItemProperty "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion").ProductName

# قيمة مع Get-ItemPropertyValue
Get-ItemPropertyValue "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion" -Name ProductName

الخطوة 1.4: تعديل السجل​

# إنشاء مفتاح
New-Item -Path "HKCU:\Software\LabTest" -Force

# إنشاء/تعديل قيمة
Set-ItemProperty -Path "HKCU:\Software\LabTest" -Name "TestValue" -Value "Hello World"
Set-ItemProperty -Path "HKCU:\Software\LabTest" -Name "TestNumber" -Value 42 -Type DWord

# التحقق
Get-ItemProperty "HKCU:\Software\LabTest"

# الحذف
Remove-ItemProperty -Path "HKCU:\Software\LabTest" -Name "TestValue"
Remove-Item -Path "HKCU:\Software\LabTest" -Recurse

السؤال 1: لماذا نستخدم -Type DWord للأرقام؟


الجزء 2: مفاتيح الأمان المهمة​

الخطوة 2.1: مفاتيح Run (التشغيل التلقائي)​

هذه المفاتيح تشغل البرامج تلقائيًا:

# عرض جميع مفاتيح Run
Write-Host "=== مفاتيح RUN ===" -ForegroundColor Cyan

$runKeys = @(
"HKCU:\Software\Microsoft\Windows\CurrentVersion\Run",
"HKCU:\Software\Microsoft\Windows\CurrentVersion\RunOnce",
"HKLM:\Software\Microsoft\Windows\CurrentVersion\Run",
"HKLM:\Software\Microsoft\Windows\CurrentVersion\RunOnce",
"HKLM:\Software\Microsoft\Windows\CurrentVersion\RunServices",
"HKLM:\Software\Microsoft\Windows\CurrentVersion\RunServicesOnce"
)

foreach ($key in $runKeys) {
Write-Host "`n$key" -ForegroundColor Yellow
if (Test-Path $key) {
Get-ItemProperty $key | Format-List
} else {
Write-Host " (غير موجود)"
}
}

الخطوة 2.2: مفاتيح الخدمات​

# الخدمات المسجلة
Get-ChildItem "HKLM:\SYSTEM\CurrentControlSet\Services" |
Select-Object -First 10 |
ForEach-Object {
$svc = Get-ItemProperty $_.PSPath
[PSCustomObject]@{
Name = $_.PSChildName
DisplayName = $svc.DisplayName
ImagePath = $svc.ImagePath
Start = $svc.Start
}
} | Format-Table -AutoSize

الخطوة 2.3: مفاتيح الأمان​

# إعدادات الأمان LSA
Write-Host "=== LSA Security ===" -ForegroundColor Cyan
Get-ItemProperty "HKLM:\SYSTEM\CurrentControlSet\Control\Lsa" -ErrorAction SilentlyContinue

# RunAsPPL (حماية LSASS)
$runAsPPL = Get-ItemProperty "HKLM:\SYSTEM\CurrentControlSet\Control\Lsa" -Name "RunAsPPL" -ErrorAction SilentlyContinue
if ($runAsPPL.RunAsPPL -eq 1) {
Write-Host "LSA Protection: ACTIVE" -ForegroundColor Green
} else {
Write-Host "LSA Protection: INACTIVE" -ForegroundColor Red
}

# WDigest (بيانات الاعتماد بنص واضح)
Write-Host "`n=== WDigest ===" -ForegroundColor Cyan
$wdigest = Get-ItemProperty "HKLM:\SYSTEM\CurrentControlSet\Control\SecurityProviders\WDigest" -Name "UseLogonCredential" -ErrorAction SilentlyContinue
if ($wdigest.UseLogonCredential -eq 1) {
Write-Host "WDigest credentials en clair: ACTIVE (DANGER!)" -ForegroundColor Red
} else {
Write-Host "WDigest credentials en clair: DESACTIVE" -ForegroundColor Green
}

الخطوة 2.4: ارتباطات الملفات​

# التحقق من الارتباطات (تستخدم للاختطاف)
Write-Host "=== ارتباطات خطيرة ===" -ForegroundColor Cyan

$extensions = @(".exe", ".bat", ".cmd", ".ps1", ".vbs", ".js")

foreach ($ext in $extensions) {
$assoc = (Get-ItemProperty "HKCR:\$ext" -ErrorAction SilentlyContinue).'(default)'
if ($assoc) {
$command = (Get-ItemProperty "HKCR:\$assoc\shell\open\command" -ErrorAction SilentlyContinue).'(default)'
Write-Host "$ext -> $assoc -> $command"
}
}

الجزء 3: اكتشاف الاستمرارية​

الخطوة 3.1: سكربت اكتشاف كامل​

<#
.SYNOPSIS
اكتشاف آليات الاستمرارية عبر السجل
#>

Write-Host @"
==============================================
اكتشاف الاستمرارية في السجل
==============================================
"@ -ForegroundColor Cyan

$findings = @()

# 1. مفاتيح Run
$runKeys = @(
"HKCU:\Software\Microsoft\Windows\CurrentVersion\Run",
"HKCU:\Software\Microsoft\Windows\CurrentVersion\RunOnce",
"HKLM:\Software\Microsoft\Windows\CurrentVersion\Run",
"HKLM:\Software\Microsoft\Windows\CurrentVersion\RunOnce"
)

Write-Host "`n[1/5] تحليل مفاتيح Run..." -ForegroundColor Yellow

foreach ($key in $runKeys) {
if (Test-Path $key) {
$props = Get-ItemProperty $key -ErrorAction SilentlyContinue
$props.PSObject.Properties | Where-Object {
$_.Name -notmatch "^PS"
} | ForEach-Object {
$value = $_.Value

# معايير الشك
$suspicious = $false
$reasons = @()

if ($value -match "\\Temp\\|\\AppData\\Local\\Temp") {
$suspicious = $true
$reasons += "مسار مؤقت"
}
if ($value -match "-[Ee]ncoded|-[Ww]indowStyle\s+[Hh]idden") {
$suspicious = $true
$reasons += "وسائط مشبوهة"
}
if ($value -match "powershell.*-[Cc]ommand") {
$suspicious = $true
$reasons += "PowerShell inline"
}

$findings += [PSCustomObject]@{
Type = "Run Key"
Location = $key
Name = $_.Name
Value = $value.Substring(0, [Math]::Min(80, $value.Length))
Suspicious = $suspicious
Reasons = ($reasons -join ", ")
}
}
}
}

# 2. الخدمات في مسارات مشبوهة
Write-Host "[2/5] تحليل الخدمات..." -ForegroundColor Yellow

Get-ChildItem "HKLM:\SYSTEM\CurrentControlSet\Services" -ErrorAction SilentlyContinue | ForEach-Object {
$svc = Get-ItemProperty $_.PSPath -ErrorAction SilentlyContinue
if ($svc.ImagePath -match "\\Temp\\|\\AppData\\|\\Downloads\\") {
$findings += [PSCustomObject]@{
Type = "Service"
Location = $_.PSPath
Name = $_.PSChildName
Value = $svc.ImagePath
Suspicious = $true
Reasons = "مسار مشبوه"
}
}
}

# 3. ملحقات Shell
Write-Host "[3/5] تحليل ملحقات Shell..." -ForegroundColor Yellow

$shellExt = @(
"HKCU:\Software\Classes\*\shellex\ContextMenuHandlers",
"HKLM:\Software\Classes\*\shellex\ContextMenuHandlers"
)

foreach ($key in $shellExt) {
if (Test-Path $key) {
Get-ChildItem $key -ErrorAction SilentlyContinue | ForEach-Object {
$handler = Get-ItemProperty $_.PSPath -ErrorAction SilentlyContinue
$clsid = $handler.'(default)'

if ($clsid -and $clsid -match "^\{") {
# التحقق مما إذا كان CLSID موجودًا
$clsidPath = "HKCR:\CLSID\$clsid"
if (-not (Test-Path $clsidPath)) {
$findings += [PSCustomObject]@{
Type = "Shell Extension"
Location = $_.PSPath
Name = $_.PSChildName
Value = $clsid
Suspicious = $true
Reasons = "CLSID مفقود"
}
}
}
}
}
}

# 4. AppInit_DLLs
Write-Host "[4/5] التحقق من AppInit_DLLs..." -ForegroundColor Yellow

$appInit = Get-ItemProperty "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Windows" -Name "AppInit_DLLs" -ErrorAction SilentlyContinue
if ($appInit.AppInit_DLLs) {
$findings += [PSCustomObject]@{
Type = "AppInit_DLLs"
Location = "HKLM:\...\Windows"
Name = "AppInit_DLLs"
Value = $appInit.AppInit_DLLs
Suspicious = $true
Reasons = "حقن DLL عند البدء"
}
}

# 5. خيارات تنفيذ ملفات الصور (Debugger)
Write-Host "[5/5] التحقق من IFEO..." -ForegroundColor Yellow

Get-ChildItem "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Image File Execution Options" -ErrorAction SilentlyContinue | ForEach-Object {
$ifeo = Get-ItemProperty $_.PSPath -ErrorAction SilentlyContinue
if ($ifeo.Debugger) {
$findings += [PSCustomObject]@{
Type = "IFEO Debugger"
Location = $_.PSPath
Name = $_.PSChildName
Value = $ifeo.Debugger
Suspicious = $true
Reasons = "Debugger محدد"
}
}
}

# تقرير
Write-Host "`n==============================================`n" -ForegroundColor Cyan

$suspiciousFindings = $findings | Where-Object { $_.Suspicious }

if ($suspiciousFindings.Count -eq 0) {
Write-Host "[OK] لم يتم اكتشاف استمرارية مشبوهة" -ForegroundColor Green
} else {
Write-Host "[!] $($suspiciousFindings.Count) عنصر(عناصر) مشبوه(ة) مكتشف(ة):" -ForegroundColor Red
$suspiciousFindings | Format-Table Type, Name, Reasons -AutoSize

Write-Host "`nالتفاصيل:" -ForegroundColor Yellow
$suspiciousFindings | ForEach-Object {
Write-Host "`n النوع: $($_.Type)" -ForegroundColor Cyan
Write-Host " الاسم: $($_.Name)"
Write-Host " القيمة: $($_.Value)"
Write-Host " الأسباب: $($_.Reasons)" -ForegroundColor Yellow
}
}

# ملخص الإدخالات العادية
Write-Host "`n==============================================`n" -ForegroundColor Cyan
$normalFindings = $findings | Where-Object { -not $_.Suspicious }
Write-Host "الإدخالات العادية المكتشفة: $($normalFindings.Count)"

السؤال 2: هل اكتشف السكربت عناصر مشبوهة على جهاز VM الخاص بك؟


الجزء 4: تمرين عملي​

الخطوة 4.1: إنشاء استمرارية اختبار​

# انتباه: لأغراض الاختبار فقط!
# أنشئ إدخال Run لفهم الآلية

# إنشاء إدخال غير ضار
Set-ItemProperty `
-Path "HKCU:\Software\Microsoft\Windows\CurrentVersion\Run" `
-Name "LabTestPersistence" `
-Value "cmd.exe /c echo Test de persistence > C:\Temp\persistence_test.txt"

Write-Host "تم إنشاء إدخال الاختبار" -ForegroundColor Yellow

الخطوة 4.2: اكتشاف الاستمرارية​

# نفذ السكربت للكشف
# يجب أن يتم اكتشاف إدخالك

# التحقق يدويًا
Get-ItemProperty "HKCU:\Software\Microsoft\Windows\CurrentVersion\Run" | Select-Object LabTestPersistence

الخطوة 4.3: التنظيف​

# حذف إدخال الاختبار
Remove-ItemProperty `
-Path "HKCU:\Software\Microsoft\Windows\CurrentVersion\Run" `
-Name "LabTestPersistence" `
-ErrorAction SilentlyContinue

Write-Host "تم الانتهاء من التنظيف" -ForegroundColor Green

الجزء 5: النسخ الاحتياطي والاستعادة​

الخطوة 5.1: تصدير مفتاح​

# تصدير مفتاح
$exportPath = "$env:USERPROFILE\Desktop\backup_run_keys.reg"
reg export "HKCU\Software\Microsoft\Windows\CurrentVersion\Run" $exportPath /y

Write-Host "تم تصدير المفتاح إلى: $exportPath" -ForegroundColor Green

الخطوة 5.2: سكربت النسخ الاحتياطي التلقائي​

# نسخ احتياطي للمفاتيح الأمنية المهمة
$backupFolder = "$env:USERPROFILE\Desktop\RegistryBackup_$(Get-Date -Format 'yyyyMMdd')"
New-Item -Path $backupFolder -ItemType Directory -Force | Out-Null

$keysToBackup = @(
@{Path = "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Run"; Name = "HKLM_Run"},
@{Path = "HKCU\Software\Microsoft\Windows\CurrentVersion\Run"; Name = "HKCU_Run"},
@{Path = "HKLM\SYSTEM\CurrentControlSet\Control\Lsa"; Name = "LSA"},
@{Path = "HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Windows"; Name = "Windows"}
)

foreach ($key in $keysToBackup) {
$file = Join-Path $backupFolder "$($key.Name).reg"
reg export $key.Path $file /y 2>$null
if ($?) {
Write-Host "[OK] $($key.Name)" -ForegroundColor Green
} else {
Write-Host "[ERREUR] $($key.Name)" -ForegroundColor Red
}
}

Write-Host "`nتم الانتهاء من النسخ الاحتياطي: $backupFolder" -ForegroundColor Cyan

التسليمات​

  1. الإجابات على السؤالين

  2. لقطات شاشة:

    • التنقل في السجل
    • سكربت الكشف منفذ
    • استمرارية الاختبار تم إنشاؤها واكتشافها
  3. سكربت الكشف مخصص

  4. تقرير (صفحة واحدة):

    • مفاتيح السجل المهمة
    • المخاطر المحددة
    • توصيات المراقبة

معايير التقييم​

المعيارالنقاط
التنقل في السجل20%
مفاتيح الأمان25%
سكربت الكشف35%
التمرين العملي20%

أوامر مرجعية​

# القراءة
Get-ItemProperty "HKLM:\chemin"
Get-ItemPropertyValue "HKLM:\chemin" -Name "valeur"

# الكتابة
Set-ItemProperty -Path "chemin" -Name "nom" -Value "valeur"
New-Item -Path "chemin" -Force

# الحذف
Remove-ItemProperty -Path "chemin" -Name "nom"
Remove-Item -Path "chemin" -Recurse

# التصدير (عبر reg.exe)
reg export "HKLM\chemin" fichier.reg /y
reg import fichier.reg

الموارد​


المختبر تم تطويره في إطار دورة أنظمة التشغيل - الوحدة 8