如何知道调用了哪个类成员函数?

时间:2021-02-16 14:53:16

标签: ruby methods variable-assignment metasploit

来自https://github.com/rapid7/metasploit-framework/blob/master/modules/auxiliary/scanner/smtp/smtp_version.rb#L26

class MetasploitModule < Msf::Auxiliary
  include Msf::Exploit::Remote::Smtp
  include Msf::Auxiliary::Scanner
  include Msf::Auxiliary::Report

  def initialize
    super(
      'Name'        => 'SMTP Banner Grabber',
      'Description' => 'SMTP Banner Grabber',
      'References'  =>
        [
          ['URL', 'http://www.ietf.org/rfc/rfc2821.txt'],
        ],
      'Author'      => 'CG',
      'License'     => MSF_LICENSE
    )
    deregister_options('MAILFROM', 'MAILTO')
  end

  def run_host(ip)
    res = connect
    banner_sanitized = Rex::Text.to_hex_ascii(banner.to_s)
    print_good("#{ip}:#{rport} SMTP #{banner_sanitized}")
    report_service(:host => rhost, :port => rport, :name => "smtp", :info => banner)
  end
end

我看到上面调用了 connectconnect 是成员函数吗?如何知道调用的是哪个超类的成员函数?谢谢。

1 个答案:

答案 0 :(得分:0)

很可能是继承或包含的方法

您没有发布所有相关代码,因此您必须深入研究源代码的其余部分才能确定。我将只讨论你特别提到的部分。

在您链接的小示例中,#connect 可能继承自 Msf::Auxiliary 或包含的模块之一。从名字上看,很可能来自 Msf::Exploit::Remote::Smtp,但我没有为你研究。

最有可能是方法的原因是您当前的类没有在任何地方使用 connect 参数。由于 Ruby 表达式本质上允许您使用方法调用,例如右侧赋值,这是一个常见的习惯用法。

如果您想确定,请搜索 def connect 的源代码以找到它的定义位置(假设它不是通过诸如 Module#define_method 之类的更元数据完成的),或 /\bconnect\b/ 以查找源代码树中您可能希望更仔细查看其用途或定义的所有位置。