0

I have an Ubuntu VM (in Virtualbox) and a Win11 local machine.

I would like to send HTTP requests (for example 127.0.0.1:{PORT}/api/1/) to the virtual machine. The VM has an app running that only accepts requests to localhost, so I need to send the request from local to guest, and redirect the request in the VM to its localhost. I also need to achieve this using Iptables.

I have tried a bunch of iptables commands but without any success. I am getting "request timed out" or "connrefused" errors. Meanwhile I am able to run simple python servers on both machines and I am able to curl them from both local and vm.

In my understanding I need to configure Iptables inside the VM to accept requests to its IP and some port, but forward them to localhost and the needed port. But I struggle to implement this anyway.

I've tried variations of this command, but I believe this is closer to how it should look like (first I enable port forwarding):

sysctl net.ipv4.ip_forward=1 

iptables -t nat -A PREROUTING -p tcp 80 -j DNAT --to-destination 
127.0.0.1:4040 

In my understanding if now I send a request from my host like this, then the vm should receive this request and re-route it to its localhost port 4040, and I should receive the response from API: curl 172.20.10.4:80/api/v1/... But nothing happens, I get "request timed out". If I curl the "172.20.10.4:80", nothing happens too

Thank you in advance!

4
  • If it’s HTTP you want, why not use a reverse proxy? There’s plenty of (more or less) specialized solutions available (HAProxy, Traefik, Caddy, …) and some “normal” web servers can do it too (nginx, Apache httpd, …).
    – Daniel B
    Oct 5, 2022 at 18:33
  • @DanielB I am actually trying to recreate one scenario where a customer at work uses iptables to forward requests, but gets errors in the app (not related to my question) Oct 5, 2022 at 19:00
  • Ah, okay. In that case, it would be very helpful what those ”bunch of iptables” commands were, to see what you tried and maybe also what’s missing. You can probably retrieve them from history.
    – Daniel B
    Oct 5, 2022 at 19:08
  • @DanielB Basically I've tried variations of this command, but I believe this is closer to how it should look like (first I enable port forwarding): sysctl net.ipv4.ip_forward=1 iptables -t nat -A PREROUTING -p tcp 80 -j DNAT --to-destination 127.0.0.1:4040 In my understanding if now I send a request from my host like this, then the vm should receive this request and re-route it to its localhost port 4040, and I should receive the response from API: curl 172.20.10.4:80/api/v1/... But nothing happens, I get "request timed out". If I curl the "172.20.10.4:80", nothing happens too Oct 5, 2022 at 20:14

1 Answer 1

0

PREROUTING will take care of packets coming in.

You also have to handle packets going out with POSTROUTING.

Here's something that might work that is adapted from my iptables script - replace variables accordingly. ($I is just /sbin/iptables)

  "$I" -A PREROUTING -t nat \
   --protocol "$PROTOCOL" \
   --destination-port "$OUTSIDE_PORT" \
    --jump DNAT --to-destination "127.0.0.1:$INSIDE_PORT"

  "$I" -A POSTROUTING -t nat \
   --protocol "$PROTOCOL" \
   --source "$INSIDE_IP" --source-port "$INSIDE_PORT" \
    --jump SNAT --to-source "$VM_EXTERNAL_IP"

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .