Walks

网络安全爱好者

Python3学习系列(三):编写属于自己的端口扫描器

《Python3学习系列(三):编写属于自己的端口扫描器》

前言:接下来会写一系列Python在安全方面的小脚本,大家可以试着敲敲代码,可以用来提升自己学习的兴趣,学习有回馈才有动力。


目录:

0x01:处理流程

0x02:代码实现

0x03:逐行解析


0x01 处理流程

《Python3学习系列(三):编写属于自己的端口扫描器》


0x02 代码实现


# -*- coding: utf-8 -*-
'''
Program:端口扫描器
Function:利用socket库连接端口,进行IP的开放端口扫描
 
Version:Python3.6
Time:2017/10/13
Author:Walks
个人博客:http://www.bywalks.com
'''
 
from socket import *
import threading
 
#建立线程锁
lock = threading.RLock()
openNum = 0
threads = []
 
#端口扫描函数
def portScanner(host,port):
global openNum
    try:
        s = socket(AF_INET,SOCK_STREAM)
        s.connect((host,port))
        if lock.acquire():
            openNum+=1
            print('[+] %d open' % port)
            lock.release()
        s.close()
    except:
        pass
 
def main():
    setdefaulttimeout(1)
    #扫描1-1024端口
    for p in range(1,1024):
        t = threading.Thread(target=portScanner,args=('119.188.97.150',p))
        threads.append(t)
        t.start()     
 
    for t in threads:
        t.join()
 
    print('[*] The scan is complete!')
    print('[*] A total of %d open port ' % (openNum))
 
if __name__ == '__main__':
    main()


0x03:逐行解析


# -*- coding: utf-8 -*-
'''
Program:端口扫描器
Function:利用socket库连接端口,进行IP的开放端口扫描
 
Version:Python3.6
Time:2017/10/13
Author:Walks
个人博客:http://www.bywalks.com
'''

#导入socket和线程库
from socket import *
import threading
 
#建立线程锁,Rlock用来确保多线程多共享资源时,只有一个线程访问共享资源
lock = threading.RLock()
#开放端口数量计算,在这为共享资源
openNum = 0
#线程列表
threads = []
 
#端口扫描函数
def portScanner(host,port):
global openNum
    try:
        #定义socket对象
        s = socket(AF_INET,SOCK_STREAM)
        #连接IP和端口,当连接了才可以运行接下来的代码
        s.connect((host,port))
        #线程锁的利用
        if lock.acquire():
            openNum+=1
            print('[+] %d open' % port)
            lock.release()
        #关闭IP:端口连接
        s.close()
    except:
        pass
 
def main():
    #设置脚本timeout,为socket库里面的
    setdefaulttimeout(1)
    #扫描1-1024端口,range(1,1024)就是1-1024
    for p in range(1,1024):
        #建立线程对象
        t = threading.Thread(target=portScanner,args=('119.188.97.150',p))
        #将对象添加到线程列表
        threads.append(t)
        #开始线程
        t.start()     
    #join:如果一个线程在执行过程中要调用另外一个线程,要等到其完成后才可以运行
    for t in threads:
        t.join()
 
    print('[*] The scan is complete!')
    print('[*] A total of %d open port ' % (openNum))
 
if __name__ == '__main__':
    main()

 

扫描成功图片


《Python3学习系列(三):编写属于自己的端口扫描器》

转载请注明来自:www.bywalks.com

