@echo off
chcp 65001 >nul
title KAPI ZIRH - Asena 6 Haneli Onay Kodlu Kaldirma Araci
color 0C

:: Yonetici (Administrator) Yetkisi Kontrolu
net session >nul 2>&1
if %errorLevel% neq 0 (
    echo [!] Yonetici yetkisi aliniyor...
    powershell -NoProfile -ExecutionPolicy Bypass -Command "Start-Process -FilePath '%~f0' -Verb RunAs"
    exit /b
)

echo ==========================================================================
echo   KAPI ZIRH - ASENA 6 HANELI YONETICI ONAY KODLU KALDIRMA ARACI
echo ==========================================================================
echo.
set "KAPI_SERVER=https://ajans.kfkhaber.com.tr:8443"
set "SHIELD_DIR=C:\ProgramData\KAPI_Shield"

echo [*] Yonetici telefonuna (Asena Telegram Botu) 6 haneli kaldirma onay kodu gonderiliyor...
powershell -NoProfile -ExecutionPolicy Bypass -Command "try { [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12; $u = ''; if (Test-Path 'C:\ProgramData\KAPI_Shield\config.json') { $cfg = Get-Content 'C:\ProgramData\KAPI_Shield\config.json' -Raw -Encoding UTF8 | ConvertFrom-Json; $u = [string]$cfg.assigned_user }; Invoke-RestMethod -Uri '%KAPI_SERVER%/api/masa-koruma/request-uninstall-code' -Method Post -Body (@{ pc_id = $u; assigned_user = $u; hostname = $env:COMPUTERNAME } | ConvertTo-Json) -ContentType 'application/json' -TimeoutSec 6 | Out-Null; exit 0 } catch { exit 1 }"
if %errorLevel% neq 0 (
    echo.
    echo [X] HATA: Sunucuya baglanilamadi! Cevrimdisi modda KAPI ZIRH kaldirilamaz!
    pause
    exit /b 1
)
echo [OK] Yoneticiye (Kubilay Bey - Asena Telegram) 6 haneli onay kodu gonderildi!
echo.
set "ASENA_CODE="
set /p ASENA_CODE="Lutfen Asena Telegram Botuna gelen 6 Haneli Onay Kodunu girin: "
if "%ASENA_CODE%"=="" (
    echo [X] Onay kodu girilmedi! Kaldirma islemi iptal edildi.
    timeout /t 3
    exit /b 1
)

echo [*] Asena 6 Haneli Onay Kodu dogrulaniyor...
powershell -NoProfile -ExecutionPolicy Bypass -Command "try { [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12; $u = ''; if (Test-Path 'C:\ProgramData\KAPI_Shield\config.json') { $cfg = Get-Content 'C:\ProgramData\KAPI_Shield\config.json' -Raw -Encoding UTF8 | ConvertFrom-Json; $u = [string]$cfg.assigned_user }; $r = Invoke-RestMethod -Uri '%KAPI_SERVER%/api/masa-koruma/verify-uninstall-code' -Method Post -Body (@{ pc_id = $u; assigned_user = $u; hostname = $env:COMPUTERNAME; code = '%ASENA_CODE%' } | ConvertTo-Json) -ContentType 'application/json' -TimeoutSec 6; if ($r.ok -and $r.verified) { exit 0 } else { exit 1 } } catch { exit 1 }"
if %errorLevel% neq 0 (
    echo.
    echo ==========================================================================
    echo   [X] YETKISIZ KALDIRMA GIRISIMI ENGELLENDI!
    echo   - Girdiginiz Asena Onay Kodu HATALI veya suresi dolmus!
    echo   - Bu deneme 5651 Siber Loglarina kaydedildi ve Yoneticiye bildirildi.
    echo ==========================================================================
    pause
    exit /b 1
)

echo [OK] Asena 6 Haneli Onay Kodu DOGRULANDI! Kaldirma islemi baslatiliyor...
echo.
echo [1/6] Sunucudan mevcut bilgisayar eslesmesi temizlendi.

echo [2/6] Zamanlanmis Koruma Gorevi (Watchdog) siliniyor...
schtasks /Delete /TN "KAPI_Masa_Koruma_Watchdog" /F >nul 2>&1
schtasks /Delete /TN "KAPI_Ortak_Siber_Kalkan_Senkron" /F >nul 2>&1

echo [3/6] Windows baslangic (Registry Run) ve Denetim Masasi kayitlari siliniyor...
reg delete "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Run" /v "KAPI_Masa_Koruma" /f >nul 2>&1
reg delete "HKCU\SOFTWARE\Microsoft\Windows\CurrentVersion\Run" /v "KAPI_Masa_Koruma" /f >nul 2>&1
reg delete "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\KAPI_Masa_Koruma" /f >nul 2>&1

echo [4/6] Arka planda calisan KAPI Zirh ve kilit ekrani surecleri durduruluyor...
powershell -NoProfile -ExecutionPolicy Bypass -Command "Get-CimInstance Win32_Process -Filter \"Name = 'wscript.exe' OR Name = 'cscript.exe'\" | Where-Object { $_.CommandLine -match 'KAPI_AutoStart' } | ForEach-Object { Stop-Process -Id $_.ProcessId -Force -ErrorAction SilentlyContinue }" >nul 2>&1
powershell -NoProfile -ExecutionPolicy Bypass -Command "Get-CimInstance Win32_Process -Filter \"Name = 'powershell.exe'\" | Where-Object { $_.CommandLine -match 'kapi_agent' } | ForEach-Object { Stop-Process -Id $_.ProcessId -Force -ErrorAction SilentlyContinue }" >nul 2>&1
timeout /t 1 /nobreak >nul

echo [5/6] Kurulum dosyalari ve onbellek (%SHIELD_DIR%) temizleniyor...
if exist "%SHIELD_DIR%" (
    del /f /q "%SHIELD_DIR%\config.json" >nul 2>&1
    del /f /q "%SHIELD_DIR%\cache_auth.json" >nul 2>&1
    del /f /q "%SHIELD_DIR%\kapi_agent.ps1" >nul 2>&1
    del /f /q "%SHIELD_DIR%\KAPI_AutoStart.vbs" >nul 2>&1
    del /f /q "%SHIELD_DIR%\*.png" >nul 2>&1
    del /f /q "%SHIELD_DIR%\*.jpg" >nul 2>&1
)

echo [6/6] Islem tamamlandi!
echo.
echo ==========================================================================
echo   [OK] KAPI ZIRH MASA KORUMA BU BILGISAYARDAN KOMPLE KALDIRILDI!
echo   - Artik sabah acilis kilidi veya otomatik masa kilidi calismayacaktir.
echo ==========================================================================
echo.
set "NEW_USER="
set /p NEW_USER="Bu bilgisayara hemen YENI bir Kullanici Kodu kurmak ister misiniz? (Orn: KFK09 yazin, sadece kaldirmak icin BOS birakip Enter'a basin): "
if not "%NEW_USER%"=="" (
    echo.
    echo [*] Yeni kullanici [%NEW_USER%] icin kurulum indiriliyor ve baslatiliyor...
    curl.exe -k -f -s -L "%KAPI_SERVER%/api/masa-koruma/download-setup?user_code=%NEW_USER%" -o "%TEMP%\KAPI_New_Setup.bat"
    if exist "%TEMP%\KAPI_New_Setup.bat" call "%TEMP%\KAPI_New_Setup.bat"
    exit /b 0
)

start "" cmd /c "ping 127.0.0.1 -n 2 >nul & rmdir /s /q ""C:\ProgramData\KAPI_Shield"" >nul 2>&1"
timeout /t 3
exit /b 0