Transferring files between a Client and Server could be restricted in some environments and we might not be able to transfer a file by using internet file sharing services as an intermediate. In this case if we can do text copy/paste clipboard between client and server, we are most likely also able to transfer binary files.
This post shows a way of transferring binaries with Base64 on both Windows and Linux.
Table of Contents
Windows Systems
In windows we could use both Powershell or Certutil.exe to to encode/decode a file. Both methods will be shown here.
Powershell
So in our first command we encode and output to base64 text from our binary.
1 2 3 |
# encode from binary file to base64txt powershell -C "& {$outpath = (Join-Path (pwd) 'out_base64.txt'); $inpath = (Join-Path (pwd) 'file.exe'); [IO.File]::WriteAllText($outpath, ([convert]::ToBase64String(([IO.File]::ReadAllBytes($inpath)))))}" |
In this command we decode and create our binary file based on our base64 output from above:
1 2 3 |
# decode from base64txt to binary file powershell -C "& {$outpath = (Join-Path (pwd) 'file2.exe'); $inpath = (Join-Path (pwd) 'out_base64.txt'); [IO.File]::WriteAllBytes($outpath, ([convert]::FromBase64String(([IO.File]::ReadAllText($inpath)))))}" |
Certutil
Like Powershell, Certutil is also built-into Windows as a native tool.
1 2 |
# Encode data certutil -encode data.txt tmp.b64 && findstr /v /c:- tmp.b64 > data.b64 |
1 2 3 |
# Decode data to file certutil -decode data.b64 data.txt |
Linux Systems
On Linux we simply use cat, echo and base64 commands.
Encode binary file with base64:
This command will print the base64 as a string.
1 |
cat ncat | base64 -w 0 |
This command will encode and pipe to a file.
1 |
cat /bin/nc | base64 -w 0 > nc.b64 |
Decode binary file with base64:
1 |
echo -n (paste) | base64 -d > ncat |
Check md5 file integrity :
You can verify the MD5 checksum of the file before and after transfer/encoding.
1 |
md5sum FILE |