Module: Msf::Simple::Buffer

Defined in:
lib/msf/base/simple/buffer.rb

Overview

Wraps interaction with a generated buffer from the framework. Its primary use is to transform a raw buffer into another format.

Defined Under Namespace

Classes: BufferFormatError

Class Method Summary collapse

Class Method Details

.comment(buf, fmt = "ruby") ⇒ Object

Creates a comment using the supplied format. The formats supported are raw, ruby, rust python, perl, bash, js_be, js_le, c, and java.



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/msf/base/simple/buffer.rb', line 87

def self.comment(buf, fmt = "ruby")
  case fmt
    when 'raw'
    when 'num', 'dword', 'dw', 'hex', 'octal', 'base64', 'base32'
      # These are string encodings, not languages; default to the js comment.
      buf = Rex::Text.to_js_comment(buf)
    when 'ruby', 'rb', 'python', 'py'
      buf = Rex::Text.to_ruby_comment(buf)
    when 'perl', 'pl'
      buf = Rex::Text.to_perl_comment(buf)
    when 'bash', 'sh'
      buf = Rex::Text.to_bash_comment(buf)
    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)
    when 'powershell','ps1'
      buf = Rex::Text.to_psh_comment(buf)
    when 'go','golang'
      buf = Rex::Text.to_golang_comment(buf)
    when 'masm','ml64'
      buf = Rex::Text.to_masm_comment(buf)
    when 'nim','nimlang'
      buf = Rex::Text.to_nim_comment(buf)
    when 'rust', 'rustlang'
      buf = Rex::Text.to_rust_comment(buf)
    when 'zig','ziglang'
      buf = Rex::Text.to_zig_comment(buf)
    else
      raise BufferFormatError, "Unsupported buffer format: #{fmt}", caller
  end

  return buf
end

.encryption_formatsObject



167
168
169
170
171
172
173
174
# File 'lib/msf/base/simple/buffer.rb', line 167

def self.encryption_formats
  [
    'xor',
    'base64',
    'aes256',
    'rc4'
  ]
end

.transform(buf, fmt = "ruby", var_name = 'buf', encryption_opts = {}) ⇒ Object

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



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/msf/base/simple/buffer.rb', line 21

def self.transform(buf, fmt = "ruby", var_name = 'buf', encryption_opts={})
  default_wrap = 60

  unless encryption_opts.empty?
    buf = encrypt_buffer(buf, encryption_opts)
  end

  case fmt
    when 'raw'
    when 'num'
      buf = Rex::Text.to_num(buf)
    when 'hex'
      buf = Rex::Text.to_hex(buf, '')
    when 'dword', 'dw'
      buf = Rex::Text.to_dword(buf)
    when 'python', 'py'
      buf = Rex::Text.to_python(buf, default_wrap, var_name)
    when 'ruby', 'rb'
      buf = Rex::Text.to_ruby(buf, default_wrap, var_name)
    when 'perl', 'pl'
      buf = Rex::Text.to_perl(buf, default_wrap, var_name)
    when 'bash', 'sh'
      buf = Rex::Text.to_bash(buf, default_wrap, var_name)
    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)
    when 'powershell', 'ps1'
      buf = Rex::Powershell.to_powershell(buf, var_name)
    when 'vbscript'
      buf = Rex::Text.to_vbscript(buf, var_name)
    when 'vbapplication'
      buf = Rex::Text.to_vbapplication(buf, var_name)
    when 'base32'
      buf = Rex::Text.encode_base32(buf)
    when 'base64'
      buf = Rex::Text.encode_base64(buf)
    when 'go','golang'
      buf = Rex::Text.to_golang(buf)
    when 'masm'
      buf = Rex::Text.to_masm(buf)
    when 'nim','nimlang'
      buf = Rex::Text.to_nim(buf)
    when 'rust', 'rustlang'
      buf = Rex::Text.to_rust(buf)
    when 'zig','ziglang'
      buf = Rex::Text.to_zig(buf)
    when 'octal'
      buf = Rex::Text.to_octal(buf)
    else
      raise BufferFormatError, "Unsupported buffer format: #{fmt}", caller
  end

  return buf
end

.transform_formatsObject

Returns the list of supported formats



129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
# File 'lib/msf/base/simple/buffer.rb', line 129

def self.transform_formats
  [
    'base32',
    'base64',
    'bash',
    'c',
    'csharp',
    'dw',
    'dword',
    'go',
    'golang',
    'hex',
    'java',
    'js_be',
    'js_le',
    'masm',
    'nim',
    'nimlang',
    'num',
    'octal',
    'perl',
    'pl',
    'powershell',
    'ps1',
    'py',
    'python',
    'raw',
    'rb',
    'ruby',
    'rust',
    'rustlang',
    'sh',
    'vbapplication',
    'vbscript',
    'zig'
  ]
end