Metasploit介绍及基本使用(一)

1.1 Metasploit框架介绍

1.1.1 框架体系

图片[1]|Metasploit介绍及基本使用(一)|leon的博客

1.1.2 架构简介

1、基础库: metasploit基础库文件位于源码根目录路径下的libraries目录中,包括Rex,framework-core和framework-base三部分:

  • Rex是整个框架所依赖的最基础的- -些组件,如包装的网络套接字、网络应用协议客户端与服务端实现、日志子系统、渗透攻击支持例程、PostgreSQL 以及MySQL数据库支持等;
  • framework-core负责实现所有与各种类型的上层模块及插件的交互接口;
  • framework-base库扩展了framework-core,提供更加简单的包装例程,并为处理框架各个方面的功能提供了一些功能类,用于支持用户接口与功能程序调用框架本身功能及框架集成模块;

2、模块:模块组织按照不同的用途分为6种类型的模块(Modules):

  • 分为辅助模块(Aux)
  • 渗透攻击模块(Exploits)
  • 后渗透攻击模块(Post)
  • 攻击载荷模块(payloads)
  • 编码器模块(Encoders)
  • 空指令模块(Nops)。

注: payload 又称为攻击载荷,主要是用来建立目标机与攻击机稳定连接的,可返回shell,也可以进行程序注入等。

3、插件:插件能够扩充框架的功能,或者组装已有功能构成高级特性的组件。插件可以集成现有的一些外部安全具,如Nessus. OpenVAS漏洞扫描器等,为用户接口提供一些新的功能。

4、接口:包括msfconsole控制终端、msfcli 命令行、msfgui 图形化界面、armitage 图形化界面以及msfapi远程调用接口。

5、功能程序:metasploit还提供了一系列可直接运行的功能程序,支持渗透测试者与安全人员快速地利用metasploit框架内部能力完成-些特定任务。比如msfpayload、msfencode 和msfvenom可以将攻击载荷封装为可执行文件、C语言、JavaScript 语言等多种形式,并可以进行各种类型的编码。

1.1.3 Metasploit目录结构

图片[2]|Metasploit介绍及基本使用(一)|leon的博客

1.1.3.1 结构介绍

  • Data:Metasploit使用的可编辑文件
  • Documentation:为框架提供文档
  • Lib:框架代码库
  • Modules:实际的MSF模块
  • Plugins:可以在运行时加载的插件
  • Scripts:Meterpreter 和其他脚本
  • Tools:各种有用的命令行工具

1.2 Metasploit使用

1.2.1 启动Metasploit

PS.Metasploit程序需要使用Postgresql数据库

1.2.1.1 第一种方式:先启动postgresql再启动metasploit

  • 启动postgresql
# systemctl start postgresql
# systemctl enable postgresql
  • 启动metasploit
# msfconsole

1.2.1.2 第二种方式:直接启动数据库和metasploit

# msfdb run

1.2.2 Metasploit常用命令

1.2.2.1 help命令

查看帮助信息。

msf6 > help

Core Commands
========================
? :帮助菜单
banner:显示一-个metasploit横幅
cd:更改当前的工作目录
color:切换颜色
connect:连接与主机通信
exit:退出控制台
get:获取特定于上下文的变量的值
getg:获取全局变量的值
grep:grep另-一个命令的输出如: grep creds help
hel:帮助菜单
histor:显示命令历史
irb:进入irb脚本模式
load:加载一个框架插件
quit:退出控制台
route:通过会话路由流量
save:保存活动的数据存储
sessions:转储会话列表并显示有关会话的信息
set:将特定于上下文的变量设置为-个值
setg:将全局变量设置为一个值
sleep:在指定的秒数内不做任何事情
spool:将控制台输出写入文件以及屏幕
threads:线程查看和操作后台线程
threads:线程查看和操作后台线程
unload:卸载框架插件
unset:取消设置一个或多个特定于上下文的变量
unsetg:取消设置一个或多个全局变量
version:显示框架和控制台库版本号

