Files
metasploit-gs/lib/msf/base/simple/buffer.rb
T

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

210 lines
5.2 KiB
Ruby
Raw Normal View History

# -*- coding: binary -*-
2010-09-21 00:13:30 +00:00
2005-07-10 09:42:49 +00:00
module Msf
module Simple
###
#
# Wraps interaction with a generated buffer from the framework.
# Its primary use is to transform a raw buffer into another
# format.
#
###
module Buffer
class BufferFormatError < ::ArgumentError; end
2005-07-10 09:42:49 +00:00
#
2005-11-15 15:29:56 +00:00
# Serializes a buffer to a provided format. The formats supported are raw,
# num, dword, ruby, rust, python, perl, bash, c, js_be, js_le, java and psh
2005-07-10 09:42:49 +00:00
#
2018-04-10 11:14:14 -05:00
def self.transform(buf, fmt = "ruby", var_name = 'buf', encryption_opts={})
default_wrap = 60
2018-04-10 11:14:14 -05:00
unless encryption_opts.empty?
buf = encrypt_buffer(buf, encryption_opts)
end
case fmt
2005-07-10 09:42:49 +00:00
when 'raw'
when 'num'
buf = Rex::Text.to_num(buf)
2015-02-24 16:05:02 +01:00
when 'hex'
2015-02-24 17:35:53 +01:00
buf = Rex::Text.to_hex(buf, '')
when 'dword', 'dw'
buf = Rex::Text.to_dword(buf)
2013-05-13 20:44:51 -05:00
when 'python', 'py'
buf = Rex::Text.to_python(buf, default_wrap, var_name)
2010-09-21 00:13:30 +00:00
when 'ruby', 'rb'
buf = Rex::Text.to_ruby(buf, default_wrap, var_name)
2010-09-21 00:13:30 +00:00
when 'perl', 'pl'
buf = Rex::Text.to_perl(buf, default_wrap, var_name)
2011-11-11 00:13:17 -08:00
when 'bash', 'sh'
buf = Rex::Text.to_bash(buf, default_wrap, var_name)
2005-07-10 09:42:49 +00:00
when 'c'
buf = Rex::Text.to_c(buf, default_wrap, var_name)
when 'csharp'
buf = Rex::Text.to_csharp(buf, default_wrap, var_name)
when 'js_be'
buf = Rex::Text.to_unescape(buf, ENDIAN_BIG)
when 'js_le'
buf = Rex::Text.to_unescape(buf, ENDIAN_LITTLE)
when 'java'
buf = Rex::Text.to_java(buf, var_name)
2013-08-23 15:59:19 +01:00
when 'powershell', 'ps1'
2016-06-21 13:56:36 -05:00
buf = Rex::Powershell.to_powershell(buf, var_name)
2013-08-23 16:26:03 +01:00
when 'vbscript'
buf = Rex::Text.to_vbscript(buf, var_name)
2013-08-23 18:00:19 +01:00
when 'vbapplication'
buf = Rex::Text.to_vbapplication(buf, var_name)
2020-04-29 17:50:07 -04:00
when 'base32'
buf = Rex::Text.encode_base32(buf)
when 'base64'
buf = Rex::Text.encode_base64(buf)
when 'go','golang'
2022-08-26 08:45:43 -04:00
buf = Rex::Text.to_golang(buf)
2023-05-31 17:17:17 +02:00
when 'masm'
buf = Rex::Text.to_masm(buf)
2022-09-02 07:55:39 -04:00
when 'nim','nimlang'
buf = Rex::Text.to_nim(buf)
when 'rust', 'rustlang'
buf = Rex::Text.to_rust(buf)
2023-09-13 12:08:21 -06:00
when 'octal'
buf = Rex::Text.to_octal(buf)
2005-07-10 09:42:49 +00:00
else
raise BufferFormatError, "Unsupported buffer format: #{fmt}", caller
2005-07-10 09:42:49 +00:00
end
return buf
end
2005-07-10 19:21:40 +00:00
#
2005-11-15 15:29:56 +00:00
# Creates a comment using the supplied format. The formats supported are
# raw, ruby, rust python, perl, bash, js_be, js_le, c, and java.
2005-07-10 19:21:40 +00:00
#
def self.comment(buf, fmt = "ruby")
case fmt
2005-07-10 19:21:40 +00:00
when 'raw'
2023-09-21 08:50:58 -06:00
when 'num', 'dword', 'dw', 'hex', 'octal', 'base64', 'base32'
# These are string encodings, not languages; default to the js comment.
2013-11-20 23:10:55 +01:00
buf = Rex::Text.to_js_comment(buf)
when 'ruby', 'rb', 'python', 'py'
2005-07-10 19:21:40 +00:00
buf = Rex::Text.to_ruby_comment(buf)
2010-09-21 00:13:30 +00:00
when 'perl', 'pl'
2005-07-10 19:21:40 +00:00
buf = Rex::Text.to_perl_comment(buf)
2011-11-11 00:13:17 -08:00
when 'bash', 'sh'
buf = Rex::Text.to_bash_comment(buf)
2005-07-10 19:21:40 +00:00
when 'c'
buf = Rex::Text.to_c_comment(buf)
when 'csharp'
buf = Rex::Text.to_c_comment(buf)
when 'js_be', 'js_le'
buf = Rex::Text.to_js_comment(buf)
when 'java'
buf = Rex::Text.to_c_comment(buf)
2019-09-18 12:20:16 -05:00
when 'powershell','ps1'
buf = Rex::Text.to_psh_comment(buf)
when 'go','golang'
2022-08-26 08:45:43 -04:00
buf = Rex::Text.to_golang_comment(buf)
2023-06-13 06:55:16 +02:00
when 'masm','ml64'
buf = Rex::Text.to_masm_comment(buf)
2022-09-02 07:55:39 -04:00
when 'nim','nimlang'
buf = Rex::Text.to_nim_comment(buf)
when 'rust', 'rustlang'
buf = Rex::Text.to_rust_comment(buf)
2005-07-10 19:21:40 +00:00
else
raise BufferFormatError, "Unsupported buffer format: #{fmt}", caller
2005-07-10 19:21:40 +00:00
end
return buf
end
2010-09-21 00:13:30 +00:00
#
# Returns the list of supported formats
#
def self.transform_formats
2013-12-02 11:57:52 +01:00
[
2020-04-29 17:50:07 -04:00
'base32',
'base64',
2013-12-02 11:57:52 +01:00
'bash',
'c',
'csharp',
'dw',
'dword',
2022-08-26 08:45:43 -04:00
'go',
'golang',
2015-02-24 16:05:02 +01:00
'hex',
2013-12-02 11:57:52 +01:00
'java',
'js_be',
'js_le',
2023-05-31 17:17:17 +02:00
'masm',
2022-09-02 07:55:39 -04:00
'nim',
'nimlang',
2013-12-02 11:57:52 +01:00
'num',
2023-09-13 12:08:21 -06:00
'octal',
2013-12-02 11:57:52 +01:00
'perl',
'pl',
'powershell',
'ps1',
'py',
'python',
'raw',
'rb',
'ruby',
'rust',
'rustlang',
2013-12-02 11:57:52 +01:00
'sh',
'vbapplication',
'vbscript'
]
2010-09-21 00:13:30 +00:00
end
2018-04-10 11:14:14 -05:00
def self.encryption_formats
[
'xor',
'base64',
'aes256',
'rc4'
]
end
private
def self.encrypt_buffer(value, encryption_opts)
buf = ''
case encryption_opts[:format]
2018-04-10 11:14:14 -05:00
when 'aes256'
if encryption_opts[:iv].blank?
raise ArgumentError, 'Initialization vector is missing'
elsif encryption_opts[:key].blank?
raise ArgumentError, 'Encryption key is missing'
end
buf = Rex::Crypto.encrypt_aes256(encryption_opts[:iv], encryption_opts[:key], value)
2018-04-10 11:14:14 -05:00
when 'base64'
buf = Rex::Text.encode_base64(value)
when 'xor'
if encryption_opts[:key].blank?
raise ArgumentError, 'XOR key is missing'
end
2018-04-10 11:14:14 -05:00
buf = Rex::Text.xor(encryption_opts[:key], value)
when 'rc4'
if encryption_opts[:key].blank?
raise ArgumentError, 'Encryption key is missing'
end
buf = Rex::Crypto.rc4(encryption_opts[:key], value)
2018-04-10 11:14:14 -05:00
else
raise ArgumentError, "Unsupported encryption format: #{encryption_opts[:format]}", caller
end
return buf
end
2005-07-10 09:42:49 +00:00
end
end
2009-01-09 02:16:02 +00:00
end