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. It happens to be the right time to put together some desires for the foreseeable future. I have looked over this document and if I could, I wish to recommend you number of remarkable recommendation.

  2. CrowdforApps说道:

    This page really has all the information and facts I needed about this subject and didn’t know who to ask.

  3. CrowdforThink说道:

    Having read this I thought it was very informative. I appreciate you spending some time and effort to put this content together. I once again find myself spending a lot of time both reading and posting comments. But so what, it was still worth it.

  4. Cherri Obyrne说道:

    Hey here, just got aware about your blog page through Yahoo and bing, and have found that it's seriously informational. I’ll appreciate should you decide retain such.

  5. Dani Shindel说道:

    Very good article. I am experiencing some of these issues as well..

  6. Maile Brusseau说道:

    I quite like looking through a post that will make men and women think. Also, thanks for allowing me to comment.

  7. Nowhere on the Internet is there this much quality and clear information on this subject. How do I know? I know because I’ve searched this topic at length. Thank you.

  8. Metroclick.com说道:

    MetroClick specializes in building completely interactive products like Photo Booth for rental or sale, Touch Screen Kiosks, Large Touch Screen Displays , Monitors, Digital Signages and experiences. With our own hardware production facility and in-house software development teams, we are able to achieve the highest level of customization and versatility for Photo Booths, Touch Screen Kiosks, Touch Screen Monitors and Digital Signage. Visit MetroClick in NYC at http://www.metroclick.com/ or , 121 Varick St, New York, NY 10013, +1 646-843-0888

  9. Sapid Agency is a Search Engine Optimization company in New York City that provides SEO Services. Their proprietary SEO strategies help struggling websites and aspiring business owners to rank their websites higher in multiple search engines like Google , Yahoo and Bing. They provide local and gmb map ranking for businesses in NYC and many other local areas. Find more at https://www.sapidagency.com/ @ 145 E 57TH NEW YORK, NY 10022, USA, +1 971 341 5608 USA

  10. Lawerence Robie说道:

    Hi here, just turned out to be conscious of your blog site through Search engine, and have found that it's very beneficial. I’ll be grateful should you decide continue on this informative article.

  11. important link说道:

    I simply want to say I'm newbie to weblog and seriously liked your blog. Likely I’m going to bookmark your blog post . You actually have great articles. Cheers for sharing with us your webpage.

  12. Sapid Agency说道:

    Sapid Agency is a Search Engine Optimization company in New York City that provides SEO Services. Their proprietary SEO strategies help struggling websites and aspiring business owners to rank their websites higher in multiple search engines like Google , Yahoo and Bing. They provide local and gmb map ranking for businesses in NYC and many other local areas. Find more at https://www.sapidagency.com/ @ 145 E 57TH NEW YORK, NY 10022, USA, +1 971 341 5608 USA

  13. Extraordinarily helpful points you have mentioned, thanks so much for publishing.

  14. Lonny Cornes说道:

    It is usually suitable opportunity to generate some plans for the long-run. I've read through this document and if I should, I want to encourage you a few important proposal.

  15. Omer Seit说道:

    Hiya here, just got aware about your web page through Search engine, and have found that it's pretty entertaining. I’ll take pleasure in if you keep up this informative article.

  16. Stevie Frantzich说道:

    It is usually suitable occasion to create some intentions for the longer term. I've go through this posting and if I should, I wish to suggest to you you some entertaining ideas.

发表评论

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

82 − 78 =