Welcome to Python Multi-Threaded Port Scanner with tkinter tutorial series. This tutorial is based on the Python version 3.x. Here, we have used tkinter module for the GUI. At the end the Port Scanner we will built will be fast because it will be multi-threaded and GUI friendly.

Initially, you are required to install python on your system. For Windows, navigate to Python Download Page and search for Windows x86 MSI Installer. Download and install it. In Ubuntu install Python 3 by opening the terminal and execute below command

sudo apt-get install python3

Today, we will understand the basics of socket and ports. Sockets are used for networking and it does the purpose of serving the data. Sockets makes the communication possible between the two entities (for ex. Client and the Server). Generally, opening or requesting a website from the browser is like your request goes on particular port (port:80) and you connect to that website via socket.

Port works as the gateway. For example the goods train gets unloaded at the junction (port). And that unloaded goods are further connected to reach the destination (socket). The railway track is like an internet.

Generally, opened-ports are not hacked but they are used to hack into the system. But if the programs/software that has opened the specific port or it works at any default port and it have some vulnerability then hackers take advantage of that and try to overflow the buffer by sending payload. This is how the hack goes. Generally all website have port 80 opened.

Generally, people visit the website and the server serve the content to the client. Here, we will receive the content via socket. In order to use the socket, you are required to import the socket module, which is inbuilt into the python package.

import socket

server = 'hackthissite.org'

port  = 80

ip = socket.gethostbyname(server)

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

s.connect((ip, port))

In the above code we want to connect to the hackthissite.org at port 80. socket.gethostbyname method will return the ip address of the server. Here, s will return the socket descriptor and s.connect method will attempt to connect to the server at port 80.

Now, after creating the connection we want to request something, so the server can response to our request. We will define the request.

request = "GET / HTTP/1.1\nHost: "+server+"\n"

Here we requested the GET data from the server or host via HTTP. Now after defining the request, this request is tend to sent to server. By defining below line the request will be sent to the server.

s.send(request.encode())

As soon as the server will receive the request the server will respond accordingly. To receive the response from the server we will use recv method of socket.

result = s.recv(1024)

1024 is the buffer data size, so that you receive the data in chunks rather then whole data at once. The full program is below.

import socket

server = 'hackthissite.org'

port  = 80

ip = socket.gethostbyname(server)

request = "GET / HTTP/1.1\nHost: "+server+"\n"

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

s.connect((ip, port))

s.send(request.encode())

result = s.recv(1024)

In the next tutorial we will discuss on how you can make the basic port scanner using python. In case of any query, feel free to comment below. Keep sharing. Stay tuned to Tech Tunes.

 

Follow me on Twitter