Skip to content

Add to_hex_cstring method#76

Merged
adfoster-r7 merged 1 commit into
rapid7:masterfrom
sjanusz-r7:add-to_hex_cstring-method
May 8, 2026
Merged

Add to_hex_cstring method#76
adfoster-r7 merged 1 commit into
rapid7:masterfrom
sjanusz-r7:add-to_hex_cstring-method

Conversation

@sjanusz-r7
Copy link
Copy Markdown
Contributor

@sjanusz-r7 sjanusz-r7 commented May 8, 2026

Needed for rapid7/metasploit-framework#21424

Add method to abstract handling of null-escaped hex strings in assembly.

Previously in Framework, an empty string was causing issues with Metasm.

Usage examples:

db #{Rex::Text.to_hex_cstring(server_uri)}

Before:

server_uri = server_uri.bytes.map { |byte| '0x%02x' % byte }.join(', ')
...
db #{server_uri}, 0x00

Broken with an empty string.

Comment thread lib/rex/text/hex.rb Outdated
#
# @param str [String] The string to convert
# @return [String] Comma-separated hex bytes with null terminator
def self.to_hex_cstring(str)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is very similiar to #numhex above it and #hexify_general notes it handles the 0xaa,0xbb,0xcc format. This differs however by not supporting the same arguments as the other hexifying methods and doesn't append a \n for better or worse. Those factors combine to make this an odd fit here.

I'm guessing the intention is to facilitate the escaped ASM strings we need. If that's the case it might be better to add a escape_cstring method Metasm directly.

@sjanusz-r7 sjanusz-r7 force-pushed the add-to_hex_cstring-method branch from e934219 to 4e54cc0 Compare May 8, 2026 13:52
@adfoster-r7
Copy link
Copy Markdown
Contributor

Looks like we can ship this for now to unblock the release and then circle back to any additional improvements that are needed 📈

@adfoster-r7 adfoster-r7 merged commit f511175 into rapid7:master May 8, 2026
20 checks passed
sjanusz-r7 pushed a commit that referenced this pull request May 18, 2026
The functionality has moved to Metasm::Shellcode.define_data and
Metasm::Shellcode.define_cstring, which emit full `db` directives and
are a better home for assembly-source generation than rex-text.
Reverts the addition from #76.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants