Computer >> Hướng Dẫn Máy Tính >  >> Kết Nối Mạng >> An Ninh Mạng

Scripting (Kịch bản) trong An ninh mạng: Tự động hóa, Phát hiện & Rủi ro

Scripting là gì?

Scripting (Kịch bản/Lập trình kịch bản) là viết code bằng ngôn ngữ thông dịch (interpreted) như Python, PowerShell, Bash, JavaScript, Lua, Perl... để tự động hóa tác vụ, xử lý dữ liệu, điều khiển hệ thống – **không cần biên dịch (compile) thành file thực thi riêng**.

Vai trò kép của Scripting trong An ninh mạng

Mặt tích cực (Defense - Blue Team)Mặt tiêu cực (Offense - Red Team / Attacker)
Tự động hóa hardening (CIS Benchmark, STIG)Tự động hóa reconnaissance, exploit, lateral movement
Log parsing, correlation, alert enrichment (SIEM/SOAR)Obfuscation, evasion (AMSI bypass, living-off-the-land)
Vulnerability scanning, compliance checkingCredential dumping, token manipulation, persistence
Incident response: containment, eradication, forensic collectionC2 framework (Cobalt Strike, Sliver, Metasploit modules)
Threat hunting: query data lake, API threat intelPhishing kit, dropper, loader, ransomware encryption
Infrastructure as Code (IaC) security scanning (Terraform, K8s manifests)Supply chain attack: malicious npm/PyPI/GitHub Actions scripts

Ngôn ngữ Scripting phổ biến trong Security

  1. PowerShell: Native Windows, truy cập WMI/COM/.NET/Registry, AMSI integration,Constrained Language Mode. Dùng cho admin, IR, hunting, nhưng cũng là "vũ khí" số 1 của attacker trên Windows (Living off the Land).
  2. Python: Cross-platform, thư viện cực 풍부 (scapy, requests, pandas, pwntools, impacket, mitmproxy, docker SDK, cloud SDK). Chuẩn cho tooling security, malware analysis, automation.
  3. Bash/Shell: Linux/Unix native, pipeline mạnh, dùng cho hardening, log analysis, container escape, cron jobs.
  4. JavaScript/TypeScript (Node.js/Deno): Web app sec, browser automation (Puppeteer/Playwright), serverless security, AWS Lambda/GCP Cloud Functions.
  5. Lua: Nhúng trong Nginx (OpenResty/WAF), Redis, Wireshark, Nmap (NSE), game anti-cheat.
  6. Go (Golang): Biên dịch nhưng viết như script, hiệu năng cao, dùng viết tool security (nuclei, gitleaks, trivy, falco, cilium).

Kịch bản tấn công điển hình (Attack Script Lifecycle)

  1. Initial Access: Phishing email chứa macro (VBA) / HTA / ISO / LNK → chạy PowerShell/JS download payload.
  2. Execution: Script tải và thực thi shellcode/reflective DLL injection (Cobalt Strike Beacon, Sliver, Meterpreter).
  3. Persistence: Script tạo Scheduled Task, Run Key, WMI Event Subscription, Service, Startup folder.
  4. Privilege Escalation: Script枚举 token, exploit kernel, DLL hijack, UAC bypass.
  5. Credential Access: Script dump LSASS (comsvcs.dll, procdump), DCSync, browser credential, Wi-Fi profile.
  6. Discovery/Lateral Movement: BloodHound (SharpHound - C#), PowerView, ADModule, SMB relay, Pass-the-Hash.
  7. Collection/Exfil: Script nén, mã hóa, upload qua DNS/HTTPS/Cloud storage API.

Bảo vệ chống Script độc hại (Script Security Controls)

1. PowerShell Hardening

  • Bật **PowerShell Script Block Logging** (Event ID 4104) + **Module Logging** (4103) + **Transcription** (4105).
  • Bật **AMSI (Antimalware Scan Interface)** – Defender/EDR quét script runtime.
  • Enforce **Constrained Language Mode** cho user thường (AppLocker/WDAC).
  • Block `powershell.exe` không cần thiết (AppLocker/WDAC/SRPs), chỉ cho phép signed script.
  • Disable `Invoke-Expression (IEX)`, `DownloadString`, `Invoke-WebRequest` qua GPO/EDR rule.

2. Application Control (AppLocker / WDAC / SELinux / Gatekeeper)

  • Chỉ cho phép script đã ký (code signing cert) chạy.
  • Block script từ temp, download, network share, user profile.
  • Audit mode trước → Enforce sau.

3. Macro & Script Office

  • Block macro từ Internet (Office 2016+ default).
  • Disable VBA nếu không cần (Group Policy).
  • Protected View cho file nguồn ngoài.

4. Network & Email Filtering

  • Block download `.ps1`, `.py`, `.sh`, `.bat`, `.cmd`, `.js`, `.vbs`, `.wsf`, `.hta`, `.jse` từ web/email (proxy, firewall, email gateway).
  • Sandbox execution (Cuckoo, Joe Sandbox, ANY.RUN, Hybrid Analysis) cho file đính kèm/link.

5. EDR / XDR Behavioral Detection

  • Detect: `powershell -enc`, `Invoke-Expression`, `DownloadString`, `ReflectiveInjection`, `LSASS access`, `WMI process creation`, `Scheduled Task creation`, `Service install`.
  • Response: Kill process, isolate host, block hash, rollback.

Best Practice cho Blue Team viết Script an toàn

  • Ký số (Code Signing) mọi script production.
  • Lưu trong repo Git (private) + CI/CD pipeline (lint, test, sign, deploy).
  • Parameter hóa (không hardcode secret – dùng Vault, Key Vault, AWS Secrets Manager).
  • Logging có cấu trúc (JSON) → SIEM.
  • Error handling, retry, idempotency, dry-run mode.
  • Document: purpose, input, output, dependency, risk, rollback.
  • Code review bắt buộc (pull request).
  • Test trên staging trước production.

Kết luận

Scripting là **dao hai lưỡi** trong an ninh mạng. Nắm vững scripting (đặc biệt PowerShell, Python) là **kỹ năng bắt buộc** cho Security Engineer, SOC Analyst, Threat Hunter, Incident Responder, Red Teamer. Tổ chức phải: Khuyến khích scripting an toàn cho Blue Team + Kiểm soát chặt chẽ thực thi script không tin cậy (AppLocker/WDAC/AMSI/EDR/Email Proxy).