IP/Network

 

 

 

 

NAT (Network Address Translation)

 

NAT means change IP address in a IP packet to another address. It is a technique that modifies the source and/or destination IP addresses of packets as they pass through a router or firewall.

 

 

 

Why NAT ?

 

We use Network Address Translation (NAT) for several reasons as follows,

 

IP address conservation: NAT allows multiple devices on a private network to share a single public IP address, which helps conserve the limited number of available public IP addresses.

 

Security: NAT provides an additional layer of security by hiding the private IP addresses of devices on the internal network from the public internet. This makes it more difficult for attackers to identify and target specific devices on the network.

 

Flexibility: NAT enables organizations to use private IP addresses on their internal networks, which are not routable on the internet, while still allowing access to the internet. This provides greater flexibility in network design and management.

 

Simplified network management: NAT allows organizations to easily manage their network infrastructure by consolidating multiple private IP addresses into a single public IP address. This simplifies network management and reduces the need for complex routing configurations.

 

 

 

Types of NAT

 

There are two types of NAT:

 

Static NAT: This is where a public IP address is permanently assigned to a device on the private network, allowing that device to always have the same IP address when communicating with the internet.

 

Dynamic NAT: In this method, the router assigns a public IP address from a pool of available addresses to a device on the private network when it requests access to the internet. Once the device is finished using the internet, the public IP address is released back into the pool for use by other devices.

 

 

 

NAT on Windows

 

A common way is to configure NAT in Windows is to use ICS (Internet Connection Sharing). I would suggest you to check out my note on ICS to understand how ICS works. Basically ICS is a type of NAT. If you get detailed understanding on how ICS works, you would have pretty clear picture on how NAT works in general.

 

ICS allows a computer with multiple network interfaces to share its internet connection with other devices on the network by acting as a NAT gateway.

 

When ICS is enabled, the computer creates a network bridge between the two network interfaces, and the NAT functionality is automatically configured to allow devices on the private network to access the internet through the computer's internet connection.

 

With ICS, the Windows computer is acting as a gateway for the private network, sharing its internet connection with other devices on the network.

 

However, there are slight difference between NAT in general and ICS. It is the scope of the sharing.

 

NAT allows multiple devices on a private network to share a single public IP address, while ICS allows a single computer to share its internet connection with other devices on the network. With NAT, the router or firewall acts as the gateway between the private network and the internet, while with ICS, a computer on the network is acting as the gateway.

 

Additionally, ICS is a built-in feature of Windows operating systems, while NAT is a more general networking concept that can be implemented on various network devices and is not limited to Windows operating systems.

 

You can setup ICS via the setup windows as shown below.

 

 

You may configure NAT setting on Windows in command line as follows :

 

Example 01 > Dynamic NAT

# Type the following command to create a NAT interface

netsh routing ip nat install

 

#Type the following command to add a private interface:

netsh interface ipv4 add interface [PrivateInterface] private

 

# Type the following command to add a public interface:

netsh interface ipv4 add interface [PublicInterface] public

 

# Type the following command to configure NAT:

netsh routing ip nat add interface [PrivateInterface] full

 

# Type the following command to configure the public IP address:

netsh routing ip nat set interface [PublicInterface] mode=full

 

# Type the following command to configure the private IP address range:

netsh routing ip nat set addressrange [PrivateInterface] [StartIP] [EndIP] [SubnetMask]

 

One specific example is as follows :

This command configures the private network to use IP addresses between 192.168.0.10 and 192.168.0.50 with a subnet mask of 255.255.255.0.

 

Once these commands are executed, the Windows computer will act as a NAT gateway, allowing devices on the private network (connected to the "Local Area Connection" interface) to access the internet through the "Wireless Network Connection" interface. Note that the interface names and IP addresses used in this example are purely imaginary and will likely not match your own network configuration.

 

netsh routing ip nat install

netsh interface ipv4 add interface "Local Area Connection" private

netsh interface ipv4 add interface "Wireless Network Connection" public

netsh routing ip nat add interface "Local Area Connection" full

netsh routing ip nat set interface "Wireless Network Connection" mode=full

netsh routing ip nat set addressrange "Local Area Connection" 192.168.0.10 192.168.0.50 255.255.255.0

 

 

Example 02 > Static NAT

 

Here's an example configuration for static NAT on Windows, using the interface named "Ethernet" and mapping a public IP address of 203.0.113.10 to a private IP address of 192.168.1.10:

 

netsh routing ip nat install

netsh interface ipv4 add interface "Local Area Connection" private

netsh interface ipv4 add interface "Wireless Network Connection" public

netsh routing ip nat add interface "Local Area Connection" full

