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/simple/buffer.rb
Views: 11784
# -*- coding: binary -*-123module Msf4module Simple56###7#8# Wraps interaction with a generated buffer from the framework.9# Its primary use is to transform a raw buffer into another10# format.11#12###13module Buffer1415class BufferFormatError < ::ArgumentError; end16#17# Serializes a buffer to a provided format. The formats supported are raw,18# num, dword, ruby, rust, python, perl, bash, c, js_be, js_le, java and psh19#20def self.transform(buf, fmt = "ruby", var_name = 'buf', encryption_opts={})21default_wrap = 602223unless encryption_opts.empty?24buf = encrypt_buffer(buf, encryption_opts)25end2627case fmt28when 'raw'29when 'num'30buf = Rex::Text.to_num(buf)31when 'hex'32buf = Rex::Text.to_hex(buf, '')33when 'dword', 'dw'34buf = Rex::Text.to_dword(buf)35when 'python', 'py'36buf = Rex::Text.to_python(buf, default_wrap, var_name)37when 'ruby', 'rb'38buf = Rex::Text.to_ruby(buf, default_wrap, var_name)39when 'perl', 'pl'40buf = Rex::Text.to_perl(buf, default_wrap, var_name)41when 'bash', 'sh'42buf = Rex::Text.to_bash(buf, default_wrap, var_name)43when 'c'44buf = Rex::Text.to_c(buf, default_wrap, var_name)45when 'csharp'46buf = Rex::Text.to_csharp(buf, default_wrap, var_name)47when 'js_be'48buf = Rex::Text.to_unescape(buf, ENDIAN_BIG)49when 'js_le'50buf = Rex::Text.to_unescape(buf, ENDIAN_LITTLE)51when 'java'52buf = Rex::Text.to_java(buf, var_name)53when 'powershell', 'ps1'54buf = Rex::Powershell.to_powershell(buf, var_name)55when 'vbscript'56buf = Rex::Text.to_vbscript(buf, var_name)57when 'vbapplication'58buf = Rex::Text.to_vbapplication(buf, var_name)59when 'base32'60buf = Rex::Text.encode_base32(buf)61when 'base64'62buf = Rex::Text.encode_base64(buf)63when 'go','golang'64buf = Rex::Text.to_golang(buf)65when 'masm'66buf = Rex::Text.to_masm(buf)67when 'nim','nimlang'68buf = Rex::Text.to_nim(buf)69when 'rust', 'rustlang'70buf = Rex::Text.to_rust(buf)71when 'zig','ziglang'72buf = Rex::Text.to_zig(buf)73when 'octal'74buf = Rex::Text.to_octal(buf)75else76raise BufferFormatError, "Unsupported buffer format: #{fmt}", caller77end7879return buf80end8182#83# Creates a comment using the supplied format. The formats supported are84# raw, ruby, rust python, perl, bash, js_be, js_le, c, and java.85#86def self.comment(buf, fmt = "ruby")87case fmt88when 'raw'89when 'num', 'dword', 'dw', 'hex', 'octal', 'base64', 'base32'90# These are string encodings, not languages; default to the js comment.91buf = Rex::Text.to_js_comment(buf)92when 'ruby', 'rb', 'python', 'py'93buf = Rex::Text.to_ruby_comment(buf)94when 'perl', 'pl'95buf = Rex::Text.to_perl_comment(buf)96when 'bash', 'sh'97buf = Rex::Text.to_bash_comment(buf)98when 'c'99buf = Rex::Text.to_c_comment(buf)100when 'csharp'101buf = Rex::Text.to_c_comment(buf)102when 'js_be', 'js_le'103buf = Rex::Text.to_js_comment(buf)104when 'java'105buf = Rex::Text.to_c_comment(buf)106when 'powershell','ps1'107buf = Rex::Text.to_psh_comment(buf)108when 'go','golang'109buf = Rex::Text.to_golang_comment(buf)110when 'masm','ml64'111buf = Rex::Text.to_masm_comment(buf)112when 'nim','nimlang'113buf = Rex::Text.to_nim_comment(buf)114when 'rust', 'rustlang'115buf = Rex::Text.to_rust_comment(buf)116when 'zig','ziglang'117buf = Rex::Text.to_zig_comment(buf)118else119raise BufferFormatError, "Unsupported buffer format: #{fmt}", caller120end121122return buf123end124125#126# Returns the list of supported formats127#128def self.transform_formats129[130'base32',131'base64',132'bash',133'c',134'csharp',135'dw',136'dword',137'go',138'golang',139'hex',140'java',141'js_be',142'js_le',143'masm',144'nim',145'nimlang',146'num',147'octal',148'perl',149'pl',150'powershell',151'ps1',152'py',153'python',154'raw',155'rb',156'ruby',157'rust',158'rustlang',159'sh',160'vbapplication',161'vbscript',162'zig'163]164end165166def self.encryption_formats167[168'xor',169'base64',170'aes256',171'rc4'172]173end174175private176177def self.encrypt_buffer(value, encryption_opts)178buf = ''179180case encryption_opts[:format]181when 'aes256'182if encryption_opts[:iv].blank?183raise ArgumentError, 'Initialization vector is missing'184elsif encryption_opts[:key].blank?185raise ArgumentError, 'Encryption key is missing'186end187188buf = Rex::Crypto.encrypt_aes256(encryption_opts[:iv], encryption_opts[:key], value)189when 'base64'190buf = Rex::Text.encode_base64(value)191when 'xor'192if encryption_opts[:key].blank?193raise ArgumentError, 'XOR key is missing'194end195196buf = Rex::Text.xor(encryption_opts[:key], value)197when 'rc4'198if encryption_opts[:key].blank?199raise ArgumentError, 'Encryption key is missing'200end201202buf = Rex::Crypto.rc4(encryption_opts[:key], value)203else204raise ArgumentError, "Unsupported encryption format: #{encryption_opts[:format]}", caller205end206207return buf208end209210end211212end213end214215216