How to deny access to specific port when destination is NOT a specific host

hostnameiptables

I'm trying to DENY access to port 1000 when it is being accessed through any host or IP that is NOT a specific host.

I.e.
If I try to access the service on sub.domain.com:1000 I should get in.
If I try to access the service on sub.domain2.com:1000 I should not get in, even though both of these domains are A-records pointing to the same server.

I'm trying to accomplish this using iptables with the following rules, but it's not working. It's allowing connections to any destination host/ip, not just the one I have set an ACCEPT rule to.

iptables -A INPUT -p tcp --destination sub.domain.com --dport 1000 -j ACCEPT
iptables -A INPUT -p tcp --dport 1000 -j DROP

The sub.domain2.com in this example, hosts different services, which is why I can't simply remove that DNS pointer.

Best Answer

IPTables is working on IP and TCP level, so it doesn't actually know DNS.

When a client creates a TCP connection to a DNS name it first looks up the IP address corresponding to the DNS name and then connects to the IP, not to the DNS name.

This means IPTables can't possibly know which DNS name a client is connecting to, it only knows which IP the client connects to.

If you specify --destination sub.domain.com as a parameter, IPTables will simply do a name look up to the IP that corresponds to this name, and then use the IP in it's rules.

If your port 1000 serves HTTP it would be possible to configure an HTTP server on that port to not handle requests to domains other than sub.domain.com, but IPTables doesn't have the necessary information to this.

If you could put sub.domain.com and sub.domain2.com onto two different IPs on the same server, then IPTables would be able to deny access to one but not to the other, because it could decide based on the IP.

This illustrates nicely that the Domain Name System is on top of Transport (TCP) and Internet (IP), for more details you can read the wikipedia article about it:

ISO OSI

Related Question