Windows File Transfer Methods

Introduction

The Windows operating system has evolved over the past few years, and new versions come with different utilities for file transfer operations. Understanding file transfer in Windows can help both attackers and defenders. Attackers can use various file transfer methods to operate and avoid being caught. Defenders can learn how these methods work to monitor and create the corresponding policies to avoid being compromised. Let's use the Microsoft Astaroth Attackarrow-up-right blog post as an example of an advanced persistent threat (APT).

The blog post starts out talking about fileless threatsarrow-up-right. The term fileless suggests that a threat doesn't come in a file, they use legitimate tools built into a system to execute an attack. This doesn't mean that there's not a file transfer operation. As discussed later in this section, the file is not "present" on the system but runs in memory.

The Astaroth attack generally followed these steps: A malicious link in a spear-phishing email led to an LNK file. When double-clicked, the LNK file caused the execution of the WMIC toolarrow-up-right with the "/Format" parameter, which allowed the download and execution of malicious JavaScript code. The JavaScript code, in turn, downloads payloads by abusing the Bitsadmin toolarrow-up-right.

All the payloads were base64-encoded and decoded using the Certutil tool resulting in a few DLL files. The regsvr32arrow-up-right tool was then used to load one of the decoded DLLs, which decrypted and loaded other files until the final payload, Astaroth, was injected into the Userinit process. Below is a graphical depiction of the attack.

This is an excellent example of multiple methods for file transfer and the threat actor using those methods to bypass defenses.

This section will discuss using some native Windows tools for download and upload operations. Later in the module, we'll discuss Living Off The Land binaries on Windows & Linux and how to use them to perform file transfer operations.

Download Operations

We have access to the machine MS02, and we need to download a file from our Pwnbox machine. Let's see how we can accomplish this using multiple File Download methods.

PowerShell Base64 Encode & Decode

Depending on the file size we want to transfer, we can use different methods that do not require network communication. If we have access to a terminal, we can encode a file to a base64 string, copy its contents from the terminal and perform the reverse operation, decoding the file in the original content. Let's see how we can do this with PowerShell.

An essential step in using this method is to ensure the file you encode and decode is correct. We can use md5sumarrow-up-right, a program that calculates and verifies 128-bit MD5 checksums. The MD5 hash functions as a compact digital fingerprint of a file, meaning a file should have the same MD5 hash everywhere. Let's attempt to transfer a sample ssh key. It can be anything else, from our Pwnbox to the Windows target.

Pwnbox Check SSH Key MD5 Hash

Pwnbox Encode SSH Key to Base64

We can copy this content and paste it into a Windows PowerShell terminal and use some PowerShell functions to decode it.

Finally, we can confirm if the file was transferred successfully using the Get-FileHasharrow-up-right cmdlet, which does the same thing that md5sum does.

Confirming the MD5 Hashes Match

triangle-exclamation

PowerShell Web Downloads

Most companies allow HTTP and HTTPS outbound traffic through the firewall to allow employee productivity. Leveraging these transportation methods for file transfer operations is very convenient. Still, defenders can use Web filtering solutions to prevent access to specific website categories, block the download of file types (like .exe), or only allow access to a list of whitelisted domains in more restricted networks.

PowerShell offers many file transfer options. In any version of PowerShell, the System.Net.WebClientarrow-up-right class can be used to download a file over HTTP, HTTPS or FTP. The following tablearrow-up-right describes WebClient methods for downloading data from a resource:

Method
Description

Returns the data from a resource as a Streamarrow-up-right.

Returns the data from a resource without blocking the calling thread.

Downloads data from a resource and returns a Byte array.

Downloads data from a resource and returns a Byte array without blocking the calling thread.

Downloads data from a resource to a local file.

Downloads data from a resource to a local file without blocking the calling thread.

Downloads a String from a resource and returns a String.

Downloads a String from a resource without blocking the calling thread.

