Saturday, February 23, 2013

Reversing Subnet mask and bit notation

     This article is a follow up to the IP subnetting. In this article I will discuss about reversing subnet mask to find IP address ranges inside a network. I will also explain what is bit notation. Let's get started...

Reversing Subnet Mask

Reversing a subnet mask is very easy to do and it is generally used to find address ranges and class of a network. The default subnet masks for three classes of networks are as follows:
Class A - 255.0.0.0
Class B - 255.255.0.0
Class C - 255.255.255.0
And as we perform subnetting, the host bits on the left side are converted to network bits.
Let's say, for example, we have a subnet mask 255.255.255.224. Now this is a class C network subnet mask (although it is not always the case). Let's convert it to binary-
11111111.11111111.11111111.11100000
Consider the last octet. It has 3 network bits and 5 host bits. It reveals two things. First, since there are 3 network bits and maximum number represented with 3 binary digits is 7 (111), the address range is divided into total 7 segments. Similarly, 5 host bits and the maximum number represented by 5 bits is 32 (11111), there are 32 addresses available per network segment. This is including the network identifier and the broadcast address, so we have to subtract that to get the actual number of usable hosts.

Let's take another example. The subnet mask is 255.255.255.252.
Converting it to binary-
11111111.11111111.11111111.11111100
Concentrate on the last octet. Since it has 6 network bits, there can be 64 individual network segments in this address range. (max. number represented by 6 binary bits (111111) is 64) The given address has only two host bits. Hence, there are only 4 addresses available per segment. Excluding the first and last. There are only two usable addresses per network segment. Two addresses per segment is not very useful inside a corporate network. This type of subnet masking is mainly used in WAN links to avoid wastage of address ranges.

Bit Notation

Bit notation is just another way of representing subnet masks. Subnet masks till now were represented as a series of 4 octets. In the bit notation, we consider only the number of 1's in the binary form of subnet mask and write it against the ip address.

For example, consider ip address 192.168.10.34 having subnet mask 255.255.255.240.
Its binary representation is 11111111.11111111.11111111.11110000
Here we count the total number of 1's in this form which is 28. It can be written with the IP address as-
192.168.10.34/28.
Another example, an ip address 192.168.2.3 has subnet mask 255.255.255.192. Its binary form is 11111111.11111111.11111111.11000000
Here there are 26 1's present and hence bit notation becomes-
192.168.2.3/26.

Thanks for reading! Do leave a comment below...

Thursday, February 21, 2013

IP Subnetting

     In this post I am going to explain what is IP subnetting and how it is done. Subnetting is used to divide a network into two or more segments of networks. Subnetting involves breaking of the available IP address range into different networks as per requirement. There are two main types of subnetting i.e. subnetting based on number of networks required and subnetting based on number of hosts required per network. You figure out the subnet mask through subnetting which will be then applied in configuring the routers and hosts on your network. Lot of subnetting calculators are available which do the calculation for you but learning it is essential if you are planning to take cisco certifications like CCENT and CCNA.

     Before getting started, I would like to talk about the structure of IP address and subnet mask. Each ip address consist of 4 octets i.e. four sets of 8 bit each. Each octet can represent a value of min. 0 (00000000) to max. 255 (11111111). All the four octets can have any value between 0 to 255. A 0 present in the last octets (e.g. 192.168.10.0) indicates or identifies the network segment. A 255 in the last octet (e.g. 192.168.10.255) is usually the broadcast IP address for the network. An address ending in 0 or 255 cannot be used to assign to a host. However, this is not always the case in IP subnetting. 
     A subnet mask defines the network part and the hosts part in an IP address. For example, the subnet mask for class C ip addresses (255.255.255.0) indicates that the first 3 octets in the address are used to indicate the network and the last octet identifies the host from an IP address. When the network is divided, the "hosts part" of the subnet mask is modified according to number of networks or hosts per network desired.
     It is recommended to have basic knowledge of binary conversion and computer networking before going ahead. The examples I will show below are specific to class C networks, however, the same concept can be extended to class A and B as well. 

Subnetting based on number of networks

     Consider a scenario, you have been given an IP address range (say 192.168.1.0 - 192.168.1.255). And you are assigned to divide this range into 10 networks and to figure out the subnet mask. The steps you will follow are as follows:

1. Convert the number of networks required to binary and find out the number of bits.
In this example, we have 10 networks. Its binary equivalent is 1010 which can be represented by 4 bits. Therefore, 4 bits are required to represent number of networks (which is 10).

2. Reserve bits in the subnet mask and find out new subnet mask
The original subnet mask of class C network is 255.255.255.0. Lets represent it in binary. 
11111111.11111111.11111111.00000000
Now, in the above step, we found out that 4 bits are required to represent networks. Hence, in the above subnet mask, we convert 4 host bits to network bits (remember that all 1's represent host bits and all 0's represent network bits.)
11111111.11111111.11111111.11110000
Notice that we converted leftmost host bits to network bits. Now this is the subnet mask required, in binary. Converting it back to decimal,
255.255.255.240 (decimal of 11110000 is 240)

