Python多线程Dirbruter目录扫描器

前言

用Python2写的一款小型的web目录扫描或Web文件扫描工具

根据提供的字典多线程对目标网站进行扫描

网上已经有很多开源和成熟的工具,为什么我们还要自己编?

因为我们要明白它的运行原理,或者我们可以对开源的工具进行修改

把优秀的代码拿来集成到自己工具里,不断扩展。从而达到我们的需求或目的

源代码

自己敲的代码很丑,请勿拍砖

  1. #-*-coding:utf-8-*-
  2. import urllib2
  3. import threading
  4. import Queue
  5. import urllib
  6.  
  7. #定义目标,线程,初始化参数
  8. threads = 10
  9. target_rul = "http://www.xss.tv"
  10. wordlist_file = "/root/python/urls.lst"
  11. resumae = None
  12. user_agaent = "Mozilla/5.0 (X11; Linux i686; rv:45.0) Gecko/20100101 Firefox/45.0"
  13. Host = 'edu.xss.tv'
  14.  
  15. #----------------------------------------------------------------------
  16. def build_wordlist(wordlist_file):
  17. #读入目录字典
  18. fd =open(wordlist_file, mode='rb')
  19. raw_words =fd.readlines()
  20. fd.close()
  21.  
  22. found_resume = False
  23. #定义线程对象
  24. words = Queue.Queue()
  25. #print raw_words
  26. for word in raw_words:
  27. word =word.rstrip()
  28. if resumae is not None:
  29. if found_resume:
  30. words.put(word)
  31. else:
  32. if word ==resumae:
  33. found_resume =True
  34. print "Resume wordlist from:%s" % resumae
  35. else:
  36. words.put(word)
  37. return words
  38.  
  39. #----------------------------------------------------------------------
  40. def dir_bruter(word_queue,extensions=None):
  41. while not word_queue.empty():
  42. #attempt =word_queue.get()
  43. attempt =word_queue.get_nowait()
  44. #attempt =get_nowait()
  45.  
  46. attempt_list = []
  47.  
  48. #检查是否为目录
  49. if "." not in attempt:
  50. attempt_list.append("%s/" % attempt)
  51. else:
  52. attempt_list.append("%s" % attempt)
  53. #print attempt_list
  54. #暴力扩展目录
  55. if extensions:
  56. for extension in extensions:
  57. attempt_list.append("/%s%s" % (attempt,extension))
  58. for brute in attempt_list:
  59. url = "%s%s" %(target_rul,urllib.quote(brute))
  60. try:
  61. headers ={}
  62. headers["User-Agant"] =user_agaent
  63. headers["Host"] =host
  64. r = urllib2.Request(url,headers=headers)
  65.  
  66. response =urllib2.urlopen(r)
  67.  
  68. #print response.read()
  69. if 'not found or template directory' not in response.read():
  70. print "[%d] => %s" %(response.code,url)
  71. except urllib2.URLError as e:
  72. #如需404,把屏蔽去了
  73. #if hasattr(e,'code') and e.code !=404:
  74. #print "!!! %d => %s" %(e.code,url)
  75. pass
  76.  
  77. word_queue = build_wordlist(wordlist_file)
  78. #print word_queue
  79. extensions = [".php",".bak",".inc",".html","bak"]
  80.  
  81. #开启线程
  82. for i in range(threads):
  83. t = threading.Thread(target=dir_bruter, args=(word_queue,None))
  84. t.start()

执行效果

发表评论

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