内容简介:分析的模块位于首先来看模块头的引入与类的定义:如下为
分析的模块位于 /modules/auxiliary/scanner/http/http_version.rb
首先来看模块头的引入与类的定义:
## |
# This module requires Metasploit: https://metasploit.com/download |
# Current source: https://github.com/rapid7/metasploit-framework |
## |
require 'rex/proto/http' |
class MetasploitModule < Msf::Auxiliary |
-
require 'rex/proto/http'表示该模块将要引入这个rex库文件目录下的所有http协议方法,所有的模块都可以被调取使用。包括用于设置连接的函数,get和post的请求和响应处理等。 -
Msf::Auxiliary定义了该代码的类为辅助模块。
如下为 /lib/rex/proto/http 目录下的内容:
[注:继承有助于重用代码和快速执行,不幸的是,Ruby不支持多继承,但是 Ruby 支持mixins。mixin就像是多继承的一个特定实现,在多继承中,只有接口部分是可继承的。]
# Exploit mixins should be called first(首先调用渗透模块mixins类) |
include Msf::Exploit::Remote::HttpClient |
include Msf::Auxiliary::WmapScanServer |
# Scanner mixin should be near last(接着是扫描模块mixins类) |
include Msf::Auxiliary::Scanner |
上面代码所必需库文件中涵盖了编写模块所需要的所有方法,这些库文件的详细信息如下:
| 语句 | 引入路径 | 用途 |
|---|---|---|
| Msf::Exploit::Remote::HttpClient | /lib/msf/core/exploit/http/client.rb | 这个库文件提供了大量方法,例如连接到目标计算机、发送请求、切断与客户端的连接等。 |
| Msf::Auxiliary::WmapScanServer | /lib/msf/core/auxiliary/wmapmodule.rb | wmap是一款基于Metasploit的通用Web应用程序扫描框架,有助于完成Metasploit的Web渗透测试。 |
| Msf::Auxiliary::Scanner | /lib/msf/core/auxiliary/scanner.rb | 这个文件包含了基于扫描模块的所有函数,提供了模块运行、模块初始化、扫描进度等各种方法。 |
- 这个
initialize方法是Ruby编程语言中的默认构造方法。它定义了名称Name、描述Description、作者Author、许可License等。许可就是'License' => MSF_LICENSE,最后用一个end结束。
def initialize |
super( |
'Name' => 'HTTP Version Detection', |
'Description' => 'Display version information about each system.', |
'Author' => 'hdm', |
'License' => MSF_LICENSE |
register_wmap_options({ |
'OrderID' => 0, |
'Require' => {}, |
}) |
end |
最后这段代码才是真正干活做事的:
def run_host(ip) |
begin |
connect |
res = send_request_raw({ 'uri' => '/', 'method' => 'GET' }) |
fp = http_fingerprint(:response => res) |
print_good("#{ip}:#{rport} #{fp}") if fp |
report_service(:host => rhost, :port => rport, :sname => (ssl ? 'https' : 'http'), :info => fp) |
rescue ::Timeout::Error, ::Errno::EPIPE |
ensure |
disconnect |
end |
end |
先来解释下在本段代码中所使用的重要函数:
| 函数 | 引入库文件 | 用途 |
|---|---|---|
| run_host | /lib/msf/core/auxiliary/scanner.rb | 使用IP与所需主机建立连接 |
| connect | /lib/msf/core/exploit/http/client.rb | 与目标服务器建立一个HTTP类型的连接 |
| send_request_raw | /lib/msf/core/exploit/http/client.rb | 用来向目标发送原始的HTTP请求 |
| http_fingerprint | /lib/msf/core/exploit/http/client.rb | 将HTTP响应解析为可以使用的变量 |
在这里会产生疑问,就是从代码上下文中根本不清楚函数的具体定义,该如何解决?这时就需要查询 Metasploit API 官方文档。
下面来具体查询分析下:
- run_host 在
run方法中,作用是与所需主机建立连接。
-
begin意味着代码块的开始。 -
如果连接成功,就进行到 connect 方法,用于与目标服务器建立一个HTTP类型的连接。
- 之后使用 send_request_raw 方法,连接到服务器,创建请求,发送请求,读取响应。并将这个方法的参数
URI的值设置为/,参数method的值设置为GET,将这个响应保存在res变量。
res = send_request_raw({ 'uri' => '/', 'method' => 'GET' })
- 如果连接成功,使用 http_fingerprint 方法,记录和过滤信息。如:Set-cookie、Powered-by等。
- 接着再把收到的响应信息赋值给
res,意味着将根据之前发送请求的响应数据进行特征匹配。
fp = http_fingerprint(:response => res)
-
之后对这些响应数据进行输出打印。
-
最后,
rescue ::Timeout::Error, ::Errno::EPIPE将会在模块超时的情况下处理程序的异常。
可见,输出的格式对应 print_good("#{ip}:#{rport} #{fp}") if fp
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网
猜你喜欢:本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
Pro Django
Marty Alchin / Apress / 2008-11-24 / USD 49.99
Django is the leading Python web application development framework. Learn how to leverage the Django web framework to its full potential in this advanced tutorial and reference. Endorsed by Django, Pr......一起来看看 《Pro Django》 这本书的介绍吧!