CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
rapid7

Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.

GitHub Repository: rapid7/metasploit-framework
Path: blob/master/lib/msf/base/simple/buffer.rb
Views: 11784
1
# -*- coding: binary -*-
2
3
4
module Msf
5
module Simple
6
7
###
8
#
9
# Wraps interaction with a generated buffer from the framework.
10
# Its primary use is to transform a raw buffer into another
11
# format.
12
#
13
###
14
module Buffer
15
16
class BufferFormatError < ::ArgumentError; end
17
#
18
# Serializes a buffer to a provided format. The formats supported are raw,
19
# num, dword, ruby, rust, python, perl, bash, c, js_be, js_le, java and psh
20
#
21
def self.transform(buf, fmt = "ruby", var_name = 'buf', encryption_opts={})
22
default_wrap = 60
23
24
unless encryption_opts.empty?
25
buf = encrypt_buffer(buf, encryption_opts)
26
end
27
28
case fmt
29
when 'raw'
30
when 'num'
31
buf = Rex::Text.to_num(buf)
32
when 'hex'
33
buf = Rex::Text.to_hex(buf, '')
34
when 'dword', 'dw'
35
buf = Rex::Text.to_dword(buf)
36
when 'python', 'py'
37
buf = Rex::Text.to_python(buf, default_wrap, var_name)
38
when 'ruby', 'rb'
39
buf = Rex::Text.to_ruby(buf, default_wrap, var_name)
40
when 'perl', 'pl'
41
buf = Rex::Text.to_perl(buf, default_wrap, var_name)
42
when 'bash', 'sh'
43
buf = Rex::Text.to_bash(buf, default_wrap, var_name)
44
when 'c'
45
buf = Rex::Text.to_c(buf, default_wrap, var_name)
46
when 'csharp'
47
buf = Rex::Text.to_csharp(buf, default_wrap, var_name)
48
when 'js_be'
49
buf = Rex::Text.to_unescape(buf, ENDIAN_BIG)
50
when 'js_le'
51
buf = Rex::Text.to_unescape(buf, ENDIAN_LITTLE)
52
when 'java'
53
buf = Rex::Text.to_java(buf, var_name)
54
when 'powershell', 'ps1'
55
buf = Rex::Powershell.to_powershell(buf, var_name)
56
when 'vbscript'
57
buf = Rex::Text.to_vbscript(buf, var_name)
58
when 'vbapplication'
59
buf = Rex::Text.to_vbapplication(buf, var_name)
60
when 'base32'
61
buf = Rex::Text.encode_base32(buf)
62
when 'base64'
63
buf = Rex::Text.encode_base64(buf)
64
when 'go','golang'
65
buf = Rex::Text.to_golang(buf)
66
when 'masm'
67
buf = Rex::Text.to_masm(buf)
68
when 'nim','nimlang'
69
buf = Rex::Text.to_nim(buf)
70
when 'rust', 'rustlang'
71
buf = Rex::Text.to_rust(buf)
72
when 'zig','ziglang'
73
buf = Rex::Text.to_zig(buf)
74
when 'octal'
75
buf = Rex::Text.to_octal(buf)
76
else
77
raise BufferFormatError, "Unsupported buffer format: #{fmt}", caller
78
end
79
80
return buf
81
end
82
83
#
84
# Creates a comment using the supplied format. The formats supported are
85
# raw, ruby, rust python, perl, bash, js_be, js_le, c, and java.
86
#
87
def self.comment(buf, fmt = "ruby")
88
case fmt
89
when 'raw'
90
when 'num', 'dword', 'dw', 'hex', 'octal', 'base64', 'base32'
91
# These are string encodings, not languages; default to the js comment.
92
buf = Rex::Text.to_js_comment(buf)
93
when 'ruby', 'rb', 'python', 'py'
94
buf = Rex::Text.to_ruby_comment(buf)
95
when 'perl', 'pl'
96
buf = Rex::Text.to_perl_comment(buf)
97
when 'bash', 'sh'
98
buf = Rex::Text.to_bash_comment(buf)
99
when 'c'
100
buf = Rex::Text.to_c_comment(buf)
101
when 'csharp'
102
buf = Rex::Text.to_c_comment(buf)
103
when 'js_be', 'js_le'
104
buf = Rex::Text.to_js_comment(buf)
105
when 'java'
106
buf = Rex::Text.to_c_comment(buf)
107
when 'powershell','ps1'
108
buf = Rex::Text.to_psh_comment(buf)
109
when 'go','golang'
110
buf = Rex::Text.to_golang_comment(buf)
111
when 'masm','ml64'
112
buf = Rex::Text.to_masm_comment(buf)
113
when 'nim','nimlang'
114
buf = Rex::Text.to_nim_comment(buf)
115
when 'rust', 'rustlang'
116
buf = Rex::Text.to_rust_comment(buf)
117
when 'zig','ziglang'
118
buf = Rex::Text.to_zig_comment(buf)
119
else
120
raise BufferFormatError, "Unsupported buffer format: #{fmt}", caller
121
end
122
123
return buf
124
end
125
126
#
127
# Returns the list of supported formats
128
#
129
def self.transform_formats
130
[
131
'base32',
132
'base64',
133
'bash',
134
'c',
135
'csharp',
136
'dw',
137
'dword',
138
'go',
139
'golang',
140
'hex',
141
'java',
142
'js_be',
143
'js_le',
144
'masm',
145
'nim',
146
'nimlang',
147
'num',
148
'octal',
149
'perl',
150
'pl',
151
'powershell',
152
'ps1',
153
'py',
154
'python',
155
'raw',
156
'rb',
157
'ruby',
158
'rust',
159
'rustlang',
160
'sh',
161
'vbapplication',
162
'vbscript',
163
'zig'
164
]
165
end
166
167
def self.encryption_formats
168
[
169
'xor',
170
'base64',
171
'aes256',
172
'rc4'
173
]
174
end
175
176
private
177
178
def self.encrypt_buffer(value, encryption_opts)
179
buf = ''
180
181
case encryption_opts[:format]
182
when 'aes256'
183
if encryption_opts[:iv].blank?
184
raise ArgumentError, 'Initialization vector is missing'
185
elsif encryption_opts[:key].blank?
186
raise ArgumentError, 'Encryption key is missing'
187
end
188
189
buf = Rex::Crypto.encrypt_aes256(encryption_opts[:iv], encryption_opts[:key], value)
190
when 'base64'
191
buf = Rex::Text.encode_base64(value)
192
when 'xor'
193
if encryption_opts[:key].blank?
194
raise ArgumentError, 'XOR key is missing'
195
end
196
197
buf = Rex::Text.xor(encryption_opts[:key], value)
198
when 'rc4'
199
if encryption_opts[:key].blank?
200
raise ArgumentError, 'Encryption key is missing'
201
end
202
203
buf = Rex::Crypto.rc4(encryption_opts[:key], value)
204
else
205
raise ArgumentError, "Unsupported encryption format: #{encryption_opts[:format]}", caller
206
end
207
208
return buf
209
end
210
211
end
212
213
end
214
end
215
216