Skip to content

Commit 6e9232b

Browse files
committed
Merge branch 'addr_hex_dump' of github.com:Meatballs1/metasploit-framework into Meatballs1-addr_hex_dump
2 parents 9fe7531 + acdd952 commit 6e9232b

File tree

1 file changed

+57
-14
lines changed

1 file changed

+57
-14
lines changed

lib/rex/text.rb

Lines changed: 57 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,8 @@ module Text
3939
UpperAlpha = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
4040
LowerAlpha = "abcdefghijklmnopqrstuvwxyz"
4141
Numerals = "0123456789"
42-
Base32 = "ABCDEFGHIJKLMNOPQRSTUVWXYZ234567"
43-
Alpha = UpperAlpha + LowerAlpha
42+
Base32 = "ABCDEFGHIJKLMNOPQRSTUVWXYZ234567"
43+
Alpha = UpperAlpha + LowerAlpha
4444
AlphaNumeric = Alpha + Numerals
4545
HighAscii = [*(0x80 .. 0xff)].pack("C*")
4646
LowAscii = [*(0x00 .. 0x1f)].pack("C*")
@@ -307,16 +307,16 @@ def self.to_hex_ascii(str, prefix = "\\x", count = 1, suffix=nil)
307307
#
308308
# Supported unicode types include: utf-16le, utf16-be, utf32-le, utf32-be, utf-7, and utf-8
309309
#
310-
# Providing 'mode' provides hints to the actual encoder as to how it should encode the string. Only UTF-7 and UTF-8 use "mode".
310+
# Providing 'mode' provides hints to the actual encoder as to how it should encode the string. Only UTF-7 and UTF-8 use "mode".
311311
#
312312
# utf-7 by default does not encode alphanumeric and a few other characters. By specifying the mode of "all", then all of the characters are encoded, not just the non-alphanumeric set.
313313
# to_unicode(str, 'utf-7', 'all')
314314
#
315315
# utf-8 specifies that alphanumeric characters are used directly, eg "a" is just "a". However, there exist 6 different overlong encodings of "a" that are technically not valid, but parse just fine in most utf-8 parsers. (0xC1A1, 0xE081A1, 0xF08081A1, 0xF8808081A1, 0xFC80808081A1, 0xFE8080808081A1). How many bytes to use for the overlong enocding is specified providing 'size'.
316-
# to_unicode(str, 'utf-8', 'overlong', 2)
316+
# to_unicode(str, 'utf-8', 'overlong', 2)
317317
#
318-
# Many utf-8 parsers also allow invalid overlong encodings, where bits that are unused when encoding a single byte are modified. Many parsers will ignore these bits, rendering simple string matching to be ineffective for dealing with UTF-8 strings. There are many more invalid overlong encodings possible for "a". For example, three encodings are available for an invalid 2 byte encoding of "a". (0xC1E1 0xC161 0xC121). By specifying "invalid", a random invalid encoding is chosen for the given byte size.
319-
# to_unicode(str, 'utf-8', 'invalid', 2)
318+
# Many utf-8 parsers also allow invalid overlong encodings, where bits that are unused when encoding a single byte are modified. Many parsers will ignore these bits, rendering simple string matching to be ineffective for dealing with UTF-8 strings. There are many more invalid overlong encodings possible for "a". For example, three encodings are available for an invalid 2 byte encoding of "a". (0xC1E1 0xC161 0xC121). By specifying "invalid", a random invalid encoding is chosen for the given byte size.
319+
# to_unicode(str, 'utf-8', 'invalid', 2)
320320
#
321321
# utf-7 defaults to 'normal' utf-7 encoding
322322
# utf-8 defaults to 2 byte 'normal' encoding
@@ -360,7 +360,7 @@ def self.to_unicode(str='', type = 'utf-16le', mode = '', size = '')
360360
string = ''
361361
str.each_byte { |a|
362362
if (a < 21 || a > 0x7f) || mode != ''
363-
# ugh. turn a single byte into the binary representation of it, in array form
363+
# ugh. turn a single byte into the binary representation of it, in array form
364364
bin = [a].pack('C').unpack('B8')[0].split(//)
365365

366366
# even more ugh.
@@ -658,6 +658,49 @@ def self.to_hex_dump(str, width=16)
658658
buf << "\n"
659659
end
660660

661+
#
662+
# Converts a string a nicely formatted and addressed ex dump
663+
#
664+
def self.to_addr_hex_dump(str, start_addr=0, width=16)
665+
buf = ''
666+
idx = 0
667+
cnt = 0
668+
snl = false
669+
lst = 0
670+
addr = start_addr
671+
672+
while (idx < str.length)
673+
674+
buf << "%08x" % addr
675+
buf << " " * 4
676+
chunk = str[idx, width]
677+
line = chunk.unpack("H*")[0].scan(/../).join(" ")
678+
buf << line
679+
680+
if (lst == 0)
681+
lst = line.length
682+
buf << " " * 4
683+
else
684+
buf << " " * ((lst - line.length) + 4).abs
685+
end
686+
687+
chunk.unpack("C*").each do |c|
688+
if (c > 0x1f and c < 0x7f)
689+
buf << c.chr
690+
else
691+
buf << "."
692+
end
693+
end
694+
695+
buf << "\n"
696+
697+
idx += width
698+
addr += width
699+
end
700+
701+
buf << "\n"
702+
end
703+
661704
#
662705
# Converts a hex string to a raw string
663706
#
@@ -691,20 +734,20 @@ def self.wordwrap(str, indent = 0, col = DefaultWrap, append = '', prepend = '')
691734
# Converts a string to a hex version with wrapping support
692735
#
693736
def self.hexify(str, col = DefaultWrap, line_start = '', line_end = '', buf_start = '', buf_end = '')
694-
output = buf_start
695-
cur = 0
696-
count = 0
737+
output = buf_start
738+
cur = 0
739+
count = 0
697740
new_line = true
698741

699742
# Go through each byte in the string
700743
str.each_byte { |byte|
701744
count += 1
702-
append = ''
745+
append = ''
703746

704747
# If this is a new line, prepend with the
705748
# line start text
706749
if (new_line == true)
707-
append << line_start
750+
append << line_start
708751
new_line = false
709752
end
710753

@@ -716,7 +759,7 @@ def self.hexify(str, col = DefaultWrap, line_start = '', line_end = '', buf_star
716759
# time to finish up this line
717760
if ((cur + line_end.length >= col) or (cur + buf_end.length >= col))
718761
new_line = true
719-
cur = 0
762+
cur = 0
720763

721764
# If this is the last byte, use the buf_end instead of
722765
# line_end
@@ -1277,7 +1320,7 @@ def self.split_to_a(str, n)
12771320
else
12781321
ret = str
12791322
end
1280-
ret
1323+
ret
12811324
end
12821325

12831326
#

0 commit comments

Comments
 (0)