Skip to content

Commit

Permalink
Added integration tests for the built-in encoders.
Browse files Browse the repository at this point in the history
  • Loading branch information
postmodern committed Aug 16, 2024
1 parent 0a01c11 commit 200b1f8
Show file tree
Hide file tree
Showing 15 changed files with 83 additions and 18 deletions.
3 changes: 2 additions & 1 deletion .github/workflows/ruby.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ jobs:
- name: Install dependencies
run: |
sudo apt update -y && \
sudo apt install -y --no-install-recommends --no-install-suggests yasm gcc-mingw-w64 default-jdk
sudo apt install -y --no-install-recommends --no-install-suggests yasm gcc-mingw-w64 default-jdk perl php-cli nodejs python2 python3 &&
echo /opt/microsoft/powershell/7 >> $GITHUB_PATH
- name: Install dependencies
run: bundle install --jobs 4 --retry 3
- name: Run tests
Expand Down
4 changes: 4 additions & 0 deletions spec/encoders/builtin/perl/base64_encode_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,9 @@
it "must encode the given Perl code as a Base64 string and embed it into the 'use MIME::Base64; eval(decode_base64(\"...\"))' string" do
expect(subject.encode(perl)).to eq(encoded)
end

it "must return valid Perl code", :integration do
expect(`perl -e '#{subject.encode(perl)}'`).to eq("PWNED#{$/}")
end
end
end
4 changes: 4 additions & 0 deletions spec/encoders/builtin/perl/hex_encode_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,9 @@
it "must encode the given Perl code as a hex string and embed it into the 'eval(pack(\"H*\",\"...\"))' string" do
expect(subject.encode(perl)).to eq(encoded)
end

it "must return valid Perl code", :integration do
expect(`perl -e '#{subject.encode(perl)}'`).to eq("PWNED#{$/}")
end
end
end
4 changes: 4 additions & 0 deletions spec/encoders/builtin/php/base64_encode_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,9 @@
it "must encode the given PHP code as a Base64 string and embed it into the 'eval(base64_decode(\"...\"));' string" do
expect(subject.encode(php)).to eq(encoded)
end

it "must return valid PHP code", :integration do
expect(`php -r '#{subject.encode(php)}'`).to eq("PWNED")
end
end
end
4 changes: 4 additions & 0 deletions spec/encoders/builtin/php/hex_encode_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,9 @@
it "must encode the given PHP code as a hex string and embed it into the 'eval(hex2bin(\"...\"));' string" do
expect(subject.encode(php)).to eq(encoded)
end

it "must return valid PHP code", :integration do
expect(`php -r '#{subject.encode(php)}'`).to eq("PWNED")
end
end
end
4 changes: 4 additions & 0 deletions spec/encoders/builtin/powershell/base64_encode_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,9 @@
it "must encode the given PowerShell code as a Base64 string and embed it into the 'Invoke-Expression([System.Text.Encoding]::ASCII.GetString([System.Convert]::FromBase64String(\"...\")))' string" do
expect(subject.encode(powershell)).to eq(encoded)
end

it "must return valid PowerShell code", :integration do
expect(`pwsh -C '#{subject.encode(powershell)}'`).to eq("PWNED#{$/}")
end
end
end
10 changes: 7 additions & 3 deletions spec/encoders/builtin/powershell/hex_encode_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,17 @@
end

describe "#encode" do
let(:data) { "dir" }
let(:powershell) { "Write-Output 'PWNED'" }
let(:encoded) do
%{Invoke-Expression "$([char]0x64)$([char]0x69)$([char]0x72)"}
%{Invoke-Expression "$([char]0x57)$([char]0x72)$([char]0x69)$([char]0x74)$([char]0x65)$([char]0x2d)$([char]0x4f)$([char]0x75)$([char]0x74)$([char]0x70)$([char]0x75)$([char]0x74)$([char]0x20)$([char]0x27)$([char]0x50)$([char]0x57)$([char]0x4e)$([char]0x45)$([char]0x44)$([char]0x27)"}
end

it "must each character of the command as PowerShell '$([char]0xXX)' characters and evaluate the resulting string using 'Invoke-Expression'" do
expect(subject.encode(data)).to eq(encoded)
expect(subject.encode(powershell)).to eq(encoded)
end

it "must return valid PowerShell code", :integration do
expect(`pwsh -C '#{subject.encode(powershell)}'`).to eq("PWNED#{$/}")
end
end
end
8 changes: 8 additions & 0 deletions spec/encoders/builtin/python/base64_encode_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,13 @@
it "must encode the given Python code as a Base64 string and embed it into the 'import base64; exec(base64.b64decode(\"...\"))' string" do
expect(subject.encode(python)).to eq(encoded)
end

it "must return valid Python 2 code", :integration do
expect(`python2 -c '#{subject.encode(python)}'`).to eq("PWNED#{$/}")
end

it "must return valid Python 3 code", :integration do
expect(`python3 -c '#{subject.encode(python)}'`).to eq("PWNED#{$/}")
end
end
end
8 changes: 8 additions & 0 deletions spec/encoders/builtin/python/hex_encode_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,13 @@
it "must encode the given Python code as a hex string and embed it into the 'import binascii; exec(binascii.unhexlify(\"...\"))' string" do
expect(subject.encode(python)).to eq(encoded)
end

