In this video you're going to learn how to code a port scanner with animate in python.
Now if you didn't know and met this probably the most popular network scanner however and map does much
more than basic and advanced for scanning.
For one it has the very powerful and map scripting engine or NFC which allows you to automatically use
and map scripts to run different vulnerability assessments and I might get into just that in an advance
penetration testing with Python course or of course you can explore the topic yourself.
My point here is to open your mind the possibilities enabled by combining your python skills with and
maps capabilities.
All right.
So first if you don't have the internet library installed you should open up a command prompt or terminal
and do a bit or bit three install.
So there are big three depending on how you actually use bip
python and map.
I need to tell you that there are many Python libraries for an map so please make sure to install this
exact one.
Now to make sure it's been installed correctly so I'm not going to run this command because I already
have Python installed in my system.
So to make sure it is installed correctly let's open up python shell so python and then type import
and map
we've got no errors so we're good to go.
So let's exit this.
All right let's go back to the code Ed before we go into coding.
I need to tell you that to be able to use the python library and add a map you need to have an map installed
of course on your system.
I already have an map installed on my system on my window assistance here.
But if you don't have an map installed just go through and map dot org and download the version suitable
for your operating system.
So once again two things you need the end map scanner installed on your system and the element by phone
library.
Now I assume you have both.
OK.
Now in this video what we will do is to code a scanner that will take an IP address and scan for ports
21 so 21 22 80 1 3 9 4 4 3 88.
OK.
Which usually represent a few popular services.
Now in my visual studio based security we're going to start by importing and map.
OK we'll also need this service module for command line arguments that will be we'll actually be providing
the target as a command line arguments or he'll say import says all right now we'll start by creating
a very local Target which is going to be taken from the first command line arguments always a target
equals strength.
Of CIS are you one all right.
And then we're gonna create a list for the port numbers we want to scan.
So let's save port sequels.
I've said 21 22 80 1 3 9 4 4 3 88.
Then we need to instantiate a scanner variable and map so scan let's say scan underscore V equals and
map port scanner and that's it.
No arguments.
And then we simply scan all the ports we provided in the fourth list and for that we're going to use
a for loop.
But first let's actually display a nice output to the user so let's say print on a new line
scanning
target for ports
21 22 18 1 3 9 4 4 3.
And let's say also 88.
Dot dot dot.
New Line.
And then we actually do our for loop so we'll say for port and ports let's say port scan is scan underscore
V.
So our scanners scan our scanner that we've instantiated scan target.
So we scan the target that we've provided.
And we also need to string define the port number.
OK.
And that's it.
What comes out of this loop is a dictionary with a lot of stuff in it.
And this is actually where the beauty of Python coding kicks in now from the dictionary.
We only want to state the port and to understand everything better let me show you the entire process
in a python shell.
So we're gonna open Python so we're gonna actually try to replicate these in a python shell to actually
better understand how an map works.
So we're gonna import and map and then we'll instantiate a scanner.
So let's a scanner equals and map port scan port scanner like that and then we're gonna provide a target.
So in this case we'll just simply provided over here so scanners that scan let's say Yahoo or yahoo
dot com and also the port we want to scan which is also provided as a string.
So I'm going to hit enter to actually run this and the response is gonna be dictionary or the result.
The result of this is actually gonna be a dictionary and it might take a few moments to actually complete
the entire scan.
Okay.
So like I said this is gonna be the result is gonna be in the form of a dictionary.
So how do I know that.
So by simply actually doing type scanner dot IP and we can see.
Class dictionary.
All right.
So let's look into this dictionary.
So if I do scan around the score IP and hit enter we can see the entire dictionary here.
So since this is a port scanner we only want to know the state of the port.
So in this case we're looking for port 80 and the state is open.
So what we actually need is to navigate this dictionary and only get this state open from here.
And for that in Python we'll actually going to use a tab autocomplete for easiness of purpose.
Now if you don't have autocomplete in Python or in the python shell try doing a bit install fiery line.
OK.
And that's actually going to enable your python autocomplete.
All right.
So we need the state of the port like I said so for that we're going to go scanner on their score IP
and then we'll look in to the scan.
All right.
And then the IP we have to specify the IP which in this case is 98 1 3 7.
So we go into the scan and then scan IP.
So ninety eight point one three seven two four six point eight.
And then we have to specify the DCP and then 80 but not as a string but as an integer and this state.
So we want to look at the state of this port.
OK.
And the result that comes out of this is open.
So let's just copy this and go back to our code and end the for loop.
Actually Gonna save print port
Port
is an hour variable.
So this is going to tell us if the port is open and closed filtered et cetera.
All right then we get out of the loop and the further expand the capabilities of our code a little bit
let's also print of the host is up or not.
So let's say print let's actually do it a line will also print on a new line say host and then target
and then another string and then paste.
But in this case we don't need.
So we go into the scanner.
We go into the scan and then we go into the target here.
So scan target and then we look into this status.
So we want to see the if our host is up or down and then we don't see the port.
Of course status.
What do we need this state for minor corrections here.
We do not need to hard code this IP because this is going to be taken from the target.
So the target which is the command line argument and then we also need the port here the port number
and not 80.
OK.
And then this is not scan V but this is the port scan.
So the dictionary is the port scan and we save this and then we open up a command prompt and navigate
to the location and run with Python and map.
And then let's specify Let's scan the local host.
So in all nobody's gonna be offended by scanning myself we should see the output coming quickly.
And there you have it the magic of combining python and penetration testing.
And like I said there is much more to it than what I've shown here.
You can greatly expand the capabilities of this script by using and maps as scripting engine and also
implement other tactics that will help facilitate your penetration testing bug bounty hunting and other
types of cybersecurity assessments.