cURL (Client URL) - Công cụ dòng lệnh truyền tải dữ liệu mạng
cURL viết tắt của "Client URL" (hoặc "Client URL Request Library"). Là công cụ dòng lệnh (CLI) và thư viện (libcurl) mã nguồn mở, cho phép truyền tải dữ liệu qua nhiều giao thức mạng (HTTP, HTTPS, FTP, FTPS, SCP, SFTP, TFTP, LDAP, SMB, SMTP, IMAP, POP3, MQTT...). Phát hành năm 1997 bởi Daniel Stenberg.
Vai trò trong An ninh mạng (Cybersecurity)
- Kiểm tra & Kiểm thử (Testing/Auditing): Quét header bảo mật (HSTS, CSP, X-Frame-Options), kiểm tra chứng chỉ SSL/TLS (expiry, SAN, cipher suites), test CORS, phương thức HTTP (OPTIONS, TRACE), enum endpoint API.
- Tự động hóa (Automation/Scripting): Tích hợp trong CI/CD pipeline (SAST/DAST), tự động khai thác PoC (Proof of Concept) lỗ hổng (SSRF, RCE, SQLi, Path Traversal), brute-force form login (kết hợp wordlist).
- Threat Intelligence & Forensics: Tải malware sample, IOC feeds, phân tích C2 beaconing, tải file từ URL đáng ngờ (sandbox).
- Red Team / Penetration Testing: Tùy biến request tinh vi (header, cookie, payload encoding, chunked transfer, HTTP smuggling), bypass WAF, test rate limiting.
- Blue Team / SOC: Health check service, kiểm tra certificate expiry tự động, pull log từ API SIEM.
Cú pháp cơ bản & Các tùy chọn an ninh quan trọng
curl [options] [URL...]
| Option | Mô tả an ninh |
|---|---|
-k, --insecure | Bỏ qua verify chứng chỉ SSL (chỉ dùng test lab, KHÔNG dùng production). |
--cacert <file> | Chỉ định CA bundle tùy chỉnh để verify cert (pinning). |
--cert <cert:pass> / --key <key> | Client certificate authentication (mTLS). |
-H, --header | Tiêm header tùy chỉnh (Authorization, X-Forwarded-For, User-Agent, Custom). |
-X, --request | Chỉ định method: GET, POST, PUT, DELETE, PATCH, OPTIONS... |
-d, --data / --data-raw / --data-binary | Gửi body request (form, JSON, binary). Quan trọng cho test injection. |
-u, --user user:pass | Basic Auth. Dùng -u "token:" cho Bearer token. |
-b, --cookie / -c, --cookie-jar | Gửi/nhận cookie. Test session fixation, hijacking. |
-v, --verbose / -I, --head | Debug: xem full request/response header (kiểm tra security headers). |
--max-time <sec> / --connect-timeout | Chống treo script (DoS vô tình). |
--proxy <host:port> / --socks5 | Đi qua proxy (Burp Suite, OWASP ZAP) để intercept/analyze traffic. |
--resolve <host:port:ip> | Override DNS, test virtual host, bypass CDN/WAF. |
--tls-max 1.2 / --tlsv1.3 | Buộc phiên bản TLS (test downgrade attack). |
--compressed | Yêu cầu nén (gzip/deflate), test decompression bomb. |
Ví dụ thực tế An ninh mạng
1. Kiểm tra Security Headers & SSL/TLS
curl -vkI https://target.com --tlsv1.2 --cacert ca-bundle.crt
Xem: `Strict-Transport-Security`, `Content-Security-Policy`, `X-Content-Type-Options`, `Certificate chain`, `Cipher suite`.
2. Test SSRF (Server-Side Request Forgery)
curl -v "https://api.target.com/fetch?url=https://169.254.169.254/latest/meta-data/" -H "User-Agent: SSRF-Test"
Thử truy cập metadata cloud (AWS/GCP/Azure).
3. Kiểm tra CORS Misconfiguration
curl -v -X OPTIONS https://api.target.com -H "Origin: https://evil.com" -H "Access-Control-Request-Method: POST"
Kiểm tra header `Access-Control-Allow-Origin`, `Access-Control-Allow-Credentials: true`.
4. Brute-force Login (Demo, có phép)
for pass in $(cat rockyou.txt); do
curl -s -X POST https://target.com/login -d "user=admin&pass=$pass" | grep -q "Welcome" && echo "Found: $pass" && break
done
Lưu ý: Luôn tuân thủ pháp luật, scope phép test. Dùng tool chuyên nghiệp (ffuf, hydra) cho hiệu năng.
5. Tải file an toàn (Malware Sample)
curl -L -o sample.exe "https://malware-repo.com/sample.exe" --max-time 30 --limit-rate 1M
`-L` follow redirect, `--limit-rate` giới hạn băng thông.
6. Tương tác API có xác thực (JWT/Bearer Token)
TOKEN=$(curl -s -X POST https://auth.target.com/token -d "client_id=id&client_secret=secret&grant_type=client_credentials" | jq -r .access_token)
curl -H "Authorization: Bearer $TOKEN" https://api.target.com/sensitive-data
cURL vs Wget vs HTTPie vs Postman
- cURL: Chuẩn mực, có mặt mọi nơi (Linux, macOS, Windows 10+), scriptable, hỗ trợ nhiều giao thức nhất, phức tạp nhưng mạnh nhất.
- Wget: Đơn giản hơn, tốt cho download đệ quy (mirror site), kém linh hoạt cho API testing.
- HTTPie: Cú pháp thân thiện, highlight JSON, màu sắc, tốt cho dev/test API thủ công.
- Postman/Insomnia/Bruno: GUI, quản lý collection, environment, test tự động, collaboration. Không phù hợp cho automation headless/server.
Lưu ý an ninh khi dùng cURL
- Không hardcode secret (password, token, API key) trong lệnh history (`.bash_history`). Dùng biến môi trường, file `.netrc`, hoặc tool quản lý secret.
- Cẩn thận `
curl | bash` (pipe to shell) - nguy hiểm cực đại (RCE). Luôn kiểm tra script trước (`curl -sL url | less`). - Verify chữ ký GPG/Checksum (SHA256) file tải về.
- Cập nhật cURL thường xuyên (fix CVE như CVE-2023-38545 - SOCKS5 heap overflow).
Tóm lại: cURL là "dao quân.swiss army knife" không thể thiếu cho chuyên gia an ninh mạng. Thành thạo cURL = khả năng tự động hóa, kiểm thử, điều tra, khai thác và bảo vệ hệ thống hiệu quả.