it "must return valid Python 2 code", :integration do
expect(`python2 -c '#{subject.encode(python)}'`).to eq("PWNED#{$/}")
end

it "must return valid Python 3 code", :integration do
expect(`python3 -c '#{subject.encode(python)}'`).to eq("PWNED#{$/}")
end
end
end
4 changes: 4 additions & 0 deletions spec/encoders/builtin/ruby/base64_encode_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,9 @@
it "must encode the given Ruby code as a Base64 string and embed it into the 'eval(\"...\".unpack1(\"m0\")' string" do
expect(subject.encode(ruby)).to eq(encoded)
end

it "must return valid Ruby code", :integration do
expect(`ruby -e '#{subject.encode(ruby)}'`).to eq("PWNED#{$/}")
end
end
end
8 changes: 6 additions & 2 deletions spec/encoders/builtin/ruby/hex_encode_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,15 @@
end

describe "#encode" do
let(:ruby) { "puts('PWNED')" }
let(:encoded) { %{eval("70757473282750574e45442729".scan(/../).map(&:hex).map(&:chr).join)} }
let(:ruby) { %{puts "PWNED"} }
let(:encoded) { %{eval("70757473202250574e454422".scan(/../).map(&:hex).map(&:chr).join)} }

it "must encode the given Ruby code as a hex string and embed it into the 'eval(\"...\".scan(/../).map(&:hex).map(&:chr).join)' string" do
expect(subject.encode(ruby)).to eq(encoded)
end

it "must return valid Ruby code", :integration do
expect(`ruby -e '#{subject.encode(ruby)}'`).to eq("PWNED#{$/}")
end
end
end
10 changes: 7 additions & 3 deletions spec/encoders/builtin/shell/base64_encode_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,16 @@
end

describe "#encode" do
let(:command) { "ls -la" }
let(:encoded) { "echo bHMgLWxh|base64 -d|bash" }
let(:command) { "echo PWNED" }
let(:encoded) { "echo ZWNobyBQV05FRA==|base64 -d|bash" }

it "must encode the given command String as Base64 and embed it into the `echo ...|base64 -d|bash` command" do
expect(subject.encode(command)).to eq(encoded)
end

context "when the shell param is set" do
let(:shell) { 'zsh' }
let(:encoded) { "echo bHMgLWxh|base64 -d|#{shell}" }
let(:encoded) { "echo ZWNobyBQV05FRA==|base64 -d|#{shell}" }

subject do
described_class.new(params: {shell: shell})
Expand All @@ -34,5 +34,9 @@
expect(subject.encode(command)).to eq(encoded)
end
end

it "must return a valid shell command", :integration do
expect(`#{subject.encode(command)}`).to eq("PWNED#{$/}")
end
end
end
10 changes: 7 additions & 3 deletions spec/encoders/builtin/shell/hex_encode_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,16 @@
end

describe "#encode" do
let(:command) { "ls -la" }
let(:encoded) { "echo 6c73202d6c61|xxd -r -p|bash" }
let(:command) { "echo PWNED" }
let(:encoded) { "echo 6563686f2050574e4544|xxd -r -p|bash" }

it "must encode the given command String into a hex string and embed it into the `xxd -r -ps|bash` command" do
expect(subject.encode(command)).to eq(encoded)
end

context "when the shell param is set" do
let(:shell) { 'zsh' }
let(:encoded) { "echo 6c73202d6c61|xxd -r -p|#{shell}" }
let(:encoded) { "echo 6563686f2050574e4544|xxd -r -p|#{shell}" }

subject do
described_class.new(params: {shell: shell})
Expand All @@ -34,5 +34,9 @@
expect(subject.encode(command)).to eq(encoded)
end
end

it "must return a valid shell command", :integration do
expect(`#{subject.encode(command)}`).to eq("PWNED#{$/}")
end
end
end
8 changes: 6 additions & 2 deletions spec/encoders/builtin/shell/hex_escape_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,15 @@
end

describe "#encode" do
let(:command) { "ls -la" }
let(:encoded) { "$'\\x6c\\x73' $'\\x2d\\x6c\\x61'" }
let(:command) { "echo PWNED" }
let(:encoded) { "$'\\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#{$/}")
end
end
end
12 changes: 8 additions & 4 deletions spec/encoders/builtin/shell/ifs_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,27 +15,31 @@
end

describe "#encode" do
let(:command) { "ls -la" }
let(:encoded) { "ls${IFS}-la" }
let(:command) { "echo PWNED" }
let(:encoded) { "echo${IFS}PWNED" }

it "must replace spaces with '${IFS}'" do
expect(subject.encode(command)).to eq(encoded)
end

context "when the input contains multiple consecutive spaces" do
let(:command) { "ls -la" }
let(:command) { "echo PWNED" }

it "must replace multiple spaces with a single '${IFS}'" do
expect(subject.encode(command)).to eq(encoded)
end
end

context "when the input contains other kinds of whitespace" do
let(:command) { "ls\t-la" }
let(:command) { "echo\tPWNED" }

it "must replace other whitespace characters with '${IFS}'" do
expect(subject.encode(command)).to eq(encoded)
end
end

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

0 comments on commit 200b1f8

Please sign in to comment.