Skip to content

Commit

Permalink
Wrap the hex escaped encoder output in bash -c "...".
Browse files Browse the repository at this point in the history
* This is because on Ubuntu/Debian systems `sh` is actually `dash`,
  which does not support `$'\xXX...'` style strings. So we must ensure
  the hex escaped command is executed under `bash`.
  • Loading branch information
postmodern committed Aug 16, 2024
1 parent 9a30540 commit acbfbd4
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 4 deletions.
6 changes: 4 additions & 2 deletions lib/ronin/payloads/encoders/builtin/shell/hex_escape.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ class HexEscape < ShellCommandEncoder
Encodes the arguments of a given command String as hex escaped shell
strings then executes it as a command in a sub-shell.
ls -la -> $'\\x6c\\x73' $'\\x2d\\x6c\\x61'
ls -la -> bash -c "$'\\x6c\\x73' $'\\x2d\\x6c\\x61'"
Note: supports bash, zsh, mksh, but *not* the dash shell which is
the default system shell on Ubuntu and Debian.
Expand All @@ -61,9 +61,11 @@ class HexEscape < ShellCommandEncoder
# @return [String]
#
def encode(command)
Shellwords.shellsplit(command).map { |arg|
escaped_command = Shellwords.shellsplit(command).map { |arg|
"$'#{Support::Encoding::Shell.encode(arg)}'"
}.join(' ')

%{bash -c "#{escaped_command}"}
end

end
Expand Down
4 changes: 2 additions & 2 deletions spec/encoders/builtin/shell/hex_escape_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@

describe "#encode" do
let(:command) { "echo PWNED" }
let(:encoded) { "$'\\x65\\x63\\x68\\x6f' $'\\x50\\x57\\x4e\\x45\\x44'" }
let(:encoded) { %{bash -c "$'\\x65\\x63\\x68\\x6f' $'\\x50\\x57\\x4e\\x45\\x44'"} }

it "must encode each argument in the given command string into a hex strings" do
expect(subject.encode(command)).to eq(encoded)
end

it "must return a valid shell command", :integration do
expect(`bash -c "#{subject.encode(command)}"`).to eq("PWNED#{$/}")
expect(`#{subject.encode(command)}`).to eq("PWNED#{$/}")
end
end
end

0 comments on commit acbfbd4

Please sign in to comment.