I read this article to fully understand DNAT. hope you can help me. for example, iptables has two NICs: 202.102.240.88192.168.1.100. for the Intranet WEB server 192.168.1.200, You need to map the Intranet WEB server to allow Internet access to http: 202.102.240.88 to access the Intranet WEB. We configure iptables-tnat.
I read this article to fully understand DNAT. I hope you can help me.
For example, iptables has two NICs: 202.102.240.88 192.168.1.100. If the Intranet WEB server is 192.168.1.200, You need to map the Intranet WEB server to allow Internet access to http: // 202.102.240.88.
We configure
Iptables-t nat-a prerouting-d 202.102.240.88-p tCp-- Dport 80-j DNAT 192.168.1.200 // The destination is changed, and the package source address is not changed.
Iptables-t nat-a postrouting-d 192.168.1.200-p tcp -- dport 80-j SNAT 192.168.1.100 // the destination address of the returned packet is the IP address of the client on the network, if the source IP address is not modified, the client will discard the IP address even if it receives the IP address.
Then you can complete the operation. The blue part here may be difficult to understand, and I am also entangled for a long time. After reading a lot, it is said that POSTROUTING indicates the package flow. I understand this.
Assume that packet A enters the Intranet 192.168.1.200 through iptables. Then, POSTROUTING-d 192.168.1.200 refers to the returned packet of A. Its source IP address is 192.168.1.200, And it is changed to 192.168.1.100
You can use forward to let the network adapter 202.102.240.88 send the packet out of the above-colored labels. How can this problem be solved? Why don't we just change it to 202.102.240.88.
Source Network>
This target is used to convert the destination network address, that is, to rewrite the destination IP address of the package. If a package matches, all packages that belong to the same stream will be automatically converted and then routed to the correct host or network. DNAT target is very useful. For example, your Web server is inside the LAN and there is no real IP address that can be used on the Internet, then we can use this target to allow the firewall to forward all packets sent to its own HTTP port to the real Web server in the LAN. The destination address can also be a range. In this way, DNAT will randomly allocate an address for each stream. Therefore, we can use this target for load balancing of some type.
Note that DANT target can only be used in the PREROUTING and OUTPUT chains of nat tables, or in the chains called by these two chains. Note that the chain containing DANT target cannot be called by other chains, such as POSTROUTING.
Table 6-16. DNAT target
Option -- to-destination
Example iptables-t nat-a prerouting-p tcp-d 15.45.23.67 -- dport 80-j DNAT -- to-destination 192.168.1.1-192.168.1.10
Explanation specifies the IP address to be written into the IP header, which is also the place where the packet will be forwarded. In the preceding example, all packets sent to the IP address 15.45.23.67 are forwarded to a private IP address used by the LAN, that is, 192.168.1.1 to 192.168.1.10. As mentioned above, in this case, each stream is randomly allocated with an address to be forwarded, but the same stream always uses the same address. We can also specify only one IP address as the parameter so that all packets are forwarded to the same machine. We can also specify a port or a range after the address. For example, -- to-destination 192.168.1.1: 80 or -- to-destination 192.168.1.1: 80-100. The syntax of SNAT is the same as that of this target, but the purpose is different. Note that only use -- protoColTCP or UDP can be used only when the port is specified.
Because DNAT has to do a lot of work, I have to be a little bit wordy. Let's take an example to get a general idea of how it works. For example, I want to publish our website over the Internet, but the HTTP server is in our intranet, and we only have a valid IP address, that is, the firewall's external IP address -- $ INET_IP. The firewall also has an intranet IP address, $ LAN_IP. The HTTP server IP address is $ HTTP_IP (this is of course the Intranet IP address ). To fulfill our vision, the first thing we need to do is to add the following simple rule to the PREROUTING chain of the nat table:
Iptables-t nat-a prerouting -- dst $ INET_IP-p tcp -- dport 80-j DNAT \ -- to-destination $ HTTP_IP
Now, all packets sent from the Internet to port 80 of the firewall will be forwarded (or called DNAT) to the HTTP server on the Intranet. If you try it on the Internet, everything works. Try again from the Intranet. It is completely unavailable. This is actually a routing problem. Next, let's analyze the problem. For ease of reading, the IP address of the machine that accesses our server on the internet is recorded as $ EXT_BOX.
The package starts from the machine with the address $ EXT_BOX and goes to the machine with the address $ INET_IP.
The package arrives at the firewall.
The firewall DNAT (that is, forwarding) package, and the package will undergo many other chain inspection and processing.
The package leaves the firewall and moves forward to $ HTTP_IP.
When the packet arrives at the HTTP server, the server will respond through the firewall. Of course, this requires that the firewall be used as the gateway for HTTP to reach $ EXT_BOX. Generally, the firewall is the default gateway of the HTTP server.
The firewall then performs Un-DNAT on the returned packet (that is, follow the DNAT steps in turn). This is like the firewall replies to the request packet from the Internet.
The returned package is returned to $ EXT_BOX without such complicated processing.
Now, let's consider the same Intranet as the HTTP server (this refers to the network in which all hosts can directly access each other without going through the router, what happens when a customer accesses a server and a client in a different subnet. Assume that the IP address of the client is $ LAN_BOX, And the other settings are the same as above.
Package leaves $ LAN_BOX and goes to $ INET_IP.
The package arrives at the firewall.
The package is DNAT and will be processed by others. But the package has not been processed by SNAT, so the package still uses its own source address, which is $ LAN_BOX, the destination address is changed only according to the destination address, but it does not change the source address as the vro passes through many routers during transmission, unless you change the source address separately. In fact, the processing in this step is the same as the processing of external packages, but the problem with the Intranet package lies in this, so here is the reason ).
The package leaves the firewall and arrives at the HTTP server.
The HTTP server tries to reply to this package. In the routing database, it can see that the package is a sub-machine from the same network, so it will directly send the reply packet to the source address of the Request Packet (now the destination address of the reply packet ), that is, $ LAN_BOX.
The reply package arrives at the client, but it is very confusing because it is not the machine that it accesses. In this way, it will discard the package and wait for the "real" response package.
There is a simple solution to this problem, because these packages need to enter the firewall, and they all go to the address that requires DNAT, therefore, we only need to perform the SNAT operation on these packages. For example, let's consider the above example. If you perform SNAT operations on packets that enter the firewall and are directed to $ HTTP_IP and port 80, these packages are from $ LAN_IP, that is, the source address of these packages is changed to $ LAN_IP. In this way, the HTTP server sends the reply packet to the firewall, and the firewall then performs the Un-DNAT operation on the packet and sends the packet to the client. The rules for solving the problem are as follows:
Iptables-t nat-a postrouting-p tcp -- dst $ HTTP_IP -- dport 80-j SNAT \ -- to-source $ LAN_IP
Remember that the POSTROUTING chain is the last of all chains in the running order. Therefore, when the package arrives at this chain, it has been DNAT, therefore, we need to match the packet in the rule based on the Intranet address $ HTTP_IP (package destination.
Warning: the rule we just wrote will have a great impact on the log, which should be said to be very bad. Because Internet packets are processed by DNAT and SNAT in the firewall before they arrive at the HTTP server (in the above example), the HTTP server considers the packet sent from the firewall, the real source is other IP addresses. In this way, when it records service conditions, the source addresses of all access records are firewall IP addresses rather than real access sources. If we want to know the access situation based on these records, it is impossible. Therefore, the "Simple Method" provided above is not a wise choice, but it does solve the "accessible" problem, but does not take logs into account.
Other services have similar problems. For example, if you have created an SMTP server in the LAN, you need to set up a firewall to forward SMTP data streams. In this way, you create an open SMTP relay server, and the log problem arises.
Note that the problems mentioned here are only for networks without DMZ or similar structures, and Intranet users access the Internet address of the server. (Note: If DMZ is set up, or the server and client are divided into different subnets, this is not necessary. Because all access sources are not in the network where the server is located, there is no need for SNAT to change the source address of the package, so that the record is no problem. If the Intranet client directly accesses the server's intranet address, it will be even better)
A better solution is to create a separate DNS server for your intranet, DNS can resolve it to an intranet address. The client can directly access the Intranet address of the HTTP server, thus avoiding operations through the firewall, and the source address of the package can also be used by the logs of the HTTP server, there is no log problem mentioned above .), Or just build DMZ (this is the best way, but you need to have money, because there are many devices ).
The above example should be more comprehensive. there is still another problem that cannot be solved, that is, what will happen when the firewall wants to access the HTTP server on its own. Can it be accessed normally? What do you think? :) unfortunately, the current configuration still does not work. Think about it and you will understand it. The basis of our discussion here is that the sub-host accesses the Internet address of the HTTP server, but this Internet address is actually the external address of the firewall, so when the firewall accesses this Internet address, is to access itself. If there is an HTTP service on the firewall, the client will see the page content, but this is not what it wants to see (it wants to be on DNAT). If there is no HTTP service, the customer can only receive the error message. The preceding rule does not work because the request packets sent from the firewall do not pass through the two chains. Remember the links through which the packets sent by the firewall will go through :) we need to add the following rules to the OUTPUT chain of the nat table:
Iptables-t nat-a output -- dst $ INET_IP-p tcp -- dport 80-j DNAT \ -- to-destination $ HTTP_IP
With the last rule, everything is normal. The server that is not in the same network as the HTTP server can access the service normally, and the server that is in the same network can also access the service normally, and the firewall itself can also access the service normally, no problem. This kind of mood applies the phrase "westward journey", that is, "The world is clean again ". (Don't say you don't know what a westward journey is.)
I think you should be able to understand that these rules only show how packets are properly handled by DNAT and SNAT. In addition, other rules (in the FORWARD chain) are required in the filter table to allow specific packages to pass through the previously written rules (in the POSTROUTING chain and OUTPUT chain. Do not forget that those packets have been dnat in the prerouting chain before they arrive at the FORWARD chain. That is to say, their destination addresses have been rewritten. Pay attention to this when writing rules.