暴力破解测试工具--Patator 源码分析

发表于:2013-8-15 11:02

字体: | 上一篇 | 下一篇 | 我要投稿

 作者:yueguanghaidao    来源:51Testing软件测试网采编

  其中telnet模块源码如下:

# Telnet {{{
from telnetlib import Telnet
class Telnet_login(TCP_Cache):
  '''Brute-force Telnet'''

  usage_hints = (
    """%prog host=10.0.0.1 inputs='FILE0\\nFILE1' 0=logins.txt 1=passwords.txt persistent=0"""
    """ prompt_re='Username:|Password:' -x ignore:egrep='Login incorrect.+Username:'""",
    )

  available_options = (
    ('host', 'target host'),
    ('port', 'target port [23]'),
    ('inputs', 'list of values to input'),
    ('prompt_re', 'regular expression to match prompts [\w+]'),
    ('timeout', 'seconds to wait for a response and for prompt_re to match received data [20]'),
    )
  available_options += TCP_Cache.available_options

  Response = Response_Base

  def connect(self, host, port, timeout):
    self.prompt_count = 0
    fp = Telnet(host, int(port), int(timeout))

    return TCP_Connection(fp)

  def execute(self, host, port='23', inputs=None, prompt_re='\w+:', timeout='20', persistent='1'):

    fp, _ = self.bind(host, port, timeout=timeout)

    trace = ''
    timeout = int(timeout)

    if self.prompt_count == 0:
      _, _, raw = fp.expect([prompt_re], timeout=timeout)
      logger.debug('raw banner: %s' % repr(raw))
      trace += raw
      self.prompt_count += 1
 
    if inputs is not None:
      for val in inputs.split(r'\n'):
        logger.debug('input: %s' % val)
        cmd = val + '\n' #'\r\x00'
        fp.write(cmd)
        trace += cmd

        _, _, raw = fp.expect([prompt_re], timeout=timeout)
        logger.debug('raw %d: %s' % (self.prompt_count, repr(raw)))
        trace += raw
        self.prompt_count += 1

    if persistent == '0':
      self.reset()

    mesg = repr(raw)[1:-1] # strip enclosing single quotes
    return self.Response(0, mesg, trace)

# }}}

  把核心部分抽出来,流程是这样的。

      创建一个连接到telnetlib.Telnet对象fp  -->> 等待出现匹配正则(\w+:)字符出现(如 login:,username等)

      -->> 发送用户名 -->>继续等待(\w+:)出现(如passwd:,Password:) -->> 发送密码  ->> 等待(\w+:)出现,输出返回字符串

      过程就是这么简单,其中expect函数接受二个参数,第一个是正则表达式的列表,可以是编译好的,也可以是字符串,第二个是超时时间。

  返回一个三项的tuple,第一个是匹配到的正则序号(从0开始),第二个是匹配match的对象,第三个是直到正则的返回数据(包括匹配内容)

  这说的有点抽象。

  请看下面:

52/5<12345>
《2023软件测试行业现状调查报告》独家发布~

关注51Testing

联系我们

快捷面板 站点地图 联系我们 广告服务 关于我们 站长统计 发展历程

法律顾问:上海兰迪律师事务所 项棋律师
版权所有 上海博为峰软件技术股份有限公司 Copyright©51testing.com 2003-2024
投诉及意见反馈:webmaster@51testing.com; 业务联系:service@51testing.com 021-64471599-8017

沪ICP备05003035号

沪公网安备 31010102002173号