Walks

网络安全爱好者

Python3学习系列(二):编写属于自己的网站后台扫描器

《Python3学习系列(二):编写属于自己的网站后台扫描器》 

前言:

最近在学Python,所以就想写点Python学习的文章,目的是为了让大家可以学习点东西的同时也让自己对这些模块的理解更加深一点。 

目录: 

0x01:处理流程 

0x02:代码实现 

0x03:逐行解析 


0x01 处理流程

《Python3学习系列(二):编写属于自己的网站后台扫描器》 

0x02 代码实现  

'''
Program:网站后台扫描工具
Function:通过字典扫描网站后台
 
Time:2017/10/13
Author:Walks
个人博客:http://www.bywalks.com
'''
 
import urllib.request
import time
import threading
 
#与用户交互,也可用命令行
url = input("输入你要扫描到的网址:")
txt = input("输入字典(php.txt):")
 
#保存存在的后台地址
open_url = []
all_url = []
 
#建立线程列表
threads = []
 
 
 
#从字典中读取每一行与url组合,然后添加到all_url
def search_url(url,txt):
    with open(txt,'r') as f:
        for each in f:
            each = each.replace('\n','')
            urllist = url + each
            all_url.append(urllist)
 
#处理URL
def handle_url(urllist):
    print("查找:"+urllist+'\n')    try:
        req = urllib.request.urlopen(urllist)
        #判断返回码
        if req.getcode()==200:
            open_url.append(urllist)
        if req.getcode()==301:
            open_url.append(urllist)
    except:
        pass
     
     
 
#主函数
def main():
    search_url(url,txt)
    #多线程处理
    for each in all_url:
        t = threading.Thread(target = handle_url,args=(each,))
        threads.append(t)
        t.start()
    #线程等待
    for t in threads:
        t.join()
    #扫描成功和不成功的回显
    if open_url:
        print("扫描成功,网站存在的后台地址为:")
        for each in open_url:
            print("[+]"+each)
    else:
        print("没有扫描到网站后台(可能是字典不够给力)")
 
if __name__=="__main__":
    #判断程序运行时间
    start = time.clock()
    main()
    end = time.clock()
    print("The function spend time is %.3f seconds" %(end-start))

0x03:逐行解析

#导入各种库,urllib.request是用来打开和阅读urls,等同于Python2.x的urllib2
#用来时间延时,time.sleep(1),就是延时一秒
#threading用来实现多线程
import urllib.request
import time
import threading
 
#与用户交互,也可用命令行argparse
url = input("输入你要扫描到的网址:")
txt = input("输入字典(php.txt):")
 
#保存存在的后台地址
open_url = []
all_url = []
 
#建立线程列表
   threads = []
 
 
#从字典中读取每一行与url组合,然后添加到all_url
def search_url(url,txt):
    with open(txt,'r') as f:
        for each in f:
            each = each.replace('\n','')
            urllist = url + each
            all_url.append(urllist)
 
#处理URL
def handle_url(urllist):
print("查找:"+urllist+'\n')
#try,防出错
try:
    #利用urlopen打开url
    req = urllib.request.urlopen(urllist)
    #判断返回码,如果为200或者301,则该网页存在,把该url加入到open_url
    if req.getcode()==200:
        open_url.append(urllist)
    if req.getcode()==301:
        open_url.append(urllist)
except:
    pass
 
 
 
#主函数
def main():
search_url(url,txt)
#多线程
for each in all_url:
#建立线程对象
t = threading.Thread(target = handle_url,args=(each,))
#把线程对象加入到线程列表中
threads.append(t)
#线程开始
t.start()
#线程等待(给每一个线程)
for t in threads:
    t.join()
#扫描成功和不成功的回显
if open_url:
    print("扫描成功,网站存在的后台地址为:")
    for each in open_url:
        print("[+]"+each)
else:
    print("没有扫描到网站后台(可能是字典不够给力)")
 
if __name__=="__main__":
    #判断程序运行时间
    #start为程序还没开始的时间,end为程序运行结束的时间
    start = time.clock()
    main()
    end = time.clock()
    print("The function spend time is %.3f seconds" %(end-start))

 最后附一张加了多线程和没加的时间区别

  《Python3学习系列(二):编写属于自己的网站后台扫描器》 

《Python3学习系列(二):编写属于自己的网站后台扫描器》 

可以看到,花费的时间是不同的,这是在扫描基数很少的情况下,我测试的时候只用了3个url,如果基数大的话,时间差距会更大

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

点赞
  1. pure boost说道:

    I enjoy you because of your entire labor on this site. Debby take interest in participating in research and it's really easy to see why. Most people notice all of the compelling mode you render precious guidelines by means of your blog and as well as cause contribution from website visitors on the area and our child is always learning a lot of things. Take pleasure in the remaining portion of the new year. You're carrying out a remarkable job.

  2. bape hoodie说道:

    I would like to express my thanks to this writer just for bailing me out of this problem. Just after scouting throughout the world wide web and getting concepts which were not beneficial, I thought my entire life was well over. Living without the presence of strategies to the problems you've resolved by way of this website is a serious case, as well as the kind which could have in a negative way damaged my career if I hadn't encountered your web blog. That competence and kindness in dealing with all the details was vital. I don't know what I would have done if I hadn't encountered such a thing like this. I can also at this point look ahead to my future. Thanks for your time so much for the professional and effective help. I won't hesitate to propose your web page to anyone who will need recommendations about this topic.

  3. adidas nmd说道:

    My husband and i were absolutely cheerful that Edward managed to complete his preliminary research by way of the ideas he discovered using your web pages. It is now and again perplexing to simply choose to be freely giving tips and hints which usually a number of people could have been making money from. And we discover we now have the website owner to be grateful to for this. Those explanations you made, the straightforward web site menu, the relationships your site aid to foster - it is all incredible, and it's assisting our son in addition to the family reckon that the article is amusing, and that is especially pressing. Many thanks for all the pieces!

  4. Needed to draft you this little bit of observation to thank you once again with your exceptional tactics you have provided on this site. It's really extremely open-handed with you to convey extensively precisely what many individuals would've marketed for an electronic book in making some money on their own, most notably considering the fact that you might have tried it if you desired. The strategies also served as a easy way to understand that other individuals have a similar zeal really like my personal own to realize a good deal more in terms of this matter. I am certain there are lots of more pleasurable moments up front for individuals that examine your blog.

  5. nike max说道:

    I precisely wanted to appreciate you all over again. I do not know the things that I would've achieved without the actual opinions contributed by you over my concern. This has been a real depressing crisis in my circumstances, but noticing the specialized way you dealt with the issue forced me to cry with delight. Now i am thankful for the support as well as pray you are aware of a powerful job you happen to be getting into teaching some other people with the aid of your site. I am sure you have never come across any of us.

  6. nike hyperdunk说道:

    My spouse and i felt now lucky that Louis managed to do his analysis using the ideas he made when using the web site. It is now and again perplexing just to happen to be giving freely steps which usually people today have been trying to sell. And now we consider we have the writer to appreciate because of that. The most important explanations you made, the straightforward website navigation, the relationships you can assist to create - it is everything spectacular, and it is letting our son in addition to the family understand that situation is fun, and that's wonderfully important. Thank you for all!

发表评论

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