3. Find out the increment and corresponding network ranges
Let's consider the binary form of subnet mask again.
11111111.11111111.11111111.11110000
To find out the increment, we consider only the octet in which the last network bit i.e. 1 lies. Here it is the last octet.
11110000
Now we find out the place value of last "1". Since it is at 5th position from right hand side, its place value becomes 2^(5-1) = 16. (this is equivalent to making all other bits except last network bit "0" and converting back to decimal).
We've got 16 as the increment. This increment is used to find the starting address of each network range. In our case, the starting address of each network range will be 192.168.1.0, 192.168.1.16, 192.168.1.32, 192.168.1.48 and so on...
And the corresponding network ranges will be
192.168.1.0 - 192.168.1.15
192.168.1.16 - 192.168.1.31
192.168.1.32 - 192.168.1.47
192.168.1.48 - 192.168.1.63
and so on...

Here, the starting address of each range will become the network identifier and last address of the range will become broadcast address for that segment. e.g. in the first range, 192.168.1.0 is identifier and 192.168.1.15 is the broadcast address.

Subnetting based on number of hosts

     Let's say we have been given the same IP address range (192.168.1.0-192.168.1.255) and now we're asked to divide this range into segments of 20 hosts/network irrespective of number of networks that will be created. The steps that we'll follow now are a little different - 

1. Convert the number of hosts required to binary and find out the number of bits.
Here, we have 20 hosts per network. 20 can be represented in binary as 10100. So, 20 can be represented in binary with 5 bits. 

2. Reserve the bits in subnet mask and find out new subnet mask
Class C subnet mask in binary form is
11111111.11111111.11111111.00000000
Here since we are concerned with number of hosts, we have to reserve host bits i.e. zeroes. The host bits are always reserved from the right hand side of the last octet.
11111111.11111111.11111111.000"00000"
Here the 5 bits (quoted) will be saved or reserved for the hosts and rest of them will be used for network (i.e. turning them to "1").
Hence the subnet mask becomes
11111111.11111111.11111111.11100000
Decimal representation : 255.255.255.224 (decimal of 11100000 is 224).

3. Find out the increment and corresponding network ranges
Increment can be found out by the similar method as last example. By considering the last bit of the network bits
11111111.11111111.11111111.11100000
Calculating place value of the highlighted bit, we get 32 (2^5).
Therefore, the starting address of each thus formed network range will be incremented by 32. The starting addresses will be 
192.168.1.0, 192.168.1.32, 192.168.1.64, 192.168.1.96, 192.168.1.128 and so on...
The network ranges thus formed are
192.168.1.0 - 192.168.1.31
192.168.1.32 - 192.168.1.63
192.168.1.64 - 192.168.1.95
so on...
and again, the starting and ending addresses of each range will be identifiers and broadcast addresses respectively. 

Well, i'll conclude here for now. In the next article I will describe reversing the subnet mask and talk about bit notation.

Thanks for reading :)

Tuesday, February 19, 2013

My first web server

Hello readers,
I had been willing to develop a small and efficient web server hosted in a LAN for quick transfer of files. A couple of days ago I had some issues with my FTP client when I wanted to transfer something from my ubuntu machine. So I decided to write a simple HTTP server from scratch.

Operation is fairly simple, it binds itself to the port 80 and listens for connections. Once it receives a request, it checks for the presence of requested file. If found, it sends back the same file with HTTP 200 response. Otherwise sends HTTP 404 with the "notfound.htm" under server root directory. If no specific file is requested, it checks for the presence of "index.htm" in the requested directory (default being '/' ) and then serves index.htm page under that directory. The server serves only static pages and files.

It is relatively easy, less than 200 lines of code. Developed on ubuntu linux 12.04. Implements only HTTP Responses 200 and 404. The default server root is "/usr/local/www/". It requires root privileges for binding of socket to the port 80. Interrupts recognized are Ctrl+C for termination. I call it the 'ViServer'.

Please note that this piece of code was written for learning purposes only and you are likely to find it buggy. 

Here is the link to the source code:
https://docs.google.com/file/d/0B_oonTu4H_SIWTRJM3ljNm1KTUk/edit

 Use it for testing on your local machine only and never deploy it in untrusted LAN or worse, facing the internet!

Thank you! :)


Thursday, February 7, 2013

Web development primer

Hello friends,

This is an on demand article about web development. I will cover some basic terms, concepts and ideas related to web development. This article will help those who are planning to jump start into learning web development. The concepts I explained are from a beginner point of view. In the end I will conclude by showing a simple HTML file.

