AutoNetOps

Cover Image for Tutorial: Using Python http.server and netsh for File Access Between Devices

Tutorial: Using Python http.server and netsh for File Access Between Devices

·

4 min read

Problem Statement

The other day I had a situation where I had two Windows machines from work that I wanted to transfer some big files (Bigger than allowed to send via email) and which I have no admin rights, so I could not really install programs and had no access to some websites to upload the files like Google, Dropbox and so on. I have Python installed though…


Solution

In this tutorial, we'll explore how I used Python's http.server module to transfer files from devices running on the same network and also expand by adding a 3rd element that will port forward data.

To facilitate understanding, here's a simplified diagram of the setup:


Tools and Setup

  • Python: We'll use Python's built-in http.server module to serve files over HTTP.

  • Windows Command Prompt: netsh will be used to set up port forwarding.

  • WSL: We'll utilize WSL (Ubuntu in this case) to receive and access the files.


Step-by-Step Tutorial

Step 1: Set Up Python http.server in WSL

  1. Navigate to the directory containing the files you want to access:

     cd /path/to/your/files
    
  2. Start the Python HTTP server on a chosen port (e.g., 8000):

     python3 -m http.server 8000
    

    This command starts an HTTP server on port 8000, serving files from the current directory.

Step 2: Find the IP Address of Target

  1. Find the IP address of your Target instance:

     #Linux
     ifconfig
     ip addr show eth0
    
     #Windows
     ipconfig
    

Step 3: Set Up Port Forwarding

Windows

  1. Make sure to disable the Windows firewall or create a rule to permit TCP connection on the listening port (8888).

  2. Open Command Prompt as Administrator on Windows.

  3. Set up port forwarding using netsh. For example, to forward port 8888 on Windows to 8000 on WSL (172.18.47.94):

     netsh interface portproxy add v4tov4 listenaddress=0.0.0.0 listenport=8888 connectaddress=172.18.47.94 connectport=8000
    
    • listenaddress=0.0.0.0: Listens on all interfaces.

    • listenport=8888: Listens port 8888 on Windows.

    • connectaddress=172.18.47.94: Forwards to Target address.

    • connectport=8000: Forwards to port 8000 on Target.

  4. To verify that the port forwarding was successful:

     netsh interface portproxy show all
    
  5. To remove the forwarding rule:

     netsh interface portproxy delete v4tov4 listenaddress=0.0.0.0 listenport=8888
    

Step 4: Access Files from Any Machine on this Network

  1. Open a web browser on another machine (can even be your phone).

  2. Navigate to http://<Windows-IP>:8888.

    This URL will access the files served by Python http.server running in Target through the port forwarding set up by netsh.

Here is an image of my cellphone accessing the shared files from target.

cellphone accessing windows files

— Linux (For Reference)

Basic Port Forwarding with netcat

Suppose you want to forward traffic from port 8888 on your Linux machine to port 8000 on a remote machine (e.g., your WSL instance with IP 172.18.47.94).

  1. Open a terminal on your Linux machine.

  2. Set up port forwarding using netcat:

     nc -l -p 8888 -c "nc 172.18.47.94 8000"
    
    • -l: Tells netcat to listen for incoming connections.

    • -p 8080: Specifies that it should listen on port 8080.

    • -c "nc 172.18.47.94 8888": When a connection is received, this runs another netcat command that forwards the traffic to the remote host (172.18.47.94) on port 8000.

Step 2: Explanation

  • Any connection to your Linux machine on port 8888 will be forwarded by netcat to port 8888 on the target machine (172.18.47.94), which is your WSL instance in this case.

  • Note: This setup is relatively simple but has limitations. It's most useful for basic forwarding in scenarios where netcat works reliably. For more complex routing, tools like iptables or socat offer greater control.

Step 3: Persistent Netcat Forwarding with -k

To keep the listener running for multiple connections (persistent mode), you can add the -k option:

nc -lk -p 8888 -c "nc 172.18.47.94 8000"

The -k flag tells netcat to continue listening for connections after the current one closes.

— SSH

Port Forwarding on Linux Using SSH (Alternative)

For simpler setups or remote access, SSH port forwarding is often easier than configuring nc. If SSH is set up, you can use this command:

  1. Run this on your Linux machine to forward port 8080 to 8000:

     ssh -L 8888:localhost:8000 user@172.18.47.94
    

    This forwards local port 8080 to the Target machine's port 8000 over SSH.


Conclusion

This tutorial demonstrated how to leverage Python's http.server module along with netsh on Windows to securely transfer files from a Windows host machine to a Target instance. We created a simple yet effective setup for accessing files across different environments within the same local network by using HTTP for file transfer and port forwarding.

Following these steps, you can overcome some limiting scenarios and set up file sharing and access between machines, enhancing your workflow in mixed environment.