netsh routing ip nat add addressmapping name="Local Area Connection" publicip=203.0.113.10 privateip=192.168.1.10 protocol=tcp privateport=80 publicport=80

 

 

 

NAT on Linux

 

Since the general concept of NAT is already explained above and it is same in Linux as well, I would not talk any further about the concept and just provide some examples here.

 

 

Example 01 > Dynamic NAT

#Type the following command to enable IP forwarding:

echo 1 > /proc/sys/net/ipv4/ip_forward

 

#Type the following command to configure NAT: (This command configures NAT on the "eth0" interface (the interface connected to the public network) and sets the MASQUERADE target to dynamically map private IP addresses to the public IP address.)

iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE

 

#Type the following command to configure the private IP address range: (This command allows traffic from the "eth1" interface (the interface connected to the private network) with source IP addresses in the range 192.168.0.0/24 to be forwarded.). See NOTE 1

iptables -A FORWARD -i eth1 -s 192.168.0.0/24 -j ACCEPT

 

#Type the following command to enable NAT on boot:(This command saves the iptables rules to a file named /etc/iptables.rules.)

sudo sh -c "iptables-save > /etc/iptables.rules"

 

#Type the following command to restore the iptables rules on boot: (This command restores the iptables rules from the /etc/iptables.rules file on boot.)

sudo sh -c "iptables-restore < /etc/iptables.rules"

 

NOTE 1 :  We need to use 192.168.0.0/24 in [iptables -A FORWARD -i eth1 -s 192.168.0.0/24 -j ACCEPT] to  allow traffic to pass through the firewall from the local network with source IP addresses in the range 192.168.0.0/24

 

 

Example 02 > Static NAT

 

#First, disable IP forwarding by running the following command: This disables IP forwarding, which is necessary for Static NAT. (Refer to NOTE 1)

echo 0 > /proc/sys/net/ipv4/ip_forward

 

#Then, create a static NAT mapping by running the following command: This maps the public IP address to the private IP address. Replace <public_ip_address> with the public IP address you want to use and <private_ip_address> with the private IP address of the internal host.

# you may assume < private_ip_address> = 192.168.0.1 just for an example, specify < private_ip_address> as per your requirement.

iptables -t nat -A PREROUTING -i eth0 -d <public_ip_address> -j DNAT --to-destination <private_ip_address>

 

#Next, allow incoming traffic from the public interface by running the following command: This allows incoming traffic from the public interface to the internal host.

# you may assume < private_ip_address> = 192.168.0.1 just for an example,

iptables -A FORWARD -i eth0 -d <private_ip_address> -j ACCEPT

 

# Then, allow outgoing traffic from the private interface by running the following command: This allows outgoing traffic from the internal host to the public network.

# you may assume < private_ip_address> = 192.168.0.0/24 just for an example, (Refer to NOTE 2)

iptables -A FORWARD -i eth1 -s <private_ip_address> -j ACCEPT

 

#Finally, save the new configuration by running the following commands: This saves the new configuration to the iptables rules file and restores the rules so that they persist across reboots.

sudo sh -c "iptables-save > /etc/iptables.rules"

sudo sh -c "iptables-restore < /etc/iptables.rules"

 

NOTE 1 : Why we need to disable ip_forwarding at the first step ?

 

Disabling IP forwarding is necessary for Static NAT because with Static NAT, the IP address translation is performed only once when the mapping is created, and all subsequent traffic to and from the internal host uses the same mapped IP address. If IP forwarding is enabled, then the translated IP address may be changed as the packet is forwarded, resulting in unpredictable behavior.

By disabling IP forwarding, you ensure that the translated IP address remains the same throughout the duration of the session, and that the Static NAT mapping works as expected

 

NOTE 2 : why do we need to use 192.168.0.0/24 in [iptables -A FORWARD -i eth1 -s 192.168.0.0/24 -j ACCEPT] instead of 192.168.0.1 ?

 

This command is still needed to allow traffic to pass through the firewall, but the IP addresses of the local network have not changed. Therefore, the same range of IP addresses, 192.168.0.0/24, is used to identify the source of the traffic.

The IP address 192.168.0.1 is the internal IP address of the device that is being NATed to the public IP address. It is not used in the FORWARD rule because it is not a range of IP addresses that traffic can come from. Instead, it is the destination IP address of the incoming traffic that is being translated by the DNAT rule.

 

 

NOTE : How to check current NAT setting ?

 

# Check the value of the ip_forward setting:

$ sysctl net.ipv4.ip_forward

 

#List the NAT rules:

$ sudo iptables -t nat -L

 

# Check the firewall rules:

$ sudo iptables -L

 

 

 

Reference