0

I have two networks sequent networks both hidden under own NAT 192.168.31.0/24 -> 192.168.33.0/24 -> Internet

I have a TCP server on 192.168.33.35 and a client on 192.168.31.32

A client does a TCP request to server located in the internet at 66.66.66.66, for example. I want the last gateway 192.168.33.1 to send the request to 192.168.33.35 instead and trick the client.

The most feasible of what I tried is:

iptables -t nat -I PREROUTING -d 66.66.66.66 -j DNAT --to-destination 192.168.33.35
iptables -t nat -I POSTROUTING -s 192.168.33.35 -j SNAT --to-source 66.66.66.66

The server received the Syn packet and replied. But the source IP of the server response seems to remain 192.168.33.35 after passing the gateway. And packet gets lost received by 192.168.31.1 gateway.

Looks like SNAT part is not working.

What can I do to emulate/spoof the server with standard openWrt tools?

5
  • Are you emulating a server, or just trying to figure out how to spoof the path/destination/source of an ICMP packet? Apr 4, 2018 at 19:06
  • @music2myear, I am emulating the server as I wrote in the header.
    – Pheu Verg
    Apr 11, 2018 at 6:10
  • 2
    See, the headline of an article or title of a book summarizes the information in the article or book, it does not replace or contain unique information. Please treat questions here the same way: The title is a summary but should not contain unique information. The body of the question should contain ALL relevant and necessary information. Please edit the body of your question to make it more clear. Apr 11, 2018 at 15:22
  • @KamilMaciorowski, exactly
    – Pheu Verg
    Apr 12, 2018 at 13:50
  • @music2myear , actually, IP spoofing is the correct term. I was confused by the fact that I am not doing any adversary actions. And still, I can't find a solution (facepalm)
    – Pheu Verg
    Apr 12, 2018 at 14:10

2 Answers 2

1

This is what happens:

  1. Your 192.168.33.35 server sees the Syn packet from 192.168.33.Z (it's unaware of 192.168.31.0/24 network and sees the "WAN" address of the 192.168.31.1 gateway, I don't know what Z is) and replies to this address.
  2. Its routing table says this address belongs to local network, so the response is destined to MAC address of 192.168.33.Z.
  3. Even if the response physically travels through the 192.168.33.1 machine, the gateway doesn't mangle with it because it is destined to MAC address of another machine.
  4. 192.168.33.Z sees the response from 192.168.33.35. It expects one from 66.66.66.66 and doesn't know what to do.

Note it doesn't matter 192.168.33.Z performs NAT (being 192.168.31.1 on its LAN side). The mechanism of failure is very similar to the one where NAT loopback (hairpin NAT) is only partially set up.

You have to make your 192.168.33.35 server send responses to the MAC address of your 192.168.33.1 gateway.

  • Either set a routing rule for 192.168.33.Z (remember I don't know Z, you do; substitute the real number) or even for entire 192.168.33.0/24 network:

    # do this on the 192.168.33.35 machine
    route add -host 192.168.33.Z gw 192.168.33.1
    

    Note it will affect all communication from 192.168.33.35 to 192.168.33.Z. In cases when 192.168.33.35 should act as 192.168.33.35 packets will be unnecessarily routed; it shouldn't break things though.

  • Or let the 192.168.33.1 gateway perform not only DNAT but SNAT as well. This solution is basically the same as the one for NAT loopback in the already linked answer:

    # do this on the 192.168.33.1 gateway
    # you already have this line
    iptables -t nat -I PREROUTING -d 66.66.66.66 -j DNAT --to-destination 192.168.33.35
    # this line is new
    iptables -t nat -I POSTROUTING -d 192.168.33.35 -j SNAT --to-source 192.168.33.1
    

This fragment applies to your situation:

Note that the NAT table in iptables is only used for the first packet of a connection. Later packets related to the connection are processed using the internal mapping tables established when the first packet was translated.

This means the second of your lines (iptables … -j SNAT --to-source 66.66.66.66) is irrelevant when you initiate a connection from a client. Later packets will be SNAT-ed and DNAT-ed properly because the first packet was.


I think if your first line

iptables -t nat -I PREROUTING -d 66.66.66.66 -j DNAT --to-destination 192.168.33.35

acted on the 192.168.31.1 gateway, it would do what you want for other clients from 192.168.31.0/24 without any additional tricks.


This second line of yours

iptables -t nat -I POSTROUTING -s 192.168.33.35 -j SNAT --to-source 66.66.66.66

will apply if you initiate a connection from 192.168.33.35. Internet servers rarely do this, so you may not need this rule at all. In case you do need it, remember that:

  • the rule will only work if packets are routed through the 192.168.33.1 gateway where this rule acts, so to reach your local computers and to present to them as 66.66.66.66 you need to adjust the routing table on the 192.168.33.35 machine similarly as above;
  • you cannot communicate as 66.66.66.66 with external hosts (beyond your network) because even if you managed to reach them acting as 66.66.66.66, their response would get to the real 66.66.66.66, not back to you.
1
  • Thank You so much for you reply. It clarified some mistakes to me. But I did not manage to do it with iptables even with the example You linked. I did it in another way.
    – Pheu Verg
    Apr 18, 2018 at 7:36
0

Did it with routing. Unfortunately it is less flexible in order to access the real server, but at least it works. Something like this:

[email protected]# route add -host 66.66.66.66 gw 192.168.33.35 dev br-lan

and on the server:

[email protected]# ip link add name s666 type dummy
[email protected]# ifconfig s666 66.66.66.66
[email protected]# ifconfig s666 up

You must log in to answer this question.

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