How does HTTP work?
HTTP is the protocol of the internet. HTTP stands for HyperText Transfer Protocol. For the time being, just keep in mind that whatever data transfer occurs through the web follows the HTTP protocol.
HTTP works on request and response. Your browser requests something, the server on the other side sends back the page you requested. Consider an example,
you enter google.com into your browser. Your browser will send a request to the Google's server requesting their homepage. Google's server will send back their homepage in response which will be displayed on your screen. Very simple.
Similar thing is repeated whenever you follow a link or navigate from one webpage to another.

 HTML-
HTML stands for HyperText Markup Language. It is the language in which webpages are written. Whenever a server sends you a webpage, It is essentially sending the webpage in plain text i.e. the contents of the webpage which is nothing but HTML code. Your browser then interprets this HTML code and constructs the webpage on screen according to HTML tags specified. As you maybe aware, HTML files end in ".htm" or ".html" extension. Webpages are just HTML files stored on the web server.

Web Server and Web Application-
Now lets take a closer look at what happens on server's side. Web servers are just like normal computers with higher processing powers. Just like your computer, an OS runs on web servers.  And web server is ultimately an application software that runs over the OS. Popular web server softwares are microsoft IIS and Apache HTTPD. Once the server's hardware receives the request, it is passed to the Operating System which runs it. The OS in turn passes this request to the web server software. The web server software is always running and is listening for requests. On receiving, the server picks up the requested webpage from its directory, processes it and sends to the user. Generally in any directory present on the web server, "index.htm" or "index.php" is the default page if the page name is not specified.
Web application is simply a set of related webpages on the server and maybe connected to a database in the background where they can store the data. Examples of web applications are online shopping websites and user forums.

Client Side Scripting and Server Side Scripting-
Websites have grown more dynamic with time and simple HTML is never enough for the functionality they provide. There are some scripts which can be embedded into HTML pages. These are client side scripts and server side scripts.
Client side scripts are executed inside the user's browser while it is interpreting the HTML code. They are used to add functionality to the user's end. Examples are JavaScript and VB Script.
In the above section, I said that the web server processes the page before sending it to the user. This means that the server itself executes the server side scripts which maybe present in the page. Again, the server side scripts are used for added functionality such as database interaction, command execution and getting user's info. Examples are PHP and ASP.

Sample HTML Program-
<html>
<head>
 <title>Sample</title>
</head>
<body>
 <h1>This is sample text.</h1>
 <br>
 <script language=JavaScript>
  document.write("<h1>This is another sample text</h1>");
 </script>
</body>
</html>

Copy the above code in notepad file and save it as sample.htm or sample.html. Then run it through your web browser, (preferably firefox or chrome). It should show two lines of text. The first line is inserted through html and second one is generated through javascript present in the page.

Resources-
There are many websites where you can learn HTML or other languages for free. The best one according to me is 
http://www.w3schools.com
Also many books focusing on a specific language and platform are also available.

Thanks for reading :)

Friday, February 1, 2013

Shut down windows from web and mobile

Hello Readers,

It's been a while since I posted on the blog and this post is little off-topic. Anyway, today my friend put forth this idea. What if we can shut down our machines remotely. I said it is possible and infact many people are using it to remotely shut down their servers. I couldn't resist the temptation to come home and try it out myself. Through this post I will share with you how we can do it.

The basic idea is very simple. Your computer runs a web server on which a php page having the shutdown code is stored. A remote host or mobile simply accesses this page which runs the code and system command is executed.

For this technique, you will need wamp or xampp server hosted on your computer and a mobile or pc preferably on the same network. I wont go into details of installing servers. For this tutorial, I am using wamp server and mobile both on the same wifi network.

First of all, create a simple PHP file-
<?php
$output = shell_exec('shutdown /s /t 00');
echo " $output ";
?>
Give it any name  (say shutdown.php) and store it under root directory of your web server.

Then start the web server. Make sure that the firewall is turned off. Note down the ip address of this computer by going into cmd and typing "ipconfig".

In my case, it is 192.168.0.100.
Check if your web server is running properly by opening http://localhost/ from the address bar of your web browser.

Now all you have to do is access this php file from any other location on the network. You can access it through mobile or any computer by typing
http://192.168.0.100/shutdown.php
replace the ip with your internal network ip address.

And once you hit enter, your mobile will request the shutdown.php page which will be received by the server installed on the pc. It will then execute the php code inside the page which contains nothing but a system command "shutdown" that starts the process of shutdown. Hence the windows will shut down!

Same concept can be extended to linux platform by simply replacing the shutdown command by its equivalent in linux. Also make sure that the server process running this code has enough privileges to execute the command.

The php program I used is very simple just for the sake of demonstration. In real life, you should not allow the remote user to execute system commands unless he/she is authenticated to the system.
With port forwarding on your router enabled, you can also shut down your PC from anywhere, but again, that is risky from security point of view.

Thanks!
Feel free to ask your doubts in the comments below :)