Python 3 Programming Tutorial - Threaded port scanner
Now that we've seen how to make a simple port scanner in Python 3, we've found that it is quite slow and cumbersome. So here, we tie in our knowledge of the threading module that we learned previously to dramatically improve our performance.
Sample code for this basics series: http://pythonprogramming.net/beginner...
Python 3 Programming tutorial Playlist: http://www.youtube.com/watch?v=oVp1vr...
http://seaofbtc.com
http://sentdex.com
http://hkinsley.com
https://twitter.com/sentdex
Bitcoin donations: 1GV7srgR4NJx4vrk7avCmmVQQrqmv87ty6
Closed Caption:
everybody went into the socket tutorial
video this video is gonna be kind of
building on the last video that were
covering and that was a port scanner and
actually i want to show you guys the
combination of socket and the threading
module which we've already covered the
threading module show you another
example of threading in action so if you
don't remember this was our threading
tutorial here and the actual job that
was being run was actually just time to
sleep 0.5 so it's just an operation of
sleep but as you can see this is very
simple we have a function this example
job . so in theory we should be able
just throw a port scanner right into
example job and that would be it right
so let me move that aside now and we're
going to go ahead i think we'll do is
we'll rewrite most of this just so we
can go over everything one more time and
really rehash got going on so I so going
for socket
we're also going to import threading and
then we're gonna say from Q import q and
then really think we need to import time
for any reason so we're not going to
import a time like we did before and now
we're ready to go we're going to use a
print lock again so was a friend lock
equals reading . capital lock and then
we're going to say our target equals
again
python programming dotnet and now ready
to get started so just like before we
need our port scanner so I'm going to
define port scan and the parameters will
have port then we're going to say s
equals socket . socket and then
parentheses your socket . AF underscore
I neck and then socket . sock stream so
again nothing new so far with sockets
we've already covered with that line
means and now i want to engage in a
trying to accept like we had before and
so we're going to say try con for
connection equals s . connects and then
we want to connect to target comma or a
nice to people here and so we're going
to try to do that and then if that gets
successful they're going to run this
with statement and we say with print
block print poor port is open
ok so we do that and then once we're
done with that we're going to say
conduct clothes and because we want to
close that connection once were finally
done and then accept and then pass so if
it doesn't work we're just going to pass
we're not gonna print out like oh this
is not open or whatever extent we're
just wasting our current statements with
this print lock so that's it for our
port scanner it should look almost
identical to the port scanner that we
built in the previous video so now we're
going to define our threader which
really is going to be exact copy of the
previous threader that we had before so
that's going to be while true and it
will be worker equals Q dot get and then
port scan worker not totally identical I
suppose this was like example job or
scan worker and then finally cute . task
underscore done just as a rehash this is
going to get the worker from the queue
this is going to run the example job
with the worker passing through as the
port numbers so
we specify a range of numbers as workers
so we can also specify them as ports why
not
and then when we're all done we say Q
dot task done to empty out there are you
awesome so then we'll come down here and
we had better to find Q so Q equals q ue
keep arms and now we're ready to create
our workers so to speak how many threads
are we going to run so for now
oh we're going to say we're going to
have also have thirty 30 different
workers so 4x in range 30 so we've got
30 workers we're going to say key equals
reading . thread target equals rudder so
what's happening here threatening .
threads or creating a thread and then
our target for that threatens threader
which does this gets a workers from the
queue so we're using q & threading and
we'll set them to work on port scanning
which is here which we've read in again
all the stuff was covered in its own
right so far we're just combining those
two things so we do what we did already
cover a specific threading tutorial and
we've already covered the port scanner
so sorry for moving too fast if we
aren't check out the previous videos
next tee . Damon equals true because we
wanted to be a demon so it will die when
the main thread guys ante . start which
has to be called after Tina now we don't
really need to be logging like we did
before so we're not going to use the
logging time now we're going to assign
the amount of jobs and so you can think
of the amount of jobs as in the amount
of ports right so we'll say for worker
in range in this time instead of just 30
or 100 or whatever we need to specify
starting point which will be
one and then we'll say 1 201 so we'll
test the first 100 ports and the reason
why we're doing we have to say one is
because port 0 is an invalid port we
can't scan port 0 so anyway moving on
for worker range 1 200 basically I don't
know why it works so finicky like that
but basically it means it will scan port
1 up 2 101 so it really means port 1 200
anyway moving along queued up put worker
so putting that worker to work ok and
then what we want to do is a cute join
so that will wait thread terminates and
that really should be it so hopefully if
you walked through the threading
tutorial with me you can see how we
basically written the same script as we
did with our initial threading tutorial
where the job was just to sleep we
basically replace that with port scan
and that's about it so we just kind of
nest those together so that's kind of
why I wanted to organize the threading
tutorial in the way that I did because
you just replace the function pretty
much with whatever you need to replace
it with so yeah so anyway let's go and
say that same around this if I made any
errors here or if it worked out for us
save run it's been popping up here yep
space cock stream so let's fix that down
to make some errors try again and
immediately we see poor 22news opens
anybody remember how long it took to get
port 22 to show that was open took a
very long time so that was actually very
quick compared to before we actually
showed poor 22 in port 80
well in advance probably wouldn't even
seen it with our original port scanner
yet so anyway that was very quick then
we can continue we could say hey we want
to do 10,000 jobs and we want to have a
hundred threads let's say so we can save
and run that and you can see 2180 world
we we knew about their they're ripping
open immediately
we're going to continue reading through
ports adventure to guess they're
probably a few more ports that are open
how far do we go 10,000 maybe some of
the 9000 will be open
not not quite sure really not sure what
number were on even right now but we'll
see anyway so now we're actually you
know we're using a hundred threads to
port scan so as you can see we can we
could use this to our advantage
threading and B port scanning you know a
lot of ports up at any one time as
compared to the relational port scan
before anyway on if this pops happen to
pop-ups more ports great otherwise I'm
going to start including this video so
that's it with this video in the next
video we're going to be moving away from
port scanning i just wanted to show you
some people port scanning is kind of a
pretty popular topic and involves
sockets it's very basic so it's really
easy to show plus i could measure it
with sockets & threading so i made a
really great example for threading so
just wanted to cover those things the
next ones that will be talking about be
back to more networking purposes sending
and receiving data using you know and
coding and decoding that data so it
works properly so stay tuned for that
you guys have any questions or comments
on this differently than below as always
thank you for watching thanks for all
the support subscriptions in the next
time
no more ports are open
Video Length: 09:36
Uploaded By: sentdex
Published: 8/19/2014
View Count: 61,693