From eb199cfc6e81ce78cf3827d638b527915090a77e Mon Sep 17 00:00:00 2001 From: Postmodern Date: Fri, 16 Aug 2024 22:52:12 -0700 Subject: [PATCH] Added `CommandPayload#to_python` (closes #142). --- lib/ronin/payloads/command_payload.rb | 15 +++++++++++++++ spec/command_payload_spec.rb | 8 ++++++++ 2 files changed, 23 insertions(+) diff --git a/lib/ronin/payloads/command_payload.rb b/lib/ronin/payloads/command_payload.rb index 90e0e327..9eb93614 100644 --- a/lib/ronin/payloads/command_payload.rb +++ b/lib/ronin/payloads/command_payload.rb @@ -24,6 +24,7 @@ require_relative 'encoders/command_encoder' require 'ronin/support/encoding/php' +require 'ronin/support/encoding/python' require 'ronin/support/encoding/ruby' require 'ronin/support/encoding/node_js' @@ -67,6 +68,20 @@ def to_php "system(#{Support::Encoding::PHP.quote(to_s)})" end + # + # Converts the built command into Python code. + # + # @return [String] + # The Python code which executes the command. + # + # @api public + # + # @since 0.3.0 + # + def to_python + "import os; os.system(#{Support::Encoding::Python.quote(to_s)})" + end + # # Converts the built command into Ruby code. # diff --git a/spec/command_payload_spec.rb b/spec/command_payload_spec.rb index 3902c5cd..ef27e801 100644 --- a/spec/command_payload_spec.rb +++ b/spec/command_payload_spec.rb @@ -39,6 +39,14 @@ def build end end + describe "#to_python" do + it "must convert the built command into a Python string that is passed to 'import os; os.system(\"...\")'" do + python_escaped_string = Ronin::Support::Encoding::Python.quote(subject.to_s) + + expect(subject.to_python).to eq("import os; os.system(#{python_escaped_string})") + end + end + describe "#to_ruby" do it "must convert the built command into a Ruby string that is passed to 'system(\"...\")'" do ruby_escaped_string = Ronin::Support::Encoding::Ruby.quote(subject.to_s)