Module Commands
========================
advanced:显示一个或多个模块的高级选项
back:从当前上下文返回
edit:使用首选编辑器编辑当前模块
info:显示有关一个或多个模块的信息
loadpath:路径从路径搜索并加载模块
options:显示全局选项或一个或多个模块
popm:将最新的模块从堆栈中弹出并使其处于活动状态
previous:将之前加载的模块设置为当前模块
pushm:将活动或模块列表推入模块堆栈
reload_all:从所有定义的模块路径重新加载所有模块
search:搜索模块名称和描述
show:显示给定类型的模块或所有模块
use:按名称选择模块

Job Commands
========================
handler:作为作业启动负载处理程序
jobs:显示和管理作业
kill:杀死一一个工作
rename_job:重命名作业

Resource Script Commands
========================
makerc:保存从开始到文件输入的命令
resource:运行存储在文件中的命令

Database Backend Commands
=========================
db_connect:连接到现有的数据库
db_disconnect:断开与当前数据库实例的连接
db_export:导出包含数据库内容的文件
db_import:导入扫描结果文件(文件类型将被自动检测)
db_nmap:执行nmap并自动记录输出
db_rebuild_cache:重建数据库存储的模块高速缓存
db_status:显示当前的数据库状态
hosts:列出数据库中的所有主机
loot:列出数据库中的所有战利品
notes:列出数据库中的所有笔记
services:列出数据库中的所有服务
vulns:列出数据库中的所有漏洞
workspace:在数据库工作区之间切换

Credentials Backend Commands
============================
creds:列出数据库中的所有凭据
Developer Commands

==================
    Command       Description
    -------       -----------
    edit          Edit the current module or a file with the preferred editor
    irb           Open an interactive Ruby shell in the current context
    log           Display framework.log paged to the end if possible
    pry           Open the Pry debugger on the current module or Framework
    reload_lib    Reload Ruby library files from specified paths
    time          Time how long it takes to run a particular command
  • 命令类型:
  • Core Commands:核心命令
  • Module Commands:模块命令
  • Job Commands:后台任务命令
  • Resource Script Commands:资源脚本命令
  • Database Backend Commands:数据库后端命令
  • Credentials Backend Commands:证书/凭证后端命令
  • Developer Commands:开发人员命令

1.2.2.2 Connect命令

用于连接远程主机,一般用于内网渗透。

msf6 > connect -h
Usage: connect [options] <host> <port>

Communicate with a host, similar to interacting via netcat, taking advantage of
any configured session pivoting.

OPTIONS:

    -c, --comm <comm>               Specify which Comm to use.
    -C, --crlf                      Try to use CRLF for EOL sequence.
    -h, --help                      Help banner.
    -i, --send-contents <file>      Send the contents of a file.
    -p, --proxies <proxies>         List of proxies to use.
    -P, --source-port <port>        Specify source port.
    -S, --source-address <address>  Specify source address.
    -s, --ssl                       Connect with SSL.
    -u, --udp                       Switch to a UDP socket.
    -w, --timeout <seconds>         Specify connect timeout.
    -z, --try-connection            Just try to connect, then return.
  • connect命令的使用
msf6 > connect 192.168.10.190 80
[*] Connected to 192.168.10.190:80 (via: 192.168.10.180:46523)
get /
HTTP/1.1 400 Bad Request
Server: LSWS
Date: Thu, 02 Jun 2022 08:00:48 GMT
Content-Type: text/html
Content-Length: 149
Connection: close

<html>
<head><title>400 Bad Request</title></head>
<body>
<center><h1>400 Bad Request</h1></center>
<hr><center>LSWS</center>
</body>
</html>

1.2.2.3 show命令

显示参数相关的内容

msf6 > show -h
[*] Valid parameters for the "show" command are: all, encoders, nops, exploits, payloads, auxiliary, post, plugins, info, options, favorites
[*] Additional module-specific parameters are: missing, advanced, evasion, targets, actions
  • show命令的使用:
