Skip to content

Commit

Permalink
Land #66, Cleanup masm format logic
Browse files Browse the repository at this point in the history
  • Loading branch information
sjanusz-r7 committed Oct 19, 2023
2 parents d02dc53 + cd52024 commit f8c7772
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 23 deletions.
6 changes: 3 additions & 3 deletions lib/rex/text/hex.rb
Original file line number Diff line number Diff line change
Expand Up @@ -171,8 +171,8 @@ def self.dehex!(str)
#
# General-case method to handle both "\xAA\xBB\xCC" format and 0xAA,0xBB,0xCC format
#
def self.hexify_general(str, char_prefix, col = DefaultWrap, line_start = '', line_end = '', buf_start = '', buf_end = '', between='')
encoded_char_length = 2 + char_prefix.length + between.length
def self.hexify_general(str, char_prefix, col = DefaultWrap, line_start = '', line_end = '', buf_start = '', buf_end = '', between='', char_suffix: '')
encoded_char_length = 2 + char_prefix.length + char_suffix.length + between.length
if col < line_start.length + encoded_char_length + line_end.length
# raise an exception
raise ArgumentError.new('insufficient column width')
Expand All @@ -188,7 +188,7 @@ def self.hexify_general(str, char_prefix, col = DefaultWrap, line_start = '', li
ret << "#{line_end}\n#{line_start}"
last_line_length = line_start.length
end
ret << char_prefix << char.unpack('H*')[0] << between
ret << char_prefix << char.unpack('H*')[0] << char_suffix << between
last_line_length += encoded_char_length
end
# Remove the last in-between characters, if required
Expand Down
25 changes: 12 additions & 13 deletions lib/rex/text/lang.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,17 +48,17 @@ def self.to_golang_comment(str, wrap = DefaultWrap)
#
# Converts to a masm style array of bytes
#
def self.to_masm(str, wrap = DefaultWrap, name = "")
raise ArgumentError.new('str can not be empty') if str.empty?
a = to_hex(str)
a.gsub!(/\\x/, '')
a.gsub!(/(.{2})/, '\1h,')
a.gsub!(/(.{32})/, '\1\n')
a.gsub!('\n', "\n")
a.gsub!(/^(.*),$/, 'DB \1')
a.gsub!(/([a-f].h)/, '0\1')
a.sub!(/^/, 'shellcode ')
return a
def self.to_masm(str, wrap = DefaultWrap, name = "buf")
result = hexify_general(str, "", wrap, "#{' ' * (name.length + 1)}DB ", '', "#{name} DB ", '', ',', char_suffix: 'h')
result.gsub!(",\n", "\n")
result
end

#
# Creates a masm style comment
#
def self.to_masm_comment(str, wrap = DefaultWrap)
return wordwrap(str, 0, wrap, '', '; ')
end

#
Expand All @@ -72,7 +72,6 @@ def self.to_masm_comment(str, wrap = DefaultWrap)
# Converts to a nim style array of bytes
#
def self.to_nim(str, wrap = DefaultWrap, name = "buf")
raise ArgumentError.new('str can not be empty') if str.empty?
return numhexify(str, wrap, '', '', "var #{name}: array[#{str.length}, byte] = [\nbyte ", "]", ',')
end

Expand All @@ -89,7 +88,7 @@ def self.to_nim_comment(str, wrap = DefaultWrap)
def self.to_rust(str, wrap = DefaultWrap, name = "buf")
return numhexify(str, wrap, '', '', "let #{name}: [u8; #{str.length}] = [", "];", ',')
end

#
# Creates a Rust style comment
#
Expand Down
31 changes: 24 additions & 7 deletions spec/rex/text/lang_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,6 @@
"buf := []byte{0x41,0x41,0x41,\n0x41,0x41,0x41,0x41,0x41,0x41,\n0x41};\n"
end

let(:expected_masm) do
"shellcode DB 41h,41h,41h,41h,41h,41h,41h,41h\nDB 41h,41h"
end

let(:expected_nim) do
"var buf: array[10, byte] = [\nbyte 0x41,0x41,0x41,0x41,0x41,\n0x41,0x41,0x41,0x41,0x41]\n"
end
Expand Down Expand Up @@ -67,9 +63,30 @@
expect(output).to eq(expected_golang)
end

it "masm is as expected" do
output = described_class.to_masm('A' * 10, 30)
expect(output).to eq(expected_masm)
describe '#to_masm' do
[
{
args: ['A' * 10, 80],
expected: "buf DB 41h,41h,41h,41h,41h,41h,41h,41h,41h,41h\n"
},
{
args: ['A' * 10, 30],
expected: "buf DB 41h,41h,41h,41h,41h\n DB 41h,41h,41h,41h,41h\n"
},
{
args: [(0..24).to_a.pack("C*"), 50],
expected: "buf DB 00h,01h,02h,03h,04h,05h,06h,07h,08h,09h\n DB 0ah,0bh,0ch,0dh,0eh,0fh,10h,11h,12h,13h\n DB 14h,15h,16h,17h,18h\n"
},
{
args: [('A'..'Z').to_a.join, 50],
expected: "buf DB 41h,42h,43h,44h,45h,46h,47h,48h,49h,4ah\n DB 4bh,4ch,4dh,4eh,4fh,50h,51h,52h,53h,54h\n DB 55h,56h,57h,58h,59h,5ah\n"
}
].each do |test|
it "formats #{test} as expected" do
output = described_class.to_masm(*test[:args])
expect(output).to eq(test[:expected])
end
end
end

it "nim is as expected" do
Expand Down

0 comments on commit f8c7772

Please sign in to comment.