Skip to content

Commit

Permalink
Use exec() to evaluate the Python code instead of eval() for Pyth…
Browse files Browse the repository at this point in the history
…on 2.

* Under Python2 `eval()` does not support `print` statements, but
  `exec()` does.
  • Loading branch information
postmodern committed Aug 16, 2024
1 parent fadb8d8 commit 0a01c11
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 18 deletions.
14 changes: 7 additions & 7 deletions lib/ronin/payloads/encoders/builtin/python/base64_encode.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,10 @@ module Encoders
module Python
#
# A Python encoder that encodes the given Python code as a Base64
# string, then decodes it using `base64.b64decode()`, and then evaluates
# the decoded Python code using `eval()`.
# string, then decodes it using `base64.b64decode()`, and then execuates
# the decoded Python code using `exec()`.
#
# print('PWNED') -> import base64; eval(base64.b64decode("cHJpbnQoJ1BXTkVEJyk="))
# print('PWNED') -> import base64; exec(base64.b64decode("cHJpbnQoJ1BXTkVEJyk="))
#
# @since 0.3.0
#
Expand All @@ -43,10 +43,10 @@ class Base64Encode < PythonEncoder

description <<~DESC
Encodes the given Python code as a Base64 string, then decodes it
using `base64.b64decode()`, and then evaluates the decoded Python
code using `eval()`.
using `base64.b64decode()`, and then execuates the decoded Python
code using `exec()`.
print('PWNED') -> import base64; eval(base64.b64decode("cHJpbnQoJ1BXTkVEJyk="))
print('PWNED') -> import base64; exec(base64.b64decode("cHJpbnQoJ1BXTkVEJyk="))
DESC

Expand All @@ -61,7 +61,7 @@ class Base64Encode < PythonEncoder
def encode(python)
base64 = Support::Encoding::Base64.encode(python, mode: :strict)

%{import base64; eval(base64.b64decode("#{base64}"))}
%{import base64; exec(base64.b64decode("#{base64}"))}
end

end
Expand Down
14 changes: 7 additions & 7 deletions lib/ronin/payloads/encoders/builtin/python/hex_encode.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,10 @@ module Encoders
module Python
#
# A Python encoder that encodes the given Python code as an hex string,
# then decodes it using `binascii.unhexlify()`, and then evaluates the
# decoded Python code using `eval()`.
# then decodes it using `binascii.unhexlify()`, and then execuates the
# decoded Python code using `exec()`.
#
# print('PWNED') -> import binascii; eval(binascii.unhexlify("7072696e74282750574e45442729"))
# print('PWNED') -> import binascii; exec(binascii.unhexlify("7072696e74282750574e45442729"))
#
# @since 0.3.0
#
Expand All @@ -43,10 +43,10 @@ class HexEncode < PythonEncoder

description <<~DESC
Encodes the given Python code as an hex string, then decodes it
using `binascii.unhexlify()`, and then evaluates the decoded Python
code using `eval()`.
using `binascii.unhexlify()`, and then execuates the decoded Python
code using `exec()`.
print('PWNED') -> import binascii; eval(binascii.unhexlify("7072696e74282750574e45442729"))
print('PWNED') -> import binascii; exec(binascii.unhexlify("7072696e74282750574e45442729"))
DESC

Expand All @@ -61,7 +61,7 @@ class HexEncode < PythonEncoder
def encode(python)
hex = Support::Encoding::Hex.encode(python)

%{import binascii; eval(binascii.unhexlify("#{hex}"))}
%{import binascii; exec(binascii.unhexlify("#{hex}"))}
end

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

describe "#encode" do
let(:python) { "print('PWNED')" }
let(:encoded) { %{import base64; eval(base64.b64decode("cHJpbnQoJ1BXTkVEJyk="))} }
let(:encoded) { %{import base64; exec(base64.b64decode("cHJpbnQoJ1BXTkVEJyk="))} }

it "must encode the given Python code as a Base64 string and embed it into the 'import base64; eval(base64.b64decode(\"...\"))' string" do
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
end
Expand Down
4 changes: 2 additions & 2 deletions spec/encoders/builtin/python/hex_encode_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@

describe "#encode" do
let(:python) { "print('PWNED')" }
let(:encoded) { %{import binascii; eval(binascii.unhexlify("7072696e74282750574e45442729"))} }
let(:encoded) { %{import binascii; exec(binascii.unhexlify("7072696e74282750574e45442729"))} }

it "must encode the given Python code as a hex string and embed it into the 'import binascii; eval(binascii.unhexlify(\"...\"))' string" do
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
end
Expand Down

0 comments on commit 0a01c11

Please sign in to comment.