msf6 > show exploits    # 列出metasploit框架中的所有渗透攻击模块
msf6 > show payloads    # 列出metasploit框架中的所有攻击载荷
msf6 > show auxiliary   # 列出metasploit框架中的所有辅助攻击载荷
# 显示模块内部信息
msf6 exploit(multi/http/spring_cloud_function_spel_injection) > show options # 查看执行模块需要的参数
msf6 exploit(multi/http/spring_cloud_function_spel_injection) > show targets # 查看模块可以攻击的操作系统类型

1.2.2.4 search命令

搜索Metasploit中各种漏洞模块、插件载荷等内容,支持模糊搜索,不区分大小写

msf6 > search -h
Usage: search [<options>] [<keywords>:<value>]

Prepending a value with '-' will exclude any matching results.
If no options or keywords are provided, cached results are displayed.

OPTIONS:

    -h, --help                      Help banner
    -I, --ignore                    Ignore the command if the only match has the same name as the search
    -o, --output <filename>         Send output to a file in csv format
    -r, --sort-descending <column>  Reverse the order of search results to descending order
    -S, --filter <filter>           Regex pattern used to filter search results
    -s, --sort-ascending <column>   Sort search results by the specified column in ascending order
    -u, --use                       Use module if there is one result

Keywords:
  aka              :  Modules with a matching AKA (also-known-as) name
  author           :  Modules written by this author
  arch             :  Modules affecting this architecture
  bid              :  Modules with a matching Bugtraq ID
  cve              :  Modules with a matching CVE ID
  edb              :  Modules with a matching Exploit-DB ID
  check            :  Modules that support the 'check' method
  date             :  Modules with a matching disclosure date
  description      :  Modules with a matching description
  fullname         :  Modules with a matching full name
  mod_time         :  Modules with a matching modification date
  name             :  Modules with a matching descriptive name
  path             :  Modules with a matching path
  platform         :  Modules affecting this platform
  port             :  Modules with a matching port
  rank             :  Modules with a matching rank (Can be descriptive (ex: 'good') or numeric with comparison operators (ex: 'gte400'))
  ref              :  Modules with a matching ref
  reference        :  Modules with a matching reference
  target           :  Modules affecting this target
  type             :  Modules of a specific type (exploit, payload, auxiliary, encoder, evasion, post, or nop)

Supported search columns:
  rank             :  Sort modules by their exploitabilty rank
  date             :  Sort modules by their disclosure date. Alias for disclosure_date
  disclosure_date  :  Sort modules by their disclosure date
  name             :  Sort modules by their name
  type             :  Sort modules by their type
  check            :  Sort modules by whether or not they have a check method

Examples:
  search cve:2009 type:exploit
  search cve:2009 type:exploit platform:-linux
  search cve:2009 -s name
  search type:exploit -s type –r
  • search命令的使用:
# 通过名称进行查找,也可以使用 name:mysql
msf6 > search mysql
# 通过路径进行查找
msf6 > search path:mysql       
# platform列出可以影响此平台的模块,查询的结果会列出rank比较高的模块可以用来缩小查询范围
msf6 > search platform:windows
# 搜索特定类型的模块(exploit, payload, auxiliary, encoder, evasion, post, or nop)
msf6 > search type:exploit
# 通过CVE进行查找,搜索2022年CVE漏洞
msf6 > search cve:2022
# 联合查找,可通过关键字自行组合
msf6 > search name:mysql type:auxiliary
  • Rank按照可靠性降序排列:
  • Excellent:漏洞利用程序绝对不会使目标服务崩溃, 就像sQL注入、命令执行、远程文件包含、本地文件包含等等。除非有特殊情况,典型的内存破坏利用程序不可以被评估为该级别。
  • Great:该漏洞利用程序有一个默认的目标系统,并且可以自动检测适当的目标系统,或者在目标服务的版本检查之后可以返回到一个特定的返回地址。
  • Good:该漏洞利用程序有一个默认目标系统,并且是这种类型软件的“常见情况”(桌面应用程序的Windows 7,服务器的2012等)
  • Normal:该漏洞利用程序是可靠的,但是依赖于特定的版本,并且不能或者不能可靠地自动检测。
  • Average:该漏洞利用程序不可靠或者难以利用。
  • Low:对于通用的平台而言,该漏洞利用程序几乎不能利用(或者低于50%的利用成功率)
  • Manual:该漏洞利用程序不稳定或者难以利用并且基于拒绝服务 (DOS)。 如果-个模块只有在用户特别配置该模块的时候才会被用到,否则该模块不会被使用到,那么也可以评为该等级。