Let's explore some examples of those methods for downloading files using PowerShell.

PowerShell DownloadFile Method

We can specify the class name Net.WebClient and the method DownloadFile with the parameters corresponding to the URL of the target file to download and the output file name.

File Download

PowerShell DownloadString - Fileless Method

As we previously discussed, fileless attacks work by using some operating system functions to download the payload and execute it directly. PowerShell can also be used to perform fileless attacks. Instead of downloading a PowerShell script to disk, we can run it directly in memory using the Invoke-Expressionarrow-up-right cmdlet or the alias IEX.

IEX also accepts pipeline input.

PowerShell Invoke-WebRequest

From PowerShell 3.0 onwards, the Invoke-WebRequestarrow-up-right cmdlet is also available, but it is noticeably slower at downloading files. You can use the aliases iwr, curl, and wget instead of the Invoke-WebRequest full name.

Harmj0y has compiled an extensive list of PowerShell download cradles herearrow-up-right. It is worth gaining familiarity with them and their nuances, such as a lack of proxy awareness or touching disk (downloading a file onto the target) to select the appropriate one for the situation.

Download Cradles

Common Errors with PowerShell

There may be cases when the Internet Explorer first-launch configuration has not been completed, which prevents the download.

This can be bypassed using the parameter -UseBasicParsing.

Another error in PowerShell downloads is related to the SSL/TLS secure channel if the certificate is not trusted. We can bypass that error with the following command:

SMB Downloads

The Server Message Block protocol (SMB protocol) that runs on port TCP/445 is common in enterprise networks where Windows services are running. It enables applications and users to transfer files to and from remote servers.

We can use SMB to download files from our Pwnbox easily. We need to create an SMB server in our Pwnbox with smbserver.pyarrow-up-right from Impacket and then use copy, move, PowerShell Copy-Item, or any other tool that allows connection to SMB.

Create the SMB Server

To download a file from the SMB server to the current working directory, we can use the following command:

New versions of Windows block unauthenticated guest access, as we can see in the following command:

To transfer files in this scenario, we can set a username and password using our Impacket SMB server and mount the SMB server on our windows target machine:

Create the SMB Server with a Username and Password

Mount the SMB Server with Username and Password

circle-exclamation

FTP Downloads

Another way to transfer files is using FTP (File Transfer Protocol), which use port TCP/21 and TCP/20. We can use the FTP client or PowerShell Net.WebClient to download files from an FTP server.

We can configure an FTP Server in our attack host using Python3 pyftpdlib module. It can be installed with the following command:

Installing the FTP Server Python3 Module

Then we can specify port number 21 because, by default, pyftpdlib uses port 2121. Anonymous authentication is enabled by default if we don't set a user and password.

Setting up a Python3 FTP Server

After the FTP server is set up, we can perform file transfers using the pre-installed FTP client from Windows or PowerShell Net.WebClient.

Transferring Files from an FTP Server Using PowerShell

When we get a shell on a remote machine, we may not have an interactive shell. If that's the case, we can create an FTP command file to download a file. First, we need to create a file containing the commands we want to execute and then use the FTP client to use that file to download that file.

Create a Command File for the FTP Client and Download the Target File

Upload Operations

There are also situations such as password cracking, analysis, exfiltration, etc., where we must upload files from our target machine into our attack host. We can use the same methods we used for download operation but now for uploads. Let's see how we can accomplish uploading files in various ways.

PowerShell Base64 Encode & Decode

We saw how to decode a base64 string using Powershell. Now, let's do the reverse operation and encode a file so we can decode it on our attack host.

Encode File Using PowerShell

We copy this content and paste it into our attack host, use the base64 command to decode it, and use the md5sum application to confirm the transfer happened correctly.

Decode Base64 String in Linux

PowerShell Web Uploads

