2012-06-29 00:18:28 -05:00
|
|
|
# -*- 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
|
|
|
|
|
|
2018-11-04 09:25:37 -05:00
|
|
|
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,
|
2022-10-30 21:14:51 +13:00
|
|
|
# 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={})
|
2014-12-15 23:59:34 -06:00
|
|
|
default_wrap = 60
|
|
|
|
|
|
2018-04-10 11:14:14 -05:00
|
|
|
unless encryption_opts.empty?
|
|
|
|
|
buf = encrypt_buffer(buf, encryption_opts)
|
|
|
|
|
end
|
|
|
|
|
|
2018-11-04 08:57:45 -05:00
|
|
|
case fmt
|
2005-07-10 09:42:49 +00:00
|
|
|
when 'raw'
|
2013-11-20 22:36:17 +01:00
|
|
|
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, '')
|
2013-11-20 22:36:17 +01:00
|
|
|
when 'dword', 'dw'
|
|
|
|
|
buf = Rex::Text.to_dword(buf)
|
2013-05-13 20:44:51 -05:00
|
|
|
when 'python', 'py'
|
2014-12-15 23:59:34 -06:00
|
|
|
buf = Rex::Text.to_python(buf, default_wrap, var_name)
|
2010-09-21 00:13:30 +00:00
|
|
|
when 'ruby', 'rb'
|
2014-12-15 23:59:34 -06:00
|
|
|
buf = Rex::Text.to_ruby(buf, default_wrap, var_name)
|
2010-09-21 00:13:30 +00:00
|
|
|
when 'perl', 'pl'
|
2014-12-15 23:59:34 -06:00
|
|
|
buf = Rex::Text.to_perl(buf, default_wrap, var_name)
|
2011-11-11 00:13:17 -08:00
|
|
|
when 'bash', 'sh'
|
2014-12-15 23:59:34 -06:00
|
|
|
buf = Rex::Text.to_bash(buf, default_wrap, var_name)
|
2005-07-10 09:42:49 +00:00
|
|
|
when 'c'
|
2014-12-15 23:59:34 -06:00
|
|
|
buf = Rex::Text.to_c(buf, default_wrap, var_name)
|
2013-06-11 19:27:06 -05:00
|
|
|
when 'csharp'
|
2014-12-15 23:59:34 -06:00
|
|
|
buf = Rex::Text.to_csharp(buf, default_wrap, var_name)
|
2006-07-31 04:05:20 +00:00
|
|
|
when 'js_be'
|
|
|
|
|
buf = Rex::Text.to_unescape(buf, ENDIAN_BIG)
|
|
|
|
|
when 'js_le'
|
|
|
|
|
buf = Rex::Text.to_unescape(buf, ENDIAN_LITTLE)
|
2007-05-07 04:42:11 +00:00
|
|
|
when 'java'
|
2014-12-15 23:59:34 -06:00
|
|
|
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'
|
2014-12-15 23:59:34 -06:00
|
|
|
buf = Rex::Text.to_vbscript(buf, var_name)
|
2013-08-23 18:00:19 +01:00
|
|
|
when 'vbapplication'
|
2014-12-15 23:59:34 -06:00
|
|
|
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)
|
2022-08-26 07:49:11 -04:00
|
|
|
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)
|
2022-10-30 21:14:51 +13:00
|
|
|
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
|
2018-11-04 09:25:37 -05:00
|
|
|
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
|
2022-10-30 21:14:51 +13:00
|
|
|
# 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")
|
2018-11-04 08:57:45 -05:00
|
|
|
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)
|
2013-05-13 20:41:15 -05:00
|
|
|
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)
|
2013-06-11 19:27:06 -05:00
|
|
|
when 'csharp'
|
|
|
|
|
buf = Rex::Text.to_c_comment(buf)
|
2006-07-31 04:05:20 +00:00
|
|
|
when 'js_be', 'js_le'
|
|
|
|
|
buf = Rex::Text.to_js_comment(buf)
|
2007-05-07 04:42:11 +00:00
|
|
|
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)
|
2022-08-26 07:49:11 -04:00
|
|
|
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)
|
2022-10-30 21:14:51 +13:00
|
|
|
when 'rust', 'rustlang'
|
2022-10-31 17:22:29 +13:00
|
|
|
buf = Rex::Text.to_rust_comment(buf)
|
2005-07-10 19:21:40 +00:00
|
|
|
else
|
2018-11-04 09:25:37 -05:00
|
|
|
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',
|
2022-10-30 21:14:51 +13:00
|
|
|
'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 = ''
|
|
|
|
|
|
2018-11-04 08:57:45 -05:00
|
|
|
case encryption_opts[:format]
|
2018-04-10 11:14:14 -05:00
|
|
|
when 'aes256'
|
2018-04-11 13:02:35 -05:00
|
|
|
if encryption_opts[:iv].blank?
|
|
|
|
|
raise ArgumentError, 'Initialization vector is missing'
|
|
|
|
|
elsif encryption_opts[:key].blank?
|
|
|
|
|
raise ArgumentError, 'Encryption key is missing'
|
|
|
|
|
end
|
|
|
|
|
|
2018-04-17 20:12:26 -05:00
|
|
|
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'
|
2018-04-11 13:02:35 -05:00
|
|
|
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'
|
2018-04-11 13:02:35 -05:00
|
|
|
if encryption_opts[:key].blank?
|
|
|
|
|
raise ArgumentError, 'Encryption key is missing'
|
|
|
|
|
end
|
|
|
|
|
|
2018-04-17 20:12:26 -05:00
|
|
|
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
|