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/rex/post/meterpreter/extensions/python/python.rb
Views: 11791
# -*- coding: binary -*-12require 'rex/post/meterpreter/extensions/python/tlv'3require 'rex/post/meterpreter/extensions/python/command_ids'4require 'set'56module Rex7module Post8module Meterpreter9module Extensions10module Python1112###13#14# Python extension - gives remote python scripting capabilities on the target.15#16###1718class Python < Extension1920PY_CODE_TYPE_STRING = 021PY_CODE_TYPE_PY = 122PY_CODE_TYPE_PYC = 22324PY_CODE_FILE_TYPES = [ '.py', '.pyc' ]2526PY_CODE_FILE_TYPE_MAP = {27'.py' => PY_CODE_TYPE_PY,28'.pyc' => PY_CODE_TYPE_PYC29}3031def self.extension_id32EXTENSION_ID_PYTHON33end3435#36# Typical extension initialization routine.37#38# @param client (see Extension#initialize)39def initialize(client)40super(client, 'python')4142client.register_extension_aliases(43[44{45'name' => 'python',46'ext' => self47}48])49end5051def reset52request = Packet.create_request(COMMAND_ID_PYTHON_RESET)53client.send_request(request)5455return true56end5758def import(file, mod_name, result_var)59unless ::File.file?(file)60raise ArgumentError, "File not found: #{file}"61end6263ext = ::File.extname(file).downcase64unless PY_CODE_FILE_TYPES.include?(ext)65raise ArgumentError, "File not a valid type: #{file}"66end6768code = ::File.read(file)6970request = Packet.create_request(COMMAND_ID_PYTHON_EXECUTE)71request.add_tlv(TLV_TYPE_PYTHON_CODE, code)72request.add_tlv(TLV_TYPE_PYTHON_CODE_LEN, code.length)73request.add_tlv(TLV_TYPE_PYTHON_CODE_TYPE, PY_CODE_FILE_TYPE_MAP[ext])74request.add_tlv(TLV_TYPE_PYTHON_NAME, mod_name) if mod_name75request.add_tlv(TLV_TYPE_PYTHON_RESULT_VAR, result_var) if result_var7677run_exec_request(request)78end7980#81# Dump the LSA secrets from the target machine.82#83# @return [Hash<Symbol,Object>]84def execute_string(code, result_var)85request = Packet.create_request(COMMAND_ID_PYTHON_EXECUTE)86request.add_tlv(TLV_TYPE_PYTHON_CODE, code)87request.add_tlv(TLV_TYPE_PYTHON_CODE_TYPE, PY_CODE_TYPE_STRING)88request.add_tlv(TLV_TYPE_PYTHON_RESULT_VAR, result_var) if result_var8990run_exec_request(request)91end9293private9495def run_exec_request(request)96response = client.send_request(request)9798result = {99result: response.get_tlv_value(TLV_TYPE_PYTHON_RESULT),100stdout: "",101stderr: ""102}103104response.each(TLV_TYPE_PYTHON_STDOUT) do |o|105result[:stdout] << o.value106end107108response.each(TLV_TYPE_PYTHON_STDERR) do |e|109result[:stderr] << e.value110end111112result113end114115end116117end; end; end; end; end118119120121