PowerShell doesn't have a built-in function for upload operations, but we can use Invoke-WebRequest or Invoke-RestMethod to build our upload function. We'll also need a web server that accepts uploads, which is not a default option in most common webserver utilities.

For our web server, we can use uploadserverarrow-up-right, an extended module of the Python HTTP.server modulearrow-up-right, which includes a file upload page. Let's install it and start the webserver.

Installing a Configured WebServer with Upload

Steps to Run uploadserver as a Module:

  1. Create a Virtual Environment: Set up a virtual environment in your user directory: python3 -m venv ~/uploadserver-venv

  2. Activate the Virtual Environment: Activate the virtual environment: source ~/uploadserver-venv/bin/activate

  3. Install uploadserver: Now that you're in the virtual environment, install uploadserver: pip install uploadserver

  4. Run uploadserver: After installation, you can run it as a module within the virtual environment: python3 -m uploadserver

  5. Deactivate the Virtual Environment: When you're done, you can deactivate the virtual environment with: deactivate

Now we can use a PowerShell script PSUpload.ps1arrow-up-right which uses Invoke-RestMethod to perform the upload operations. The script accepts two parameters -File, which we use to specify the file path, and -Uri, the server URL where we'll upload our file. Let's attempt to upload the host file from our Windows host.

PowerShell Script to Upload a File to Python Upload Server

PowerShell Base64 Web Upload

Another way to use PowerShell and base64 encoded files for upload operations is by using Invoke-WebRequest or Invoke-RestMethod together with Netcat. We use Netcat to listen in on a port we specify and send the file as a POST request. Finally, we copy the output and use the base64 decode function to convert the base64 string into a file.

We catch the base64 data with Netcat and use the base64 application with the decode option to convert the string to the file.

SMB Uploads

We previously discussed that companies usually allow outbound traffic using HTTP (TCP/80) and HTTPS (TCP/443) protocols. Commonly enterprises don't allow the SMB protocol (TCP/445) out of their internal network because this can open them up to potential attacks. For more information on this, we can read the Microsoft post Preventing SMB traffic from lateral connections and entering or leaving the networkarrow-up-right.

An alternative is to run SMB over HTTP with WebDav. WebDAV (RFC 4918)arrow-up-right is an extension of HTTP, the internet protocol that web browsers and web servers use to communicate with each other. The WebDAV protocol enables a webserver to behave like a fileserver, supporting collaborative content authoring. WebDAV can also use HTTPS.

When you use SMB, it will first attempt to connect using the SMB protocol, and if there's no SMB share available, it will try to connect using HTTP. In the following Wireshark capture, we attempt to connect to the file share testing3, and because it didn't find anything with SMB, it uses HTTP.

Configuring WebDav Server

To set up our WebDav server, we need to install two Python modules, wsgidav and cheroot (you can read more about this implementation here: wsgidav githubarrow-up-right). After installing them, we run the wsgidav application in the target directory.

Using the WebDav Python module

Connecting to the Webdav Share

Now we can attempt to connect to the share using the DavWWWRoot directory.

circle-exclamation

Uploading Files using SMB

circle-exclamation

FTP Uploads

Uploading files using FTP is very similar to downloading files. We can use PowerShell or the FTP client to complete the operation. Before we start our FTP Server using the Python module pyftpdlib, we need to specify the option --write to allow clients to upload files to our attack host.

Now let's use the PowerShell upload function to upload a file to our FTP Server.

PowerShell Upload File

Create a Command File for the FTP Client to Upload a File

RDP Shares

Using Xfreerdp you can RDP to a target and setup a share for easy transference of files:

  • /drive:LOCAL_DIRECTOY,SHARE_NAME: Creates a shared drive between the attacking machine and the target. This is very useful as it allows us to very easily use our toolkit on the remote target, and save any outputs back to our own hard drive. This means we may never actually have to create any files on the target. For example, to share the current directory in a share called share, you could use: /drive:.,share, with the period (.) referring to the current directory

Last updated