Protect your webserver from censys & from direct ip access

Hey, in this tutorial I’ll show you how to protect your webserver from censys & direct-ip web access. Why should you block censys ? Because your server could get leaked by Censys and some ill-intentioned people could attack your webserver easily with the ip of it.

Let’s start !

1. Block censys

iptables -A INPUT -s 141.212.121.0/24 -j DROP
iptables -A INPUT -s 141.212.122.0/24 -j DROP

2. Block direct-access & authorize cloudflare ips

Drop http/https request on the direct-ip.
iptables -A INPUT -p tcp --dport http -j REJECT --reject-with tcp-reset
iptables -A INPUT -p tcp --dport https -j REJECT --reject-with tcp-reset

Authorize CloudFlare traffic

iptables -A INPUT -p tcp -m tcp -m multiport --dports 80,443 -s 127.0.0.1/8 -j ACCEPT
iptables -A INPUT -p tcp -m tcp -m multiport --dports 80,443 -s 103.21.244.0/22 -j ACCEPT
iptables -A INPUT -p tcp -m tcp -m multiport --dports 80,443 -s 103.22.200.0/22 -j ACCEPT
iptables -A INPUT -p tcp -m tcp -m multiport --dports 80,443 -s 103.31.4.0/22 -j ACCEPT
iptables -A INPUT -p tcp -m tcp -m multiport --dports 80,443 -s 104.16.0.0/12 -j ACCEPT
iptables -A INPUT -p tcp -m tcp -m multiport --dports 80,443 -s 108.162.192.0/18 -j ACCEPT
iptables -A INPUT -p tcp -m tcp -m multiport --dports 80,443 -s 131.0.72.0/22 -j ACCEPT
iptables -A INPUT -p tcp -m tcp -m multiport --dports 80,443 -s 141.101.64.0/18 -j ACCEPT
iptables -A INPUT -p tcp -m tcp -m multiport --dports 80,443 -s 162.158.0.0/15 -j ACCEPT
iptables -A INPUT -p tcp -m tcp -m multiport --dports 80,443 -s 172.64.0.0/13 -j ACCEPT
iptables -A INPUT -p tcp -m tcp -m multiport --dports 80,443 -s 173.245.48.0/20 -j ACCEPT
iptables -A INPUT -p tcp -m tcp -m multiport --dports 80,443 -s 188.114.96.0/20 -j ACCEPT
iptables -A INPUT -p tcp -m tcp -m multiport --dports 80,443 -s 190.93.240.0/20 -j ACCEPT
iptables -A INPUT -p tcp -m tcp -m multiport --dports 80,443 -s 197.234.240.0/22 -j ACCEPT
iptables -A INPUT -p tcp -m tcp -m multiport --dports 80,443 -s 198.41.128.0/17 -j ACCEPT
iptables -A INPUT -p tcp -m tcp -m multiport --dports 80,443 -s 199.27.128.0/21 -j ACCEPT
iptables -A INPUT -p tcp -m tcp -m multiport --dports 80,443 -j DROP`

3. Save permanently the iptables

apt-get install iptables-persistent
It will ask you if you want to still use your current iptables, press YES everytime.

Save your iptables
iptables-save > /etc/iptables/rules.v4

Such an unnecessary waste of RAM with that many rules, when this would do the same thing

iptables -A INPUT -p tcp -m tcp -m multiport --dports 80,443 -s 127.0.0.1/8 -j ACCEPT
iptables -A INPUT -p tcp -m tcp -m multiport --dports 80,443 -s 103.21.244.0/22 -j ACCEPT
iptables -A INPUT -p tcp -m tcp -m multiport --dports 80,443 -s 103.22.200.0/22 -j ACCEPT
iptables -A INPUT -p tcp -m tcp -m multiport --dports 80,443 -s 103.31.4.0/22 -j ACCEPT
iptables -A INPUT -p tcp -m tcp -m multiport --dports 80,443 -s 104.16.0.0/12 -j ACCEPT
iptables -A INPUT -p tcp -m tcp -m multiport --dports 80,443 -s 108.162.192.0/18 -j ACCEPT
iptables -A INPUT -p tcp -m tcp -m multiport --dports 80,443 -s 131.0.72.0/22 -j ACCEPT
iptables -A INPUT -p tcp -m tcp -m multiport --dports 80,443 -s 141.101.64.0/18 -j ACCEPT
iptables -A INPUT -p tcp -m tcp -m multiport --dports 80,443 -s 162.158.0.0/15 -j ACCEPT
iptables -A INPUT -p tcp -m tcp -m multiport --dports 80,443 -s 172.64.0.0/13 -j ACCEPT
iptables -A INPUT -p tcp -m tcp -m multiport --dports 80,443 -s 173.245.48.0/20 -j ACCEPT
iptables -A INPUT -p tcp -m tcp -m multiport --dports 80,443 -s 188.114.96.0/20 -j ACCEPT
iptables -A INPUT -p tcp -m tcp -m multiport --dports 80,443 -s 190.93.240.0/20 -j ACCEPT
iptables -A INPUT -p tcp -m tcp -m multiport --dports 80,443 -s 197.234.240.0/22 -j ACCEPT
iptables -A INPUT -p tcp -m tcp -m multiport --dports 80,443 -s 198.41.128.0/17 -j ACCEPT
iptables -A INPUT -p tcp -m tcp -m multiport --dports 80,443 -s 199.27.128.0/21 -j ACCEPT	
iptables -A INPUT -p tcp -m tcp -m multiport --dports 80,443 -j DROP

And for anyone that wants the IPv6 rules:

ip6tables -A INPUT -p tcp -m tcp -m multiport --dports 80,443 -s 2400:cb00::/32 -j ACCEPT
ip6tables -A INPUT -p tcp -m tcp -m multiport --dports 80,443 -s 2405:8100::/32 -j ACCEPT
ip6tables -A INPUT -p tcp -m tcp -m multiport --dports 80,443 -s 2405:b500::/32 -j ACCEPT
ip6tables -A INPUT -p tcp -m tcp -m multiport --dports 80,443 -s 2606:4700::/32 -j ACCEPT
ip6tables -A INPUT -p tcp -m tcp -m multiport --dports 80,443 -s 2803:f800::/32 -j ACCEPT
ip6tables -A INPUT -p tcp -m tcp -m multiport --dports 80,443 -s 2c0f:f248::/32 -j ACCEPT
ip6tables -A INPUT -p tcp -m tcp -m multiport --dports 80,443 -s 2a06:98c0::/29 -j ACCEPT
ip6tables -A INPUT -p tcp -m tcp -m multiport --dports 80,443 -j DROP

P.S. If the default ‘iptables’ policy is set to “DROP” with iptables -P INPUT DROP & ip6tables -P INPUT DROP then the last rule in each rule set is redundant.

Thank you, I modified it.