How to create nmap scanner using python3.9 Best Way

Using Nmap Port Scanner with Python-nmap

Welcome to Cyber Geek in this lecture we’re going to make our own nmap scanner using python module with the help of these libraries.So first of all you have to go to these sweeps said that is Or simply you can search for nmap for your machine and after that I’m on windows that means I’ll just download this Windows binary. You can simply click on this installer. You’ll save this by downloading.nmap scanner using python

And after that you just download that you should open it and you should install on these set  C drive and you’ll see that it is installed in program files. And here is your map. So next thing is just to copy the installation the file location. And after that you have to go to your P.C. and go to the properties up to that advanced system setting environmental variables inside this path.

You have to add this inside the path which I have already did and put the map location but now you can simply click on new and you can just paste it over there. So now after  you apply all of this, then go back here. And now we have a successful installation map.
 Next thing is to install these Python Nmap module. So you have to open your terminal and simply [ pip install python-nmap ] copy from here and this go back to the terminal paste right here.
 After that you have to this install this Python Nmap. I have a already install and now after closing this you are good to go with the coding.nmap scanner using python
So before that I’ll just open my Python IDLE and see what we are just doing here.nmap scanner using python

Nmap scanner using python

So in this we have to make these Nmap module and we’ll just make a port scanner with the help of these python nmap module. So first of all you have to import that Nmap that is (import nmap). And now let’s instantiate this in map and let me see scan is the object and after that Nmap Dot Port Scanner Class ( scan = nmap.PortScanner () ) . So this is the important class. So it is a port scanner if I just click Okay. There is single pop up screen that means port scanner is up and running now so you can now scan any IP field over here. 
So you have to first type the scan and the method over here is scan method. So in this method you have to give the host hostname here is IP address so I’ll simply give the local just to give you a real quick example after that you have to give the port number. So I’ll just say “22-443”  let’s take this port number and hear the arguments is optional. So I’ll just leave it here and I’ll just Hit Enter. 
It is going to scan and it will come after a minute so let’s wait and see the results. 
It will take some time and give you results like this so you can see you have the Dictionary over here. The result is in the form of Dictionary containing some of this information you could see the host over here you could see the Version you could see the name you could see everything over here. You can see the services you can see the method which is obviously the syning method you’ll see all of this in the next minute but for now you can see the scaninfo which is TCP protocol over here and now you can reference each of these keys and values of this Dictionary. So if you want let me say command line you can simply do something like this is scan dot and command line ( scan.command_line() ).  So if you see this and if I hit enter you are going to get the command line over here  and the next thing you can also do is you can also get the protocols all of these protocols up. 
So you can have something like this IP over here. So first of all it should be IP which is obviously our local host. And if we scan and get all the protocols through this all protocol method and if you just hit enter you’ll just get TCP over here which is our protocol. 
If it has more protocol than this will just print out in the form of list. So next thing what you can also do is you can also see this step if it is running or not. So we’ll just say scan again and local host which is obviously these host We are just scanning and now I have to just see they state if it is up and running or not if a hit enter it is saying up because it is running so you can just take all of these states and all of these key and value appear from this dictionary so you can also take something like the hostname if you let me just grab all of this which is obviously our host. 
And if I just stick for the hostname with this hostname method you’ll see it is local host. 
nmap scanner using python
So you can do multiple things from this scan nmap port scanner which is going to scan us with this source name(Local IP). And this is the port to be the source (22-443). And next one here will be the argument. So argument will be in the form of the characters and you can the search in the Google so something like this will appear.
So these are all your arguments. So if you see yes “v” over here it is the port. It is basically searching for the open port to determine the service of version info. That means if we go back to our IDLE this “-sV” argument refers to the open port because it is searching for the open port to determine the service and version info. So these are some arguments to be search. 

Now Let’s Make Our Own Nmap scanner using python :

So we’ll make now our nmap scanner and we’ll see how we can just scan the IP addresses, so with the UDP TCP and the comprehensive scan so let’s open up our PyCharm.nmap scanner using python
 Let’s do real state coding now. So first of all we are going to see the three type of scanner which we are going to encounter over here. So first over here we’ll be seeing a “SYN scan”, and the next one would be your “UDP scan”. And the last one would be the “Comprehensive scan”.

# SYN Scanning :

So first of all let’s see about the single scan which is also known is that TCP scan. So this single scan is detected specifically it is a way that the Malicious hacker or cracker will use to communicate with the victim’s port without establish a full connection. That means this scanning will just scan the port number but it won’t have any connection with the victim computer. This approach is the oldest approach. Basically it’s the cracker use and it is also used to perform the denial of service attack and the sync scanning is also known as the half open scanning because in this case we are not connecting fully with the client computer. Instead we are just having a half open scanning attack over here.

# UDP Scanning :

 So next one would be your UDP scan. So it is also quite similar to syn scan. But the UDP is connectionless protocol that means there is no equivalent to this TCP syn packet. However, if the UDP packet is ascend to the victim computer or open port the system will respond within Port. That is the unreachable message. And in that way the response will indicate that the port is open. So it is basically a two type of scanner will see in the argument section how this is can differ from each other. nmap scanner using python

Let’s Create the Nmap Scanner Module :