1.2.2.5 use命令

使用模块

msf6 > use -h
Usage: use <name|term|index>

Interact with a module by name or search term/index.
If a module name is not found, it will be treated as a search term.
An index from the previous search results can be selected if desired.

Examples:
  use exploit/windows/smb/ms17_010_eternalblue

  use eternalblue
  use <name|index>

  search eternalblue
  use <name|index>
  • use命令的使用:
msf6 > use exploit/multi/http/spring_cloud_function_spel_injection  # 使用模块完整路径
msf6 > use 1                                                        # 使用模块编号

1.2.2.6 info命令

显示模块的相关信息

msf6 > info -h
Usage: info <module name> [mod2 mod3 ...]

Options:
* The flag '-j' will print the data in json format
* The flag '-d' will show the markdown version with a browser. More info, but could be slow.
Queries the supplied module or modules for information. If no module is given,
show info for the currently active module.
  • info命令的使用:
msf6 > use exploit/multi/http/spring_cloud_function_spel_injection
msf6 exploit(multi/http/spring_cloud_function_spel_injection) > info
# 或
msf6 > info exploit/multi/http/spring_cloud_function_spel_injection
  • 重点关注内容:
  • Available targets:可用目标,就是查看可以攻击哪些操作系统

图片[3]|Metasploit介绍及基本使用(一)|leon的博客

  • Basic options:调用漏洞需要的相关参数

图片[4]|Metasploit介绍及基本使用(一)|leon的博客

  • Description:漏洞执行过程

图片[5]|Metasploit介绍及基本使用(一)|leon的博客

  • References:参考文档

图片[6]|Metasploit介绍及基本使用(一)|leon的博客

1.2.2.7 back命令

回退命令,如果用exit则会直接退出msf

msf6 > use exploit/multi/http/spring_cloud_function_spel_injection
[*] Using configured payload linux/x64/meterpreter/reverse_tcp
msf6 exploit(multi/http/spring_cloud_function_spel_injection) > back
msf6 >

1.2.2.8 set命令

设置相关参数

msf6 > use exploit/multi/http/spring_cloud_function_spel_injection
[*] Using configured payload linux/x64/meterpreter/reverse_tcp
msf6 exploit(multi/http/spring_cloud_function_spel_injection) > set RHOSTS 192.168.10.190
RHOSTS => 192.168.10.190

1.2.2.9 exploit和run命令

执行攻击

msf6 exploit(multi/http/spring_cloud_function_spel_injection) > exploit
msf6 exploit(multi/http/spring_cloud_function_spel_injection) > run

1.2.3 导入Nmap扫描结果

1.2.3.1 导入扫描结果

msf6 > db_import /tmp/192.168.1.X.xml

1.2.3.2 查看主机

msf6 > hosts                            # 查看metasploit中所有主机
msf6 > hosts -h                         # 显示命令帮助
msf6 > hosts -c address,name,os_name    # 显示指定列的主机信息
msf6 > hosts -d 192.168.1.254           # 删除主机

1.2.3.3 查看服务

msf6 > services                         # 查看所有主机的服务状态
msf6 > services -R 192.168.10.156       # 查看指定主机的服务状态
温馨提示:本文最后更新于2022-12-20 20:57:32,已超过464天没有更新。某些文章具有时效性,若文章内容或图片资源有错误或已失效,请联系站长。谢谢!
转载请注明本文链接:https://blog.leonshadow.cn/763482/2911.html
© 版权声明
THE END
喜欢就支持一下吧
点赞0 分享