Task To Ping To Google But Not Facebook:

ARTH TASK 13:


task-13.png

TASK DESCRIPTION:

  • Create a Setup so that you can ping google but not able to ping Facebook from same system

TASK SOLUTION:

Firstly to ping to any IP in the world we need to have a routing table for it. The route table can be seen by the command route -n . We have a default route that will help in connecting to any IP in the world. Following is the Output of the command that I got when I entered the command route -n

task-13a.png
Route table and showing able to connect to any public IP

In the above image I have also show that we can connect to any public IP address in the world. This is because of the first record in the routing table which says to reach any destination i.e. 0.0.0.0/0 use the gw 192.168.0.1 . Now lets delete this record and see what happens, to delete the record we can use the command route del default <network_card> . Now after deleting this record we will not be able to connect to any public IP address as there is no route to reach public IP. This is shown in the following image

task-13b.png
route del command and trying to connect to a public IP

Now firstly here in the above case we are using a domain name to connect to a public IP. The only way the computers can communicate with each other is with the help of IP addresses but we have used a domain name to connect to a public IP. As we used a domain name we need to have DNS server to give the IP address for the domain name the DNS server from where the public IPs are obtained is stored in /etc/resolv.conf . The same is shown in figure below

task-13c.png
/etc/resolv.conf to see the DNS servers

In the above image nslookup tool is also used it check for the IP of a given domain name form the DNS records and returns the result, but as the DNS servers are located publicly at 8.8.8.8 and 8.8.4.4 hence as we don’t have a record to connect to the public DNS in the routing table we will not be able to get the IP address for a given domain name this can be solved by adding a record for the DNS server in the routing table this is done by using the command route add <ip_of_host> gw <gateway_ip_address> <network_card> or route add -host <ip_of_dns_host> gw <gateway_ip_address> <network_card>. After doing this nslookup will be able to get the IP address of a given domain name. The same is show in the below picture

task-13d.png
Adding route to DNS server
task-13e.png
Still unable to connect to IP addresses

Now its not all now we can just be able to know the public IP of the google but we will not be able to connect to it as there is no route in our routing table to connect to the IP address given by the DNS server. Here is the tricky part google has lot of public IP addresses DNS finds a public IP that has a less latency in connecting to us and provides that public IP to connect for us. To overcome this problem I have collected the IP addresses where the google server is working and stored them in a github repo (Its not a perfect one but mostly works you can add IP addresses that you think are right one but not present in the list in the repo). Now using the list of the public IP addresses we need to add routes but adding one by one is a slow process and takes lot of time hence I used the power of bash to create the routes in one go this is the command I used

for i in `cat <path_to_the_google_IP_file>`; do route add -net $i gw <ip_address_of_gateway> <network_card>;done;
The same is show in below picture
task-13f.png
Public IP addresses of google
task-13g.png
Adding routes using shell scripts

And the final output is we can now ping to google as we only have the IP address of google in our routing table. Following image shows that I am able to connect to google.com after adding the routes

task-13h.png
Image showing that we are able to connect to google.com
task13i.png
Image showing that no connectivity to fb.com

To delete the routes that you have created above we can just replace del instead of add in the route add command that we used to add the IP addresses using shell. The command looks as follows

for i in `cat <path_to_the_google_IP_file>`; do route del -net $i gw <ip_address_of_gateway> <network_card>;done;
task-13j.png
How to delete the routes created for the google

To revert back to the route table that we have at the begining delete the routes that we added for DNS using route del 8.8.8.8 <network_card> or route del -host 8.8.4.4 <network_card> command. To delete the route for the DNS and to add the route for public world connectivity use the command route add default gw <gateway_IP_address> <network_card_name>

Here you can find the github repo where the public IP addresses of the google are save