So let’s start coding here. So first of all as always we have to import the nmap module. So let’s make this scanner now. So first of all we have to instantiate this nmap module that means with the help of port  scanner which we have seen about this port a minute before. So first of all let me just give the message to the user saying welcome to our nmap port scanner. So this will be the message to the user.  And now after this what do you need to do is you need to have IP address so nmap scanner using python
IP address would be input from the user. So you knew how to input it is going to give us the message to the user saying please enter the IP address you want to scan OK. This will be the message to the user. It will just input the IP address and let’s bring this IP address again to the user saying the IP address is you can simply format this with your IP address. So let’s see the type of IP address so you know what this type method does.nmap scanner using python nmap scanner using python
 So next thing is to get the response so what this response does is it take the input from the user to see what the user want.
 So if a user want to have the syn scanning they need to just get to the syn scanning. So what this response does it, it will have the flow control. That means we’ll use the conditional statement to have the flow control between this scan. So if user presses one then it will redirect to the syn scan if he presses two it redirect to the UDP scan if he presses three then it will just redirect to the comprehensive scan. So that means there will be flow controls. So in order to get that number in turn will use input again and if we remember we have this triple quote to have the multiple line. 
So I’ll just make a multiple line in a single new line over here that is with this line and I’ll just say please enter the type of scan  you want to perform so we can just go down because this is a multiline comment. So first of all let’s give one here saying if you want to sync scan and the second one will be UDP scan and the third one here will be comprehensive so this will be three scans and if I  put the new line again. So this will be the response from the user or basically to the user. So after that they will just get that number as a print. You have selected option and that will be “resp”. Which is basically the number.nmap scanner using python
So lets check now if (resp=1)  so if we just do one. This is not going to work. Because if we see this resp is from input method and we have talked about this in a multiple of times before in the basic Python Lexa that this input method is going to give us this string and this one is basically an integer so we cannot compare this input which is in the form of the string with this one. nmap scanner using python
So we have to pass this as a string.nmap scanner using python So first of all we just compare with the one and if it is the case we have to now do something that is your sync is scanning. So let’s do a syn scanning so first of all let’s nmap scanner using python check for the version of the nmap. So I’ll say nmap version, print this nmap version first. And it will be done with this scanner which you can see here which is the object of this map and will make use of map version method over here.
So this method will be called to check the version of the nmap that is installed on our machine. So next thing is to see that what this scan does. So we have seen this scan was going to give us the dictionary. So let’s see how we can just get the dictionary. So first of all first parameter that is to be seen in this scan is IP address which is entered by the user here. So the next thing was the port range . So I want to check from 1 to 1024  the number of port to be scanned is still here and the next one is argument. So let’s talk about this argument. So first of all see V and the next one is sS.nmap scanner using python
 Yes. So what we did here was we just gave the argument to this scan saying that it has to just scan with the verbosity and the SS is to scan for the syn connect acknowledges windows or minimum scans.
If you want to know for further then you can check this documentation online in this book reference page. So search for V which is obviously our verbosity. So that should be right here. It is saying increase verbosity level and you can also use VB for more greater effect. So this is your argument which is just we just pass right here .And this sS means that we want to just see the TCP syn scanning. So this is the way to put the argument to our scan method. So next thing nmap scanner using python what you need to do is to print that. So basically what we get from here is the dictionary. So I want to get the scanning info from the dictionary. So what this  scanning info does is it will just give the Dictionary for the method like syn in services.
So next thing what I want is the IP status so let’s print. If that IP status is up or down. So for that we have to first of all have to type the scanner and we have seen these in a minute before.
 Because in that case if I just go to IDLE I’ll so you over here what we just did was in the case of this state. So we just pass here the local host. We saw the state which was up running or not. So we’ll do the same over here. So instead of local host we have to pass IP address that is enter from the user. And now we have to see the state before that. Let me just print some messages over here.
And after i’ll just put the status which will be done with this state method. So we have now the state method which is going to give us the status either up or down and the next thing I want to give here is our protocols which will be TCP protocol. nmap scanner using python
So let’s make use of this method to  this a scanner and let’s put again the IP address inside the scanner and let’s check for all protocols.So we will check for the open ports so to do that we have again this scanner and we’ll just pass IP address that is enter from the user.
Now in this case we have to make use of the TCP keys because this is the TCP scanning So we will  pass TCP and now we’ll just get the keys which is for the values. So what this result is basically this will give this last statement will give us the dictionary and dictionary will be in the form of the port number. So we’ll get number ports that is open so that can be something like the dictionary which will be in the form of 123 or 45. Any port that is open will be result from this is statement
So this is our pretty much the first scanning which is our TCP scan. So we have completed our TCP scan over here. Let’s go to the elif part and type something like if the response is greater than maybe 20 then print Invalid something message like this. So first of all I’ll just go to the run run the main so it is showing  error.

 We have to check if it is one or not. So let’s run this again on the main. It is saying enter the IP address to scan.
 So it is going to give us the IP address to  scan. So let’s see our IP address from here. IP config (open command prompt).nmap scanner using python
 So I’ll just type here my ip address. And it is the message that we just get here now which is saying input which scan we have.
So in this case we have only the one which is obviously the ACK scan which is syn ACK scan. So that means all this put here one. So I’ll just enter one now and it is giving us the selective options
 f you see here we are just printing the nmap version so the first thing we get is nmap version  and the next we get is this TCP nmap scanner using python so this is in the form of the dictionary which are the result of this scanning for method and it is giving us the method which is obviously sync method which we just did here.nmap scanner using python
This is how you can create you own nmap scanner using python nmap scanner module if you try this on your own system you’ll get a better hold of how this is working and the why we used all those methods like service method to see what are the services , we also saw the protocols method to see the protocols running.

Thanks for Reading.
By : Akash Kumar Singh 

Leave a Comment

sixteen − 9 =