点赞
  1. I wanted to jot down a small word in order to appreciate you for all of the fantastic strategies you are placing here. My extensive internet look up has now been paid with excellent knowledge to talk about with my family and friends. I 'd repeat that we visitors are truly lucky to exist in a good place with so many lovely people with good solutions. I feel somewhat happy to have discovered the web page and look forward to tons of more amazing moments reading here. Thanks a lot once again for a lot of things.

  2. My spouse and i got absolutely relieved when Chris could carry out his analysis via the precious recommendations he acquired from your site. It's not at all simplistic to just continually be giving for free guides which most people could have been selling. And now we see we need you to give thanks to for that. The specific illustrations you have made, the straightforward blog menu, the relationships your site help promote - it's got most spectacular, and it is letting our son and our family know that the subject is exciting, and that's exceedingly mandatory. Thanks for the whole lot!

  3. I as well as my guys have already been checking the great secrets from the website and then then came up with a terrible suspicion I never thanked the web blog owner for those tips. All the people came as a result passionate to study all of them and have in effect without a doubt been enjoying them. Thank you for indeed being so considerate and also for utilizing certain nice things most people are really eager to discover. Our own honest apologies for not expressing appreciation to earlier.

  4. nmd说道:

    I am also writing to make you understand of the exceptional discovery my cousin's daughter went through studying yuor web blog. She mastered plenty of issues, not to mention what it's like to possess an excellent helping mindset to make the rest with no trouble learn several impossible subject matter. You actually did more than visitors' expected results. Many thanks for delivering these valuable, dependable, edifying and cool tips on the topic to Emily.

  5. yeezy 500说道:

    I would like to express appreciation to the writer for rescuing me from such a incident. Because of researching throughout the the net and getting basics which are not helpful, I was thinking my life was over. Existing without the approaches to the issues you have sorted out as a result of your entire guideline is a critical case, as well as the kind which might have in a negative way damaged my career if I hadn't discovered the website. Your know-how and kindness in playing with every aspect was valuable. I don't know what I would've done if I had not discovered such a stuff like this. It's possible to at this time look forward to my future. Thanks a lot very much for your professional and effective guide. I will not be reluctant to refer your web sites to anybody who desires tips on this subject matter.

  6. chrome hearts说道:

    I not to mention my guys were actually looking at the excellent suggestions located on the blog while immediately got a terrible feeling I had not expressed respect to the site owner for those secrets. The boys happened to be consequently warmed to read through all of them and have simply been taking pleasure in them. Appreciation for getting indeed helpful as well as for selecting these kinds of high-quality subject matter millions of individuals are really wanting to be informed on. My honest apologies for not expressing appreciation to you sooner.

  7. reebok outlet说道:

    I enjoy you because of your whole effort on this web site. My mother enjoys carrying out investigation and it is easy to understand why. Most people hear all of the lively mode you render rewarding solutions by means of the web blog and as well as recommend response from other individuals about this content so my daughter is truly learning a great deal. Have fun with the remaining portion of the new year. You are always doing a fabulous job.

  8. Thanks a lot for giving everyone an extraordinarily splendid chance to read in detail from here. It is always very beneficial and as well , packed with a lot of fun for me and my office peers to visit your website really three times per week to see the latest tips you have got. And indeed, I'm so always amazed considering the awesome things you give. Some 4 facts in this article are ultimately the most effective I have ever had.

  9. off-white说道:

    I would like to convey my affection for your kind-heartedness for persons that really want help on this one content. Your special commitment to getting the message all over appeared to be exceptionally important and has all the time enabled men and women like me to reach their desired goals. This important useful information implies a great deal to me and substantially more to my fellow workers. Thanks a lot; from all of us.

  10. nike foamposite说道:

    Needed to draft you this little bit of observation just to thank you so much yet again for the superb methods you've documented on this site. It was certainly surprisingly open-handed with you to provide unreservedly what exactly a lot of folks would have offered for an electronic book to get some dough on their own, primarily now that you could have done it if you decided. These advice additionally served like the good way to be sure that other people have a similar dream like mine to know the truth way more in respect of this problem. I am sure there are many more pleasant opportunities in the future for individuals who see your blog post.

  11. I intended to put you one little bit of note to be able to thank you as before for your superb concepts you have provided on this page. It was certainly unbelievably open-handed with you to make without restraint precisely what most of us could have made available as an ebook to make some cash on their own, primarily seeing that you might well have done it in the event you considered necessary. The thoughts additionally acted to become good way to be aware that the rest have similar dream just as mine to understand great deal more on the subject of this problem. Certainly there are a lot more fun opportunities ahead for individuals that find out your website.

  12. yeezy boost 350说道:

    I wish to show my thanks to you just for bailing me out of such a setting. Right after surfing throughout the the web and getting ideas which are not powerful, I figured my entire life was done. Living minus the approaches to the difficulties you have solved as a result of your main posting is a serious case, and those that would have adversely damaged my career if I hadn't come across your web blog. Your own understanding and kindness in controlling all the details was valuable. I am not sure what I would've done if I hadn't come upon such a stuff like this. I am able to now relish my future. Thanks a lot so much for the professional and amazing help. I won't hesitate to recommend your blog to any person who wants and needs support about this subject matter.

  13. kd shoes说道:

    I happen to be commenting to make you know of the remarkable discovery my cousin's child went through reading your site. She learned some issues, not to mention what it is like to have a wonderful coaching mindset to get men and women really easily comprehend certain advanced things. You actually exceeded her expectations. Many thanks for showing those powerful, trusted, edifying and also cool tips on this topic to Tanya.

  14. I want to express my thanks to the writer for bailing me out of this trouble. Just after researching through the world wide web and getting techniques which were not powerful, I believed my life was gone. Living without the presence of solutions to the difficulties you've sorted out by way of your main guideline is a serious case, and ones that would have adversely affected my entire career if I hadn't come across your web site. Your good competence and kindness in taking care of every item was vital. I'm not sure what I would've done if I had not come upon such a step like this. I can also at this time relish my future. Thanks so much for your reliable and results-oriented guide. I will not be reluctant to endorse your blog to any individual who ought to have counselling on this subject matter.

  15. ferragamo belt说道:

    I precisely needed to appreciate you all over again. I am not sure the things that I would have achieved without the advice shared by you on my theme. This was a very frightening condition in my position, but spending time with your well-written technique you solved it took me to weep with joy. Now i am happy for your advice and hope you know what a great job you're undertaking instructing people today using your web site. I am sure you've never encountered all of us.

  16. gucci belts说道:

    I needed to write you that bit of observation to help thank you once again for these unique pointers you have featured at this time. It is certainly shockingly generous of you to allow unreservedly what a few people would've offered for sale as an e-book in order to make some bucks for their own end, principally considering the fact that you might have done it in case you wanted. Those tricks additionally served like the great way to comprehend other individuals have the same interest like my very own to learn many more when considering this problem. I know there are millions of more pleasurable times up front for people who scan through your blog.

  17. kobe shoes说道:

    I precisely wished to thank you so much yet again. I am not sure the things that I might have carried out in the absence of those points shown by you over such a field. It became a very daunting matter in my opinion, nevertheless noticing your professional tactic you solved it took me to leap with contentment. Now i'm grateful for this work and pray you realize what a great job you have been putting in training most people all through a site. Probably you have never come across all of us.

  18. nike air max 90说道:

    I together with my buddies have already been reviewing the good tricks found on your web page and then immediately got a horrible suspicion I never thanked the site owner for those strategies. These young men ended up for this reason happy to learn them and have now extremely been enjoying these things. Thank you for simply being indeed considerate as well as for finding some outstanding areas millions of individuals are really desperate to understand about. Our own sincere apologies for not saying thanks to sooner.

  19. I truly wanted to type a quick word to express gratitude to you for some of the awesome items you are giving out on this site. My time consuming internet investigation has now been rewarded with good information to exchange with my classmates and friends. I would express that most of us site visitors actually are undeniably endowed to be in a notable community with very many marvellous professionals with useful techniques. I feel really blessed to have seen the web pages and look forward to tons of more entertaining minutes reading here. Thank you once again for everything.

  20. I in addition to my pals happened to be following the best helpful tips on your website then instantly got a terrible feeling I had not thanked the web site owner for those tips. These men became consequently warmed to read all of them and now have in reality been taking advantage of them. We appreciate you simply being indeed helpful and then for utilizing this kind of amazing subject matter millions of individuals are really desirous to know about. Our sincere regret for not saying thanks to you earlier.

  21. nike roshe说道:

    My spouse and i ended up being cheerful Peter managed to finish off his research out of the ideas he discovered from your own web site. It is now and again perplexing to simply be offering points which usually the others may have been trying to sell. Therefore we fully grasp we need the writer to be grateful to for that. The most important explanations you made, the straightforward site menu, the friendships you will give support to engender - it is many terrific, and it's really aiding our son in addition to us recognize that this theme is enjoyable, and that is unbelievably important. Many thanks for all the pieces!

  22. adidas nmd说道:

    I wish to voice my passion for your generosity in support of those individuals that require guidance on this important matter. Your special dedication to passing the message all around appears to be astonishingly important and has continuously made workers just like me to attain their pursuits. Your interesting tips and hints entails a whole lot a person like me and a whole lot more to my fellow workers. Many thanks; from all of us.

  23. chrome hearts说道:

    I in addition to my buddies were actually studying the excellent solutions from your web site then quickly came up with a terrible suspicion I never expressed respect to the blog owner for those strategies. These guys were definitely certainly joyful to read them and have honestly been enjoying them. Appreciate your actually being very thoughtful and for considering varieties of marvelous subjects millions of individuals are really needing to be informed on. My very own sincere regret for not saying thanks to earlier.

发表评论

电子邮件地址不会被公开。 必填项已用*标注